public static void cruiserPlacement(Player player) { while (true) { Console.WriteLine($"{player.Name} Place your Cruiser"); PlaceShipRequest request = new PlaceShipRequest(); request.ShipType = ShipType.Cruiser; request.Coordinate = getPlayerCoord(); request.Direction = getPlayerShipDirection(); ShipPlacement response = new ShipPlacement(); response = player.PlayerBoard.PlaceShip(request); switch (response) { case ShipPlacement.NotEnoughSpace: Console.WriteLine("Not enough space, try again"); continue; case ShipPlacement.Overlap: Console.WriteLine("Ship overlap, try again"); continue; case ShipPlacement.Ok: break; } Console.Clear(); break; } }
//public static Player PlayAgain(string name) //{ // Player player = new Player(name); // player.PlayerBoard = SetUpBoard(player.Name); // return player; //} public static Board SetUpBoard(string name) { Console.WriteLine($"{name}, set up your ships."); Board board = new Board(); ConsoleOutput.DrawBoard(board); for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++) { Console.WriteLine("Place your " + s); PlaceShipRequest request = new PlaceShipRequest(); request.ShipType = s; request.Coordinate = ConsoleInput.EnterCoords(); request.Direction = ConsoleInput.EnterDirection(); ShipPlacement result = board.PlaceShip(request); if (result != ShipPlacement.Ok) { Console.WriteLine("Invalid location, try again."); s--; //could do error too } } Console.WriteLine("You are finished."); Console.ReadLine(); return(board); }
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 void SetUpBoard() { var ships = new Dictionary <ShipType, int>() { { ShipType.Destroyer, 2 }, { ShipType.Cruiser, 3 }, { ShipType.Submarine, 3 }, { ShipType.Battleship, 4 }, { ShipType.Carrier, 5 } }; foreach (KeyValuePair <ShipType, int> vessel in ships) { ShipPlacement response; do { Console.Clear(); ConsoleOutput.DisplayBoard(this, false); ConsoleOutput.DisplayPlacementMessage(this, vessel.Key, vessel.Value); PlaceShipRequest request = GetPlaceShipRequest(vessel.Key); response = Board.PlaceShip(request); Console.Clear(); ConsoleOutput.DisplayBoard(this, false); ConsoleOutput.DisplayShipResponse(request, response); ConsoleInput.Continue(); } while (response != ShipPlacement.Ok); } Console.Clear(); ConsoleOutput.DisplayBoard(this, false); ConsoleOutput.ClearScreenPrompt(); }
//Place ships on board via player input public static Board PlaceShipLoop(string name) { Board playerBoard = new Board(); for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"{name}, place {s} on your board:"); Console.ForegroundColor = ConsoleColor.Black; PlaceShipRequest ship = new PlaceShipRequest(); ship.Coordinate = GetPlayerCoordinate(); ship.Direction = GetPlayerDirection(); ship.ShipType = s; ShipPlacement result = playerBoard.PlaceShip(ship); if (result != ShipPlacement.Ok) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid ship placement."); Console.ForegroundColor = ConsoleColor.Black; s--; } } return(playerBoard); }
public static Board BoardCreated(string PlayerName) { Board toReturn = new Board(); for (ShipType s = ShipType.Carrier; s >= ShipType.Destroyer; s--) { bool IsShipSpotValid = false; do { PlaceShipRequest request = new PlaceShipRequest(); request.Coordinate = ConsoleInput.GetCoordinate(PlayerName); request.Direction = ConsoleInput.GetDirection(PlayerName, s); request.ShipType = s; var result = toReturn.PlaceShip(request); if (result == ShipPlacement.NotEnoughSpace) { ConsoleOutput.TooFar(); } if (result == ShipPlacement.Overlap) { ConsoleOutput.Overlap(); } if (result == ShipPlacement.Ok) { ConsoleOutput.PlaceSuccess(); IsShipSpotValid = true; } }while (!IsShipSpotValid); } return(toReturn); }
public void PlaceShipOnBoard(Player player) { try { ControlOutput.ShowWhoseTurn(player); Console.WriteLine("Input the location and direction(l, r, u, d) of the ships. Ex:) a2, r:"); PlaceShipRequest ShipToPlace = new PlaceShipRequest(); ShipPlacement result; do { ShipToPlace = ControlInput.GetLocationFromUser(ShipType.Battleship.ToString()); ShipToPlace.ShipType = ShipType.Battleship; result = player.PlayerBoard.PlaceShip(ShipToPlace); if (result == ShipPlacement.NotEnoughSpace) { Console.WriteLine("Not Enough Space!"); } else if (result == ShipPlacement.Overlap) { Console.WriteLine("Overlap placement!"); } } while (result != ShipPlacement.Ok); } catch (Exception) { throw; } }
public void PlaceShip(PlaceShipRequest request) { while (true) { ShipPlacement place = game.Players[game.CurrentPlayer].PlayerBoard.PlaceShip(request); if (place == ShipPlacement.Ok) { return; } else if (place == ShipPlacement.NotEnoughSpace) { Console.WriteLine("There is not enough space there. Try again."); Console.WriteLine("Press any key to continue."); Console.ReadLine(); return; } else if (place == ShipPlacement.Overlap) { Console.WriteLine("There is another ship in the way. Try again."); Console.WriteLine("Press any key to continue."); Console.ReadLine(); return; } } }
public void PlaceShips() { foreach (ShipType i in Enum.GetValues(typeof(ShipType))) { Coordinate coord = GetPlacementCoord(i); ShipDirection dir = GetShipDirection(); PlaceShipRequest request = new PlaceShipRequest() { Coordinate = coord, Direction = dir, ShipType = i }; DisplayPlacementBoard(); PlaceShip(request); DisplayPlacementBoard(); } Console.WriteLine($"{game.Players[game.CurrentPlayer].Name}, your ships are in place. Press any key to continue."); Console.ReadLine(); if (game.CurrentPlayer == 0) { game.CurrentPlayer = 1; game.OtherPlayer = 0; return; } else { game.CurrentPlayer = 0; game.OtherPlayer = 1; return; } }
Board SetUpBoard(string name) { Board toReturn = new Board(); for (int i = 0; i < 5; i++) { ShipType currentType = SelectShip(i); PlaceShipRequest req = PlaceShip(toReturn, currentType, name); ShipPlacement response = toReturn.PlaceShip(req); if (response == ShipPlacement.NotEnoughSpace) { UserOutput.NotEnoughSpace(); i--; } else if (response == ShipPlacement.Overlap) { UserOutput.OverLap(); i--; } else { UserOutput.ShipPlacedSuccessfully(currentType); } } return(toReturn); }
//each player place their ships public static void PlaceShips(Player player, int i) { PlaceShipRequest placeShipRequest = new PlaceShipRequest(); string shipName; ShipPlacement placement; shipName = player.PlayerBoard.Ships[i].ShipName; do { ConsoleOutput.ShowBoard(player.PlayerBoard.DisplayBoard); Console.WriteLine($"{player.Name}, enter a starting coordinate for your {shipName}: (Ex. A2) "); placeShipRequest.Coordinate = ConsoleInput.GetCoordinate(); placeShipRequest.Direction = ConsoleInput.GetDirection(); placeShipRequest.ShipType = player.PlayerBoard.Ships[i].ShipType; placement = player.PlayerBoard.PlaceShip(placeShipRequest); if (placement != ShipPlacement.Ok) { Console.WriteLine($"{placement}.\nPlease press enter to try a new coordinate for your {shipName}."); Console.ReadKey(); Console.Clear(); } } while (placement != ShipPlacement.Ok); Console.WriteLine($"Your {shipName} has been placed. Press enter to continue."); Console.ReadKey(); }
private Board GameBoard(string Playername) { Board board = new Board(); for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++) { bool IsPlacementValid = false; do { PlaceShipRequest request = new PlaceShipRequest(); request.Coordinate = ConsoleInput.GetCoord(Playername); request.Direction = ConsoleInput.GetDirect(Playername); request.ShipType = s; var result = board.PlaceShip(request); if (result == ShipPlacement.NotEnoughSpace) { ConsoleOutput.Out(); } else if (result == ShipPlacement.Overlap) { ConsoleOutput.Overlap(); } else if (result == ShipPlacement.Ok) { ConsoleOutput.Ok(); IsPlacementValid = true; } }while (!IsPlacementValid); } return(board); }
private void PlaceShip(Board placeOn, ShipType currentType, string playerName) { bool validPlacement = false; while (!validPlacement) { Output.PlaceShipNow(playerName, currentType); var req = new PlaceShipRequest { ShipType = currentType, Coordinate = Input.GetCoordinateFromUser(playerName), Direction = Input.GetDirectionFromUser() }; var placementResult = placeOn.PlaceShip(req); switch (placementResult) { case ShipPlacement.NotEnoughSpace: Output.NoSpace(); break; case ShipPlacement.Ok: validPlacement = true; break; case ShipPlacement.Overlap: Output.Overlap(); break; } } }
private Board BuildBoard(string name) { Board gameBoard = new Board(); for (ShipType s = ShipType.Carrier; s >= ShipType.Destroyer; s--) { PlaceShipRequest shipRequest = new PlaceShipRequest(); shipRequest.ShipType = s; ConsoleOutput.ShipToPlace(shipRequest.ShipType); shipRequest.Coordinate = ConsoleInput.GetCoordinate(); shipRequest.Direction = ConsoleInput.GetDirection(); ShipPlacement shipPlaced = gameBoard.PlaceShip(shipRequest); switch (shipPlaced) { case ShipPlacement.Overlap: ConsoleOutput.ShipOverlappedAlert(shipRequest.ShipType); s++; break; case ShipPlacement.NotEnoughSpace: ConsoleOutput.ShipNoSpaceAlert(shipRequest.ShipType); s++; break; } } Console.Clear(); return(gameBoard); //ConsoleInput.GetCoordinate(); }
public ShipPlacement PlaceCarrier(Board board) { ShipPlacement response = default(ShipPlacement); ConsoleOutput.ShipPlacementMessage(ShipType.Carrier); Console.WriteLine(); do { var request = new PlaceShipRequest(); request.ShipType = ShipType.Carrier; request.Coordinate = input.GetCoordinates(); request.Direction = input.GetShipDirection(ShipType.Carrier); response = board.PlaceShip(request); if (response == ShipPlacement.NotEnoughSpace) { ConsoleOutput.NotEnoughSpaceMessage(); } else if (response == ShipPlacement.Overlap) { ConsoleOutput.OverlappingMessage(); } } while (response == ShipPlacement.NotEnoughSpace || response == ShipPlacement.Overlap); return(response); }
public async Task GivenShip_Must_Be_Able_To_Place_On_Board_Horizontally() { using (var server = new ServerFixture(databasePort: _databaseFixture.Port)) { var boardId = Guid.NewGuid(); var shipId = Guid.NewGuid(); server .Arrange() .AddBoard(new Board { Id = boardId, RowSize = 10, ColumnSize = 10 }) .AddShip(new Ship { Id = shipId, Size = 3 }); var placeShipRequest = new PlaceShipRequest { RowStartPosition = 1, ColumnStartPosition = 1, PositionStyle = PositionStyle.Horizontal }; var response = await server.Client.PostAsJsonAsync($"boards/{boardId}/ships/{shipId}/place", placeShipRequest); response.StatusCode.Should().Be(HttpStatusCode.NoContent); var shipPositions = await server.Assert().GetShipPositions(); shipPositions.Any(x => x.RowPosition == 1 && x.ColumnPosition == 1).Should().BeTrue(); shipPositions.Any(x => x.RowPosition == 1 && x.ColumnPosition == 2).Should().BeTrue(); shipPositions.Any(x => x.RowPosition == 1 && x.ColumnPosition == 3).Should().BeTrue(); } }
private static void SetShips(Game game, Player player) { if (game.Player1Turn) { player = game.Player1; } else { player = game.Player2; } foreach (ShipType ship in Enum.GetValues(typeof(ShipType))) { ShipPlacement result = ShipPlacement.NotEnoughSpace; while (result != ShipPlacement.Ok) { PlaceShipRequest request = new PlaceShipRequest() { ShipType = ship, Coordinate = CoordinateWorkflow.GetCoordinate(game, $"time to place your {ship}"), Direction = GetDirectionChoice(player, ship) }; result = player.PlayerBoard.PlaceShip(request); ShipPlacementResponse(result, ship); } } Console.Clear(); ConsoleIO.WriteInColor(player.Name, ConsoleColor.Green); Console.Write(", all ships placed successfully! Press any key to continue..."); Console.ReadKey(); }
public ShipDirection GetShipDirection(ShipType ship) { PlaceShipRequest placeShip = new PlaceShipRequest(); ShipDirection shipDirection; ConsoleOutput.AskForDirection(ship); string givenDirection = Console.ReadLine(); switch (givenDirection) { case "up": shipDirection = ShipDirection.Up; break; case "down": shipDirection = ShipDirection.Down; break; case "left": shipDirection = ShipDirection.Left; break; //case "right": // shipDirection = ShipDirection.Right; // break; default: shipDirection = ShipDirection.Right; break; } return(shipDirection); }
public static PlaceShipRequest GetLocation(string location) { string strX, strY, strDirection; int x, y; if (location.Split(',').Length == 2) { if (location.Split(',')[0].Trim().Length > 1) { strX = location.Split(',')[0].Trim().Substring(0, 1); strY = location.Split(',')[0].Trim().Substring(1); strDirection = location.Split(',')[1].ToLower().Trim(); x = GetNumberFromLetter(strX); if (x > 0 && x < 11 && int.TryParse(strY, out y) && y > 0 && y < 11 && (strDirection == "l" || strDirection == "r" || strDirection == "u" || strDirection == "d")) { PlaceShipRequest ShipToPlace = new PlaceShipRequest(); ShipToPlace.Direction = getDirection(strDirection); ShipToPlace.Coordinate = new Coordinate(x, y); return(ShipToPlace); } } } return(null); }
public Board BuildBoard(Board boardname) { boardname = new Board(); PlaceShipRequest request = new PlaceShipRequest(); ConsoleOutput.DrawBlankBoard(); for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++) { request.ShipType = s; while (true) { Console.WriteLine($"Place your {s}, by indicating the starting coordinate.\n Press 'm' at any time to redraw the board"); request.Coordinate = ConsoleInput.GetCoordinateFromUser(); request.Direction = ConsoleInput.GetShipDirection(); var result = boardname.PlaceShip(request); if (result == ShipPlacement.Overlap) { ConsoleOutput.OverlappingShip(); } if (result == ShipPlacement.NotEnoughSpace) { ConsoleOutput.NotEnoughSpace(); } if (result == ShipPlacement.Ok) { ConsoleOutput.ShipSuccessPlaced(); break; } } } return(boardname); }
public void PlaceShip(Player player, ShipType shiptype) { Console.Write($"{player.Name}, Please enter in the coordinates you would like to place your {shiptype} (X,Y) : "); string userInput = Console.ReadLine(); ShipPlacement result; do { PlaceShipRequest request = new PlaceShipRequest(); request.ShipType = shiptype; request.Coordinate = CreateCord(userInput); request.Direction = ReadDirection(); result = player.PlayerBoard.PlaceShip(request); if (result == ShipPlacement.NotEnoughSpace) { Console.WriteLine("There is not enough space for your placement, please try again"); } else if (result == ShipPlacement.Overlap) { Console.WriteLine("You have overlapped with another ship, please choose a different spot."); } }while (result != ShipPlacement.Ok); { Console.WriteLine("Success!"); } }
public static PlaceShipRequest Request(Board x, int Count) { PlaceShipRequest Requests = new PlaceShipRequest(); Requests = Prompt(x, Count); return(Requests); }
private Board BuildBoard(string playerName) { Board board = new Board(); for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++) { bool isValidPlacement = false; do { PlaceShipRequest request = new PlaceShipRequest(); request.Coordinate = ConsoleInput.GetCoord(playerName, s); request.Direction = ConsoleInput.GetDir(playerName, s); request.ShipType = s; var result = board.PlaceShip(request); if (result == ShipPlacement.Overlap) { ConsoleOutput.ShipOverlap(playerName); } else if (result == ShipPlacement.NotEnoughSpace) { ConsoleOutput.NotEnoughSpace(playerName); } else { ConsoleOutput.ShipPlaceOk(playerName); isValidPlacement = true; } } while (!isValidPlacement); } return(board); }
public Board SetupBoard(Board board, Player currentPlayer) { ConsoleIO.DisplayMessage(message: "Please set up your board " + currentPlayer.Name); foreach (ShipType shipType in Enum.GetValues(typeof(ShipType))) { while (true) { string currentShip = Enum.GetName(typeof(ShipType), shipType); PlaceShipRequest shipRequest = new PlaceShipRequest(); shipRequest.ShipType = shipType; shipRequest.Coordinate = ConsoleIO.PromptPlayerForCoordinate(message: "Please enter coordinate for " + shipType); shipRequest.Direction = ConsoleIO.PromptPlayerForShipDirection(); ShipPlacement result = board.PlaceShip(shipRequest); if (result == ShipPlacement.NotEnoughSpace) { ConsoleIO.DisplayMessage(message: "Not enough space."); } else if (result == ShipPlacement.Overlap) { ConsoleIO.DisplayMessage(message: "Ships are overlapping"); } else if (result == ShipPlacement.Ok) { break; } } } return(board); }
private void promptForShipCoordinate(string _firstPlayer, Board board, ShipType type) { PlaceShipRequest placeRequest = new PlaceShipRequest(); while (true) { Console.WriteLine($"{_firstPlayer}, enter coordinates to place your {type}"); Coordinate coordinate = _gameIn.GetSuperSecretCoordinates(); ShipDirection direction = _gameIn.GetShipDirection(); placeRequest.Coordinate = coordinate; placeRequest.Direction = direction; placeRequest.ShipType = type; ShipPlacement validate = board.PlaceShip(placeRequest); switch (validate) { case ShipPlacement.NotEnoughSpace: Console.WriteLine("Sorry, it isn't valid due to the coordinate being off the board."); break; case ShipPlacement.Overlap: Console.WriteLine("Two ships cannot share the same space...why?...Boundary issues"); break; case ShipPlacement.Ok: Console.WriteLine("Good! This space is available"); return; } } }
public static void PlaceCarrier(Player player) { while (true) { PlaceShipRequest request = new PlaceShipRequest(); request.ShipType = ShipType.Carrier; request.Direction = ConsoleIO.getDirection(ConsoleIO.GetStringFromUser("What direction will you place the Carrier? (l, r, u, d)")); request.Coordinate = request.Coordinate = ConsoleIO.GetCoordinate(); ShipPlacement response = new ShipPlacement(); response = player.Board.PlaceShip(request); switch (response) { case ShipPlacement.NotEnoughSpace: Console.WriteLine("Not enough space try again."); continue; case ShipPlacement.Overlap: Console.WriteLine("Ships are overlapping"); continue; case ShipPlacement.Ok: Console.WriteLine("Ship has been placed"); break; } break; } }
public Board GameSetUp() { //create board instance for current player //create placementRequest Board playerBoard = new Board(); PlaceShipRequest placementRequest = new PlaceShipRequest(); int shipsPlaced = 0; while (shipsPlaced < 5) { //populate ships on board for current player based on their input //get ship type ShipType ship = ConsoleInput.GetShipType(); placementRequest.ShipType = ship; //get coordinates Coordinate shipCoordinates = GetShipCoordinates(); placementRequest.Coordinate = shipCoordinates; //get ship direction ShipDirection direction = ConsoleInput.GetShipDirection(); placementRequest.Direction = direction; ShipPlacement response = playerBoard.PlaceShip(placementRequest); ConsoleOutput.DisplayShipPlacementResult(response); if (response == ShipPlacement.Ok) { shipsPlaced++; } } Console.Clear(); return(playerBoard); }
public PlaceShipRequest ShipRequest(ShipType sType) { Coordinate coord; PlaceShipRequest request; string shipCoord = ConsoleInput.GetRequiredCoordinateFromUser($"Enter coordinates for your {sType}: "); coord = _input.InputCoordinate(shipCoord); //check if valid ShipDirection direction; Console.Write($"Enter direction to place {sType} (up, down, left, right): "); string shipDirection = Console.ReadLine(); direction = _input.InputDirection(shipDirection); request = new PlaceShipRequest() { Coordinate = coord, ShipType = sType, Direction = direction }; return(request); }
public ShipPlacement PlaceShip(PlaceShipRequest request) { if (_currentShipIndex > 4) { throw new Exception("You cannot 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)); } }
/// <summary> /// Gets the location selected by the user /// </summary> /// <param name="ShipType"></param> /// <returns></returns> public static PlaceShipRequest GetLocationFromUser(string ShipType) { PlaceShipRequest result = null; try { do { Console.Write("- " + ShipType + ": "); result = GetLocation(Console.ReadLine()); if (result is null) { ; } else { return(result); } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid input. Please input location and direction. Ex:) a2, r"); Console.ForegroundColor = ConsoleColor.White; } while (result is null); } catch (Exception) { throw; } return(result); }