2024-12-09 20:21:38 +01:00
|
|
|
import math
|
|
|
|
|
|
|
|
|
2024-12-09 19:29:11 +01:00
|
|
|
class Plansza:
|
|
|
|
_instance = None
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
if not cls._instance:
|
|
|
|
cls._instance = super().__new__(cls)
|
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
if not hasattr(self, '_initialized'):
|
|
|
|
self.pole = [['O' for x in range(5)] for y in range(5)]
|
|
|
|
self._initialized = True
|
|
|
|
|
|
|
|
def dodaj_jednostke(self, jednostka, x, y):
|
|
|
|
if self.czy_zajete(x, y):
|
|
|
|
print('Nie mozna dodać jednostki na tym polu')
|
|
|
|
else:
|
|
|
|
self.pole[x][y] = jednostka
|
|
|
|
|
|
|
|
def usun_jednostke(self, x, y):
|
|
|
|
if self.czy_zajete(x, y):
|
|
|
|
self.pole[x][y] = 'O'
|
|
|
|
else:
|
|
|
|
print('To pole juz jest puste')
|
2024-12-11 15:20:49 +01:00
|
|
|
|
|
|
|
def rusz_jednostke(self, jednostka, x, y):
|
|
|
|
x1, y1 = self.znajdz(jednostka)
|
|
|
|
if self.czy_zajete(x, y):
|
|
|
|
print('Nie mozna ruszyc jednostki na to pole - pole zajete')
|
|
|
|
else:
|
|
|
|
dystans = self.sprawdz_dystans(x1, y1, x, y)
|
|
|
|
if dystans <= jednostka.szybkosc_ruchu:
|
|
|
|
self.dodaj_jednostke(jednostka, x, y)
|
|
|
|
self.usun_jednostke(x1, y1)
|
|
|
|
else:
|
|
|
|
print('Nie mozna ruszyc jednostki na to pole - za duzy dystans')
|
2024-12-09 19:29:11 +01:00
|
|
|
|
2024-12-09 20:21:38 +01:00
|
|
|
def wykonaj_atak(self, atakujacy, cel):
|
|
|
|
if self.sprawdz_zasieg(atakujacy, cel):
|
|
|
|
cel.otrzymaj_obrazenia(self.oblicz_obrazenia(atakujacy, cel))
|
|
|
|
else:
|
|
|
|
print('Cel jest za daleko')
|
|
|
|
|
|
|
|
def sprawdz_zasieg(self, atakujacy, cel):
|
|
|
|
x1, y1 = self.znajdz(atakujacy)
|
|
|
|
x2, y2 = self.znajdz(cel)
|
|
|
|
|
2024-12-11 15:20:49 +01:00
|
|
|
dystans = self.sprawdz_dystans(x1, y1, x2, y2)
|
2024-12-09 20:21:38 +01:00
|
|
|
|
2024-12-11 15:28:10 +01:00
|
|
|
return atakujacy.zasieg_ataku >= dystans
|
2024-12-09 20:21:38 +01:00
|
|
|
|
2024-12-11 15:20:49 +01:00
|
|
|
def sprawdz_dystans(self, x1, y1, x2, y2):
|
|
|
|
dystans = math.sqrt(math.pow(math.fabs(x1 - x2), 2) + math.pow(math.fabs(y1 - y2), 2))
|
|
|
|
return round(dystans, 0)
|
|
|
|
|
2024-12-09 20:21:38 +01:00
|
|
|
def oblicz_obrazenia(self, atakujacy, cel):
|
|
|
|
obrazenia = atakujacy.sila_ataku
|
|
|
|
return obrazenia
|
|
|
|
|
2024-12-11 15:30:34 +01:00
|
|
|
def czy_zajete(self, x, y):
|
|
|
|
if self.pole[x][y] == 'O':
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2024-12-09 20:21:38 +01:00
|
|
|
def znajdz(self, jednostka):
|
|
|
|
for y in range(5):
|
|
|
|
for x in range(5):
|
|
|
|
if self.pole[x][y] == jednostka:
|
|
|
|
return x, y
|
2024-12-11 15:30:34 +01:00
|
|
|
|
|
|
|
def wyswietl(self):
|
|
|
|
print()
|
|
|
|
for y in range(5):
|
|
|
|
for x in range(5):
|
|
|
|
print(self.pole[x][y], end=' ')
|
|
|
|
print()
|
2024-12-11 15:30:52 +01:00
|
|
|
print()
|