75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
import math
|
|
|
|
|
|
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):
|
|
x -= 1
|
|
y -= 1
|
|
if self.czy_zajete(x, y):
|
|
print('Nie mozna dodać jednostki na tym polu')
|
|
else:
|
|
self.pole[x][y] = jednostka
|
|
self.wyswietl()
|
|
|
|
def usun_jednostke(self, x, y):
|
|
x -= 1
|
|
y -= 1
|
|
if self.czy_zajete(x, y):
|
|
self.pole[x][y] = 'O'
|
|
else:
|
|
print('To pole juz jest puste')
|
|
self.wyswietl()
|
|
|
|
def czy_zajete(self, x, y):
|
|
if self.pole[x][y] == 'O':
|
|
return False
|
|
return True
|
|
|
|
def wyswietl(self):
|
|
print()
|
|
for y in range(5):
|
|
for x in range(5):
|
|
print(self.pole[x][y], end=' ')
|
|
print()
|
|
print()
|
|
|
|
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)
|
|
|
|
dystans = math.sqrt(math.pow(math.fabs(x1 - x2), 2) + math.pow(math.fabs(y1- y2), 2))
|
|
dystans = round(dystans, 0)
|
|
|
|
if atakujacy.zasieg_ataku >= dystans:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def oblicz_obrazenia(self, atakujacy, cel):
|
|
obrazenia = atakujacy.sila_ataku
|
|
return obrazenia
|
|
|
|
def znajdz(self, jednostka):
|
|
for y in range(5):
|
|
for x in range(5):
|
|
if self.pole[x][y] == jednostka:
|
|
return x, y
|