Lodě
C# .NET
class Ctverec
{
public int X { get; set; }
public int Y { get; set; }
public ConsoleColor Barva { get; set; }
public bool MimoKonzoli
{
get
{
return (X >= Console.WindowWidth / 2 || Y >= Console.WindowHeight || X < 0 || Y < 0);
}
}
public Ctverec(int x, int y, ConsoleColor barva)
{
X = x;
Y = y;
Barva = barva;
}
public void Vykresli()
{
if (!MimoKonzoli)
{
Console.CursorLeft = X * 2;
Console.CursorTop = Y;
Console.ForegroundColor = Barva;
Console.Write("██");
}
}
public bool Kolize(Ctverec ctverec)
{
return (X == ctverec.X && Y == ctverec.Y);
}
}
class Lode
{
private List<Ctverec> clanky = new List<Ctverec>();
Random random = new Random();
private ConsoleColor barva = ConsoleColor.DarkMagenta;
public Lode()
{
UmistiStredniLod();
UmistiStredniLod();
UmistiStredniLod();
UmistiStredniLod();
UmistiStredniLod();
UmistiStredniLod();
UmistiStredniLod();
UmistiStredniLod();
}
public void Vykresli()
{
foreach (Ctverec clanek in clanky)
{
clanek.Vykresli();
}
}
public void UmistiStredniLod()
{
bool jeKolize = true;
while (jeKolize)
{
int X = random.Next(1, ((Console.WindowWidth / 2) - 2));
int Y = random.Next(1, (Console.WindowHeight - 1));
Ctverec clanek1 = new Ctverec(X, Y, barva);
Ctverec clanek2 = new Ctverec(X + 1, Y, barva);
Ctverec clanek3 = new Ctverec(X - 1, Y, barva);
Ctverec clanek4 = new Ctverec(X, Y - 1, barva);
List<Ctverec> tempClanky = new List<Ctverec>();
tempClanky.Add(clanek1);
tempClanky.Add(clanek2);
tempClanky.Add(clanek3);
tempClanky.Add(clanek4);
jeKolize = false;
foreach (Ctverec c in clanky)
{
for (int i = 0; i == tempClanky.Count; i++ )
{
if (tempClanky[i].Kolize(c))
{
jeKolize = true;
}
}
}
clanky.AddRange(tempClanky);
}
}
class Program
{
static void Main(string[] args)
{
Console.WindowWidth = 85;
Console.WindowHeight = 25;
Lode lod = new Lode();
lod.Vykresli();
Console.ReadKey();
}
}
Neformátovaný
Přidáno: 27.1.2020
Expirace: Neuvedeno