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); }
private Coordinate GetShotCoordinate() { //Designed to use the CurrentPlayer/Opponent - no need to pass in a Player object!!! Coordinate shotXY = null; //Returns Null Coordinate if the users Quits before valid Coordinate is created. Nullable <CoordinateInput> evalCoordinate = null; ConsoleKeyInfo consoleInput; string strX = "", strY = ""; DisplayGrid myGrid = new DisplayGrid(_gridSize); //Get X Coordinate do { Console.Clear(); if (evalCoordinate == CoordinateInput.InvalidXCoordinate && evalCoordinate != null) { Console.WriteLine("Invalid X Coordinate, try again!!!"); Console.WriteLine(); } Console.WriteLine($"{CurrentPlayer.Name}, your current shot history:"); myGrid.Grid(Opponent, BoardViewType.ShotHistory); Console.WriteLine($"{CurrentPlayer.Name}, your opponent's shot history:"); myGrid.Grid(CurrentPlayer, BoardViewType.ShipShotStatus); Console.WriteLine("Please enter the X coordinate for your next shot. \n(Examples:A <A>, b <B>) \n\nQuit anytime by pressing (Q/q)..."); consoleInput = Console.ReadKey(); if (consoleInput.Key.Equals(ConsoleKey.Q)) { break; } else { strX = consoleInput.KeyChar.ToString().ToUpper(); EvaluateCoordinates(strX, "", out CoordinateInput response); evalCoordinate = response; } } while (evalCoordinate != CoordinateInput.ValidCoordinate && !consoleInput.Key.Equals(ConsoleKey.Q)); if (consoleInput.Key.Equals(ConsoleKey.Q)) { return(null); //User pressed Q } //Get Y Coordinate do { Console.Clear(); if (evalCoordinate == CoordinateInput.InvalidYCoordinate && evalCoordinate != null) { Console.WriteLine("Invalid Y Coordinate, try again!!!"); Console.WriteLine(); } Console.WriteLine($"{CurrentPlayer.Name}, your current shot history:"); myGrid.Grid(Opponent, BoardViewType.ShotHistory); Console.WriteLine($"{CurrentPlayer.Name}, your opponent's shot history:"); myGrid.Grid(CurrentPlayer, BoardViewType.ShipShotStatus); Console.WriteLine("Please enter the Y coordinate for your next shot. \n(Examples:1 <1>, 0 <10>) \n\nQuit anytime by pressing (Q/q)..."); consoleInput = Console.ReadKey(); if (consoleInput.Key.Equals(ConsoleKey.Q)) { break; } else { strY = consoleInput.KeyChar.ToString().ToUpper(); shotXY = EvaluateCoordinates(strX, strY, out CoordinateInput response); evalCoordinate = response; } } while (evalCoordinate != CoordinateInput.ValidCoordinate && !consoleInput.Key.Equals(ConsoleKey.Q)); if (consoleInput.Key.Equals(ConsoleKey.Q)) { return(null); //User pressed Q } //shotXY now has valid X & Y coordinates! //Ask user if they want to fire the shot to these coordinates Console.Clear(); Console.WriteLine($"{CurrentPlayer.Name}, your current shot history:"); myGrid.Grid(Opponent, BoardViewType.ShotHistory); Console.WriteLine($"{CurrentPlayer.Name}, your opponent's shot history:"); myGrid.Grid(CurrentPlayer, BoardViewType.ShipShotStatus); Console.WriteLine($"Great! Press any key to fire a shot at coordinate ({strX},{strY}). \nIf you want to pick a different coordinate, press (N/n) \n\nQuit anytime by pressing (Q/q)..."); consoleInput = Console.ReadKey(); //Check if Coordinate used before (and that neither Q or N are pressed) if (Opponent.board.CheckCoordinate(shotXY) != ShotHistory.Unknown && !consoleInput.Key.Equals(ConsoleKey.Q) && !consoleInput.Key.Equals(ConsoleKey.N)) { //Invalid shot coordinate, recurse to get valid shot! Console.Clear(); Console.WriteLine($"{CurrentPlayer.Name}, your current shot history:"); myGrid.Grid(Opponent, BoardViewType.ShotHistory); Console.WriteLine($"{CurrentPlayer.Name}, your opponent's shot history:"); myGrid.Grid(CurrentPlayer, BoardViewType.ShipShotStatus); Console.WriteLine($"You have previously fired a shot at coordinate ({strX},{strY}). \nPress any key to pick a different coordinate. \n\nQuit anytime by pressing (Q/q)..."); consoleInput = Console.ReadKey(); if (!consoleInput.Key.Equals(ConsoleKey.Q)) { //Recursive call -- handle correctly... shotXY = GetShotCoordinate(); if (shotXY != null) { return(shotXY); } } } else if (consoleInput.Key.Equals(ConsoleKey.N)) { //User wants to enter a different coordinate, recurse to get a valid shot! Console.Clear(); Console.WriteLine($"{CurrentPlayer.Name}, your current shot history:"); myGrid.Grid(Opponent, BoardViewType.ShotHistory); Console.WriteLine($"{CurrentPlayer.Name}, your opponent's shot history:"); myGrid.Grid(CurrentPlayer, BoardViewType.ShipShotStatus); Console.WriteLine($"You choose not to fire at coordinate ({strX},{strY}). \nPress any key to pick a different coordinate. \n\nQuit anytime by pressing (Q/q)..."); consoleInput = Console.ReadKey(); if (!consoleInput.Key.Equals(ConsoleKey.Q)) { //Recursive call -- handle correctly... shotXY = GetShotCoordinate(); if (shotXY != null) { return(shotXY); } } else { return(null); //User pressed Q } } return(shotXY); }
private ReturnType SetupPlayer(Player myPlayer, string myPlayerName) { //Setup a SPECIFIC PLAYER - Do not use currentPlayer object here!!! //Does the player have a name from before? if (myPlayer.Name.Length == 0) { bool skipError = true; while (myPlayerName.Length == 0) { //Prompt user for name Console.Clear(); if (skipError != true) { Console.WriteLine("Invalid Entry, try again... \n\n"); } skipError = false; Console.WriteLine("Enter a name for this player: \n\nPress ENTER to continue, (Q/q) to quit..."); myPlayerName = Console.ReadLine(); if (myPlayerName.ToUpper() == "Q") { return(ReturnType.Quit); } } myPlayer.SetName(myPlayerName); } //ReInit if (myPlayer.PlayerStatus == PlayerStatus.FinishedGame || myPlayer.PlayerStatus == PlayerStatus.Initialized) { //No current need to do any other kind of other cleanup (SetupPlayerBoard method initializes a new board each time) } myPlayer.SetPlayerStatus(PlayerStatus.Uninitialized); //Going to reset the player's board, uninitialized until SetupPlayerBoard returns true //StubSetupPlayerBoard(myPlayer) can alternately be used for UI testing -- non-interactive board setup if (SetupPlayerBoard(myPlayer) == ReturnType.Success) { myPlayer.SetPlayerStatus(PlayerStatus.Initialized); } else { return(ReturnType.Error); } Console.Clear(); Console.WriteLine($"Great! Your board is now completely setup, {myPlayer.Name}! \nIt is setup as shown below:"); _grid.Grid(myPlayer, BoardViewType.ShipPositions); if (P1.PlayerStatus == PlayerStatus.Uninitialized || P2.PlayerStatus == PlayerStatus.Uninitialized) { Console.WriteLine("\nSwitch Seats with the other player, and let them set up their board. :-)"); } else if (P1.PlayerStatus == PlayerStatus.Initialized && P2.PlayerStatus == PlayerStatus.Initialized) { Console.WriteLine("\nThe game is now ready to play. :-)"); } else { //Something unexpected is happening to PlayerStatus - you need to debug and refactor... } Console.WriteLine("Press any key to continue..."); if (Console.ReadKey().Key == ConsoleKey.Q) { return(ReturnType.Quit); } return(ReturnType.Success); }