コード例 #1
0
        [ProducesResponseType(typeof(int), StatusCodes.Status400BadRequest)] // Tells swagger that the response format will be an int for a BadRequest (400)
        public ActionResult <OutputPayload> ProcessComplexInput([FromBody] InputPayload inputPayload)
        {
            // Ensure valid player markers, valid board
            if (!(inputPayload.ValidPlayerMarkers()) ||
                (!(TicTacToe.BoardIsValid(inputPayload.gameBoard))))
            {
                return(BadRequest(400));
            }

            // Compute response
            TicTacToe ttt = new TicTacToe(inputPayload.gameBoard,
                                          inputPayload.azurePlayerSymbol, inputPayload.humanPlayerSymbol);
            WinStatus ws = ttt.winStatus;

            // If game isn't over, get the AI's next move
            int?nextMove;

            if (ws.winner.Equals(TicTacToe.GAME_NOT_DONE_STR))
            {
                int choice = ttt.MinimaxWithPruningMove();
                ttt[TicTacToe.IndexToTuple(choice)] = inputPayload.azurePlayerSymbol;
                nextMove = choice;

                // Recalculate win status
                ws = TicTacToe.GetWinStatus(ttt.board);
            }
            else
            {
                nextMove = null;
            }

            // Assemble response payload
            OutputPayload complexOutput = new OutputPayload()
            {
                move = nextMove,
                azurePlayerSymbol = inputPayload.azurePlayerSymbol,
                humanPlayerSymbol = inputPayload.humanPlayerSymbol,
                winner            = ws.winner,
                winPositions      = (ws.winPositions == null) ? null : ws.winPositions.ConvertAll(i => i.ToString()),
                gameBoard         = TicTacToe.BoardToList(ttt.board),
            };

            return(complexOutput);
        }