Ejemplo n.º 1
0
    def test_3_state_game_win_third_row(self):
        '''Win crosses or circles in third row.
        set fields of board 1:
          [O][ ][ ]
          [ ][O][ ]
          [X][X][X]

        set fields of board 2:
          [X][ ][ ]
          [ ][X][ ]
          [O][O][O]
        '''
        self.board[0] = Field.CIRCLE
        self.board[4] = Field.CIRCLE
        self.board[6] = Field.CROSS
        self.board[7] = Field.CROSS
        self.board[8] = Field.CROSS

        self.assertEqual(state_game(self.board), StateGame.WIN_CROSS)

        self.board[0] = Field.CROSS
        self.board[4] = Field.CROSS
        self.board[6] = Field.CIRCLE
        self.board[7] = Field.CIRCLE
        self.board[8] = Field.CIRCLE

        self.assertEqual(state_game(self.board), StateGame.WIN_CIRCLE)
Ejemplo n.º 2
0
    def test_4_state_game_win_first_column(self):
        '''Win crosses or circles in first column.
        set fields of board 1:
          [X][O][ ]
          [X][O][ ]
          [X][ ][ ]

        set fields of board 2:
          [O][X][ ]
          [O][X][ ]
          [O][ ][ ]

        '''
        self.board[0] = Field.CROSS
        self.board[1] = Field.CIRCLE
        self.board[3] = Field.CROSS
        self.board[4] = Field.CIRCLE
        self.board[6] = Field.CROSS

        self.assertEqual(state_game(self.board), StateGame.WIN_CROSS)

        self.board[0] = Field.CIRCLE
        self.board[1] = Field.CROSS
        self.board[3] = Field.CIRCLE
        self.board[4] = Field.CROSS
        self.board[6] = Field.CIRCLE

        self.assertEqual(state_game(self.board), StateGame.WIN_CIRCLE)
Ejemplo n.º 3
0
    def test_8_state_game_win_second_diagonal(self):
        '''Win crosses or circles in second diagonal.
        set fields of board 1:
          [O][ ][X]
          [ ][X][ ]
          [X][ ][O]

        set fields of board 2:
          [X][ ][O]
          [ ][O][ ]
          [O][ ][X]

        '''
        self.board[0] = Field.CIRCLE
        self.board[2] = Field.CROSS
        self.board[4] = Field.CROSS
        self.board[6] = Field.CROSS
        self.board[8] = Field.CIRCLE

        self.assertEqual(state_game(self.board), StateGame.WIN_CROSS)

        self.board[0] = Field.CROSS
        self.board[2] = Field.CIRCLE
        self.board[4] = Field.CIRCLE
        self.board[6] = Field.CIRCLE
        self.board[8] = Field.CROSS

        self.assertEqual(state_game(self.board), StateGame.WIN_CIRCLE)
Ejemplo n.º 4
0
    def test_2_state_game_win_second_row(self):
        '''Win crosses or cirlces in second row.
        set fields of board 1:
          [ ][ ][ ]
          [X][X][X]
          [O][O][ ]

         set fields of board 2:
           [ ][ ][ ]
           [O][O][O]
           [X][X][ ]
        '''
        self.board[3] = Field.CROSS
        self.board[4] = Field.CROSS
        self.board[5] = Field.CROSS
        self.board[6] = Field.CIRCLE
        self.board[7] = Field.CIRCLE

        self.assertEqual(state_game(self.board), StateGame.WIN_CROSS)

        self.board[3] = Field.CIRCLE
        self.board[4] = Field.CIRCLE
        self.board[5] = Field.CIRCLE
        self.board[6] = Field.CROSS
        self.board[7] = Field.CROSS

        self.assertEqual(state_game(self.board), StateGame.WIN_CIRCLE)
Ejemplo n.º 5
0
    def test_7_state_game_win_first_diagonal(self):
        '''Win crosses or circles in first diagonal.
        set fields of board 1:
          [X][ ][ ]
          [O][X][O]
          [ ][ ][X]

        set fields of board 2:
          [O][ ][ ]
          [X][O][X]
          [ ][ ][O]

        '''
        self.board[0] = Field.CROSS
        self.board[3] = Field.CIRCLE
        self.board[4] = Field.CROSS
        self.board[5] = Field.CIRCLE
        self.board[8] = Field.CROSS

        self.assertEqual(state_game(self.board), StateGame.WIN_CROSS)

        self.board[0] = Field.CIRCLE
        self.board[3] = Field.CROSS
        self.board[4] = Field.CIRCLE
        self.board[5] = Field.CROSS
        self.board[8] = Field.CIRCLE

        self.assertEqual(state_game(self.board), StateGame.WIN_CIRCLE)
Ejemplo n.º 6
0
    def test_5_state_game_win_second_column(self):
        '''Win crosses or circles in second column.
        set fields of board 1:
          [O][X][ ]
          [ ][X][ ]
          [ ][X][O]

        set fields of board 2:
          [X][O][ ]
          [ ][O][ ]
          [ ][O][X]

        '''
        self.board[0] = Field.CIRCLE
        self.board[1] = Field.CROSS
        self.board[4] = Field.CROSS
        self.board[7] = Field.CROSS
        self.board[8] = Field.CIRCLE

        self.assertEqual(state_game(self.board), StateGame.WIN_CROSS)

        self.board[0] = Field.CROSS
        self.board[1] = Field.CIRCLE
        self.board[4] = Field.CIRCLE
        self.board[7] = Field.CIRCLE
        self.board[8] = Field.CROSS

        self.assertEqual(state_game(self.board), StateGame.WIN_CIRCLE)
Ejemplo n.º 7
0
    def test_9_state_game_continues(self):
        '''Continues game.
        set fields of board (finish):
          [X][X][O]
          [O][O][X]
          [X][O][X]
        '''
        self.board[0] = Field.CROSS
        self.assertEqual(state_game(self.board), StateGame.CONTINUES)

        self.board[1] = Field.CROSS
        self.assertEqual(state_game(self.board), StateGame.CONTINUES)

        self.board[2] = Field.CIRCLE
        self.assertEqual(state_game(self.board), StateGame.CONTINUES)

        self.board[3] = Field.CIRCLE
        self.assertEqual(state_game(self.board), StateGame.CONTINUES)

        self.board[4] = Field.CIRCLE
        self.assertEqual(state_game(self.board), StateGame.CONTINUES)

        self.board[5] = Field.CROSS
        self.assertEqual(state_game(self.board), StateGame.CONTINUES)

        self.board[6] = Field.CROSS
        self.assertEqual(state_game(self.board), StateGame.CONTINUES)

        self.board[7] = Field.CIRCLE
        self.assertEqual(state_game(self.board), StateGame.CONTINUES)

        self.board[8] = Field.CROSS
        self.assertNotEqual(state_game(self.board), StateGame.CONTINUES)
Ejemplo n.º 8
0
    def test_10_state_game_draw(self):
        '''Draw game.
        set fields of board:
          [X][X][O]
          [O][O][X]
          [X][O][X]
        '''
        self.board[0] = Field.CROSS
        self.board[1] = Field.CROSS
        self.board[2] = Field.CIRCLE
        self.board[3] = Field.CIRCLE
        self.board[4] = Field.CIRCLE
        self.board[5] = Field.CROSS
        self.board[6] = Field.CROSS
        self.board[7] = Field.CIRCLE
        self.board[8] = Field.CROSS

        self.assertEqual(state_game(self.board), StateGame.DRAW)
Ejemplo n.º 9
0
def moves_left(board: Board) -> bool:
    return state_game(board) == StateGame.CONTINUES
Ejemplo n.º 10
0
def main():
    banner()
    player1_name = None
    player2_name = None

    try:
        config = menu()
    except QuitException:
        print(_('Bye, bye...'))
        return

    if config.player1 == PlayerType.HUMAN:
        player1_name = enter_name(1, 'X')

    if config.player2 == PlayerType.HUMAN:
        player2_name = enter_name(2, 'O')

    player1 = Player(config.player1, Field.CROSS, player1_name)
    player2 = Player(config.player2, Field.CIRCLE, player2_name)
    board = Board()
    choice = None
    current_player = player1
    turn = 0

    if config.debug:
        run = timeit.default_timer()

    display_board(board)

    while True:
        if turn % 2 == 0:
            current_player = player1
        else:
            current_player = player2

        while True and not current_player.ai:
            choice = enter_number(
                _('{} choice field number [1-9]: ').format(
                    current_player.name))

            if choice < 1 or choice > 9:
                print(_('Enter number from 1 to 9!'))
                continue

            if board[choice - 1] != Field.EMPTY:
                print(_('Field number {} is not empty.').format(choice))
                continue

            break

        if current_player.ai:
            if config.debug:
                start = timeit.default_timer()

            if current_player is player1:
                opponent = player2
            else:
                opponent = player1

            choice = find_best_move(board, current_player.type,
                                    opponent.type) + 1

            print(
                f'{current_player.name} {current_player.xo}: choice {choice}')

            config.debug and print(f'Time of turn {turn}:',
                                   timeit.default_timer() - start)

        board[choice - 1] = current_player.type
        display_board(board)
        state = state_game(board)

        config.debug and print(f'State game: {state}')

        if state == StateGame.WIN_CROSS:
            print(_('Crosses won.'))
            break
        elif state == StateGame.WIN_CIRCLE:
            print(_('Circles won.'))
            break
        elif state == StateGame.DRAW:
            print(_('Draw.'))
            break

        turn += 1

    config.debug and print('Ran et', timeit.default_timer() - run)