Skip to content

Instantly share code, notes, and snippets.

@danuker
Created February 6, 2018 13:25
Show Gist options
  • Save danuker/cbd1d15d6c50b547d82f1c247398aaef to your computer and use it in GitHub Desktop.
Save danuker/cbd1d15d6c50b547d82f1c247398aaef to your computer and use it in GitHub Desktop.
X si Zero - reparat pentru Python 3
# X si zero
tabla = [
[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
]
def afisare(t):
for rand in t:
rand_afisat = '|'.join(rand)
print('|' + rand_afisat + '|')
def get_elem(tabla, poz):
return tabla[(poz-1)//3][(poz-1)%3]
def set_elem(tabla, poz, elem):
if get_elem(tabla, poz) == ' ':
tabla[(poz-1)//3][(poz-1)%3] = elem
return True # s-a facut mutarea
else:
return False # nu s-a facut
def mutare(tabla, jucator):
succes = False
while not succes:
poz = int(input(jucator + ': '))
succes = set_elem(tabla, poz, jucator)
def verifica_pozitii(tabla, pozitii):
casute = [get_elem(tabla, poz) for poz in pozitii]
if len(set(casute)) == 1 and set(casute) != {' ',}:
return set(casute).pop()
else:
return ''
def sfarsit(tabla):
# 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 = [
verifica_pozitii(tabla, pozitii)
for pozitii in pozitii_de_verificat
]
castigator = max(pozitii_verificate)
if castigator:
return castigator
# remiza (tabla e plina)
remiza = True
for rand in tabla:
for poz in rand:
if poz == ' ':
remiza = False
if remiza:
return 'remiza'
while True:
for jucator in ['X', '0']:
afisare(tabla)
mutare(tabla, jucator)
castigator = sfarsit(tabla)
if castigator == 'remiza':
print('Remiza! Bravo amandoi!')
exit()
elif castigator:
afisare(tabla)
print('Felicitari, {}! Ai castigat!'.format(castigator))
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment