Exemple #1
0
        public async Task <bool> ResetGameAsync()
        {
            ActorState state = await StateManager.GetStateAsync <ActorState>("GameState");

            state.Board           = new int[9];
            state.Winner          = "";
            state.NextPlayerIndex = 0;
            state.NumberOfMoves   = 0;
            await StateManager.AddOrUpdateStateAsync <ActorState>("GameState", state, (key, value) => state);

            return(true);
        }
Exemple #2
0
        private async Task <bool> HasWon(int sum)
        {
            ActorState state = await StateManager.GetStateAsync <ActorState>("GameState");

            return(state.Board[0] + state.Board[1] + state.Board[2] == sum ||
                   state.Board[3] + state.Board[4] + state.Board[5] == sum ||
                   state.Board[6] + state.Board[7] + state.Board[8] == sum ||
                   state.Board[0] + state.Board[3] + state.Board[6] == sum ||
                   state.Board[1] + state.Board[4] + state.Board[7] == sum ||
                   state.Board[2] + state.Board[5] + state.Board[8] == sum ||
                   state.Board[0] + state.Board[4] + state.Board[8] == sum ||
                   state.Board[2] + state.Board[4] + state.Board[6] == sum);
        }
Exemple #3
0
        public async Task <bool> MakeMoveAsync(long playerId, int x, int y)
        {
            ActorState state = await StateManager.GetStateAsync <ActorState>("GameState");

            if (x < 0 || x > 2 || y < 0 || y > 2 || state.Players.Count != 2 || state.NumberOfMoves >= 9 || state.Winner != "")
            {
                return(false);
            }

            int index = state.Players.FindIndex(p => p.Item1 == playerId);

            if (index == state.NextPlayerIndex)
            {
                if (state.Board[y * 3 + x] == 0)
                {
                    int piece = index * 2 - 1;
                    state.Board[y * 3 + x] = piece;
                    state.NumberOfMoves++;

                    if (await HasWon(piece * 3))
                    {
                        state.Winner = state.Players[index].Item2 + " (" + (piece == -1 ? "X" : "O") + ")";
                    }
                    else if (state.Winner == "" && state.NumberOfMoves >= 9)
                    {
                        state.Winner = "TIE";
                    }

                    state.NextPlayerIndex = (state.NextPlayerIndex + 1) % 2;

                    await StateManager.AddOrUpdateStateAsync <ActorState>("GameState", state, (key, value) => state);

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Exemple #4
0
        public async Task <string> GetWinnerAsync()
        {
            ActorState state = await StateManager.GetStateAsync <ActorState>("GameState");

            return(state.Winner);
        }
Exemple #5
0
        public async Task <int[]> GetGameBoardAsync()
        {
            ActorState state = await StateManager.GetStateAsync <ActorState>("GameState");

            return(state.Board);
        }