Ejemplo n.º 1
0
        public static bool PlayTurn(string player,
                                    int id, int outerRowIndex,
                                    int outerColIndex,
                                    int innerRowIndex,
                                    int innerColIndex,
                                    out NextMoveState nextMove)
        {
            nextMove = null;

            if (!_games.TryGetValue(id, out var game))
            {
                return(false);
            }

            if (game.Status == GameStatus.Completed)
            {
                return(false);
            }

            return(game.Play(player, outerRowIndex, outerColIndex, innerRowIndex, innerColIndex, out nextMove));
        }
Ejemplo n.º 2
0
        public bool Play(
            string player,
            int outerRowIndex,
            int outerColIndex,
            int innerRowIndex,
            int innerColIndex,
            out NextMoveState nextMove)
        {
            var board         = Board.Boards[outerRowIndex][outerColIndex];
            var expectedBoard = NextBoardPosition.IsValid ? Board.Boards[NextBoardPosition.Row][NextBoardPosition.Column] : null;
            var cell          = board.Cells[innerRowIndex][innerColIndex];

            nextMove = new NextMoveState();

            if (cell != 0)
            {
                // Can't play over an existing cell
                return(false);
            }

            if (expectedBoard != null && board != expectedBoard)
            {
                // Invalid move, the next player has to play in the specific board
                return(false);
            }

            if (board.IsFull)
            {
                // This board is done
                return(false);
            }

            nextMove.CellValue = GetPlayerValue(player);
            string playerTurn = null;

            switch (nextMove.CellValue)
            {
            case 0:
                return(false);

            case 1:
                playerTurn = Player2;
                break;

            case 2:
                playerTurn = Player1;
                break;
            }

            board.Play(innerRowIndex, innerColIndex, nextMove.CellValue);

            if (board.IsFull)
            {
                // Remove a board if we can no longer play here
                Board.RemoveBoard();
            }

            var nextBoard = Board.Boards[innerRowIndex][innerColIndex];

            if (nextBoard.IsFull)
            {
                NextBoardPosition = new BoardPosition(-1, -1);
            }
            else
            {
                NextBoardPosition = new BoardPosition(innerRowIndex, innerColIndex);
            }

            Board.CheckWinner();

            if (Winner != null || Board.IsFull)
            {
                Status = GameStatus.Completed;
            }

            nextMove.OuterRowIndex     = outerRowIndex;
            nextMove.OuterColIndex     = outerColIndex;
            nextMove.InnerRowIndex     = innerRowIndex;
            nextMove.InnerColIndex     = innerColIndex;
            nextMove.PlayerTurn        = playerTurn;
            nextMove.NextBoardPosition = NextBoardPosition;

            nextMove.GameStatus = Status;
            nextMove.GameWinner = Winner;

            nextMove.GameBoardWinner = Board.Winner;
            nextMove.GameBoardIsFull = Board.IsFull;

            nextMove.BoardIsFull = board.IsFull;
            nextMove.BoardWinner = board.Winner;

            PlayerTurn = playerTurn;

            return(true);
        }