diff --git a/Torpedo/Boat.cs b/Torpedo/Boat.cs new file mode 100644 index 0000000..59f9328 --- /dev/null +++ b/Torpedo/Boat.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Torpedo +{ + public class Boat + { + public int Size { get; set; } + public int Direction { get; set; } + public int[] Coordinate = new int[2]; + public Boat(int size, int direction) + { + Size = size; + Direction = direction; + } + } +} diff --git a/Torpedo/Program.cs b/Torpedo/Program.cs index 0385de9..7ffb935 100644 --- a/Torpedo/Program.cs +++ b/Torpedo/Program.cs @@ -10,8 +10,8 @@ namespace Torpedo { static void Main(string[] args) { - TorpedoJatek jatek = new TorpedoJatek(8); - jatek.Indul(); + TorpedoGame Game = new TorpedoGame(8); + Game.Start(); } } } diff --git a/Torpedo/Torpedo.csproj b/Torpedo/Torpedo.csproj index 7f69fcc..c0b73ce 100644 --- a/Torpedo/Torpedo.csproj +++ b/Torpedo/Torpedo.csproj @@ -43,9 +43,10 @@ + - + diff --git a/Torpedo/TorpedoGame.cs b/Torpedo/TorpedoGame.cs new file mode 100644 index 0000000..96d2355 --- /dev/null +++ b/Torpedo/TorpedoGame.cs @@ -0,0 +1,188 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Torpedo +{ + internal class TorpedoGame + { + private int FieldSize { get; set; } + private char[,] Field { get; set; } + private bool[,] BoatField { get; set; } + private Boat[] Boats = new Boat[5]; + private bool GameOver = false; + private Random random = new Random(); + private int Health {get; set;} + public TorpedoGame(int fieldSize) + { + FieldSize = fieldSize; + Field = new char[FieldSize,FieldSize]; + BoatField = new bool[FieldSize, FieldSize]; + Health = 1; + } + public void Start() + { + Console.WriteLine("TORPEDÓ JÁTÉK"); + GenerateField(); + GenerateBoats(); + Draw(); + Update(); + } + + private void GenerateField() + { + for (int row = 0; row < FieldSize; row++) + { + for (int col = 0; col < FieldSize; col++) + { + Field[row, col] = '~'; + } + } + } + private void GenerateBoats() + { + int[] boatSizes = { 2, 3, 3, 4, 5 }; + for (int boatNumber = 0; boatNumber < boatSizes.Length; boatNumber++) + { + Boats[boatNumber] = new Boat(boatSizes[boatNumber], random.Next(2) == 0 ? 'h' : 'v'); + PlaceBoat(Boats[boatNumber]); + } + } + private void PlaceBoat(Boat boat) + { + int placable = 0; + bool placed = false; + while (!placed) + { + boat.Coordinate[0] = random.Next(FieldSize); + boat.Coordinate[1] = random.Next(FieldSize); + + // checking wall collision + if (boat.Coordinate[0] + boat.Size > FieldSize || boat.Coordinate[1] + boat.Size > FieldSize) continue; + + switch (boat.Direction) + { + // horizontal collision with other boat + case 'h': + for (int boatSize = 0; boatSize < boat.Size; boatSize++) + { + if (!BoatField[boat.Coordinate[0], boat.Coordinate[1] + boatSize]) + { + placable++; + } + else + { + placable = 0; + } + } + //horizontal placing + if (placable == boat.Size) + { + for (int boatSize = 0; boatSize < boat.Size; boatSize++) + { + if (!BoatField[boat.Coordinate[0], boat.Coordinate[1] + boatSize]) + { + BoatField[boat.Coordinate[0], boat.Coordinate[1] + boatSize] = true; + placed = true; + } + } + } + break; + // vertical collision with other boat + case 'v': + for (int boatSize = 0; boatSize < boat.Size; boatSize++) + { + if (!BoatField[boat.Coordinate[0] + boatSize, boat.Coordinate[1]]) + { + placable++; + } + else + { + placable = 0; + } + } + // vertical placing + if (placable == boat.Size) + { + for (int boatSize = 0; boatSize < boat.Size; boatSize++) + { + if (!BoatField[boat.Coordinate[0] + boatSize, boat.Coordinate[1]]) + { + BoatField[boat.Coordinate[0] + boatSize, boat.Coordinate[1]] = true; + placed = true; + } + } + } + break; + } + placable = 0; + } + } + private void ShotHandling(string shot) + { + int shotRow; + int shotCol; + try + { + if (char.IsDigit(shot[0])) + { + shotRow = int.Parse(shot[0].ToString()) - 1; + shotCol = shot[1] - 'A'; + } + else + { + shotRow = int.Parse(shot[1].ToString()) - 1; + shotCol = shot[0] - 'A'; + } + if (BoatField[shotRow, shotCol]) + { + Health--; + Field[shotRow, shotCol] = 'X'; + } + else + { + Field[shotRow, shotCol] = '*'; + } + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + private void Draw() + { + Console.Clear(); + Console.Write(' '); + for (int ch = 0; ch < FieldSize; ch++) + { + Console.Write((char) ('A' + ch)); + } + Console.WriteLine(); + for (int row = 0; row < Field.GetLength(0); row++) + { + Console.Write(row + 1); + for (int col = 0; col < Field.GetLength(1); col++) + { + Console.Write(Field[row,col]); + } + Console.WriteLine(); + } + } + private void Update() + { + while (!GameOver) + { + Console.Write("Lövés: "); + ShotHandling(Console.ReadLine().ToUpper().Trim()); + Draw(); + if (Health == 0) + { + Console.WriteLine("Gratulálok, nyertél!"); + GameOver = true; + } + } + } + } +} diff --git a/Torpedo/TorpedoJatek.cs b/Torpedo/TorpedoJatek.cs deleted file mode 100644 index 5034eae..0000000 --- a/Torpedo/TorpedoJatek.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Torpedo -{ - internal class TorpedoJatek - { - public int Meret { get; set; } - public int HajoMeret { get; set; } - public char[,] Jatekmezo { get; set; } - public int[] HajoHelye { get; set; } - - private int Talalat; - - public TorpedoJatek(int meret) - { - Meret = meret; - Jatekmezo = new char[meret, meret]; - MezotGeneral(); - HajoMeret = 3; - KifutAHajo(); - } - - private void KifutAHajo() - { - Random rd = new Random(); - int sor = rd.Next(Meret); - int oszlop = rd.Next(Meret-HajoMeret+1); - HajoHelye = new int[2]; - HajoHelye[0] = sor; - HajoHelye[1] = oszlop; - } - - private void MezotGeneral() - { - for (int sor = 0; sor < Meret; sor++) - { - for (int oszlop = 0; oszlop < Meret; oszlop++) - { - Jatekmezo[sor, oszlop] = '~'; - } - } - } - - public void Indul() - { - Console.WriteLine("TORPEDÓ JÁTÉK"); - Talalat = 0; - while (Talalat ('A' + Meret - 1)) throw new Exception("Érvénytelen oszlop!"); - if (sor < 1 || sor > Meret) throw new Exception("Érvénytelen sor"); - Console.Clear(); - LovesKezeles(oszlop, sor - 1); - } - catch (Exception hiba) - { - Console.Clear(); - Console.WriteLine(hiba.Message); - continue; - } - - } - Console.Clear(); - Console.WriteLine("Vége a játéknak! Gratulálok!"); - MezoRajzol(); - Console.ReadKey(); - } - - private void LovesKezeles(char oszlop, int sor) - { - int oszlopSzam = oszlop-'A'; - if (Jatekmezo[sor,oszlopSzam]!='~') - { - Console.WriteLine("Ez már volt!"); - } else - { - if(sor == HajoHelye[0] && (oszlopSzam >= HajoHelye[1] && oszlopSzam <= HajoHelye[1]+HajoMeret-1)) - { - Jatekmezo[sor, oszlopSzam] = 'x'; - Talalat++; - Console.WriteLine("Találat!"); - } else - { - Jatekmezo[sor, oszlopSzam] = '*'; - Console.WriteLine("Nem talált!"); - } - } - } - - private void MezoRajzol() - { - Console.Write("\n "); - for(int o = 0; o