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); }
public async Task <bool> JoinGameAsync(long playerId, string playerName) { var state = (await StateManager.TryGetStateAsync <ActorState>(GameState)).Value; if (state.Players.Count >= 2 || state.Players.Any(p => p.Item2 == playerName)) { return(false); } state.Players.Add(new Tuple <long, string>(playerId, playerName)); await StateManager.AddOrUpdateStateAsync <ActorState>(GameState, state, (k, v) => state); return(true); }
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); } }
private async Task SetActorState(ActorState state) { await StateManager.AddOrUpdateStateAsync <ActorState>(STATE_KEY, state, (ke, v) => state); return; }