public GameFlow() { _gridSize = 10; //Default size is 10x10 _grid = new DisplayGrid(_gridSize); //Board Sizes between 5-10 should be playable. //Board Sizes between 10-26 probably require extensive retesting... _rnd = new Random(); P1 = new Player(); P2 = new Player(); }
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 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); }