Пример #1
0
 def test_find_best_move_start(self):
     '''Find the best move of start game.
     set fields of board:
       [ ][ ][ ]
       [ ][ ][ ]
       [ ][ ][ ]
     '''
     self.assertEqual(find_best_move(self.board, Field.CROSS, Field.CIRCLE),
                      0)
     self.assertEqual(find_best_move(self.board, Field.CIRCLE, Field.CROSS),
                      0)
Пример #2
0
    def test_find_best_move_half(self):
        '''Find the best move of half game.
        set fields of board:
          [O][ ][X]
          [O][X][ ]
          [ ][ ][ ]
        '''
        self.board[0] = Field.CIRCLE
        self.board[2] = Field.CROSS
        self.board[3] = Field.CIRCLE
        self.board[4] = Field.CROSS

        self.assertEqual(find_best_move(self.board, Field.CROSS, Field.CIRCLE),
                         6)
        self.assertEqual(find_best_move(self.board, Field.CIRCLE, Field.CROSS),
                         6)
Пример #3
0
def computer_input(board, turn):
    if turn == 0:
        return choice([0, 2, 6, 8])
    else:
        b_obj = Board(board, "X")
        move = find_best_move(b_obj, "X", 8)
        return 3 * move[0] + move[1]
Пример #4
0
 def key_down(self, event):
     game_board = self.matrix
     move = find_best_move(
         list(
             map(
                 lambda x: list(
                     map(lambda y: 0 if y == 0 else y.bit_length() - 1, x)),
                 game_board)))
     if (move == 0):
         move = 'w'
     elif (move == 1):
         move = 's'
     elif (move == 2):
         move = 'a'
     elif (move == 3):
         move = 'd'
     else:
         print('matrix', self.matrix)
         print('move', move)
         return
     key = repr(move)
     if key in self.commands:
         self.matrix, done, self.score = self.commands[repr(move)](
             self.matrix, self.score)
         #print("SCORE:")
         #print(self.score)
         if done:
             #self.matrix = logic.add_two(self.matrix)
             # record last move
             part_out = get_participant_output(self.player_file,
                                               self.matrix)
             if self.check_participant_output_validity(
                     part_out, game_board):
                 self.matrix[part_out[0][1]][part_out[0][0]] = part_out[1]
             else:
                 penalty(game_board)
             self.highest_tile = max(map(lambda l: max(l), self.matrix))
             self.num_moves += 1
             print('move', self.num_moves)
             self.max_at_instance.append(self.highest_tile)
             self.history_matrixs.append(self.matrix)
             self.update_grid_cells()
             done = False
             if logic.game_state(self.matrix) == 'win':
                 self.grid_cells[1][1].configure(
                     text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
                 self.grid_cells[1][2].configure(
                     text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
             if logic.game_state(self.matrix) == 'lose':
                 self.grid_cells[1][1].configure(
                     text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
                 self.grid_cells[1][2].configure(
                     text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
Пример #5
0
def request_cpu_move(board):
    return ai.find_best_move(board)
Пример #6
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)