Ejemplo n.º 1
0
        public override void UpdateState(dynamic newState, HubCallerContext context)
        {
            var currentUserId = LobbyHub.UserMatches[context.ConnectionId];

            if (newState["turn"] != null)
            {
                if (PlayerTurns.Count > 0 && PlayerTurns.Peek() == currentUserId)
                {
                    return;
                }
                if (!BoardValues.ContainsKey(currentUserId))
                {
                    return;
                }

                var index = (int)newState["turn"]["index"];
                if (0 > index || index >= Board.Items.Length)
                {
                    return;
                }

                var boardItem = Board[index];
                if (boardItem.Value != GameBoard.BoardItem.BoardValue.None)
                {
                    return;
                }

                PlayerTurns.Push(currentUserId);
                boardItem.Value     = BoardValues[currentUserId];
                newState.turn.value = boardItem.Value;
                UpdateGameState(new OnGameStateUpdateArgs(newState, context));
                CheckFinshed();
            }
        }
Ejemplo n.º 2
0
        private void CheckFinshed()
        {
            Func <string, GameBoard.BoardItem> getItemFromPos = pos => Board.Items.First(x => x.Position == pos);

            for (int i = 1; i < 3; i++)
            {
                var value = (GameBoard.BoardItem.BoardValue)i;
                //#
                // #
                //  #
                if (getItemFromPos(GameBoard.BoardItem.TopLeft).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.MiddleMiddle).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.BottomRight).Value == value)
                {
                    EndGame(new OnGameEndArgs(OnGameEndArgs.GameWinReason.PlayerWin, PlayerTurns.Peek(), new
                    {
                        BoardWin = new [] { GameBoard.BoardItem.TopLeft, GameBoard.BoardItem.MiddleMiddle, GameBoard.BoardItem.BottomRight }
                    }));
                    return;
                }
                //  #
                // #
                //#
                if (getItemFromPos(GameBoard.BoardItem.TopRight).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.MiddleMiddle).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.BottomLeft).Value == value)
                {
                    EndGame(new OnGameEndArgs(OnGameEndArgs.GameWinReason.PlayerWin, PlayerTurns.Peek(), new
                    {
                        BoardWin = new[] { GameBoard.BoardItem.TopRight, GameBoard.BoardItem.MiddleMiddle, GameBoard.BoardItem.BottomLeft }
                    }));
                    return;
                }
                //###
                //
                //
                if (getItemFromPos(GameBoard.BoardItem.TopLeft).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.TopMiddle).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.TopRight).Value == value)
                {
                    EndGame(new OnGameEndArgs(OnGameEndArgs.GameWinReason.PlayerWin, PlayerTurns.Peek(), new
                    {
                        BoardWin = new[] { GameBoard.BoardItem.TopLeft, GameBoard.BoardItem.TopMiddle, GameBoard.BoardItem.TopRight }
                    }));
                    return;
                }
                //
                //###
                //
                if (getItemFromPos(GameBoard.BoardItem.MiddleLeft).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.MiddleMiddle).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.MiddleRight).Value == value)
                {
                    EndGame(new OnGameEndArgs(OnGameEndArgs.GameWinReason.PlayerWin, PlayerTurns.Peek(), new
                    {
                        BoardWin = new[] { GameBoard.BoardItem.MiddleLeft, GameBoard.BoardItem.MiddleMiddle, GameBoard.BoardItem.MiddleRight }
                    }));
                    return;
                }
                //
                //
                //###
                if (getItemFromPos(GameBoard.BoardItem.BottomLeft).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.BottomMiddle).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.BottomRight).Value == value)
                {
                    EndGame(new OnGameEndArgs(OnGameEndArgs.GameWinReason.PlayerWin, PlayerTurns.Peek(), new
                    {
                        BoardWin = new[] { GameBoard.BoardItem.BottomLeft, GameBoard.BoardItem.BottomMiddle, GameBoard.BoardItem.BottomRight }
                    }));
                    return;
                }
                //#
                //#
                //#
                if (getItemFromPos(GameBoard.BoardItem.TopLeft).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.MiddleLeft).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.BottomLeft).Value == value)
                {
                    EndGame(new OnGameEndArgs(OnGameEndArgs.GameWinReason.PlayerWin, PlayerTurns.Peek(), new
                    {
                        BoardWin = new[] { GameBoard.BoardItem.TopLeft, GameBoard.BoardItem.MiddleMiddle, GameBoard.BoardItem.BottomRight }
                    }));
                    return;
                }
                // #
                // #
                // #
                if (getItemFromPos(GameBoard.BoardItem.TopMiddle).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.MiddleMiddle).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.BottomMiddle).Value == value)
                {
                    EndGame(new OnGameEndArgs(OnGameEndArgs.GameWinReason.PlayerWin, PlayerTurns.Peek(), new
                    {
                        BoardWin = new[] { GameBoard.BoardItem.TopMiddle, GameBoard.BoardItem.MiddleMiddle, GameBoard.BoardItem.BottomMiddle }
                    }));
                    return;
                }
                //  #
                //  #
                //  #
                if (getItemFromPos(GameBoard.BoardItem.TopLeft).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.MiddleLeft).Value == value &&
                    getItemFromPos(GameBoard.BoardItem.BottomLeft).Value == value)
                {
                    EndGame(new OnGameEndArgs(OnGameEndArgs.GameWinReason.PlayerWin, PlayerTurns.Peek(), new
                    {
                        BoardWin = new[] { GameBoard.BoardItem.TopLeft, GameBoard.BoardItem.MiddleLeft, GameBoard.BoardItem.BottomLeft }
                    }));
                    return;
                }
            }
        }