public void CanNotOverlapShips() { Board board = new Board(); // let's put a carrier at (10,10), (9,10), (8,10), (7,10), (6,10) var carrierRequest = new PlaceShipRequest() { Coordinate = new Coordinate(10, 10), Direction = ShipDirection.Left, ShipType = ShipType.Carrier }; var carrierResponse = board.PlaceShip(carrierRequest); Assert.AreEqual(ShipPlacement.Ok, carrierResponse); // now let's put a destroyer overlapping the y coordinate var destroyerRequest = new PlaceShipRequest() { Coordinate = new Coordinate(9, 9), Direction = ShipDirection.Down, ShipType = ShipType.Destroyer }; var destroyerResponse = board.PlaceShip(destroyerRequest); Assert.AreEqual(ShipPlacement.Overlap, destroyerResponse); }
public Coordinate[] ShipCoordinator(PlaceShipRequest request) { switch (request.ShipType) { case ShipType.Destroyer: CheckDirection(request.Coordinate, request.Direction, 2); break; case ShipType.Submarine: CheckDirection(request.Coordinate, request.Direction, 3); break; case ShipType.Cruiser: CheckDirection(request.Coordinate, request.Direction, 3); break; case ShipType.Battleship: CheckDirection(request.Coordinate, request.Direction, 4); break; case ShipType.Carrier: CheckDirection(request.Coordinate, request.Direction, 5); break; } return Coordinates; }
public void AllowUserToPlace5Ships(GameBoard gameBoard, PlayerInfo playerInfo) { Console.WriteLine("Hello, {0}! Let's place your ships \n", playerInfo.UserName); //places ship int counter = 0; //iterates through for all 5 placements while (counter < 5) { Console.WriteLine("\n-- Place Ship #{0}", counter+1); ShipSetUp setUpYourShip = new ShipSetUp(); // acces UI Ship Placement PlaceShipRequest shipRequest = new PlaceShipRequest(); // initiates placeship request business logic //assigns user entered ship placeemnt biz logic request using the associated board dictionary shipRequest = setUpYourShip.SetUpShip(gameBoard.BoardDictionary,counter); //assigns ship request to player1's board //PlaceShip method on the Board(biz logic) checks if the PlaceShip is valid ShipPlacement placeShipResult = playerInfo.MyBoard.PlaceShip(shipRequest); if (placeShipResult != ShipPlacement.Ok ) { Console.WriteLine("\n\t\t****ERROR -- INVALID SHIP PLACEMENT****\n"); counter--; } ; counter++; } Console.WriteLine("Thank you for your input {0}! Press enter to clear the console so the other player cannot cheat!", playerInfo.UserName); Console.ReadLine(); Console.Clear(); }
public void CanNotPlaceShipPartiallyOnBoard() { Board board = new Board(); PlaceShipRequest request = new PlaceShipRequest() { Coordinate = new Coordinate(10, 10), Direction = ShipDirection.Right, ShipType = ShipType.Carrier }; var response = board.PlaceShip(request); Assert.AreEqual(ShipPlacement.NotEnoughSpace, response); }
public void CanNotPlaceShipOffBoard() { Board board = new Board(); PlaceShipRequest request = new PlaceShipRequest() { Coordinate = new Coordinate(15, 10), Direction = ShipDirection.Up, ShipType = ShipType.Destroyer }; var response = board.PlaceShip(request); Assert.AreEqual(ShipPlacement.NotEnoughSpace, response); }
// this was used for testing purposes public static void AutoPlaceShips(Board board) { int i = 1; foreach (ShipType ship in Enum.GetValues(typeof(ShipType))) { PlaceShipRequest request = new PlaceShipRequest() { Coordinate = new Coordinate(1, i), Direction = ShipDirection.Right, ShipType = ship, }; i++; board.PlaceShip(request); board.UpdateBoardImg(); } }
public ShipPlacement PlaceShip(PlaceShipRequest request) { if (_currentShipIndex > 4) throw new Exception("You can not add another ship, 5 is the limit!"); if (!IsValidCoordinate(request.Coordinate)) return ShipPlacement.NotEnoughSpace; Ship newShip = ShipCreator.CreateShip(request.ShipType); switch (request.Direction) { case ShipDirection.Down: return PlaceShipDown(request.Coordinate, newShip); case ShipDirection.Up: return PlaceShipUp(request.Coordinate, newShip); case ShipDirection.Left: return PlaceShipLeft(request.Coordinate, newShip); default: return PlaceShipRight(request.Coordinate, newShip); } }
private void PlaceSubmarine(Board board) { var request = new PlaceShipRequest() { Coordinate = new Coordinate(3, 5), Direction = ShipDirection.Left, ShipType = ShipType.Submarine }; board.PlaceShip(request); }
private void PlaceDestroyer(Board board) { var request = new PlaceShipRequest() { Coordinate = new Coordinate(1,8), Direction = ShipDirection.Right, ShipType = ShipType.Destroyer }; board.PlaceShip(request); }
private void PlaceCruiser(Board board) { var request = new PlaceShipRequest() { Coordinate = new Coordinate(3, 3), Direction = ShipDirection.Up, ShipType = ShipType.Cruiser }; board.PlaceShip(request); }
private void PlaceCarrier(Board board) { var request = new PlaceShipRequest() { Coordinate = new Coordinate(4,4), Direction = ShipDirection.Right, ShipType = ShipType.Carrier }; board.PlaceShip(request); }
private void PlaceBattleship(Board board) { var request = new PlaceShipRequest() { Coordinate = new Coordinate(10, 6), Direction = ShipDirection.Down, ShipType = ShipType.Battleship }; board.PlaceShip(request); }
//Starts the ship placement phase public void DeployShips() { //This counter follows the amount of ships. //0 is used in order to display the grid before the ship array in the Board obj is initialized. //1 means the player is placing the first ship, and has none on the board. int counter = 0; drawPlacementBoard(counter); counter++; Console.WriteLine(); Console.Write("Place your ships, "); PlayerNameColor(); Console.WriteLine(); foreach (ShipType ship in Enum.GetValues(typeof(ShipType))) { Board board = getCurrentPlayer(true); Console.WriteLine(); Console.WriteLine("You will be placing a {0}", ship.ToString()); Console.WriteLine("Enter your coordinates for this ship."); bool validPlacement = false; while (!validPlacement) { Coordinate coord = inputToCoordinate(); Console.WriteLine("Now enter the direction to place the {0}. Values are D, U, L, R.", ship); ShipDirection direction = directionInput(); PlaceShipRequest ShipRequest = new PlaceShipRequest(coord, direction, ship); switch (board.PlaceShip(ShipRequest)) { case ShipPlacement.NotEnoughSpace: Console.WriteLine("Not enough space to place the ship in that direction. Try elsewhere."); break; case ShipPlacement.Overlap: Console.WriteLine("This placement would overlap another ship. Please enter a new coordinate."); break; case ShipPlacement.Ok: validPlacement = true; break; default: throw new ArgumentOutOfRangeException(); } } Console.Clear(); drawPlacementBoard(counter); counter++; } }
// get player 2 to place ships public void Player2ShipPlacement() { // display empty game board for player1 (I put this in the loop) // prompt player1 for coordinate entry (use letterconverter for xcoordinate) foreach (ShipType stype in Enum.GetValues(typeof(ShipType))) { bool placementIsBad = false; do { BoardUI.DisplayShipBoard(_player2Board); //Testing if valid input bool coordIsValid = false; string shipplacecoord = ""; do { IsPlayercoordValid IsItValid = new IsPlayercoordValid(); Console.Write("{0}, pick a coordinate for your {1} : ", Player.Name2, stype); shipplacecoord = Console.ReadLine(); coordIsValid = IsItValid.IsItGood(shipplacecoord); } while (coordIsValid == false); //end of testing //Console.Write("{0}, pick a coordinate for your {1} : ", Player.Name2, stype); //shipplacecoord = Console.ReadLine(); string xAsLetter = shipplacecoord.Substring(0, 1); int shipX = LetterConverter.ConvertToNumber(xAsLetter); //Convert 1st char from player input to int. //TODO Bug, ensure correct length. int shipY = int.Parse(shipplacecoord.Substring(1)); //Assign 2nd coord. Coordinate shipcoord = new Coordinate(shipX, shipY); // and then, asking for ship direction Console.Write("{0}, Enter a direction (up, down, left, right) for your {1} (length {2}) : ", Player.Name2, stype, "ship length"); string shipPlacementDirection = Console.ReadLine(); IsDirectionValid IsDirInputValid = new IsDirectionValid(); int InputResponse = IsDirInputValid.WhatIsDirection(shipPlacementDirection); if (InputResponse == 1) { PlaceShipRequest shipRequest = new PlaceShipRequest { Coordinate = shipcoord, Direction = ShipDirection.Up, ShipType = stype }; var WhereIsShip = _player2Board.PlaceShip(shipRequest); //Refactor for class if (WhereIsShip == ShipPlacement.NotEnoughSpace) { Console.Clear(); Console.WriteLine("Not enough space to place ship there, Try again!"); } else if (WhereIsShip == ShipPlacement.Overlap) { Console.Clear(); Console.WriteLine("You are overlapping another ship, try again!"); } else if (WhereIsShip == ShipPlacement.Ok) { ShipCreator.CreateShip(stype); placementIsBad = true; } //checks //ShipCreator.CreateShip(stype); } if (InputResponse == 2) { PlaceShipRequest shipRequest = new PlaceShipRequest { Coordinate = shipcoord, Direction = ShipDirection.Down, ShipType = stype }; //checks var WhereIsShip = _player2Board.PlaceShip(shipRequest); //Refactor for class if (WhereIsShip == ShipPlacement.NotEnoughSpace) { Console.Clear(); Console.WriteLine("Not enough space to place ship there, Try again!"); } else if (WhereIsShip == ShipPlacement.Overlap) { Console.Clear(); Console.WriteLine("You are overlapping another ship, try again!"); } else if (WhereIsShip == ShipPlacement.Ok) { ShipCreator.CreateShip(stype); placementIsBad = true; } //ShipCreator.CreateShip(stype); } if (InputResponse == 3) { PlaceShipRequest shipRequest = new PlaceShipRequest { Coordinate = shipcoord, Direction = ShipDirection.Left, ShipType = stype }; //checks var WhereIsShip = _player2Board.PlaceShip(shipRequest); //Refactor for class if (WhereIsShip == ShipPlacement.NotEnoughSpace) { Console.Clear(); Console.WriteLine("Not enough space to place ship there, Try again!"); } else if (WhereIsShip == ShipPlacement.Overlap) { Console.Clear(); Console.WriteLine("You are overlapping another ship, try again!"); } else if (WhereIsShip == ShipPlacement.Ok) { ShipCreator.CreateShip(stype); placementIsBad = true; } //ShipCreator.CreateShip(stype); } if (InputResponse == 4) { PlaceShipRequest shipRequest = new PlaceShipRequest { Coordinate = shipcoord, Direction = ShipDirection.Right, ShipType = stype }; //checks var WhereIsShip = _player2Board.PlaceShip(shipRequest); //Refactor for class if (WhereIsShip == ShipPlacement.NotEnoughSpace) { Console.Clear(); Console.WriteLine("Not enough space to place ship there, Try again!"); } else if (WhereIsShip == ShipPlacement.Overlap) { Console.Clear(); Console.WriteLine("You are overlapping another ship, try again!"); } else if (WhereIsShip == ShipPlacement.Ok) { ShipCreator.CreateShip(stype); placementIsBad = true; } //ShipCreator.CreateShip(stype); } } while (placementIsBad == false); Console.Clear(); } }
public void PlaceTheShips(Player player) { PlaceShipRequest ShipToPlace = new PlaceShipRequest(); int howManyShipsPlaced = 0; for (int i = 0; i < 5; i++) { ShipPlacement response; do { Console.WriteLine("Place your {0} on the x axis with a letter: ", ShipToPlace.ShipType); string userinputXCoord = Console.ReadLine(); int Xcoord = 0; switch (userinputXCoord.ToUpper()) { case "A": Xcoord = 1; break; case "B": Xcoord = 2; break; case "C": Xcoord = 3; break; case "D": Xcoord = 4; break; case "E": Xcoord = 5; break; case "F": Xcoord = 6; break; case "G": Xcoord = 7; break; case "H": Xcoord = 8; break; case "I": Xcoord = 9; break; case "J": Xcoord = 10; break; } Console.WriteLine("\nPlace your {0} on the y axis with a number: ", ShipToPlace.ShipType); string inputYCoord = Console.ReadLine(); var new_string = 0; if (int.TryParse(inputYCoord, out new_string)) { new_string = new_string; } else { new_string = 0; } //int Ycoord = int.Parse(inputYCoord); int Ycoord = new_string; Console.WriteLine("\nWhat direction should your carrier point? (Up, Down, Right, Left): "); string inputCarrierDirection = Console.ReadLine(); ShipDirection myDirection = 0; switch (inputCarrierDirection.ToUpper()) { case "UP": case "U": myDirection = ShipDirection.Up; break; case "DOWN": case "D": myDirection = ShipDirection.Down; break; case "RIGHT": case "R": myDirection = ShipDirection.Right; break; case "LEFT": case "L": myDirection = ShipDirection.Left; break; } ShipToPlace.Direction = myDirection; ShipToPlace.Coordinate = new Coordinate(Xcoord, Ycoord); response = player.playerBoard.PlaceShip(ShipToPlace); if (ShipToPlace.Coordinate.Equals(new Coordinate(0, 0))) { Console.WriteLine("\nInvalid input...Make sure x is a letter, and y is a number. " + "Press enter to try again.\n"); Console.ReadLine(); } else { if (response == ShipPlacement.NotEnoughSpace) { Console.WriteLine("\nThere isn't enough room. Try again.\n"); Console.ReadLine(); } else if (response == ShipPlacement.Overlap) { Console.WriteLine("\nThat ship overlaps another. Please try again.\n"); Console.ReadLine(); } } Console.Clear(); //wf.ShowBoard(wf.p1); WorkFlowObject.ShowBoard1(player); Console.WriteLine("\n"); } while (response != ShipPlacement.Ok); ShipToPlace.ShipType++; Console.WriteLine("\n"); howManyShipsPlaced++; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("You've placed {0} of 5 ships so far. \n", howManyShipsPlaced); Console.ResetColor(); } }
// Prompt user to place a ship on their board. public static void Place(Board board, string playerName, ShipType ship) { BoardImage.Present(board, playerName + "'s setup turn"); ShipPlacement placementResult; do { // prompt for coord and direction while listing ship and ship size int[] coords = Prompt.Coord($"{ship} ({_shipDict[ship]})\t"); ShipDirection direction = DirectionPrompt(); // create ship request var request = new PlaceShipRequest() { Coordinate = new Coordinate(coords[0], coords[1]), Direction = direction, ShipType = ship, }; // request a ship placement placementResult = board.PlaceShip(request); Console.Clear(); if (placementResult == ShipPlacement.NotEnoughSpace) { BoardImage.Present(board, playerName + "'s setup turn"); ConsoleWriter.Write("\t\t\t*** Not enough space. Try again. ***", 10, ConsoleColor.Red); } else if (placementResult == ShipPlacement.Overlap) { BoardImage.Present(board, playerName + "'s setup turn"); ConsoleWriter.Write("\t\t*** Another ship is already there. Try again. ***", 10, ConsoleColor.Red); } } while (placementResult != ShipPlacement.Ok); // loop until ship placement is valid board.UpdateBoardImg(); // update board image to reflect placed ships }
private PlaceShipRequest GetShipPlacementRequestFromUser(Player activePlayer, Dictionary<ShipType, int> unplacedShips) { DisplayShipPlacementAndUnplacedShips(activePlayer, unplacedShips); PlaceShipRequest placementRequest = new PlaceShipRequest(); string invalidInputMessage = "Sorry Captain, we couldn't understand you!\n"; bool validInput = false; //get choice of ship from user while (!validInput) { Console.ForegroundColor = TextEffects.HighlightColor; TextEffects.CenterAlignWriteLine("Which ship would you like to deploy?"); Console.ResetColor(); TextEffects.CenterAlignWrite("Enter your choice: "); Console.ForegroundColor = TextEffects.HighlightColor; string shipChoice = Console.ReadLine().Replace(" ", "").ToUpper(); Console.ResetColor(); switch (shipChoice) { case "D": case "DESTROYER": if (unplacedShips.ContainsKey(ShipType.Destroyer)) { placementRequest.ShipType = ShipType.Destroyer; validInput = true; break; } else { Console.Clear(); DisplayShipPlacementAndUnplacedShips(activePlayer, unplacedShips); TextEffects.CenterAlignWriteLine("You already deployed the Destroyer."); break; } case "S": case "SUBMARINE": if (unplacedShips.ContainsKey(ShipType.Submarine)) { placementRequest.ShipType = ShipType.Submarine; validInput = true; break; } else { Console.Clear(); DisplayShipPlacementAndUnplacedShips(activePlayer, unplacedShips); TextEffects.CenterAlignWriteLine("You already deployed the Submarine."); break; } case "C": case "CRUISER": if (unplacedShips.ContainsKey(ShipType.Cruiser)) { placementRequest.ShipType = ShipType.Cruiser; validInput = true; break; } else { Console.Clear(); DisplayShipPlacementAndUnplacedShips(activePlayer, unplacedShips); TextEffects.CenterAlignWriteLine("You already deployed the Cruiser."); break; } case "B": case "BATTLESHIP": if (unplacedShips.ContainsKey(ShipType.Battleship)) { placementRequest.ShipType = ShipType.Battleship; validInput = true; break; } else { Console.Clear(); DisplayShipPlacementAndUnplacedShips(activePlayer, unplacedShips); TextEffects.CenterAlignWriteLine("You already deployed the Battleship."); break; } case "A": case "AC": case "CARRIER": case "AIRCRAFTCARRIER": if (unplacedShips.ContainsKey(ShipType.Carrier)) { placementRequest.ShipType = ShipType.Carrier; validInput = true; break; } else { Console.Clear(); DisplayShipPlacementAndUnplacedShips(activePlayer, unplacedShips); TextEffects.CenterAlignWriteLine("You already deployed the Carrier."); break; } default: Console.Clear(); DisplayShipPlacementAndUnplacedShips(activePlayer, unplacedShips); Console.ForegroundColor = TextEffects.HighlightColor; TextEffects.CenterAlignWriteLine(invalidInputMessage); Console.ResetColor(); break; } } Console.Clear(); DisplayScreenTitle(activePlayer.name + " Ship Deployment"); activePlayer.board.DisplayAll(); placementRequest.Coordinate = GetShipCoordFromUser("Enter a starting coordinate for the " + placementRequest.ShipType.ToString() + " (length " + unplacedShips[placementRequest.ShipType] + "): ", invalidInputMessage); Console.Clear(); DisplayScreenTitle(activePlayer.name + " Ship Deployment"); activePlayer.board.DisplayAll(); //now I have to ask for the direction of the ship. validInput = false; while (!validInput) { //write the choices of direction, with color changes and leading spaces for center alignment for (int i = 1; i <= 33; i++) Console.Write(" "); Console.Write("("); Console.ForegroundColor = TextEffects.HighlightColor; Console.Write("U"); Console.ResetColor(); Console.WriteLine(")p"); for (int i = 1; i <= 33; i++) Console.Write(" "); Console.Write("("); Console.ForegroundColor = TextEffects.HighlightColor; Console.Write("D"); Console.ResetColor(); Console.WriteLine(")own"); for (int i = 1; i <= 33; i++) Console.Write(" "); Console.Write("("); Console.ForegroundColor = TextEffects.HighlightColor; Console.Write("L"); Console.ResetColor(); Console.WriteLine(")eft"); for (int i = 1; i <= 33; i++) Console.Write(" "); Console.Write("("); Console.ForegroundColor = TextEffects.HighlightColor; Console.Write("R"); Console.ResetColor(); Console.WriteLine(")ight"); Console.WriteLine(); //prompt TextEffects.CenterAlignWrite("Starting at " + placementRequest.Coordinate + ", enter a direction to deploy the ship (length " + unplacedShips[placementRequest.ShipType] + "): "); Console.ForegroundColor = TextEffects.HighlightColor; string directionChoice = Console.ReadLine().Trim().ToUpper(); Console.ResetColor(); switch (directionChoice) { case "U": case "UP": placementRequest.Direction = ShipDirection.Up; validInput = true; break; case "D": case "DOWN": placementRequest.Direction = ShipDirection.Down; validInput = true; break; case "L": case "LEFT": placementRequest.Direction = ShipDirection.Left; validInput = true; break; case "R": case "RIGHT": placementRequest.Direction = ShipDirection.Right; validInput = true; break; default: Console.Clear(); DisplayScreenTitle(activePlayer.name + " Ship Placement"); activePlayer.board.DisplayAll(); Console.ForegroundColor = TextEffects.HighlightColor; TextEffects.CenterAlignWriteLine(invalidInputMessage); Console.ResetColor(); continue; } } Console.Clear(); return placementRequest; }
private void SetUp() { for (var i = 0; i < 5; i++) { do { Coordinate coord = AskCoordinate(); ShipDirection direction = AskDirection(); ShipType type = AskShipType(); PlaceShipRequest req = new PlaceShipRequest(); req.Coordinate = coord; req.Direction = direction; req.ShipType = type; ShipPlacement sp = p1board.PlaceShip(req); if (sp == ShipPlacement.NotEnoughSpace) { Console.WriteLine("Not enough space to put a ship there! Try again!\n"); nextShip = false; } else if (sp == ShipPlacement.Overlap) { Console.WriteLine("That ship would overlap one you already placed! Try again!\n"); nextShip = false; } else // shipplacement.ok { nextShip = true; } } while (!nextShip); } Console.Clear(); Console.WriteLine("{0}'s turn!!", player2name); for (int i = 0; i < 5; i++) { do { Coordinate coord = AskCoordinate(); ShipDirection direction = AskDirection(); ShipType type = AskShipType(); PlaceShipRequest req = new PlaceShipRequest(); req.Coordinate = coord; req.Direction = direction; req.ShipType = type; ShipPlacement sp = p2board.PlaceShip(req); if (sp == ShipPlacement.NotEnoughSpace) { Console.WriteLine("Not enough space to put a ship there! Try again!"); nextShip = false; } if (sp == ShipPlacement.Overlap) { Console.WriteLine("That ship would overlap one you already placed! Try again!"); nextShip = false; } else // shipplacement.ok { nextShip = true; } } while (!nextShip); } Console.Clear(); }
public PlaceShipRequest SetUpBoard(PlaceShipRequest request) { bool isValid = false; Coordinate validCoord; do { Console.Write("Pick a coordinate for your {0}: ", request.ShipType.ToString()); string userInput = Console.ReadLine().ToUpper(); validCoord = ValidateCoord.getValidCoord(userInput); if (validCoord == null) { Console.WriteLine("That was not a valid coordinate."); } else { isValid = true; } } while (!isValid); request.Coordinate = validCoord; Console.Write("Choose a direction UP, DOWN, LEFT, RIGHT: "); string userDirection = Console.ReadLine().ToUpper(); switch (userDirection) { case "UP": request.Direction = ShipDirection.Up; break; case "DOWN": request.Direction = ShipDirection.Down; break; case "LEFT": request.Direction = ShipDirection.Left; break; case "RIGHT": request.Direction = ShipDirection.Right; break; } Console.Clear(); return request; }
public PlaceShipRequest SetUpShip(Dictionary<string, Coordinate> userDictionary, int shipTypeAsNum) { string coordinateRequested = ""; string shipDirectionRequest = ""; int shipDirAsNum = 0; //get ship type ShipType shipType = SetShipType(shipTypeAsNum); Console.WriteLine("You are placing a {0}....", shipType); //get X & Y Console.Write("\t(1) Please enter your X & Y coordinate(Ex: A1): "); coordinateRequested = Console.ReadLine(); //get ship direction while (!int.TryParse(shipDirectionRequest, out shipDirAsNum)) { Console.Write("\t(2) Please enter your ship direction (up=0,down=1,left=2,right=3): "); shipDirectionRequest = Console.ReadLine(); } shipDirAsNum = int.Parse(shipDirectionRequest); ShipDirection shipDirection = SetShipDirection(shipDirAsNum); //get coordinate ConvertX convertX = new ConvertX(); Coordinate aCoordinate = convertX.Conversion(userDictionary, coordinateRequested); //place ship PlaceShipRequest placeShips = new PlaceShipRequest { Coordinate = aCoordinate, Direction = shipDirection, ShipType = shipType }; return placeShips; }
//New SetBoard Method public static void SetBoard(Player[] players) { Console.Clear(); for (int p = 0; p < players.Length; p++) { Console.Clear(); Console.WriteLine( "{0}, you will now place your 5 battleships: Destroyer (2 coordinates), Submarine (3), Cruiser (3), Battleship (4), Carrier (5)." + "\nPress Enter to continue." , players[p].Name); Console.ReadLine(); for (int s = 0; s < 5; s++) { ShipPlacement placementIsValid = new ShipPlacement(); ShipType shipType = (ShipType) s; int shipLength = 0; switch (s) { case 0: shipLength = 2; break; case 1: case 2: shipLength = 3; break; case 3: shipLength = 4; break; case 4: shipLength = 5; break; } do { Console.Clear(); DrawShipPlacement(players[p]); Console.WriteLine("\n\n\n{0}, get ready to place your {1} ({2} coordinates)", players[p].Name, shipType.ToString(), shipLength); Console.WriteLine( "\nFirst, choose an initial coordinate (alphanumeric) within the 10x10 grid for your ship.\n\nWe will ask for the direction/orientation of the ship afterward."); string coordString = Console.ReadLine(); Coordinate baseCoord = Convert(coordString); PlaceShipRequest requestShip = new PlaceShipRequest(); requestShip.Coordinate = baseCoord; requestShip.ShipType = (ShipType) s; Console.WriteLine("{0}, now choose a direction to place your {1} ({2} coordinates):\n" + "(U)p, (D)own, (L)eft, or (R)ight.", players[p].Name, shipType.ToString(), shipLength); bool validShipDirection = false; string shipDirection = Console.ReadLine().ToUpper(); while (!validShipDirection) { switch (shipDirection) { case "R": requestShip.Direction = ShipDirection.Right; validShipDirection = true; break; case "L": requestShip.Direction = ShipDirection.Left; validShipDirection = true; break; case "U": requestShip.Direction = ShipDirection.Up; validShipDirection = true; break; case "D": requestShip.Direction = ShipDirection.Down; validShipDirection = true; break; default: Console.WriteLine( "That is not a valid direction. Please choose (U)p, (D)own, (L)eft or (R)ight."); shipDirection = Console.ReadLine().ToUpper(); break; } } placementIsValid = players[p].PlayerBoard.PlaceShip(requestShip); switch (placementIsValid) { case ShipPlacement.NotEnoughSpace: Console.WriteLine("There is not enough space to place the ship there. Try again."); break; case ShipPlacement.Overlap: Console.WriteLine("The ship is overlapping with a previously placed ship. Try again."); break; default: Console.WriteLine("That ship placement is OK!!"); break; } //Console.WriteLine(placementIsValid); Console.ReadLine(); } while (placementIsValid != ShipPlacement.Ok); //Adding ship to ShipBoard Dictionary Ship shipToDraw = players[p].PlayerBoard._ships[s]; foreach (Coordinate shipCoord in shipToDraw.BoardPositions) { players[p].PlayerBoard.ShipBoard.Add(shipCoord, shipToDraw.ShipType); } Console.Clear(); DrawShipPlacement(players[p]); Console.WriteLine("\n\nCongratulations, {0}! You have placed your {1}. Press enter to continue...", players[p].Name, shipType.ToString()); Console.ReadLine(); } } }
public static void SetShip(GameWorkflow game) { foreach (Ship ship in game.CurrentPlayer.Ships) { PlaceShipRequest placeShipRequest = new PlaceShipRequest(); placeShipRequest.ShipType = ship.ShipType; string coordinateInput = ""; char[] coordinateOutput = new char[2]; bool isValidInput = false; ShipPlacement shipPlacement; do { do { Console.WriteLine("{0}, place your {1} ship on the board which has {2} slots.", game.CurrentPlayer.Name, ship.ShipType, ship.BoardPositions.Length); Console.WriteLine(); Console.WriteLine("Here is the game board for your reference."); Console.WriteLine(); BoardDrawer.DrawOwnShipBoard(game); Console.WriteLine(); Console.WriteLine("Choose a position on the board, for example \"E5\": "); coordinateInput = Console.ReadLine().ToUpper(); Console.WriteLine(); if (coordinateInput.Length == 2 && (coordinateInput[1] >= '1' && coordinateInput[1] <= '9') && (coordinateInput[0] >= 'A' && coordinateInput[0] <= 'J')) { isValidInput = true; coordinateOutput = coordinateInput.ToCharArray(); } else if (coordinateInput.Length == 3 && (coordinateInput[1] >= '1' && coordinateInput[2] <= '0') && (coordinateInput[0] >= 'A' && coordinateInput[0] <= 'J')) { isValidInput = true; coordinateOutput = coordinateInput.ToCharArray(); } else { isValidInput = false; Console.WriteLine("Invalid input {0}, please try again!", game.CurrentPlayer.Name); ScreenCleaner.ClearBoard(); } } while (!isValidInput); Coordinate coordinateForShip; if (coordinateOutput.Length == 3) { int yCoordinate = int.Parse(string.Concat(coordinateOutput[1].ToString(), coordinateOutput[2].ToString())); coordinateForShip = new Coordinate(coordinateOutput[0] - 'A' + 1, yCoordinate); } else { coordinateForShip = new Coordinate((coordinateOutput[0] - 'A' + 1), int.Parse(coordinateOutput[1].ToString())); } placeShipRequest.Coordinate = coordinateForShip; isValidInput = false; string directionInput = ""; bool needBoard = false; do { if (needBoard) { Console.WriteLine("Here is the game board for your reference."); Console.WriteLine(); BoardDrawer.DrawOwnShipBoard(game); Console.WriteLine(); Console.WriteLine("Your coordinate input was: {0}", coordinateInput); Console.WriteLine(); } Console.WriteLine("Please choose the direction to place the ship: \"U\" up, \"D\" down, \n \"L\" left, or \"R \" right."); directionInput = Console.ReadLine().ToUpper(); if ((directionInput.Length == 1) && directionInput == "U" || directionInput == "D" || directionInput == "L" || directionInput == "R") { isValidInput = true; needBoard = false; } else { Console.WriteLine(); Console.WriteLine("Input not valid!"); ScreenCleaner.ClearBoard(); isValidInput = false; needBoard = true; } } while (!isValidInput); switch (directionInput) { case "U": placeShipRequest.Direction = ShipDirection.Up; break; case "D": placeShipRequest.Direction = ShipDirection.Down; break; case "L": placeShipRequest.Direction = ShipDirection.Left; break; case "R": placeShipRequest.Direction = ShipDirection.Right; break; default: Console.WriteLine(); Console.WriteLine("You need to enter a direction: either \"U\" up, \"D\" down,\n \"L\" left, or \"R\" right"); ScreenCleaner.ClearBoard(); break; } shipPlacement = game.CurrentPlayer.PlayerBoard.PlaceShip(placeShipRequest); if (shipPlacement == ShipPlacement.NotEnoughSpace) { Console.WriteLine(); Console.WriteLine("There is not enough space on the board. Please choose another direction."); ScreenCleaner.ClearBoard(); } else if (shipPlacement == ShipPlacement.Overlap) { Console.WriteLine(); Console.WriteLine("You already have a ship placed there. Please choose another direction"); ScreenCleaner.ClearBoard(); } else { Console.WriteLine(); Console.WriteLine("{0} placed.", placeShipRequest.ShipType); ShipCoordinates shipCoordinates = new ShipCoordinates(); Coordinate[] coordinates = shipCoordinates.ShipCoordinator(placeShipRequest); for (int i = 0; i < coordinates.Length; i++) { game.CurrentPlayer.ShipLocations.Add(coordinates[i], placeShipRequest.ShipType); } ScreenCleaner.ClearBoard(); } } while (shipPlacement != ShipPlacement.Ok); } Console.WriteLine("This is your completed ship board, {0}.\n", game.CurrentPlayer.Name); BoardDrawer.DrawOwnShipBoard(game); ScreenCleaner.ClearBoard(); }
private void ShipPlacementRequest() { var request = new PlaceShipRequest { Coordinate = new Coordinate(_xCoordinate,_yCoordinate), Direction = _shipDirection, ShipType = _shipType }; if (_player1Turn) { _response = player1Board.PlaceShip(request); } else { _response = player2Board.PlaceShip(request); } switch (_response) { case ShipPlacement.Overlap: Console.WriteLine("\n*Coordinates result in overlapping ship locations*\n"); break; case ShipPlacement.NotEnoughSpace: Console.WriteLine("\n*Coordinates exceed board parameters*\n"); break; case ShipPlacement.Ok: Console.WriteLine("\nShip placed successfully! Press any key to continue."); Console.ReadKey(); break; } }
private void PlaceShips(PlayerDto inDto) { int a = Enum.GetNames(typeof(ShipType)).Length; for (int i = 0; i < a; i++) { bool isPlacementOk = false; while (!isPlacementOk) { string shipType = ((ShipType)i).ToString(); var coordGetter = new CoordGetter(); var shipPointer = new ShipPointer(); PlaceShipRequest request = new PlaceShipRequest() { Coordinate = coordGetter.GetCoord((i + 1).ToString() + ". " + "Enter coordinate for your " + shipType.ToString() + ": "), Direction = shipPointer.PointShip(" Ship direction: (L-left, R-right, U-up, D-down): "), ShipType = (ShipType)i }; var responce = inDto.Board.PlaceShip(request); string r = responce.ToString(); if (r == ShipPlacement.Ok.ToString()) { isPlacementOk = true; Console.WriteLine(" Ship placement status: {0}", r); } else Console.WriteLine(" Ship NOT PLACED! : {0}", r); } } }
public void PlaceTheShipsAuto(Player player) { var request = new PlaceShipRequest() { Coordinate = new Coordinate(4, 4), Direction = ShipDirection.Right, ShipType = ShipType.Carrier }; player.playerBoard.PlaceShip(request); var request1 = new PlaceShipRequest() { Coordinate = new Coordinate(10, 6), Direction = ShipDirection.Down, ShipType = ShipType.Battleship }; player.playerBoard.PlaceShip(request1); var request2 = new PlaceShipRequest() { Coordinate = new Coordinate(3, 5), Direction = ShipDirection.Left, ShipType = ShipType.Submarine }; player.playerBoard.PlaceShip(request2); var request3 = new PlaceShipRequest() { Coordinate = new Coordinate(3, 3), Direction = ShipDirection.Up, ShipType = ShipType.Cruiser }; player.playerBoard.PlaceShip(request3); var request4 = new PlaceShipRequest() { Coordinate = new Coordinate(1, 8), Direction = ShipDirection.Right, ShipType = ShipType.Destroyer }; player.playerBoard.PlaceShip(request4); }
public static void PlayerShipSetup(Player player) { Coordinate coord = new Coordinate(0,0); bool isValidCoordinate = false; string userInput; //build ShipReq with coord, shiptype, direction PlaceShipRequest place = new PlaceShipRequest(); //Talk SpeechSynthesizer talk = new SpeechSynthesizer(); talk.SetOutputToDefaultAudioDevice(); talk.SelectVoiceByHints(VoiceGender.Female); Console.Clear(); Console.Beep(); Console.Beep(); Console.Beep(); talk.Speak("Obie one Konobie. You're our only hope!"); System.Threading.Thread.Sleep(1000); talk.SelectVoiceByHints(VoiceGender.Male); talk.Speak(String.Format("{0}, It's time to place your ships.", player.Name)); for (int i = 0; i < _shipArray.Length; i++) { ShipPlacement response; do { Console.Clear(); GameWorkflow.DrawBoard.DrawBoardShips(player); place.ShipType = _shipArray[i]; do { Console.WriteLine("\n\nEnter Coordinate for: {0}", _shipArray[i]); userInput = Console.ReadLine(); coord = gameFlow.ConvertInputToCoordinate(userInput); isValidCoordinate = gameFlow.CheckValidCoord(coord); if (!isValidCoordinate) { Console.WriteLine("\nInvalid Coordinate - Reenter Coordinate"); } } while (!isValidCoordinate); //populate coord place.Coordinate = gameFlow.ConvertInputToCoordinate(userInput); string userDirection = ""; do { Console.WriteLine("\nEnter direction for ship placement: 1-Up, 2-Down, 3-Right, 4-Left"); userDirection = Console.ReadLine(); } while (!(userDirection == "1" || userDirection == "2" || userDirection == "3" || userDirection == "4")); switch (userDirection) { case "1": place.Direction = ShipDirection.Up; break; case "2": place.Direction = ShipDirection.Down; break; case "3": place.Direction = ShipDirection.Right; break; case "4": place.Direction = ShipDirection.Left; break; } response = player.PlayerBoard.PlaceShip(place); //if statement If palcement ok move on if not go back to top if (response != ShipPlacement.Ok) { Console.WriteLine("\nYour ship placement was: {0}, press Enter to try again. ", response); Console.ReadLine(); } } while (response != ShipPlacement.Ok); } Console.Clear(); GameWorkflow.DrawBoard.DrawBoardShips(player); Console.WriteLine("\n\nGreat job! All your ships are setup. Press Enter to continue."); //SpeechSynthesizer talk = new SpeechSynthesizer(); talk.SetOutputToDefaultAudioDevice(); talk.Speak("Great place for your ships...I feel a disturbance in the force.."); Console.ReadLine(); }