// The players move. // The player should choose the spot he wants to put his sign. public void doYourMove(Board board) { Console.WriteLine("Choose your place: "); int playerChoise; bool flag = true; while (flag) { playerChoise = Convert.ToInt32(Console.ReadLine()); if (playerChoise > 9 || playerChoise < 1) { Console.WriteLine("You need to choose number between 1 to 9"); } else if (board.isNotTaken(playerChoise)) { board.setPlace(playerChoise, this.num); flag = false; } else { Console.WriteLine("This place is taken, try anoter one!"); } } }
// The computer stratagy for the game. // Every round the computer has a different technique. public void strategy(Board board, int turn) { Console.WriteLine("OK...let me think"); // The compuer is starting first if (this.isFirst) { if (turn == 1) { board.setPlace(5, num); } else if (turn == 3) { if (board.isNotTaken(1)) { board.setPlace(1, num); path = 1; } else if (board.isNotTaken(3)) { board.setPlace(3, num); path = 2; } } else if (turn == 5) { // Always check first if can win // If the compuer can't win check if the player can win and if he can block him. int canWin = makeAMove(board, this.num); int block = makeAMove(board, this.enemyNum); if (canWin != -1) { board.setPlace(canWin, num); } else if (block != -1) { board.setPlace(block, num); } // If the compuer or the player can't win. put in random spot // contains the path that started last turn. else { if (path == 1) { if (board.isNotTaken(2) && board.isNotTaken(8) && board.isNotTaken(3)) { board.setPlace(2, num); } else if (board.isNotTaken(4) && board.isNotTaken(6) && board.isNotTaken(7)) { board.setPlace(4, num); } } else if (path == 2) { if (board.isNotTaken(2) && board.isNotTaken(8) && board.isNotTaken(1)) { board.setPlace(2, num); } else if (board.isNotTaken(6) && board.isNotTaken(4) && board.isNotTaken(9)) { board.setPlace(6, num); } } } } else if (turn == 7) { // Always check first if can win // If the compuer can't win check if the player can win and if he can block him. int canWin = makeAMove(board, this.num); int block = makeAMove(board, this.enemyNum); if (canWin != -1) { board.setPlace(canWin, num); } else if (block != -1) { board.setPlace(block, num); } // If the compuer or the player can't win. put in random spot else { for (int i = 0; i < 9; i++) { if (board.getPlace(i + 1) == -1) { board.setPlace(i + 1, num); break; } } } } else if (turn == 9) { // Fill in the only place that got left for (int i = 0; i < 9; i++) { if (board.getPlace(i + 1) == -1) { board.setPlace(i + 1, num); } } } } // The compuer is starting second else { // Try to get the center, if its already taken, choose a path if (turn == 2) { if (board.isNotTaken(5)) { board.setPlace(5, num); } else { if (board.isNotTaken(1)) { board.setPlace(1, num); path = 1; } else if (board.isNotTaken(3)) { board.setPlace(3, num); path = 2; } } } else if (turn == 4) { // Check if the player can win and if he can block him. int block = makeAMove(board, this.enemyNum); if (block != -1) { board.setPlace(block, num); } else { if (path == 1) { if (board.isNotTaken(2) && board.isNotTaken(3)) { board.setPlace(3, num); } else if (board.isNotTaken(4) && board.isNotTaken(7)) { board.setPlace(7, num); } } else if (path == 2) { if (board.isNotTaken(1) && board.isNotTaken(2)) { board.setPlace(1, num); } else if (board.isNotTaken(6) && board.isNotTaken(9)) { board.setPlace(9, num); } } } } else if (turn == 6) { // Always check first if can win // If the compuer can't win check if the player can win and if he can block him. int canWin = makeAMove(board, this.num); int block = makeAMove(board, this.enemyNum); if (canWin != -1) { board.setPlace(canWin, num); } else if (block != -1) { board.setPlace(block, num); } else { for (int i = 0; i < 9; i++) { if (board.getPlace(i + 1) == -1) { board.setPlace(i + 1, num); break; } } } } else if (turn == 8) { // Always check first if can win // If the compuer can't win check if the player can win and if he can block him. int canWin = makeAMove(board, this.num); int block = makeAMove(board, this.enemyNum); if (canWin != -1) { board.setPlace(canWin, num); } else if (block != -1) { board.setPlace(block, num); } else { for (int i = 0; i < 9; i++) { if (board.getPlace(i + 1) == -1) { board.setPlace(i + 1, num); break; } } } } } }