Example #1
0
def playMove():
    '''contain:, x, y, id'''
    requestBody = json.loads(request.get_data())
    print(requestBody)
    '''START OF VALIDITY CHECKS'''
    game = None
    rbid = requestBody['id']
    #Safari issue fix
    if '=' in requestBody['id']:
        rbid = requestBody['id'].replace('=', '')
    #get data from mongodb
    games = mongo.db.AISinglePlayer
    gameDict = games.find_one({'gameID': rbid})
    game = Game(gameDict)
    if not gameDict:
        return jsonify({'err_msg': 'No game found'}), 404
    #Check if the board has a winner and return winner
    if game.gameOver:
        return jsonify({'err_msg': 'The game is over!'}), 401
    '''END OF VALIDITY CHECKS '''
    '''Add the move to the game board'''
    x = int(requestBody['x'])
    y = int(requestBody['y'])
    try:
        game.makeMove(x, y)
    except (ValidationFailed) as e:
        jsonify(e.body), e.status_code

    game.checkWinner('white')
    comp_row, comp_col = game.makeAIMove(x, y)
    game.checkWinner('black')

    winner = game.winner
    if game.winner is None:
        winner = ''

    #update the data in mongodb
    gameDictRes = game.__dict__
    myquery = {'gameID': rbid}
    newvalues = {"$set": gameDictRes}
    games.update_one(myquery, newvalues)
    print(game.gameOver)
    return jsonify({
        'Computer_Move_Row': comp_row,
        'Computer_Move_Col': comp_col,
        'Winner': winner,
        'GameOver': game.gameOver
    }), 201
Example #2
0
def testAI():
    game_test = Game()
    game_test.playerColor = 'white'
    board_test = []
    for x in range(0, 19):
        board_test.append([''] * 19)

    game_test.board = board_test
    game_test.move = 4
    board_test[0][0] = 'white'
    board_test[1][1] = 'white'
    board_test[2][2] = 'white'
    board_test[3][3] = 'white'
    ''' board_test[16][17] = 'black'
    board_test[15][18] = 'black'
    board_test[18][1] = 'black'
    board_test[18][2] = 'black' '''

    game_test.board = board_test

    comp_row, comp_col = game_test.makeAIMove(5, 6)
    print(board_test)
    return jsonify({'comp_row': comp_row, 'comp_col': comp_col}), 200