service {
const html game =
7<[slot7]> | 8<[slot8]> | 9<[slot9]> |
4<[slot4]> | 5<[slot5]> | 6<[slot6]> |
1<[slot1]> | 2<[slot2]> | 3<[slot3]> |
Where do you want to move?
;
const html gameOver =
7<[slot7]> | 8<[slot8]> | 9<[slot9]> |
4<[slot4]> | 5<[slot5]> | 6<[slot6]> |
1<[slot1]> | 2<[slot2]> | 3<[slot3]> |
The game is over. <[winner]>
;
session Game() {
string slot1, slot2, slot3, slot4, slot5, slot6, slot7, slot8, slot9;
int move;
string comment;
int moveIsValid;
int playerWon;
int AIWon;
slot1 = "";
slot2 = "";
slot3 = "";
slot4 = "";
slot5 = "";
slot6 = "";
slot7 = "";
slot8 = "";
slot9 = "";
move = 0;
comment = "Not sure if WIG supports comments.";
moveIsValid;
playerWon = 0;
AIWon = 0;
while (playerWon == 0 && AIWon == 0) {
comment = "Initially assume an invalid move";
moveIsValid = 0;
comment = "Show the grid.";
show plug game[slot1 = slot1, slot2 = slot2, slot3 = slot3, slot4 = slot4, slot5 = slot5, slot6 = slot6, slot7 = slot7, slot8 = slot8, slot9 = slot9]
receive [move = move];
if (move == 1) {
comment = "Check if the move is valid.";
if (slot1 == "") {
slot1 = "X";
moveIsValid = 1;
}
} else if (move == 2) {
if (slot2 == "") {
slot2 = "X";
moveIsValid = 1;
}
} else if (move == 3) {
if (slot3 == "") {
slot3 = "X";
moveIsValid = 1;
}
} else if (move == 4) {
if (slot4 == "") {
slot4 = "X";
moveIsValid = 1;
}
} else if (move == 5) {
if (slot5 == "") {
slot5 = "X";
moveIsValid = 1;
}
} else if (move == 6) {
if (slot6 == "") {
slot6 = "X";
moveIsValid = 1;
}
} else if (move == 7) {
if (slot7 == "") {
slot7 = "X";
moveIsValid = 1;
}
} else if (move == 8) {
if (slot8 == "") {
slot8 = "X";
moveIsValid = 1;
}
} else if (move == 9) {
if (slot9 == "") {
slot9 = "X";
moveIsValid = 1;
}
}
comment = "If the player made a valid move, check if the player won.";
if (moveIsValid == 1) {
comment = "Check all vertical wins.";
if (slot1 == "X" && slot4 == "X" && slot7 == "X") {
playerWon = 1;
}
if (slot2 == "X" && slot5 == "X" && slot8 == "X") {
playerWon = 1;
}
if (slot3 == "X" && slot6 == "X" && slot9 == "X") {
playerWon = 1;
}
comment = "Check all horizontal wins.";
if (slot1 == "X" && slot2 == "X" && slot3 == "X") {
playerWon = 1;
}
if (slot4 == "X" && slot5 == "X" && slot6 == "X") {
playerWon = 1;
}
if (slot7 == "X" && slot8 == "X" && slot9 == "X") {
playerWon = 1;
}
comment = "Check all diagonal wins.";
if (slot1 == "X" && slot5 == "X" && slot9 == "X") {
playerWon = 1;
}
if (slot3 == "X" && slot5 == "X" && slot7 == "X") {
playerWon = 1;
}
}
comment = "If the player made a valid move and didn't win, let the AI play.";
if (moveIsValid == 1 && playerWon == 0) {
comment = "Simplistic AI: Tries slot n. If n is taken, tries slot n + 1";
if (slot1 == "") {
slot1 = "O";
} else if (slot1 == "") {
slot1 = "O";
} else if (slot2 == "") {
slot2 = "O";
} else if (slot3 == "") {
slot3 = "O";
} else if (slot4 == "") {
slot4 = "O";
} else if (slot5 == "") {
slot5 = "O";
} else if (slot6 == "") {
slot6 = "O";
} else if (slot7 == "") {
slot7 = "O";
} else if (slot8 == "") {
slot8 = "O";
} else if (slot9 == "") {
slot9 = "O";
}
}
comment = "check if the AI won.";
comment = "Check all vertical wins.";
if (slot1 == "O" && slot4 == "O" && slot7 == "O") {
AIWon = 1;
}
if (slot2 == "O" && slot5 == "O" && slot8 == "O") {
AIWon = 1;
}
if (slot3 == "O" && slot6 == "O" && slot9 == "O") {
AIWon = 1;
}
comment = "Check all horizontal wins.";
if (slot1 == "O" && slot2 == "O" && slot3 == "O") {
AIWon = 1;
}
if (slot4 == "O" && slot5 == "O" && slot6 == "O") {
AIWon = 1;
}
if (slot7 == "O" && slot8 == "O" && slot9 == "O") {
AIWon = 1;
}
comment = "Check all diagonal wins.";
if (slot1 == "O" && slot5 == "O" && slot9 == "O") {
AIWon = 1;
}
if (slot3 == "O" && slot5 == "O" && slot7 == "O") {
AIWon = 1;
}
comment = "check if the game has ended in a tie.";
if (playerWon == 0 && AIWon == 0) {
if (slot1 != "") {
if (slot2 != "") {
if (slot3 != "") {
if (slot4 != "") {
if (slot5 != "") {
if (slot6 != "") {
if (slot7 != "") {
if (slot8 != "") {
if (slot9 != "") {
AIWon = 1;
playerWon = 1;
}
}
}
}
}
}
}
}
}
}
}
if (playerWon == 1 && AIWon == 1) {
comment = "Game ended in a tie.";
} else if (playerWon == 1) {
comment = "Player won.";
} else {
comment = "AI won.";
}
exit plug gameOver[slot1 = slot1, slot2 = slot2, slot3 = slot3, slot4 = slot4, slot5 = slot5, slot6 = slot6, slot7 = slot7, slot8 = slot8, slot9 = slot9, winner = comment];
}
}