Ejemplo n.º 1
0
 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"))
Ejemplo n.º 2
0
 def test_add_mark_already_selected(self):
     """
     Test adding a mark to an already-selected position results in an error
     """
     board = Board()
     board.add_mark(0, "X")
     self.assertRaises(TicTacToeError, board.add_mark, 0, "O")
Ejemplo n.º 3
0
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")
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 def test_print_board_in_play(self, mock_stdout):
     """
     Test printing the game board when in play
     """
     board = Board()
     board.add_mark(0, "X")
     board.add_mark(6, "O")
     board.add_mark(1, "X")
     board.add_mark(7, "O")
     board.print_board()
     printed_text = self.get_printed_boards(mock_stdout)[0]
     self.assertNotIn("Winner", printed_text)
     self.assertEquals(printed_text.count("X"), 2)
     self.assertEquals(printed_text.count("O"), 2)
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
 def test_operating_completed_game(self):
     """
     Test that finding a move or adding a mark on a completed board results in an error
     """
     board = Board()
     board.add_mark(0, "X")
     board.add_mark(1, "O")
     board.add_mark(4, "X")
     board.add_mark(7, "O")
     board.add_mark(8, "X")  # X has Tic-Tac-Toe on the diagonal
     self.assertRaises(TicTacToeError, board.find_next_move, "O")
     self.assertRaises(TicTacToeError, board.add_mark, 2, "O")
Ejemplo n.º 10
0
 def test_print_board_with_winner(self, mock_stdout):
     """
     Test printing the game board after a winner has been determined.
     """
     board = Board()
     board.add_mark(0, "X")
     board.add_mark(6, "O")
     board.add_mark(1, "X")
     board.add_mark(7, "O")
     board.add_mark(2, "X")
     board.print_board()
     printed_text = self.get_printed_boards(mock_stdout)[0]
     self.assertIn("Winner", printed_text)
     self.assertEquals(printed_text.count("X"), 4)
     self.assertEquals(printed_text.count("O"), 2)
Ejemplo n.º 11
0
class TestBoard(unittest.TestCase):

    """
    1 | 2 | 3
    ----------
    4 | 5 | 6  
    ----------
    7 | 8 | 9

    """

    def setUp(self):
        self.board = Board()

    def test_add_mark(self):
        #position must be a dict
        position = None
        self.assertRaises(TypeError, self.board.add_mark, 'X', position)

        #fail if passed a value that's not X or O
        position = 5
        self.assertRaises(ValueError, self.board.add_mark, 'M', position)

        #fail if x, y passed position greater than 2
        position = 10
        self.assertRaises(ValueError, self.board.add_mark, 'X', position)

        position = 5
        self.board.add_mark('X', position)
        #assert self.board.get_value

    def test_valid_position(self):
        position1 = 5
        position2 = 5
        position3 = 4

        self.board.add_mark('X', position1)
        valid = self.board._is_valid_position(position2)
        self.assertEqual(valid, False)

        valid = self.board._is_valid_position(position3)
        self.assertEqual(valid, True)

    def test_reset_board(self):
        position1 = 5
        position2 = 5

        self.board.add_mark('X', position1)
        valid = self.board._is_valid_position(position2)
        self.assertEqual(valid, False)

        self.board.reset_board()
        valid = self.board._is_valid_position(position2)
        self.assertEqual(valid, True)

    def test_get_horizontal(self):
        position1 = 1
        position2 = 2
        position3 = 3

        """
        
        X | X | X
        ----------
        4 | 5 | 6  
        ----------
        7 | 8 | 9

        """

        self.board.reset_board()
        self.board.add_mark('X', position1)
        self.board.add_mark('X', position2)
        self.board.add_mark('X', position3)

        self.assertEqual(self.board.get_horizontal(0), ['X', 'X', 'X'])

    def test_get_vertical(self):
        position4 = 3
        position5 = 6
        position6 = 9

        """

        1 | 2 | X
        ----------
        4 | 5 | X  
        ----------
        7 | 8 | X
        
        """

        self.board.reset_board()
        self.board.add_mark('X', position4)
        self.board.add_mark('X', position5)
        self.board.add_mark('X', position6)

        #check if other combinations match expected
        self.assertEqual(self.board.get_vertical(2), ['X', 'X', 'X'])
        self.assertEqual(self.board.get_forward_diagonal(), ['X', '5', '7'])
        self.assertEqual(self.board.get_reverse_diagonal(), ['1', '5', 'X'])

    def test_get_reverse_diagonal(self):
        position7 = 1
        position8 = 5
        position9 = 9

        """

        O | 2 | 3
        ----------
        4 | O | 6  
        ----------
        7 | 8 | O
        
        """

        self.board.reset_board()
        self.board.add_mark('O', position7)
        self.board.add_mark('O', position8)
        self.board.add_mark('O', position9)

        self.assertEqual(self.board.get_reverse_diagonal(), ['O', 'O', 'O'])

        #check if other combinations match expected
        self.assertEqual(self.board.get_horizontal(2), ['7', '8', 'O'])
        self.assertEqual(self.board.get_vertical(2), ['3', '6', 'O'])
        self.assertEqual(self.board.get_forward_diagonal(), ['3', 'O', '7'])

    def test_get_forward_diagonal(self):
        position10 = 7
        position11 = 5
        position12 = 3
        """

        1 | 2 | X
        ----------
        4 | X | 6  
        ----------
        X | 8 | 9 
        
        """

        self.board.reset_board()
        self.board.add_mark('X', position10)
        self.board.add_mark('X', position11)
        self.board.add_mark('X', position12)

        self.assertEqual(self.board.get_forward_diagonal(), ['X', 'X', 'X'])

        #check if other combinations match expected
        self.assertEqual(self.board.get_horizontal(1), ['4', 'X', '6'])
        self.assertEqual(self.board.get_vertical(1), ['2', 'X', '8'])
        self.assertEqual(self.board.get_reverse_diagonal(), ['1', 'X', '9'])