Example #1
0
        private async Task <Tuple <WinVector, bool, PlayerType> > CheckGameStatus()
        {
            WinVector  winVector = WinVector.NONE;
            PlayerType?player    = null;
            bool       isDraw    = false;

            var gameState = await StateManager.GetStateAsync <GameState>("GameState");

            var matrix     = gameState.Matrix;
            var nextPlayer = gameState.NextPlayer;

            player = PlayerType.Cross;
            if (!FindWinner(matrix, ref winVector, player.Value))
            {
                player = PlayerType.Zero;
                if (!FindWinner(matrix, ref winVector, player.Value))
                {
                    if (matrix[0][0] != null && matrix[0][1] != null && matrix[0][2] != null &&
                        matrix[1][0] != null && matrix[1][1] != null && matrix[1][2] != null &&
                        matrix[2][0] != null && matrix[2][1] != null && matrix[2][2] != null)
                    {
                        isDraw = true;
                    }
                }
            }

            return(new Tuple <WinVector, bool, PlayerType>(
                       winVector, isDraw, player.Value));
        }
        public void GameEnded(GameEndedInfo info)
        {
            switch (info.EventType)
            {
            case GameEndedEventType.Won:
                _winVector = info.WinVector;
                Invalidate();

                GameEndedCommon(info.Player, "{0} has won the game !");
                break;

            case GameEndedEventType.Tie:
                GameEndedCommon(info.Player, "It's a tie !");
                break;

            case GameEndedEventType.BailedOutEarly:
                GameEndedCommon(info.Player, "{0} has end the game.");
                break;

            case GameEndedEventType.TimedOut:
                GameEndedCommon(info.Player, "{0} has end the game.");
                break;
            }
        }
Example #3
0
        private static bool FindWinner(
            MoveMetadata[][] moveMatrix,
            ref WinVector winVector,
            PlayerType player)
        {
            // Horizontal
            for (byte i = 0; i <= 2; i++)
            {
                if ((moveMatrix[i][0] != null && moveMatrix[i][0].Player == player) &&
                    (moveMatrix[i][1] != null && moveMatrix[i][1].Player == player) &&
                    (moveMatrix[i][2] != null && moveMatrix[i][2].Player == player))
                {
                    if (i == 0)
                    {
                        winVector = WinVector.TOP;
                    }
                    else if (i == 1)
                    {
                        winVector = WinVector.CENTER;
                    }
                    else if (i == 2)
                    {
                        winVector = WinVector.BOTTOM;
                    }

                    return(true);
                }
            }

            // Vertical
            for (byte i = 0; i <= 2; i++)
            {
                if ((moveMatrix[0][i] != null && moveMatrix[0][i].Player == player) &&
                    (moveMatrix[1][i] != null && moveMatrix[1][i].Player == player) &&
                    (moveMatrix[2][i] != null && moveMatrix[2][i].Player == player))
                {
                    if (i == 0)
                    {
                        winVector = WinVector.LEFT;
                    }
                    else if (i == 1)
                    {
                        winVector = WinVector.MIDDLE;
                    }
                    else if (i == 2)
                    {
                        winVector = WinVector.RIGHT;
                    }

                    return(true);
                }
            }


            // Diagonal
            if ((moveMatrix[0][0] != null && moveMatrix[0][0].Player == player) &&
                (moveMatrix[1][1] != null && moveMatrix[1][1].Player == player) &&
                (moveMatrix[2][2] != null && moveMatrix[2][2].Player == player))
            {
                winVector = WinVector.BACK_DIAGONAL;
                return(true);
            }

            if ((moveMatrix[0][2] != null && moveMatrix[0][2].Player == player) &&
                (moveMatrix[1][1] != null && moveMatrix[1][1].Player == player) &&
                (moveMatrix[2][0] != null && moveMatrix[2][0].Player == player))
            {
                winVector = WinVector.FORWARD_DIAGONAL;
                return(true);
            }

            return(false);
        }
Example #4
0
        private void CheckGameStatus(MoveMetadata moveMetadata)
        {
            WinVector winVector = WinVector.NONE;

            // Horizontal
            for (byte i = 0; i <= 2; i++)
            {
                if ((this._moveMatrix[i, 0] != null && this._moveMatrix[i, 0].Player == moveMetadata.Player) &&
                    (this._moveMatrix[i, 1] != null && this._moveMatrix[i, 1].Player == moveMetadata.Player) &&
                    (this._moveMatrix[i, 2] != null && this._moveMatrix[i, 2].Player == moveMetadata.Player))
                {
                    if (i == 0)
                    {
                        winVector = WinVector.TOP;
                    }
                    else if (i == 1)
                    {
                        winVector = WinVector.CENTER;
                    }
                    else if (i == 2)
                    {
                        winVector = WinVector.BOTTOM;
                    }

                    this._winVectorsToPaint.Add(winVector);

                    // There can't be move than one filled horizontal rows.
                    break;
                }
            }

            // Vertical
            for (byte i = 0; i <= 2; i++)
            {
                if ((this._moveMatrix[0, i] != null && this._moveMatrix[0, i].Player == moveMetadata.Player) &&
                    (this._moveMatrix[1, i] != null && this._moveMatrix[1, i].Player == moveMetadata.Player) &&
                    (this._moveMatrix[2, i] != null && this._moveMatrix[2, i].Player == moveMetadata.Player))
                {
                    if (i == 0)
                    {
                        winVector = WinVector.LEFT;
                    }
                    else if (i == 1)
                    {
                        winVector = WinVector.MIDDLE;
                    }
                    else if (i == 2)
                    {
                        winVector = WinVector.RIGHT;
                    }

                    this._winVectorsToPaint.Add(winVector);

                    // There can't be move than one filled vertical rows.
                    break;
                }
            }

            // Diagonal
            if ((this._moveMatrix[0, 0] != null && this._moveMatrix[0, 0].Player == moveMetadata.Player) &&
                (this._moveMatrix[1, 1] != null && this._moveMatrix[1, 1].Player == moveMetadata.Player) &&
                (this._moveMatrix[2, 2] != null && this._moveMatrix[2, 2].Player == moveMetadata.Player))
            {
                winVector = WinVector.BACK_DIAGONAL;
                this._winVectorsToPaint.Add(winVector);
            }

            if ((this._moveMatrix[0, 2] != null && this._moveMatrix[0, 2].Player == moveMetadata.Player) &&
                (this._moveMatrix[1, 1] != null && this._moveMatrix[1, 1].Player == moveMetadata.Player) &&
                (this._moveMatrix[2, 0] != null && this._moveMatrix[2, 0].Player == moveMetadata.Player))
            {
                winVector = WinVector.FORWARD_DIAGONAL;
                this._winVectorsToPaint.Add(winVector);
            }

            if (this._winVectorsToPaint.Count > 0)
            {
                Won(moveMetadata.Player);
                return;
            }

            // Draw
            if (this._moveMatrix[0, 0] != null &
                this._moveMatrix[0, 1] != null &
                this._moveMatrix[0, 2] != null &
                this._moveMatrix[1, 0] != null &
                this._moveMatrix[1, 1] != null &
                this._moveMatrix[1, 2] != null &
                this._moveMatrix[2, 0] != null &
                this._moveMatrix[2, 1] != null &
                this._moveMatrix[2, 2] != null &
                this._winVectorsToPaint.Count == 0)
            {
                Draw();
            }
        }