44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
|
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()
|