コード例 #1
0
        public async Task <ActionResult <EndGameItem> > PostGameItem(string id, MovementItem item)
        {
            var board = _context.BoardItems.Find(item.id);

            EndGameItem endGameItem = Util.CheckErrors(board, id, item);

            if (endGameItem != null)
            {
                return(endGameItem);
            }

            string oldPositionIdentifier = item.position.X.ToString() + item.position.Y.ToString() + "_";
            var    positionValue         = board.boardData.FirstOrDefault(x => x == oldPositionIdentifier);

            if (positionValue != null)
            {
                int keyIndex = Array.IndexOf(board.boardData, oldPositionIdentifier);

                string newPositionIdentifier = item.position.X.ToString() + item.position.Y.ToString() + item.player;
                board.internalBoardData = board.internalBoardData.Replace(oldPositionIdentifier, newPositionIdentifier);

                if (item.player == 'X')
                {
                    board.nextPlayer = 'O';
                }
                else
                {
                    board.nextPlayer = 'X';
                }

                await _context.SaveChangesAsync();

                endGameItem = Util.CheckIfGameIsOver(board, item.player);
                if (endGameItem != null)
                {
                    return(endGameItem);
                }
            }
            else
            {
                EndGameItem endGameReturn = new EndGameItem();
                endGameReturn.msg = "Posição já preenchida, jogue novamente.";

                return(endGameReturn);
            }

            return(Ok());
        }
コード例 #2
0
        public static EndGameItem CheckIfGameIsOver(BoardItem board, char player)
        {
            if (IsDraw(board.internalBoardData))
            {
                EndGameItem endGameReturn = new EndGameItem();
                endGameReturn.msg    = "Partida finalizada";
                endGameReturn.winner = "Draw";

                return(endGameReturn);
            }
            else
            if (HasWinner(board.boardData))
            {
                EndGameItem endGameReturn = new EndGameItem();
                endGameReturn.msg    = "Partida finalizada";
                endGameReturn.winner = player.ToString();

                return(endGameReturn);
            }

            return(null);
        }
コード例 #3
0
        public static EndGameItem CheckErrors(BoardItem board, string id, MovementItem item)
        {
            if (board == null)
            {
                EndGameItem endGameReturn = new EndGameItem();
                endGameReturn.msg = "Partida não encontrada";

                return(endGameReturn);
            }

            if (board.nextPlayer != item.player)
            {
                EndGameItem endGameReturn = new EndGameItem();
                endGameReturn.msg = "Não é turno do jogador";

                return(endGameReturn);
            }

            if (id != item.id)
            {
                EndGameItem endGameReturn = new EndGameItem();
                endGameReturn.msg = "Os id's não são iguais. Confira o id da URL e o id do input e tente novamente.";

                return(endGameReturn);
            }

            if (item.player != 'X' && item.player != 'O')
            {
                EndGameItem endGameReturn = new EndGameItem();
                endGameReturn.msg = "Os jogadores aceitos são X e O.";

                return(endGameReturn);
            }

            return(null);
        }