/// <summary> /// Displaying the playing fields. /// </summary> /// <param name="computerArea"></param> /// <param name="playerArea"></param> /// <param name="rowLetters"></param> public static void ShowGameArea(GameArea computerArea, GameArea playerArea, string[] rowLetters) { Console.WriteLine(" |=========================================|========================================|"); Console.WriteLine(" | Computer game area | Player game area |"); Console.WriteLine(" |=========================================|========================================|"); Console.WriteLine(" | A B C D E F G H I J | A B C D E F G H I J |"); Console.WriteLine(); for (int i = 0; i < computerArea.GameAreaArray.GetLength(0); i++) { Console.Write(" | "); Console.Write($"{rowLetters[i],3} "); for (int j = 0; j < computerArea.GameAreaArray.GetLength(1); j++) { // Console.Write("X "); Console.Write($"{computerArea.GameAreaArray[i, j]} "); } Console.Write(" | "); Console.Write($"{rowLetters[i],3} "); for (int k = 0; k < playerArea.GameAreaArray.GetLength(1); k++) { // Console.Write("X "); Console.Write($"{computerArea.GameAreaArray[i, k]} "); } Console.Write(" |\n\n"); } Console.WriteLine(" |=========================================|========================================|"); }
public void FillGameArea(GameArea computerArea, GameArea playerArea) { for (int i = 0; i < computerArea.GameAreaArray.GetLength(0); i++) { for (int j = 0; j < computerArea.GameAreaArray.GetLength(1); j++) { computerArea.GameAreaArray[i, j] = 0; } for (int k = 0; k < playerArea.GameAreaArray.GetLength(1); k++) { playerArea.GameAreaArray[i, k] = 0; } } }
public void PlacePlayerShip(GameArea playerArea, Ship choosenPlayerShip, List <Ship> ships, List <Ship> playerShips, byte xCord, byte yCord) { try { if (playerArea.GameAreaArray[xCord, yCord] == 0)//0=empty, 1=ship, 2=ship hit, 3=not shot at, 4=already shot at { if (choosenPlayerShip.Direction == true) { if (playerArea.GameAreaArray.GetLength(1) - (playerArea.GameAreaArray.GetLength(1) - choosenPlayerShip.Length) >= choosenPlayerShip.Length) { byte count = 0; for (int i = 0; i < choosenPlayerShip.Length; i++) { if (playerArea.GameAreaArray[xCord, yCord + i] == 0)//0=empty, 1=ship, 2=ship hit, 3=not shot at, 4=already shot at { count = count++; } } if (count < choosenPlayerShip.Length) { //Here the ship can be placed. for (int i = 0; i < choosenPlayerShip.Length; i++) { playerArea.GameAreaArray[xCord, yCord + i] = 1; } playerShips.Add(choosenPlayerShip); ships.Remove(choosenPlayerShip); } } } else { if (playerArea.GameAreaArray.GetLength(0) - (playerArea.GameAreaArray.GetLength(0) - choosenPlayerShip.Length) >= choosenPlayerShip.Length) { } } } } catch (Exception) { Console.WriteLine("You can't place your ship in those cordinates"); } }
static void Main(string[] args) { //Creating gameareas GameArea playerArea = gameController.CreateGameArea(10, 10, 0); GameArea computerArea = gameController.CreateGameArea(10, 10, 0); string[] rowLetters = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; string[] colLetters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; List <Ship> ships = new List <Ship>(); ships.Add(new Ship("Carrier", 5, 0, true)); ships.Add(new Ship("Battleship", 4, 0, true)); ships.Add(new Ship("Cruiser", 3, 0, true)); ships.Add(new Ship("Submarine", 3, 0, true)); ships.Add(new Ship("Destroyer", 2, 0, true)); List <Ship> playerShips = new List <Ship>(); playerShips.Add(new Ship("Carrier", 5, 0, true)); playerShips.Add(new Ship("Battleship", 4, 0, true)); playerShips.Add(new Ship("Cruiser", 3, 0, true)); playerShips.Add(new Ship("Submarine", 3, 0, true)); playerShips.Add(new Ship("Destroyer", 2, 0, true)); List <Ship> computerShips = new List <Ship>(); computerShips.Add(new Ship("Carrier", 5, 0, true)); computerShips.Add(new Ship("Battleship", 4, 0, true)); computerShips.Add(new Ship("Cruiser", 3, 0, true)); computerShips.Add(new Ship("Submarine", 3, 0, true)); computerShips.Add(new Ship("Destroyer", 2, 0, true)); Console.WriteLine(); Console.WriteLine(" |==================================================================================|"); Console.WriteLine(" |================================ Place your ships! =============================|"); Console.WriteLine(" |==================================================================================|"); ShowBanner(); //Creating a bautifull banner from an ASCii file. //Sending the arguments to the methods by including those in the brackets. They can only be read this way //gameController.FillGameArea(computerArea, playerArea); ShowGameArea(computerArea, playerArea, rowLetters); ShowChooseShip(playerShips); Ship choosenShip = gameController.ChoosePlayerShip(ships, playerShips); ChooseDirection(choosenShip); //ShowPlaceYourShip(); ShowPlaceXCordinate(); var xTuple = gameController.PlaceX(xCord, xCordChar); //I am so sorry for using var. But as it returs two different variables, then var is the only choice i know Console.Write($"{xTuple.Item1}\t"); ShowPlaceYCordinate(); //gameController.PlaceY(); Console.Write($"{gameController.PlaceY(yCord)}\n\n"); gameController.PlacePlayerShip(playerArea, choosenShip, xCord, yCord); }
public GameArea CreateGameArea(byte width, byte height, byte state) { GameArea area = new GameArea(width, height, state); return(area); }