Exemplo n.º 1
0
        public GameState Shoot(string gameId, int row, int column)
        {
            if (_shots.Count > 100)
                throw new Exception("Too many shots!");

            Debug.Assert(gameId == "42");

            var shot = new CellCoords(row, column);
            Debug.Assert(!_shots.Contains(shot));
            _shots.Add(shot);

            var hitShip = _ships
                .Where(s => s.IsSunken == false)
                .FirstOrDefault(s => s.PositionCells.Contains(shot));
            if (hitShip == null)
            {
                return new GameState { IsFinished = false, LastShot = ShotResult.Miss };
            }

            hitShip.HitCells.Add(shot);
            var state = new GameState {
                IsFinished = _ships.Count(s => s.IsSunken == false) == 0,
                LastShot = hitShip.IsSunken ? ShotResult.HitAndSunk : ShotResult.Hit
            };

            if (state.IsFinished)
            {
                CalculateScore(_shots.Count);
            }

            return state;
        }
Exemplo n.º 2
0
 /// <summary>
 /// Move the game to a new state. The current state is maintained
 /// so that it can be returned to.
 /// </summary>
 /// <param name="state">the new game state</param>
 public static void AddNewState(GameState state)
 {
     _state.Push(state);
     UtilityFunctions.Message = "";
 }
Exemplo n.º 3
0
 /// <summary>
 /// End the current state and add in the new state.
 /// </summary>
 /// <param name="newState">the new state of the game</param>
 public static void SwitchState(GameState newState)
 {
     EndCurrentState();
     AddNewState(newState);
 }