Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@danuker
Created November 19, 2018 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danuker/6782a190731494502c6d770d2afe7ada to your computer and use it in GitHub Desktop.
Save danuker/6782a190731494502c6d770d2afe7ada to your computer and use it in GitHub Desktop.
X și Zero cu clasa
# X si zero - Tutorial #9 - Clase
class Tabla:
def __init__(self):
self.reprezentare = [
[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
]
def afisare(self):
for rand in self.reprezentare:
rand_afisat = '|'.join(rand)
print('|' + rand_afisat + '|')
def get_elem(self, poz):
return self.reprezentare[(poz-1)//3][(poz-1)%3]
def set_elem(self, poz, elem):
if self.get_elem(poz) == ' ':
self.reprezentare[(poz-1)//3][(poz-1)%3] = elem
return True # s-a facut mutarea
else:
return False # nu s-a facut
def mutare(self, jucator):
succes = False
while not succes:
poz = int(input(jucator + ': '))
succes = self.set_elem(poz, jucator)
def verifica_pozitii(self, pozitii):
casute = [self.get_elem(poz) for poz in pozitii]
if len(set(casute)) == 1 and set(casute) != {' ',}:
return set(casute).pop()
else:
return ''
def sfarsit(self):
# castiga un jucator
pozitii_de_verificat = [
[1, 5, 9],
[3, 5, 7],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
]
pozitii_verificate = [
self.verifica_pozitii(pozitii)
for pozitii in pozitii_de_verificat
]
castigator = max(pozitii_verificate)
if castigator:
return castigator
# remiza (tabla e plina)
remiza = True
for rand in self.reprezentare:
for poz in rand:
if poz == ' ':
remiza = False
if remiza:
return 'remiza'
def main():
tabla = Tabla()
castigator = None
while not castigator:
for jucator in ['X', '0']:
if castigator == 'remiza':
print('Remiza! Bravo amandoi!')
return
elif castigator:
tabla.afisare()
print('Felicitari, {}! Ai castigat!'.format(castigator))
return
tabla.afisare()
tabla.mutare(jucator)
castigator = tabla.sfarsit()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment