Looped, fixed bugs, wrote game logic

This commit is contained in:
medojanos
2025-09-13 11:48:39 +02:00
parent ed2860e8b3
commit 901ac1fe50
2 changed files with 27 additions and 14 deletions

View File

@@ -9,9 +9,17 @@ namespace Torpedo
internal class Program internal class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{
Console.WriteLine("TORPEDÓ JÁTÉK\n");
Console.WriteLine("Indításhoz nyomj meg egy gombot");
Console.ReadKey();
while (true)
{ {
TorpedoGame Game = new TorpedoGame(8); TorpedoGame Game = new TorpedoGame(8);
Game.Start(); Game.Start();
Console.WriteLine("Újraindításhoz nyomj meg egy gombot");
Console.ReadKey();
}
} }
} }
} }

View File

@@ -11,23 +11,22 @@ namespace Torpedo
private int FieldSize { get; set; } private int FieldSize { get; set; }
private char[,] Field { get; set; } private char[,] Field { get; set; }
private bool[,] BoatField { get; set; } private bool[,] BoatField { get; set; }
private bool[,] ShotField { get; set; }
private Boat[] Boats = new Boat[5]; private Boat[] Boats = new Boat[5];
private bool GameOver = false; private bool GameOver = false;
private Random random = new Random();
private int Health { get; set; } private int Health { get; set; }
private Random random = new Random();
public TorpedoGame(int fieldSize) public TorpedoGame(int fieldSize)
{ {
FieldSize = fieldSize; FieldSize = fieldSize;
Field = new char[FieldSize,FieldSize]; Field = new char[FieldSize,FieldSize];
BoatField = new bool[FieldSize, FieldSize]; BoatField = new bool[FieldSize, FieldSize];
Health = 1; ShotField = new bool[FieldSize, FieldSize];
} }
public void Start() public void Start()
{ {
Console.WriteLine("TORPEDÓ JÁTÉK");
GenerateField(); GenerateField();
GenerateBoats(); GenerateBoats();
Draw();
Update(); Update();
} }
@@ -48,6 +47,7 @@ namespace Torpedo
{ {
Boats[boatNumber] = new Boat(boatSizes[boatNumber], random.Next(2) == 0 ? 'h' : 'v'); Boats[boatNumber] = new Boat(boatSizes[boatNumber], random.Next(2) == 0 ? 'h' : 'v');
PlaceBoat(Boats[boatNumber]); PlaceBoat(Boats[boatNumber]);
Health += boatSizes[boatNumber];
} }
} }
private void PlaceBoat(Boat boat) private void PlaceBoat(Boat boat)
@@ -122,6 +122,7 @@ namespace Torpedo
} }
private void ShotHandling(string shot) private void ShotHandling(string shot)
{ {
shot = shot.ToUpper().Trim();
int shotRow; int shotRow;
int shotCol; int shotCol;
try try
@@ -136,19 +137,22 @@ namespace Torpedo
shotRow = int.Parse(shot[1].ToString()) - 1; shotRow = int.Parse(shot[1].ToString()) - 1;
shotCol = shot[0] - 'A'; shotCol = shot[0] - 'A';
} }
if (BoatField[shotRow, shotCol]) if (BoatField[shotRow, shotCol] && !ShotField[shotRow, shotCol])
{ {
Health--; Health--;
ShotField[shotRow, shotCol] = true;
Field[shotRow, shotCol] = 'X'; Field[shotRow, shotCol] = 'X';
} }
else else if (!BoatField[shotRow, shotCol])
{ {
Field[shotRow, shotCol] = '*'; Field[shotRow, shotCol] = '*';
} }
} }
catch (Exception e) catch
{ {
Console.WriteLine(e); Console.WriteLine("\nMegengedett formátum: A1, a1, 1A, 1a");
Console.Write("Lövés: ");
ShotHandling(Console.ReadLine());
} }
} }
private void Draw() private void Draw()
@@ -174,12 +178,13 @@ namespace Torpedo
{ {
while (!GameOver) while (!GameOver)
{ {
Console.Write("Lövés: ");
ShotHandling(Console.ReadLine().ToUpper().Trim());
Draw(); Draw();
Console.Write("Lövés: ");
ShotHandling(Console.ReadLine());
if (Health == 0) if (Health == 0)
{ {
Console.WriteLine("Gratulálok, nyertél!"); Draw();
Console.WriteLine("Gratulálok, nyertél!\n");
GameOver = true; GameOver = true;
} }
} }