def test_gameplay_first_player_winning_setup(self): """ Test that the first player will have a winning setup if the middle square is left unplayed. """ board = Board() board.add_mark(0, "O") board.add_mark(1, "X") board.add_mark(6, "O") board.add_mark(board.find_next_move("X"), "X") self.assertIn(board.find_next_move("O"), [4, 8]) # 4 and 8 guarantee the win for O board.add_mark(4, "O") board.add_mark(board.find_next_move("X"), "X") board.add_mark(board.find_next_move("O"), "O") self.assertEquals(board.get_winner(), "O") # Test that O actually wins the game
def test_gameplay_identical_opposite_mark(self): """ Test that the gameplay followed in the above test is identical when the symbols are reversed """ board = Board() board.add_mark(0, "X") board.add_mark(1, "O") board.add_mark(6, "X") board.add_mark(board.find_next_move("O"), "O") self.assertIn(board.find_next_move("X"), [4, 8]) # 4 and 8 guarantee the win for X board.add_mark(4, "X") board.add_mark(board.find_next_move("O"), "O") board.add_mark(board.find_next_move("X"), "X") self.assertEquals(board.get_winner(), "X") # Test that X actually wins the game
def test_gameplay_second_player(self): """ Test that the AI picks the middle square as quickly as possible when playing second """ board = Board() board.add_mark(0, "O") self.assertEquals(4, board.find_next_move("X"))
def test_gameplay_first_player_first_move(self): """ Test initial gameplay logic and/or AI when going first """ board = Board() first_move = board.find_next_move('X') self.assertIn(first_move, board.CORNERS)
def main(): """ Run the main program! """ game_board = Board() while True: print "Classic Console Tic-Tac-Toe" print "" print "Make a selection:" print "" print "n) new game" print "p) print the board" print "q) quit" print "0-8) make the given move" print "" print "Enter your selection: " selection = raw_input() if selection in ("q", "Q"): break elif selection in ("p", "P"): game_board.print_board() elif selection in ("n", "N"): game_board = Board() if not is_user_first(): game_board.add_mark(game_board.find_next_move("X"), "X") elif selection in ('0', '1', '2', '3', '4', '5', '6', '7', '8'): position = int(selection) take_turn(game_board, position, "O")
def test_gameplay_winning_blocking(self): """ Test that if a player can win in one move, it becomes both the player and opponent's next move """ board = Board() board.add_mark(0, "O") board.add_mark(3, "O") board.add_mark(1, "X") self.assertEquals(6, board.find_next_move("O")) # Test winning move is made by O if left open self.assertEquals(6, board.find_next_move("X")) # Test winning move is blocked by X
def test_gameplay_first_player_second_move(self): """ Test that second play as AI makes sense (assuming AI was the first player) """ board = Board() board.add_mark(0, "X") pos_move_dict = {1: [2, 6], 8: [2, 6], 2: [8]} for pos, expected_moves in pos_move_dict.iteritems(): board.o_positions.clear() board.add_mark(pos, "O") second_move = board.find_next_move("X") self.assertIn(second_move, expected_moves)