def test_get_columns(self):
     columns = Board(sample_input()).get_columns()
     self.assertEqual(columns[0].get_data(), ['x', 'o', 'o'])
     self.assertEqual(columns[0].get_positions(), [0, 3, 6])
     self.assertEqual(columns[1].get_data(), ['x', ' ', ' '])
     self.assertEqual(columns[1].get_positions(), [1, 4, 7])
     self.assertEqual(columns[2].get_data(), [' ', ' ', 'x'])
     self.assertEqual(columns[2].get_positions(), [2, 5, 8])
 def test_get_rows(self):
     lines = Board(sample_input()).get_rows()
     self.assertEqual(lines[0].get_data(), ['x', 'x', ' '])
     self.assertEqual(lines[0].get_positions(), [0, 1, 2])
     self.assertEqual(lines[1].get_data(), ['o', ' ', ' '])
     self.assertEqual(lines[1].get_positions(), [3, 4, 5])
     self.assertEqual(lines[2].get_data(), ['o', ' ', 'x'])
     self.assertEqual(lines[2].get_positions(), [6, 7, 8])
Beispiel #3
0
 def test_wins_if_possible(self):
     data = [
         ' ',
         ' ',
         ' ',
         'o',
         'x',
         ' ',
         'o',
         ' ',
         'x',
     ]
     cpu = Center()
     move = cpu.play(Board(data), 'x', 'o')
     self.assertEquals(move, 0)
 def test_defend_middle_if_free(self):
     data = [
         'x',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
     ]
     cpu = Experimental()
     move = cpu.play(Board(data), 'o', 'x')
     self.assertEquals(move, 4)
 def test_picks_another_corner_in_2nd(self):
     data = [
         'x',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         'o',
     ]
     cpu = Experimental()
     move = cpu.play(Board(data), 'x', 'o')
     self.assertEquals(move, 6)
 def test_win_if_possible(self):
     data = [
         'x',
         ' ',
         'o',
         ' ',
         ' ',
         ' ',
         'x',
         ' ',
         ' ',
     ]
     cpu = Experimental()
     move = cpu.play(Board(data), 'x', 'o')
     self.assertEquals(move, 3)
 def test_picks_corner_on_start(self):
     data = [
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
         ' ',
     ]
     cpu = Experimental()
     move = cpu.play(Board(data), 'x', 'o')
     self.assertEquals(move, 0)
 def test_pick_1(self):
     data = [
         'x',
         ' ',
         ' ',
         ' ',
         'o',
         ' ',
         ' ',
         ' ',
         'x',
     ]
     cpu = Experimental()
     move = cpu.play(Board(data), 'o', 'x')
     self.assertEquals(move, 1)
Beispiel #9
0
 def test_defends_if_needed(self):
     data = [
         ' ',
         ' ',
         ' ',
         ' ',
         'x',
         ' ',
         'o',
         ' ',
         'x',
     ]
     cpu = Center()
     move = cpu.play(Board(data), 'o', 'x')
     self.assertEquals(move, 0)
 def test_defend_corner_if_middle_taken(self):
     data = [
         ' ',
         ' ',
         ' ',
         ' ',
         'x',
         ' ',
         ' ',
         ' ',
         ' ',
     ]
     cpu = Experimental()
     move = cpu.play(Board(data), 'o', 'x')
     self.assertEquals(move, 8)
Beispiel #11
0
 def test_picks_random(self):
     data = sample_input()
     ai = Random()
     move = ai.play(Board(data), 'x', 'o')
     self.assertEquals(data[move], ' ')
Beispiel #12
0
 def test_picks_center(self):
     data = [' '] * 9
     cpu = Center()
     move = cpu.play(Board(data), 'x', 'o')
     self.assertEquals(move, 4)
Beispiel #13
0
    def play(self, board: Board, player_side, opposing_side):

        winning_position = find_winning_position(board, player_side)
        if winning_position is not None:
            return winning_position

        losing_position = find_winning_position(board, opposing_side)
        if losing_position is not None:
            return losing_position

        # board is empty
        if board.played_moves_count() == 0:
            return 0

        if board.played_moves_count() == 2:
            if board.data[8] == ' ':
                return 8
            else:
                return 6

        if board.played_moves_count() == 4:
            if board.data[0] == player_side and board.data[6] == player_side:
                return 2
            else:
                raise RuntimeError("Not reachable")

        if board.played_moves_count() == 1:
            if board.data[4] == opposing_side:
                return 8
            else:
                return 4
        if board.played_moves_count() == 3:
            if board.data[0] == opposing_side and board.data[8] == opposing_side:
                return 1
            if board.data[2] == opposing_side and board.data[6] == opposing_side:
                return 1
            if board.data[2] == opposing_side and board.data[6] == opposing_side:
                return 1
            if board.data[4] == opposing_side and board.data[0] == opposing_side:
                return 2
            if board.data[1] == opposing_side and board.data[5] == opposing_side:
                return 2
            if board.data[1] == opposing_side and board.data[3] == opposing_side:
                return 0
            if board.data[5] == opposing_side and board.data[7] == opposing_side:
                return 8
            if board.data[3] == opposing_side and board.data[7] == opposing_side:
                return 6
            if board.data[1] == opposing_side and board.data[8] == opposing_side:
                return 2
            if board.data[2] == opposing_side and board.data[7] == opposing_side:
                return 8
            if board.data[2] == opposing_side and board.data[3] == opposing_side:
                return 0
            if board.data[0] == opposing_side and board.data[5] == opposing_side:
                return 2
            if board.data[1] == opposing_side and board.data[6] == opposing_side:
                return 0
            if board.data[0] == opposing_side and board.data[7] == opposing_side:
                return 6
            if board.data[3] == opposing_side and board.data[8] == opposing_side:
                return 6
            if board.data[6] == opposing_side and board.data[5] == opposing_side:
                return 8
        if board.played_moves_count() == 5:
            if board.data[1] == opposing_side and board.data[3] == opposing_side and board.data[0] == ' ':
                return 0
            if board.data[1] == opposing_side and board.data[5] == opposing_side and board.data[2] == ' ':
                return 2
            if board.data[3] == opposing_side and board.data[7] == opposing_side and board.data[6] == ' ':
                return 6
            if board.data[5] == opposing_side and board.data[7] == opposing_side and board.data[8] == ' ':
                return 8
        possible_plays = []
        for i in range(9):
            if board.data[i] == ' ':
                possible_plays.append(i)

        return random.choice(possible_plays)
    def test_get_winner(self):
        self.assertEquals(Board(sample_input()).get_winning_side(), None)

        all_x = ['x'] * 9
        self.assertEquals(Board(all_x).get_winning_side(), 'x')
    def test_is_game_over(self):
        self.assertFalse(Board(sample_input()).is_game_over())

        all_x = ['x'] * 9
        self.assertTrue(Board(all_x).is_game_over())
 def test_get_lines(self):
     tuples = Board(sample_input()).get_lines()
     self.assertEqual(len(tuples), 8)
 def test_played_moves_count(self):
     played_moves = Board(sample_input()).played_moves_count()
     self.assertEqual(played_moves, 5)
 def test_get_diagonals(self):
     diagonals = Board(sample_input()).get_diagonals()
     self.assertEqual(diagonals[0].get_data(), ['x', ' ', 'x'])
     self.assertEqual(diagonals[0].get_positions(), [0, 4, 8])
     self.assertEqual(diagonals[1].get_data(), [' ', ' ', 'o'])
     self.assertEqual(diagonals[1].get_positions(), [2, 4, 6])