public void RecordResult(string player, MoveBase move) { try { var turnResult = _game.RecordTurn(player, move); if (turnResult.Result != TurnResult.Unknown) { if(_timer.Enabled) _timer.Stop(); if (_game.IsGameOver) OnGameOver(_game); else { OnTurnCompleted(_game, turnResult); _game.NewTurn(); OnTurnStarted(_game); } } } catch (Exception ex) { OnGameError(player, ex.Message); } }
public TurnData RecordTurn(string player, MoveBase move) { move = EnforceGameRules(player == Player1, move); var currentTurn = CurrentTurn; if (Player1 == player) { if (currentTurn.Player1Move != null) throw new ArgumentException(string.Format("Player 1 ({0}) already made a move.", player)); currentTurn.Player1Move = move; } else if (Player2 == player) { if (currentTurn.Player2Move != null) throw new ArgumentException(string.Format("Player 2 ({0}) already made a move.", player)); currentTurn.Player2Move = move; } else throw new ArgumentException(string.Format("Player ({0}) is not playing game ({1})", player, GameId)); return currentTurn; }
private MoveBase EnforceGameRules(bool isPlayer1, MoveBase move) { //Can't throw bubbles if they're not allowed if (move.Name == MoveNames.Bubbles && !_settings.AllowBubbles) return new NoMove(); //Can't throw dynamite if it's not allowed if (move.Name == MoveNames.Dynamite) { if(_settings.DynamiteCount == 0) return new NoMove(); int dynamiteTurns = 0; if(isPlayer1) dynamiteTurns = _turnData.Count(x => x.Player1Move != null && x.Player1Move.Name == MoveNames.Dynamite); else dynamiteTurns = _turnData.Count(x => x.Player2Move != null && x.Player2Move.Name == MoveNames.Dynamite); if (dynamiteTurns >= _settings.DynamiteCount) return new NoMove(); } return move; }
public static TurnResult DetermineWinner(MoveBase player1Move, MoveBase player2Move) { if (player1Move == null || player2Move == null) return TurnResult.Unknown; if(player1Move.Equals(player2Move)) return TurnResult.Tie; switch (player1Move.Name) { case MoveNames.NoMove: return TurnResult.Player2; case MoveNames.Rock: switch (player2Move.Name) { //Wins case MoveNames.NoMove: case MoveNames.Bubbles: case MoveNames.Scissors: return TurnResult.Player1; //Loses case MoveNames.Paper: case MoveNames.Dynamite: return TurnResult.Player2; default: throw new ArgumentException("Did not understand Player 2's move: " + player2Move.Name); } case MoveNames.Paper: switch (player2Move.Name) { //Wins case MoveNames.Rock: case MoveNames.NoMove: case MoveNames.Bubbles: return TurnResult.Player1; //Loses case MoveNames.Scissors: case MoveNames.Dynamite: return TurnResult.Player2; default: throw new ArgumentException("Did not understand Player 2's move: " + player2Move.Name); } case MoveNames.Scissors: switch (player2Move.Name) { //Wins case MoveNames.Paper: case MoveNames.NoMove: case MoveNames.Bubbles: return TurnResult.Player1; //Loses case MoveNames.Rock: case MoveNames.Dynamite: return TurnResult.Player2; default: throw new ArgumentException("Did not understand Player 2's move: " + player2Move.Name); } case MoveNames.Dynamite: switch (player2Move.Name) { //Wins case MoveNames.Rock: case MoveNames.Paper: case MoveNames.Scissors: case MoveNames.NoMove: return TurnResult.Player1; //Loses case MoveNames.Bubbles: return TurnResult.Player2; default: throw new ArgumentException("Did not understand Player 2's move: " + player2Move.Name); } case MoveNames.Bubbles: switch (player2Move.Name) { //Wins case MoveNames.Dynamite: return TurnResult.Player1; //Loses case MoveNames.NoMove: case MoveNames.Paper: case MoveNames.Rock: case MoveNames.Scissors: return TurnResult.Player2; default: throw new ArgumentException("Did not understand Player 2's move: " + player2Move.Name); } default: throw new ArgumentException("Did not understand Player 1's move: " + player1Move.Name); } }