public void MakeShipsTest() { ShipBuilder sb = new ShipBuilder(); Fleet fleet = sb.MakeFleet(); Assert.AreEqual(Fleet.ShipLengths.Length, fleet.NumberOfShips); for (int i = 0; i < fleet.NumberOfShips; ++i) { Ship ship = fleet.GetShips()[i]; int left = ship.Squares[0].Column - 1; if (left < 0) left = 0; int top = ship.Squares[0].Row - 1; if (top < 0) top = 0; int right = ship.Squares[ship.Squares.Length - 1].Column + 1; if (right >= Grid.NumberOfColumns) right = Grid.NumberOfColumns - 1; int bottom = ship.Squares[ship.Squares.Length - 1].Row + 1; if (bottom >= Grid.NumberOfRows) bottom = Grid.NumberOfRows - 1; for (int j = i + 1; j < fleet.NumberOfShips; ++j) { Ship shipToCompare = fleet.GetShips()[j]; foreach (Square square in shipToCompare.Squares) Assert.IsFalse(IsSquareInside(square, top, left, bottom, right)); } } }
public void Play() { ShipBuilder sb = new ShipBuilder(); Fleet fleet = sb.MakeFleet(); Gunnary gunnary = new Gunnary(); List<int> opponentShipsLeft = new List<int>(Fleet.ShipLengths); Players currentPlayer = (Players)(new Random().Next(Enum.GetValues(typeof(Players)).Length)); bool noWinner = true; do { currentPlayer = SwitchPlayers(currentPlayer); switch (currentPlayer) { case Players.Me: int[] rc = ReadNextTarget(); HitResults hitResult = fleet.Fire(rc[0], rc[1]); Console.WriteLine(hitResult.ToString().ToUpper()); // check if all ships are sunken if (hitResult == HitResults.Sunk) { opponentShipsLeft.Remove(fleet.LastSunkenShip.Squares.Length); if (opponentShipsLeft.Count == 0) noWinner = false; } break; case Players.Computer: Console.Write("My next target is: "); Square nextTarget = gunnary.NextTarget(); Console.WriteLine(string.Format("{0} {1}", (char)(nextTarget.Column + 'A'), nextTarget.Row + 1)); hitResult = ReadHitResult(); gunnary.ProcessHitResult(hitResult); if (gunnary.AnyShipLeft() == false) noWinner = false; break; default: throw new ArgumentOutOfRangeException(); } } while (noWinner == true); Console.WriteLine("And the winner is: {0}", currentPlayer); Console.ReadKey(); }