redone, translated

This commit is contained in:
medojanos
2025-09-10 21:31:02 +02:00
parent caf6afbc77
commit ed2860e8b3
5 changed files with 212 additions and 121 deletions

20
Torpedo/Boat.cs Normal file
View File

@@ -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;
}
}
}

View File

@@ -10,8 +10,8 @@ namespace Torpedo
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
TorpedoJatek jatek = new TorpedoJatek(8); TorpedoGame Game = new TorpedoGame(8);
jatek.Indul(); Game.Start();
} }
} }
} }

View File

@@ -43,9 +43,10 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Boat.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TorpedoJatek.cs" /> <Compile Include="TorpedoGame.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />

188
Torpedo/TorpedoGame.cs Normal file
View File

@@ -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;
}
}
}
}
}

View File

@@ -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<HajoMeret)
{
MezoRajzol();
Console.Write("\nLőjjé': ");
string loves = Console.ReadLine().ToUpper();
try
{
if (loves.Length < 2) throw new Exception("Hibás lövés!");
char oszlop = loves[0];
int sor = int.Parse(loves.Substring(1));
if (oszlop < 'A' || oszlop > ('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<Meret; o++) {
Console.Write((char)('A'+o));
}
for (int sor = 0; sor < Meret; sor++)
{
Console.Write("\n{0:00}",(sor + 1));
for (int oszlop = 0; oszlop < Meret; oszlop++)
{
Console.Write(Jatekmezo[sor, oszlop]);
}
}
}
}
}