static void Main() { var tasks = typeof(Battleship).Assembly.GetTypes() .Where(type => type.IsSubclassOf(typeof(Battleship))) .Select(type => (Battleship)Activator.CreateInstance(type)) .Where(inst => inst.Overriden) .OrderBy(i => i.Index) .ToList(); while (true) { Console.Clear(); Battleship task = Select(tasks); Console.Clear(); if (task == null) { return; } try { task.Solve(); } catch (Exception e) { Console.Clear(); Console.WriteLine(e); Console.ReadKey(); } } }
public void PlaceBattleship(Player player) { Battleship battleship = new Battleship(); display.PrintBoard(player.grids[0], player); GetShipInfo(player, battleship); }
public void PlaceShip(Submarine submarine, Battleship battleship, AircraftCarrier aircraftCarrier) { locationSet = false; location = ClearLocation(location); Console.WriteLine($"Where would you like to place your {name}?"); Console.WriteLine($"This ship requires {size} spots. Pleae give the first spots x coordinate:"); string x1 = Console.ReadLine(); Console.WriteLine($"Now, pleae give the first spots y coordinate:"); string y1 = Console.ReadLine(); Console.WriteLine($"This ship requires {size} spots. Pleae give the second spots x coordinate:"); string x2 = Console.ReadLine(); Console.WriteLine($"Now, pleae give the second spots y coordinate:"); string y2 = Console.ReadLine(); if (!GoodCoordinates(x1, y1, x2, y2, submarine, battleship, aircraftCarrier)) { Console.WriteLine($"I'm sorry, it doesn't seem like those coordinates work for the {name}. Let's try again."); location = ClearLocation(location); PlaceShip(submarine, battleship, aircraftCarrier); } if (!locationSet) { SetCoordinates(x1, y1, x2, y2); } }
static void Main(string[] args) { Console.WriteLine("Starting Battleship"); IBattleship battleship = new Battleship(); battleship.StartBattleship(); }
public void StartMenu() { Console.WriteLine("==============================BATTLESHIPS==============================\n\n"); Console.WriteLine(" # # ( )"); Console.WriteLine(" ___#_#___|__"); Console.WriteLine(" _ |____________| _"); Console.WriteLine(" _=====| | | | | |==== _"); Console.WriteLine(" =====| |.---------------------------. | |===="); Console.WriteLine(" <--------------------' . . . . . . . . '--------------/"); Console.WriteLine(" \\ /"); Console.WriteLine(" \\_______________________________________________WWS_________/"); Console.WriteLine(" wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"); Console.WriteLine(" wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"); Console.WriteLine("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"); Console.WriteLine(" wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"); Console.WriteLine("ASCII Ship from http://ascii.co.uk/art/battleship\n\n"); Console.WriteLine("(S)ingle Player"); Console.WriteLine("(M)ultiplayer"); Console.WriteLine("(I)mport game"); Console.WriteLine(); Console.WriteLine(String.Format("© Thomas Rogers 1982.")); ConsoleKey choice = ConsoleKey.S; while (!SelectOption(Console.ReadKey(), out choice)) { Console.WriteLine("Invalid selection, please choose again."); } Console.Clear(); string file = null; string name; if (choice == ConsoleKey.I) { file = ImportMenu(); } else { Console.WriteLine("What is Player 1's name?"); name = Console.ReadLine(); name = name != "" ? name : "Player 1"; player1 = new Player(name); if (choice == ConsoleKey.S) { player2 = new AI(); } else if (choice == ConsoleKey.M) { Console.WriteLine("What is Player 2's name?"); name = Console.ReadLine(); name = name != "" ? name : "Player 2"; player2 = new Player(Console.ReadLine()); } } battleShip = new Battleship(player1, player2, file); }
//constr public AIPlayer(int playerNumber, int size) { rng = new Random(); BoardSize = size; PlayerNumber = playerNumber; MyBoard = new Board(true, size); MyEnemyBoard = new Board(false, size); MyShips = new List <Ship>(); ShipsIveSunk = new List <Ship>(); LastShipsIveSunk = new List <Ship> { }; MyDestroyer = new Destroyer(PlayerNumber); MyShips.Add(MyDestroyer); MySub = new Submarine(PlayerNumber); MyShips.Add(MySub); MyBattleship = new Battleship(PlayerNumber); MyShips.Add(MyBattleship); MyCarrier = new Carrier(PlayerNumber); MyShips.Add(MyCarrier); MyBoard.FillBoard(); MyEnemyBoard.FillBoard(); HasUnplacedShips = true; HasShipsAfloat = true; LastGuess = new int[2]; PreviousLastGuess = new int[2]; if (PlayerNumber == 1) { DesyncRandoms(); } }
public Board() { GetBoardSize(); destroyer = new Destroyer(); submarine = new Submarine(); battleship = new Battleship(); aircraftCarrier = new AircraftCarrier(); }
public static void setShipArray(Ship ship, int[] arr, int axisValue, string cons) { if (arr.Length == 2) { Scout.SetArray(arr[0], arr[1], axisValue, cons, (Scout)ship); } else { Battleship.SetArray(arr[0], arr[1], arr[2], arr[3], axisValue, cons, (Battleship)ship); } }
public Gameboard() { playerGameBoard = new string[21, 21]; opponentGameBoard = new string[21, 21]; PopulateEmptyBoard(opponentGameBoard); PopulateEmptyBoard(playerGameBoard); destroyer = new Destroyer(); submarine = new Submarine(); carrier = new Carrier(); battleship = new Battleship(); }
public Result AddShip(Battleship battleship) { var result = CanAddShip(battleship); if (result.IsSuccess) { _battleships.Add(battleship); } return(result); }
public void BattleshipPositionIsCorrectForVerticalOrientation() { var battleshipSpaces = 4; var startCoordinate = new Coordinate(1, 1); var battleship = new Battleship.Battleship(startCoordinate, Orientation.Horizontal, battleshipSpaces); var expectedEndCoordinate = new Coordinate(startCoordinate.XCoordinate + battleshipSpaces, startCoordinate.YCoordinate); battleship.Position.StartCoordinate.Should().Be(startCoordinate); battleship.Position.EndCoordinate.Should().Be(expectedEndCoordinate); }
public Result AddShip(int shipSize, Orientation orientation, int horizontalCoordinate, int verticalCoordinate) { var coordinate = new Coordinate(horizontalCoordinate, verticalCoordinate); var battleship = new Battleship(coordinate, orientation, shipSize); var result = IsPositionValid(battleship); if (result.IsSuccess) { result = _fleet.AddShip(battleship); } return(result); }
public Player() { gameboard = new Gameboard(); name = Console.ReadLine(); Battleship battleship = new Battleship(); Carrier carrier = new Carrier(); Submarine submarine = new Submarine(); Destroyer destroyer = new Destroyer(); allShips.Add(battleship); allShips.Add(carrier); allShips.Add(submarine); allShips.Add(destroyer); }
public bool NoOtherShipConflict(Destroyer destroyer, Submarine submarine, Battleship battleship) { if (destroyer.GetLocationIsSet()) { for (int i = 0; i < destroyer.location.GetLength(0); i++) { if ((destroyer.location[i, 0] == location[0, 0] && destroyer.location[i, 1] == location[0, 0]) || (destroyer.location[i, 0] == location[1, 0] && destroyer.location[i, 1] == location[1, 1]) || (destroyer.location[i, 0] == location[2, 0] && destroyer.location[i, 1] == location[2, 1]) || (destroyer.location[i, 0] == location[3, 0] && destroyer.location[i, 1] == location[3, 1]) || (destroyer.location[i, 0] == location[4, 0] && destroyer.location[i, 1] == location[4, 1])) { return(false); } } } if (submarine.GetLocationIsSet()) { for (int i = 0; i < submarine.location.GetLength(0); i++) { if ((submarine.location[i, 0] == location[0, 0] && submarine.location[i, 1] == location[0, 0]) || (submarine.location[i, 0] == location[1, 0] && submarine.location[i, 1] == location[1, 1]) || (submarine.location[i, 0] == location[2, 0] && submarine.location[i, 1] == location[2, 1]) || (submarine.location[i, 0] == location[3, 0] && submarine.location[i, 1] == location[3, 1]) || (submarine.location[i, 0] == location[4, 0] && submarine.location[i, 1] == location[4, 1])) { return(false); } } } if (battleship.GetLocationIsSet()) { for (int i = 0; i < battleship.location.GetLength(0); i++) { if ((battleship.location[i, 0] == location[0, 0] && battleship.location[i, 1] == location[0, 0]) || (battleship.location[i, 0] == location[1, 0] && battleship.location[i, 1] == location[1, 1]) || (battleship.location[i, 0] == location[2, 0] && battleship.location[i, 1] == location[2, 1]) || (battleship.location[i, 0] == location[3, 0] && battleship.location[i, 1] == location[3, 1]) || (battleship.location[i, 0] == location[4, 0] && battleship.location[i, 1] == location[4, 1])) { return(false); } } } return(true); }
private Result IsPositionValid(Battleship newlyAddedBattleship) { var shipStartingPositionOutsideBoard = IsShipStartingPositionOutsideBoard(newlyAddedBattleship); if (shipStartingPositionOutsideBoard) { return(Result.Fail(ShipStartPositionViolationMessage)); } var shipDoesNotFitOnBoard = DoesShipFitOnBoard(newlyAddedBattleship); if (shipDoesNotFitOnBoard) { return(Result.Fail(ShipDoesNotFitViolationMessage)); } return(Result.Ok()); }
public void WhenAllOccupiedPositionOnTheShipAreDamaged_ShipIsSunk() { var battleshipSpaces = 2; var orientation = Orientation.Horizontal; var xCoordinate = 1; var yCoordinate = 1; var startCoordinate = new Coordinate(xCoordinate, yCoordinate); var battleship = new Battleship.Battleship(startCoordinate, orientation, battleshipSpaces); var response = battleship.IncomingAttack(startCoordinate); response.Should().Be(TurnResponse.Hit); battleship.Status.Should().Be(ShipStatus.Alive); response = battleship.IncomingAttack(new Coordinate(xCoordinate + 1, yCoordinate)); response.Should().Be(TurnResponse.Hit); battleship.Status.Should().Be(ShipStatus.Sunk); }
//MembVars //Constr public HumanPlayer(int playerNumber, int size) { PlayerNumber = playerNumber; BoardSize = size; MyBoard = new Board(true, size); MyEnemyBoard = new Board(false, size); MyShips = new List <Ship>(); ShipsIveSunk = new List <Ship>(); MyDestroyer = new Destroyer(PlayerNumber); MyShips.Add(MyDestroyer); MySub = new Submarine(PlayerNumber); MyShips.Add(MySub); MyBattleship = new Battleship(PlayerNumber); MyShips.Add(MyBattleship); MyCarrier = new Carrier(PlayerNumber); MyShips.Add(MyCarrier); MyBoard.FillBoard(); MyEnemyBoard.FillBoard(); HasUnplacedShips = true; HasShipsAfloat = true; }
public bool NoOtherShipConflict(Destroyer destroyer, Battleship battleship, AircraftCarrier aircraftCarrier) { if (destroyer.GetLocationIsSet()) { for (int i = 0; i < destroyer.location.GetLength(0); i++) { if ((destroyer.location[i, 0] == location[0, 0] && destroyer.location[i, 1] == location[0, 0]) || (destroyer.location[i, 0] == location[1, 0] && destroyer.location[i, 1] == location[1, 1]) || (destroyer.location[i, 0] == location[2, 0] && destroyer.location[i, 1] == location[2, 1])) { return(false); } } } if (battleship.GetLocationIsSet()) { for (int i = 0; i < battleship.location.GetLength(0); i++) { if ((battleship.location[i, 0] == location[0, 0] && battleship.location[i, 1] == location[0, 0]) || (battleship.location[i, 0] == location[1, 0] && battleship.location[i, 1] == location[1, 1]) || (battleship.location[i, 0] == location[2, 0] && battleship.location[i, 1] == location[2, 1])) { return(false); } } } if (aircraftCarrier.GetLocationIsSet()) { for (int i = 0; i < aircraftCarrier.location.GetLength(0); i++) { if ((aircraftCarrier.location[i, 0] == location[0, 0] && aircraftCarrier.location[i, 1] == location[0, 0]) || (aircraftCarrier.location[i, 0] == location[1, 0] && aircraftCarrier.location[i, 1] == location[1, 1]) || (aircraftCarrier.location[i, 0] == location[2, 0] && aircraftCarrier.location[i, 1] == location[2, 1])) { return(false); } } } return(true); }
public bool AttackGrid(Battleship battleship, int number, int letter) { Console.Clear(); Ship hitShip = null; Player player = battleship.CurrentPlayer; try { if (!GridLayout[number, letter].HasChecked) { if (GridLayout[number, letter].HasShip != null) { hitShip = GridLayout[number, letter].HasShip.Hit(); GridLayout[number, letter].HasChecked = true; Console.WriteLine("You hit a ship!"); return(true); } else { GridLayout[number, letter].HasChecked = true; //Changes the current player if the move hits (Will stay the same if it misses or has invalid input) battleship.CurrentPlayer = battleship.CurrentPlayer == battleship.Player1 ? battleship.Player2 : battleship.Player1; Console.WriteLine("You missed!"); return(false); } } else { Console.WriteLine("Move already made. Try again"); return(false); } } catch (IndexOutOfRangeException) { Console.WriteLine("Invalid Selection. Try again."); return(false); } }
// randomly generates initial ship positions for the AI private void SetupShips() { ShipPlacement randomPosition; // place the carrier randomPosition = PlaceShip(5); Carrier carrier = new Carrier(randomPosition.startPosition, randomPosition.horizontal); ships.Add(carrier); // place the battleship randomPosition = PlaceShip(4); Battleship battleship = new Battleship(randomPosition.startPosition, randomPosition.horizontal); ships.Add(battleship); // place the destroyer randomPosition = PlaceShip(3); Destroyer destroyer = new Destroyer(randomPosition.startPosition, randomPosition.horizontal); ships.Add(destroyer); // place the submarine randomPosition = PlaceShip(3); Submarine submarine = new Submarine(randomPosition.startPosition, randomPosition.horizontal); ships.Add(submarine); // place the PT boat randomPosition = PlaceShip(2); PTBoat ptboat = new PTBoat(randomPosition.startPosition, randomPosition.horizontal); ships.Add(ptboat); shipsSunk = 0; }
public bool NoOtherShipConflict(int x1, int y1, int x2, int y2, Submarine submarine, Battleship battleship, AircraftCarrier aircraftCarrier) { if (submarine.GetLocationIsSet()) { for (int i = 0; i < submarine.location.GetLength(0); i++) { if ((submarine.location[i, 0] == x1 && submarine.location[i, 1] == y1) || (submarine.location[i, 0] == x2 && submarine.location[i, 1] == y2)) { return(false); } } } if (battleship.GetLocationIsSet()) { for (int i = 0; i < battleship.location.GetLength(0); i++) { if ((battleship.location[i, 0] == x1 && battleship.location[i, 1] == y1) || (battleship.location[i, 0] == x2 && battleship.location[i, 1] == y2)) { return(false); } } } if (aircraftCarrier.GetLocationIsSet()) { for (int i = 0; i < aircraftCarrier.location.GetLength(0); i++) { if ((aircraftCarrier.location[i, 0] == x1 && aircraftCarrier.location[i, 1] == y1) || (aircraftCarrier.location[i, 0] == x2 && aircraftCarrier.location[i, 1] == y2)) { return(false); } } } return(true); }
private bool DoesShipFitOnBoard(Battleship newlyAddedBattleship) { return(newlyAddedBattleship.Position.EndCoordinate.XCoordinate >= _horizontalSize || newlyAddedBattleship.Position.EndCoordinate.YCoordinate >= _verticalSize); }
private bool IsShipStartingPositionOutsideBoard(Battleship newlyAddedBattleship) { return(newlyAddedBattleship.Position.StartCoordinate.XCoordinate >= _horizontalSize || newlyAddedBattleship.Position.StartCoordinate.YCoordinate >= _verticalSize); }
public void AddShips(int frontRow, int frontColumn, int backRow, int backColumn, int choiceOfShip) { string[,] establishedPositions = new string[20, 20]; string[,] proposedPosition = new string[20, 20]; bool isOverlapping = false; switch (choiceOfShip) { case 1: if (!destroyerIsChosen) { if (frontRow == backRow || frontColumn == backColumn) //this is a check for correct placement / not diagonal { if ((frontRow == backRow) && (frontColumn + 1 == backColumn || backColumn + 1 == frontColumn) || (frontColumn == backColumn) && (frontRow + 1 == backRow || backRow + 1 == frontRow)) // check for length { establishedPositions = GetPlacedShipPositions(); //overlapping logic begins proposedPosition = GetShipAboutToBePlacedsPosition(frontRow, frontColumn, backRow, backColumn); for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { if ((establishedPositions[i, j] == proposedPosition[i, j]) && (establishedPositions[i, j] != null)) { isOverlapping = true; } } } //overlapping logic ends if (!isOverlapping) //overlapping check { Ship destroyer = new Destroyer(); destroyer.frontRow = frontRow; destroyer.frontColumn = frontColumn; destroyer.backRow = backRow; destroyer.backColumn = backColumn; this.fleet.Add(destroyer); destroyerIsChosen = true; } else { Console.WriteLine("The destroyer is overlapping."); } } else { Console.WriteLine("The destroyer is not the correct length."); } } else { Console.WriteLine("The destroyer is diagonal."); } } else { Console.WriteLine("The destroyer has already been chosen."); } break; case 2: if (!submarineIsChosen) { if (frontRow == backRow || frontColumn == backColumn) //this is a check for correct placement / not diagonal { if ((frontRow == backRow) && (frontColumn + 2 == backColumn || backColumn + 2 == frontColumn) || (frontColumn == backColumn) && (frontRow + 2 == backRow || backRow + 2 == frontRow)) { establishedPositions = GetPlacedShipPositions(); proposedPosition = GetShipAboutToBePlacedsPosition(frontRow, frontColumn, backRow, backColumn); for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { if ((establishedPositions[i, j] == proposedPosition[i, j]) && (establishedPositions[i, j] != null)) { isOverlapping = true; } } } if (!isOverlapping) { Ship submarine = new Submarine(); submarine.frontRow = frontRow; submarine.frontColumn = frontColumn; submarine.backRow = backRow; submarine.backColumn = backColumn; this.fleet.Add(submarine); submarineIsChosen = true; } else { Console.WriteLine("The submarine is overlapping."); } } else { Console.WriteLine("The submarine is not the correct length."); } } else { Console.WriteLine("The submarine is diagonal."); } } else { Console.WriteLine("The submarine has already been chosen."); } break; case 3: if (!battleshipIsChosen) { if (frontRow == backRow || frontColumn == backColumn) //this is a check for correct placement / not diagonal { if ((frontRow == backRow) && (frontColumn + 3 == backColumn || backColumn + 3 == frontColumn) || (frontColumn == backColumn) && (frontRow + 3 == backRow || backRow + 3 == frontRow)) { establishedPositions = GetPlacedShipPositions(); proposedPosition = GetShipAboutToBePlacedsPosition(frontRow, frontColumn, backRow, backColumn); for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { if ((establishedPositions[i, j] == proposedPosition[i, j]) && (establishedPositions[i, j] != null)) { isOverlapping = true; } } } if (!isOverlapping) { Ship battleship = new Battleship(); battleship.frontRow = frontRow; battleship.frontColumn = frontColumn; battleship.backRow = backRow; battleship.backColumn = backColumn; this.fleet.Add(battleship); battleshipIsChosen = true; } else { Console.WriteLine("The battleship is overlapping."); } } else { Console.WriteLine("The battleship is not the correct length."); } } else { Console.WriteLine("The battleship is diagonal."); } } else { Console.WriteLine("The battleship has already been chosen."); } break; case 4: if (!aircraftCarrierIsChosen) { if (frontRow == backRow || frontColumn == backColumn) //this is a check for correct placement / not diagonal { if ((frontRow == backRow) && (frontColumn + 4 == backColumn || backColumn + 4 == frontColumn) || (frontColumn == backColumn) && (frontRow + 4 == backRow || backRow + 4 == frontRow)) { establishedPositions = GetPlacedShipPositions(); proposedPosition = GetShipAboutToBePlacedsPosition(frontRow, frontColumn, backRow, backColumn); for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { if ((establishedPositions[i, j] == proposedPosition[i, j]) && (establishedPositions[i, j] != null)) { isOverlapping = true; } } } if (!isOverlapping) { Ship aircraftCarrier = new AircraftCarrier(); aircraftCarrier.frontRow = frontRow; aircraftCarrier.frontColumn = frontColumn; aircraftCarrier.backRow = backRow; aircraftCarrier.backColumn = backColumn; this.fleet.Add(aircraftCarrier); aircraftCarrierIsChosen = true; } else { Console.WriteLine("The aircraft carrier is overlapping."); } } else { Console.WriteLine("The aircraft carrier is not the correct length."); } } else { Console.WriteLine("The aircraft carrier is diagonal."); } } else { Console.WriteLine("The aircraft carrier has already been chosen."); } break; default: Console.WriteLine("Error, please try again."); break; } }
public Logging(Battleship battleship) { this.battleship = battleship; }
private Result CanAddShip(Battleship battleship) { var positionIntersects = _battleships.Any(x => x.Position.DoesPositionIntersect(battleship.Position)); return(positionIntersects ? Result.Fail(ShipCannotBePlacedOnTopOfEachOtherMessage) : Result.Ok()); }
public bool GoodCoordinates(string stringx1, string stringy1, string stringx2, string stringy2, Submarine submarine, Battleship battleship, AircraftCarrier aircraftCarrier) { bool verifyCoordinates = false; int x1; int x2; int y1; int y2; if (CoordinateIsInt(stringx1) && CoordinateIsInt(stringx2) && CoordinateIsInt(stringy1) && CoordinateIsInt(stringy2)) { x1 = Convert.ToInt32(stringx1); y1 = Convert.ToInt32(stringy1); x2 = Convert.ToInt32(stringx2); y2 = Convert.ToInt32(stringy2); if (ValidBoardCoordinate(x1, y1) && ValidBoardCoordinate(x2, y2)) { if (ValidShipOnBoard(x1, y1, x2, y2)) { if (NoOtherShipConflict(x1, y1, x2, y2, submarine, battleship, aircraftCarrier)) { verifyCoordinates = true; } } } } return(verifyCoordinates); }
public bool GoodCoordinates(string stringx1, string stringy1, string stringx2, string stringy2, Destroyer destroyer, Submarine submarine, Battleship battleship) { bool verifyCoordinates = false; int x1; int x2; int y1; int y2; if (CoordinateIsInt(stringx1) && CoordinateIsInt(stringx2) && CoordinateIsInt(stringy1) && CoordinateIsInt(stringy2)) { x1 = Convert.ToInt32(stringx1); y1 = Convert.ToInt32(stringy1); x2 = Convert.ToInt32(stringx2); y2 = Convert.ToInt32(stringy2); if (ValidBoardCoordinate(x1, y1) && ValidBoardCoordinate(x2, y2)) { if (ValidShipOnBoard(x1, y1, x2, y2)) { FillShipLocation(x1, y1, x2, y2); if (NoOtherShipConflict(destroyer, submarine, battleship)) { verifyCoordinates = true; } } } } return(verifyCoordinates); }
public static void SetArray(int x1, int x2, int x3, int x4, int axisValue, string cons, Battleship ship) { //ShipArray[0] is bugged for some reason, this is temporary fix while I //figure out what is going on with the SetShipArray function ship.ShipArray[0] = x2 - 1; ship.ShipArray[1] = x2; ship.ShipArray[2] = x3; ship.ShipArray[3] = x4; ship.axisValue = axisValue; ship.axis = cons; }
public bool NoConflictWithOtherShips(Submarine Submarine, Battleship battleship, AircraftCarrier aircraftCarrier) { return(true); }