Пример #1
0
def take_turn():
    data = request.get_json()
    game = Game(data)
    #Small optimization for the first turn. Since the first turn requires the most computational power, they've been pre-computed
    if 'firstTurnX' in data:
        game.board = random.choice(app.startBoard.moves)
        game.player = game.board.player
        game.opponent = game.board.opponent
        return jsonify(game.constructResponse())
    elif 'firstTurnO' in data:
        board = Game.findBoard(app.startBoard, data['board'])
        game.board = board.optimalMove
        game.player = game.board.player
        game.opponent = game.board.opponent
        return jsonify(game.constructResponse())
    else:
        return jsonify(game.takeTurn())
Пример #2
0
    def test_findBoard(self):
        data = {
            'top-left': {
                'value': ''
            },
            'top-center': {
                'value': ''
            },
            'top-right': {
                'value': ''
            },
            'middle-left': {
                'value': ''
            },
            'middle-center': {
                'value': ''
            },
            'middle-right': {
                'value': ''
            },
            'bottom-left': {
                'value': ''
            },
            'bottom-center': {
                'value': ''
            },
            'bottom-right': {
                'value': ''
            },
        }

        board1 = Board(data, 'x', 'o')

        data = {
            'top-left': {
                'value': 'x'
            },
            'top-center': {
                'value': ''
            },
            'top-right': {
                'value': ''
            },
            'middle-left': {
                'value': ''
            },
            'middle-center': {
                'value': ''
            },
            'middle-right': {
                'value': ''
            },
            'bottom-left': {
                'value': ''
            },
            'bottom-center': {
                'value': ''
            },
            'bottom-right': {
                'value': ''
            },
        }

        board2 = Board(data, 'x', 'o')

        boards = [board1, board2]

        self.game.board.moves = boards

        data = {'top-left': {'value': 'x'}}

        foundBoard = Game.findBoard(self.game.board, data)

        self.assertEqual(foundBoard, board2)