Exemplo n.º 1
0
        /// <summary>
        /// Determines if the given state is the winning one.
        /// </summary>
        /// <param name="session">GameSession object</param>
        /// <param name="field">Current click position</param>
        /// <param name="state">Current state (player)</param>
        /// <param name="size">Size of the grid</param>
        /// <returns>
        /// GridItemState.Empty if no winner has been found; 
        /// otherwise GridItemState.Player1 or GridItemState.Player1
        /// </returns>
        public static GridItemState GetWinningState(
            GameSession session, 
            Field field, 
            GridItemState state, 
            int size)
        {
            // TODO: Determine whether the given state is the winning one

            return GridItemState.Empty;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Responses to a player move.
        /// </summary>
        /// <param name="gameId">Game ID</param>
        /// <param name="currentPlayer">Current player index</param>
        /// <param name="field">Current move</param>
        public void SelectField(string gameId, int currentPlayer, Field field)
        {
            GameSession session = TicTacToeHelpers.GetGameSessionByGameId(Games, gameId);

            GridItemState currentState;

            if (currentPlayer == 0)
            {
                // Current turn: Player 1

                // Switch to Player 2.
                Clients[session.Player2Id].setOtherPlayerField(field);

                // Set the current state.
                currentState = GridItemState.Player1;
            }
            else
            {
                // Current turn: Player 2

                // Switch to Player 1.
                Clients[session.Player1Id].setOtherPlayerField(field);

                // Set the current state.
                currentState = GridItemState.Player2;
            }

            // Update the game grid.
            session.Grid[field.x, field.y] = currentState;

            // Determine if the current move yields the winning state.
            GridItemState winningState =
                TicTacToeHelpers.GetWinningState(session, field, currentState, Constants.SIZE);

            if (winningState != GridItemState.Empty)
            {
                // We have a winner!

                if (winningState == GridItemState.Player1)
                {
                    // Player 1 won!
                    Clients[session.Player1Id].addMessage("You won!");
                    Clients[session.Player2Id].addMessage("Player 1 won :-(");
                }
                else
                {
                    // Player 2 won!
                    Clients[session.Player2Id].addMessage("You won!");
                    Clients[session.Player1Id].addMessage("Player 2 won :-(");
                }

                Leave(gameId);

            }
            else
            {
                // No winner yet. Switch the turn.

                SetTurn(session, currentPlayer == 0 ? 1 : 0);
            }
        }