Ejemplo n.º 1
0
    public ActionResult <ExecuteMoveResponse> executeMove([FromBody] MessagePayload messagePayload)
    {
        // Check the format of the given message payload.
        // Return status 400 to the client if the format is incorrect.
        try
        {
            messagePayload.IsExpectedFormat();
        }
        catch (ArgumentException error)
        {
            return(BadRequest(error.Message));
        }

        // Check the initial state of the given game board.
        //
        // If the given game board contains a conclusive state,
        // return the result to the client without taking another turn.
        GameBoard gameBoard      = new GameBoard(messagePayload.gameBoard);
        string    earlyGameState = gameBoard.GetGameState();

        if (earlyGameState != Constants.INCONCLUSIVE)
        {
            return(GenerateExecuteMoveResponse(earlyGameState, messagePayload.move, gameBoard, true, messagePayload.azurePlayerSymbol, messagePayload.humanPlayerSymbol));
        }

        // If the given game board has an inconclusive state, tell Azure to take its turn.
        Player    azure        = new Player(messagePayload.azurePlayerSymbol, messagePayload.humanPlayerSymbol, gameBoard);
        GameBoard newGameBoard = azure.ExecuteMove();

        // Check the state of the game board after Azure's turn.
        // Use the new game state to generate and return the result to the client.
        string gameState = newGameBoard.GetGameState();

        return(GenerateExecuteMoveResponse(gameState, azure.lastMove, newGameBoard, false, messagePayload.azurePlayerSymbol, messagePayload.humanPlayerSymbol));
    }