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

@@ -10,8 +10,16 @@ namespace Torpedo
{
static void Main(string[] args)
{
TorpedoGame Game = new TorpedoGame(8);
Game.Start();
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);
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 char[,] Field { get; set; }
private bool[,] BoatField { get; set; }
private bool[,] ShotField { get; set; }
private Boat[] Boats = new Boat[5];
private bool GameOver = false;
private int Health { get; set; }
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;
ShotField = new bool[FieldSize, FieldSize];
}
public void Start()
{
Console.WriteLine("TORPEDÓ JÁTÉK");
GenerateField();
GenerateBoats();
Draw();
Update();
}
@@ -43,11 +42,12 @@ namespace Torpedo
}
private void GenerateBoats()
{
int[] boatSizes = { 2, 3, 3, 4, 5 };
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]);
Health += boatSizes[boatNumber];
}
}
private void PlaceBoat(Boat boat)
@@ -122,6 +122,7 @@ namespace Torpedo
}
private void ShotHandling(string shot)
{
shot = shot.ToUpper().Trim();
int shotRow;
int shotCol;
try
@@ -136,19 +137,22 @@ namespace Torpedo
shotRow = int.Parse(shot[1].ToString()) - 1;
shotCol = shot[0] - 'A';
}
if (BoatField[shotRow, shotCol])
if (BoatField[shotRow, shotCol] && !ShotField[shotRow, shotCol])
{
Health--;
ShotField[shotRow, shotCol] = true;
Field[shotRow, shotCol] = 'X';
}
else
else if (!BoatField[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()
@@ -174,12 +178,13 @@ namespace Torpedo
{
while (!GameOver)
{
Console.Write("Lövés: ");
ShotHandling(Console.ReadLine().ToUpper().Trim());
Draw();
Console.Write("Lövés: ");
ShotHandling(Console.ReadLine());
if (Health == 0)
{
Console.WriteLine("Gratulálok, nyertél!");
Draw();
Console.WriteLine("Gratulálok, nyertél!\n");
GameOver = true;
}
}