public static Board PlayerBoard() { Board board = new Board(); for (int i = 0; i < 5; i++) { board.Ships[i] = ShipCreator.CreateShip((ShipType)i); } string[,] boardGrid = new string[11, 11]; boardGrid[0, 0] = " "; //grid that the user can see: columns boardGrid[0, 1] = "|_1_|"; boardGrid[0, 2] = "_2_|"; boardGrid[0, 3] = "_3_|"; boardGrid[0, 4] = "_4_|"; boardGrid[0, 5] = "_5_|"; boardGrid[0, 6] = "_6_|"; boardGrid[0, 7] = "_7_|"; boardGrid[0, 8] = "_8_|"; boardGrid[0, 9] = "_9_|"; boardGrid[0, 10] = "_10_|"; //grid that the user can see: rows boardGrid[1, 0] = " A |"; boardGrid[2, 0] = " B |"; boardGrid[3, 0] = " C |"; boardGrid[4, 0] = " D |"; boardGrid[5, 0] = " E |"; boardGrid[6, 0] = " F |"; boardGrid[7, 0] = " G |"; boardGrid[8, 0] = " H |"; boardGrid[9, 0] = " I |"; boardGrid[10, 0] = " J |"; boardGrid[1, 1] = " "; boardGrid[2, 1] = " "; boardGrid[3, 1] = " "; boardGrid[4, 1] = " "; boardGrid[5, 1] = " "; boardGrid[6, 1] = " "; boardGrid[7, 1] = " "; boardGrid[8, 1] = " "; boardGrid[9, 1] = " "; boardGrid[10, 1] = " "; boardGrid[1, 2] = " "; boardGrid[2, 2] = " "; boardGrid[3, 2] = " "; boardGrid[4, 2] = " "; boardGrid[5, 2] = " "; boardGrid[6, 2] = " "; boardGrid[7, 2] = " "; boardGrid[8, 2] = " "; boardGrid[9, 2] = " "; boardGrid[10, 2] = " "; boardGrid[1, 3] = " "; boardGrid[2, 3] = " "; boardGrid[3, 3] = " "; boardGrid[4, 3] = " "; boardGrid[5, 3] = " "; boardGrid[6, 3] = " "; boardGrid[7, 3] = " "; boardGrid[8, 3] = " "; boardGrid[9, 3] = " "; boardGrid[10, 3] = " "; boardGrid[1, 4] = " "; boardGrid[2, 4] = " "; boardGrid[3, 4] = " "; boardGrid[4, 4] = " "; boardGrid[5, 4] = " "; boardGrid[6, 4] = " "; boardGrid[7, 4] = " "; boardGrid[8, 4] = " "; boardGrid[9, 4] = " "; boardGrid[10, 4] = " "; boardGrid[1, 5] = " "; boardGrid[2, 5] = " "; boardGrid[3, 5] = " "; boardGrid[4, 5] = " "; boardGrid[5, 5] = " "; boardGrid[6, 5] = " "; boardGrid[7, 5] = " "; boardGrid[8, 5] = " "; boardGrid[9, 5] = " "; boardGrid[10, 5] = " "; boardGrid[1, 6] = " "; boardGrid[2, 6] = " "; boardGrid[3, 6] = " "; boardGrid[4, 6] = " "; boardGrid[5, 6] = " "; boardGrid[6, 6] = " "; boardGrid[7, 6] = " "; boardGrid[8, 6] = " "; boardGrid[9, 6] = " "; boardGrid[10, 6] = " "; boardGrid[1, 7] = " "; boardGrid[2, 7] = " "; boardGrid[3, 7] = " "; boardGrid[4, 7] = " "; boardGrid[5, 7] = " "; boardGrid[6, 7] = " "; boardGrid[7, 7] = " "; boardGrid[8, 7] = " "; boardGrid[9, 7] = " "; boardGrid[10, 7] = " "; boardGrid[1, 8] = " "; boardGrid[2, 8] = " "; boardGrid[3, 8] = " "; boardGrid[4, 8] = " "; boardGrid[5, 8] = " "; boardGrid[6, 8] = " "; boardGrid[7, 8] = " "; boardGrid[8, 8] = " "; boardGrid[9, 8] = " "; boardGrid[10, 8] = " "; boardGrid[1, 9] = " "; boardGrid[2, 9] = " "; boardGrid[3, 9] = " "; boardGrid[4, 9] = " "; boardGrid[5, 9] = " "; boardGrid[6, 9] = " "; boardGrid[7, 9] = " "; boardGrid[8, 9] = " "; boardGrid[9, 9] = " "; boardGrid[10, 9] = " "; boardGrid[1, 10] = " "; boardGrid[2, 10] = " "; boardGrid[3, 10] = " "; boardGrid[4, 10] = " "; boardGrid[5, 10] = " "; boardGrid[6, 10] = " "; boardGrid[7, 10] = " "; boardGrid[8, 10] = " "; boardGrid[9, 10] = " "; boardGrid[10, 10] = " "; board.DisplayBoard = boardGrid; return(board); }
public void ShouldFailToPlaceAShip_WhenIncorrectCorrectPositionsProvided( int boardRows, int boardColumns, int placementRow, int placementColumn, ShipType shipType) { //arrange //first create a board var boardCreator = new BoardCreator(); var board = boardCreator.CreateBoard(boardRows, boardColumns); //then create a ship var shipCreator = new ShipCreator(); var ship = shipCreator.CreateShip(shipType); //act //now place the ship on the board var shipPlacer = new ShipPlacer(); IndexOutOfRangeException ex = Assert.Throws <IndexOutOfRangeException>(() => shipPlacer.AddShipToBoard(ship, board, placementRow, placementColumn)); //assert Assert.Equal("Ship's placement position is out of bounds", ex.Message); }
public void ShouldNotReturnLostGameStatusTrue_WhenShipsNotSunk() { //arrange //first create a board var boardCreator = new BoardCreator(); var board = boardCreator.CreateBoard(10, 10); //then create a ship var shipCreator = new ShipCreator(); var ship = shipCreator.CreateShip(ShipType.Destroyer); //place the ship on the board var shipPlacer = new ShipPlacer(); shipPlacer.AddShipToBoard(ship, board, 0, 0); //act //now attack the ship at all given positions for the ship var attacker = new Attacker(); attacker.Attack(board, 0, 1); //assert that the status is not lost Assert.False(board.HasLost); }
public void ShouldPlaceAShip_WhenCorrectPositionsProvided( int boardRows, int boardColumns, int placementRow, int placementColumn, ShipType shipType) { //arrange //first create a board var boardCreator = new BoardCreator(); var board = boardCreator.CreateBoard(boardRows, boardColumns); //then create a ship var shipCreator = new ShipCreator(); var ship = shipCreator.CreateShip(shipType); //act //now place the ship on the board var shipPlacer = new ShipPlacer(); shipPlacer.AddShipToBoard(ship, board, placementRow, placementColumn); //assert /*check that the ship has been placed on the board and that the board's * status is occupied for the ship*/ Assert.True( board.BoardCellStatuses[placementRow, placementColumn] == BoardCellStatus.Occupied && board.BoardCellStatuses[placementRow, placementColumn + ship.Size - 1] == BoardCellStatus.Occupied ); }
public void ShouldReturnException_WhenIncorrectAttackCoordinatesProvided( int boardRows, int boardColumns, int placementRow, int placementColumn, int attackRow, int attackColumn, ShipType shipType) { //arrange //first create a board var boardCreator = new BoardCreator(); var board = boardCreator.CreateBoard(boardRows, boardColumns); //then create a ship var shipCreator = new ShipCreator(); var ship = shipCreator.CreateShip(shipType); //place the ship on the board var shipPlacer = new ShipPlacer(); shipPlacer.AddShipToBoard(ship, board, placementRow, placementColumn); //act //now attack the ship at the given position var attacker = new Attacker(); IndexOutOfRangeException ex = Assert.Throws <IndexOutOfRangeException>(() => attacker.Attack(board, attackRow, attackColumn)); //assert Assert.Equal("Attack position is out of bounds", ex.Message); }
public void ShouldReturnCorrectAttackStatus_WhenAttackLaunched( int boardRows, int boardColumns, int placementRow, int placementColumn, int attackRow, int attackColumn, ShipType shipType, BoardCellStatus boardCellStatus) { //arrange //first create a board var boardCreator = new BoardCreator(); var board = boardCreator.CreateBoard(boardRows, boardColumns); //then create a ship var shipCreator = new ShipCreator(); var ship = shipCreator.CreateShip(shipType); //place the ship on the board var shipPlacer = new ShipPlacer(); shipPlacer.AddShipToBoard(ship, board, placementRow, placementColumn); //act //now attack the ship at the given position var attacker = new Attacker(); attacker.Attack(board, attackRow, attackColumn); //assert /*check that the status on the board is hit*/ Assert.True( board.BoardCellStatuses[attackRow, attackColumn] == boardCellStatus ); }
public void ShouldReturnShip_WhenShipCreated(ShipType shipType) { //arrange var shipCreator = new ShipCreator(); //act var ship = shipCreator.CreateShip(shipType); //assert Assert.NotNull(ship); }
public ShipType GetShipType() { List <string> shipsAlreadyPlaced = new List <string>(); // Gets and preps ship type from user valid = false; bool valid2 = false; while (valid == false) { while (valid2 == false) { Console.WriteLine("\nEnter ship type. Choices are: "); Console.WriteLine("\nDestroyer\nCruiser\nSubmarine\nBattleship\nCarrier\n"); c1p1ShipType = Console.ReadLine(); c1p1ShipType = myTI.ToTitleCase(c1p1ShipType); if (!(c1p1ShipType == "Destroyer" || c1p1ShipType == "Cruiser" || c1p1ShipType == "Submarine" || c1p1ShipType == "Battleship" || c1p1ShipType == "Carrier")) { Console.WriteLine("Not a valid ship type. Try again."); valid2 = false; } else { valid2 = true; } } shipsAlreadyPlaced.Add(c1p1ShipType); valid = true; } // Use the ShipType enum here to prep the entry into proper PlaceShipRequest Class format ShipType shipType = (ShipType)Enum.Parse(typeof(ShipType), c1p1ShipType); c1p1ShipTypeFinal = ShipCreator.CreateShip(shipType); //NOT SURE I NEED THIS HERE!! So far I haven't used it. return(shipType); }
private PlaceShipRequest GetShipRequest(ShipType myShipType, Player myPlayer) { Coordinate shipXY = null; Nullable <ShipDirection> shipDirection = null; Nullable <CoordinateInput> evalCoordinate = null; ConsoleKeyInfo consoleInput; string strX = "", strY = "", strDirection = ""; DisplayGrid myGrid = new DisplayGrid(_gridSize); //Used to display Ship locations. Ship myShip = ShipCreator.CreateShip(myShipType); //Used to display ShipName & BoardPositions.Length properties. PlaceShipRequest retVal = new PlaceShipRequest(); //Set the ShipType for the return retVal.ShipType = myShipType; //Get X Coordinate do { Console.Clear(); if ((evalCoordinate == CoordinateInput.InvalidXCoordinate || evalCoordinate == CoordinateInput.NoValidCoordinates) && evalCoordinate != null) { Console.WriteLine("Invalid X Coordinate, try again!!!"); Console.WriteLine(); } Console.WriteLine($"{myPlayer.Name}, your current board setup:"); myGrid.Grid(myPlayer, BoardViewType.ShipPositions); Console.WriteLine($"Please enter the X coordinate for your {myShip.ShipName}. \nA {myShip.ShipName} takes up {myShip.BoardPositions.Length} spots on the board. \n(Examples:A <A>, b <B>) \n\nQuit anytime by pressing (Q/q)..."); consoleInput = Console.ReadKey(); if (consoleInput.Key.Equals(ConsoleKey.Q)) { return(null); //User entered Q } else { strX = consoleInput.KeyChar.ToString().ToUpper(); EvaluateCoordinates(strX, null, out CoordinateInput response); evalCoordinate = response; } } while (evalCoordinate != CoordinateInput.ValidCoordinate && !consoleInput.Key.Equals(ConsoleKey.Q)); //Get Y Coordinate do { Console.Clear(); if ((evalCoordinate == CoordinateInput.InvalidYCoordinate || evalCoordinate == CoordinateInput.NoValidCoordinates) && evalCoordinate != null) { Console.WriteLine("Invalid Y Coordinate, try again!!!"); Console.WriteLine(); } Console.WriteLine($"{myPlayer.Name}, your current board setup:"); myGrid.Grid(myPlayer, BoardViewType.ShipPositions); Console.WriteLine($"Please enter the Y coordinate for your {myShip.ShipName}. \nA {myShip.ShipName} takes up {myShip.BoardPositions.Length} spots on the board. \n(Examples:1 <1>, 0 <10>) \n\nQuit anytime by pressing (Q/q)..."); consoleInput = Console.ReadKey(); if (consoleInput.Key.Equals(ConsoleKey.Q)) { return(null); //User entered Q } else { strY = consoleInput.KeyChar.ToString().ToUpper(); shipXY = EvaluateCoordinates(strX, strY, out CoordinateInput response); evalCoordinate = response; } } while (evalCoordinate != CoordinateInput.ValidCoordinate && !consoleInput.Key.Equals(ConsoleKey.Q)); //Now have a valid coordinate: shipXY retVal.Coordinate = shipXY; //Get the direction: do { Console.Clear(); if (strDirection.Length > 0 && shipDirection == null) { Console.WriteLine("Invalid direction, try again!!!"); Console.WriteLine(); } Console.WriteLine($"{myPlayer.Name}, your current board setup:"); myGrid.Grid(myPlayer, BoardViewType.ShipPositions); Console.WriteLine($"Please enter a direction to use in placing your {myShip.ShipName}. \nDirection is the direction from ({strX},{strY}) the ship will extend. \nA {myShip.ShipName} takes up {myShip.BoardPositions.Length} spots on the board. \nValid directions are: {ShipDirection.Up}, {ShipDirection.Right}, {ShipDirection.Down} & {ShipDirection.Left} (type and press ENTER) \n\nQuit anytime by pressing (Q/q)..."); strDirection = Console.ReadLine(); shipDirection = EvaluateDirection(strDirection); if (shipDirection != null) { //Valid shipDirection retVal.Direction = shipDirection.Value; } } while (shipXY == null || shipDirection == null); return(retVal); }
/// <summary> /// Runs commands on ships when called /// </summary> /// <param name="shipIndex">index of ship in the ship data to call</param> private void ShipControl(int shipIndex) { ShipModel model = data.ships[shipIndex]; double deltaTime = data.date.time - model.age.time - model.dateCreated.time; model.age.AddTime(deltaTime); if (model.timeUpdate < model.age.time) { model.timeUpdate += Dated.Hour; //Money Evaluation model.money -= model.runningCost; model.money -= model.workers * 15; if (model.captain.Model != model.owner.Model) { model.captain.Model.money += 35; model.money -= 35; } double moneyEarned = model.money - model.moneyStats.data["Money"][model.moneyStats.data["Money"].Count - 1].y; if (moneyEarned > 0) { model.captain.Model.money += moneyEarned * .1f; model.money -= moneyEarned * .1f; } moneyEarned = model.money - model.moneyStats.data["Money"][model.moneyStats.data["Money"].Count - 1].y; model.moneyChange = moneyEarned; model.moneyStats.data["Money Change"].Add(new Stat(model.age.time, model.moneyChange)); model.moneyStats.data["Money"].Add(new Stat(model.age.time, model.money)); model.owner.Model.money += model.money; model.money = 0; //if (model.money < 0) //{ // if (model.mode == ShipMode.Sell && model.target != null && model.target.Model != null) // { // ((StationModel) model.target.Model).SellIncomplete(model.item); // } // print(model.name + " Died"); // model.Delete(); // return; //} } if ((float)model.fuel.amount / model.fuelCapacity < .25f && model.target.Model == null) { model.target = new ModelRef <StructureModel>(FindClosestStation(ItemTypes.Fuel.ToString(), model)); if (model.target.Model != null) { StationModel station = (StationModel)model.target.Model; Items buyItem = new Items(ItemTypes.Fuel, model.fuelCapacity); model.items.Add(station.Buy(buyItem, model)); model.spriteColor = Color.yellow; model.mode = ShipMode.Buy; station.incomingShips.Add(model); model.NotifyChange(); return; } else if (model.fuel.amount < 0) { } } foreach (Items item in model.items) { if (item.name == ItemTypes.Ship.ToString() && item.amount > 0) { for (int i = 0; i < item.amount; i++) { CreatureModel captain = new CreatureModel(model.owner.Model.name + " Ship Captain " + model.owner.Model.itemsBought, 1000); ShipModel ship = ShipCreator.CreateShip(model.owner.Model.name + "." + model.owner.Model.itemsBought, model.solarIndex, model.parentIndex, model.orbit, model.owner.Model, captain); data.ships.Add(ship); data.creatures.Add(captain); model.owner.Model.itemsBought++; } item.amount = 0; } if (item.amount == 0 && item.pendingAmount == 0) { model.items.Remove(item); } } //if (model.money > 10000 && model.target.Model == null) //{ // model.target.Model = FindClosestStation("Ship", model); // if (model.target.Model != null) // { // StationModel station = (StationModel) model.target.Model; // foreach (Items outputItem in station.factory.outputItems) // { // if (outputItem.name == "Ship") // { // if (outputItem.price + 2000 < model.money) // { // model.item = new Items("Ship", 1); // model.spriteColor = Color.cyan; // model.mode = ShipMode.Buy; // station.incomingShips.Add(model); // model.NotifyChange(); // return; // } // else // { // model.target.Model = null; // } // } // } // } //} if (model.mode == ShipMode.Idle) { model.mode = ShipMode.SearchingTradeRoute; TradeRouteRequestManager.RequestTradeRoute(model, OnFindRouteFinished); } ShipTravel(model, deltaTime); }