def init():
k = 9 * ['.']
return k
def afisare(joc):
print(joc[0:3])
print(joc[3:6])
print(joc[6:9])
def alegere_jucator(joc):
sum1=0
sum2=0
for i in range(9):
if joc[i]=='X':
sum1+=1
elif joc[i]=='0':
sum2+=1
if sum1==sum2:
return 'X'
else:
return '0'
def lista_mutari():
L = []
for i in range(9):
if joc[i]=='.':
L.append(i)
return L
def castigator():
lista1=['X','X','X']
lista2=['0','0','0']
if joc[0:3] == lista1 or joc[3:6] == lista1 or joc[6:9] == lista1 or [joc[0],joc[4],joc[8]] == lista1 or [joc[6],joc[4],joc[2]] == lista1:
return 1
elif joc[0:3] == lista2 or joc[3:6] == lista2 or joc[6:9] == lista2 or [joc[0],joc[4],joc[8]] == lista2 or [joc[6],joc[4],joc[2]] == lista2:
return -1
else:
return 0
def joaca_mutare(joc,mutare):
import copy
joc2=copy.copy(joc)
piesa = alegere_jucator(joc)
if mutare in lista_mutari(joc):
joc2[mutare] = piesa
return joc2
def valMax(joc):
mutari = lista_mutari(joc)
if len(mutari) == 0 or castig(joc) != 0:
return castig(joc), -1
else:
calculmax = -2
ind_cel_mai_bun = -1
for mutare in mutari:
joc2=joaca_mutare(joc,mutare)
calcul, _ =valMin(joc2)
if calcul > calculmax:
calculmax = calcul
ind_cel_mai_bun = mutare
return calculmax, ind_cel_mai_bun
def valMin(joc):
mutari = lista_mutari(joc)
if len(mutari) == 0 or castig(joc) != 0:
return castig(joc), -1
else:
calculmax = -2
ind_cel_mai_bun = -1
for mutare in mutari:
joc2 = joaca_mutare(joc, mutare)
calcul, _ = valMin(joc2)
if calcul > calculmax:
calculmax = calcul
ind_cel_mai_bun = mutare
return calculmax, ind_cel_mai_bun
joc = []
joc=init()
afisare(joc)
ZGVmIGluaXQoKToKICAgIGsgPSA5ICogWycuJ10KICAgIHJldHVybiBrCgpkZWYgYWZpc2FyZShqb2MpOgogICAgcHJpbnQoam9jWzA6M10pCiAgICBwcmludChqb2NbMzo2XSkKICAgIHByaW50KGpvY1s2OjldKQoKZGVmIGFsZWdlcmVfanVjYXRvcihqb2MpOgogICAgc3VtMT0wCiAgICBzdW0yPTAKICAgIGZvciBpIGluIHJhbmdlKDkpOgogICAgICAgIGlmIGpvY1tpXT09J1gnOgogICAgICAgICAgICBzdW0xKz0xCiAgICAgICAgZWxpZiBqb2NbaV09PScwJzoKICAgICAgICAgICAgc3VtMis9MQogICAgaWYgc3VtMT09c3VtMjoKICAgICAgICByZXR1cm4gJ1gnCiAgICBlbHNlOgogICAgICAgIHJldHVybiAnMCcKCgpkZWYgbGlzdGFfbXV0YXJpKCk6CiAgICBMID0gW10KICAgIGZvciBpIGluIHJhbmdlKDkpOgogICAgICAgIGlmIGpvY1tpXT09Jy4nOgogICAgICAgICAgICBMLmFwcGVuZChpKQoKICAgIHJldHVybiBMCgoKZGVmIGNhc3RpZ2F0b3IoKToKICAgIGxpc3RhMT1bJ1gnLCdYJywnWCddCiAgICBsaXN0YTI9WycwJywnMCcsJzAnXQoKICAgIGlmIGpvY1swOjNdID09IGxpc3RhMSBvciBqb2NbMzo2XSA9PSBsaXN0YTEgb3Igam9jWzY6OV0gPT0gbGlzdGExIG9yIFtqb2NbMF0sam9jWzRdLGpvY1s4XV0gPT0gbGlzdGExIG9yIFtqb2NbNl0sam9jWzRdLGpvY1syXV0gPT0gbGlzdGExOgogICAgICAgIHJldHVybiAxCiAgICBlbGlmIGpvY1swOjNdID09IGxpc3RhMiBvciBqb2NbMzo2XSA9PSBsaXN0YTIgb3Igam9jWzY6OV0gPT0gbGlzdGEyIG9yIFtqb2NbMF0sam9jWzRdLGpvY1s4XV0gPT0gbGlzdGEyIG9yIFtqb2NbNl0sam9jWzRdLGpvY1syXV0gPT0gbGlzdGEyOgogICAgICAgIHJldHVybiAtMQogICAgZWxzZToKICAgICAgICByZXR1cm4gMAoKZGVmIGpvYWNhX211dGFyZShqb2MsbXV0YXJlKToKICAgIGltcG9ydCBjb3B5CiAgICAKICAgIGpvYzI9Y29weS5jb3B5KGpvYykKICAgIAogICAgcGllc2EgPSBhbGVnZXJlX2p1Y2F0b3Ioam9jKQogICAgCiAgICBpZiBtdXRhcmUgaW4gbGlzdGFfbXV0YXJpKGpvYyk6CiAgICAgICAgam9jMlttdXRhcmVdID0gcGllc2EKICAgIAogICAgcmV0dXJuIGpvYzIKCmRlZiB2YWxNYXgoam9jKToKICAgIAogICAgbXV0YXJpID0gbGlzdGFfbXV0YXJpKGpvYykKICAgIAogICAgaWYgbGVuKG11dGFyaSkgPT0gMCBvciBjYXN0aWcoam9jKSAhPSAwOgogICAgICAgIHJldHVybiBjYXN0aWcoam9jKSwgLTEKICAgIAogICAgZWxzZToKICAgICAgICBjYWxjdWxtYXggPSAtMgogICAgICAgIGluZF9jZWxfbWFpX2J1biA9IC0xCiAgICAgICAgCiAgICBmb3IgbXV0YXJlIGluIG11dGFyaToKICAgICAgICBqb2MyPWpvYWNhX211dGFyZShqb2MsbXV0YXJlKQogICAgICAgIGNhbGN1bCwgXyA9dmFsTWluKGpvYzIpCiAgICAgICAgaWYgY2FsY3VsID4gY2FsY3VsbWF4OgogICAgICAgICAgICBjYWxjdWxtYXggPSBjYWxjdWwKICAgICAgICAgICAgaW5kX2NlbF9tYWlfYnVuID0gbXV0YXJlCiAgICAKICAgIHJldHVybiBjYWxjdWxtYXgsIGluZF9jZWxfbWFpX2J1bgoKCmRlZiB2YWxNaW4oam9jKToKICAgIG11dGFyaSA9IGxpc3RhX211dGFyaShqb2MpCgogICAgaWYgbGVuKG11dGFyaSkgPT0gMCBvciBjYXN0aWcoam9jKSAhPSAwOgogICAgICAgIHJldHVybiBjYXN0aWcoam9jKSwgLTEKCiAgICBlbHNlOgogICAgICAgIGNhbGN1bG1heCA9IC0yCiAgICAgICAgaW5kX2NlbF9tYWlfYnVuID0gLTEKCiAgICBmb3IgbXV0YXJlIGluIG11dGFyaToKICAgICAgICBqb2MyID0gam9hY2FfbXV0YXJlKGpvYywgbXV0YXJlKQogICAgICAgIGNhbGN1bCwgXyA9IHZhbE1pbihqb2MyKQogICAgICAgIGlmIGNhbGN1bCA+IGNhbGN1bG1heDoKICAgICAgICAgICAgY2FsY3VsbWF4ID0gY2FsY3VsCiAgICAgICAgICAgIGluZF9jZWxfbWFpX2J1biA9IG11dGFyZQoKICAgIHJldHVybiBjYWxjdWxtYXgsIGluZF9jZWxfbWFpX2J1bgoKCmpvYyA9IFtdCgpqb2M9aW5pdCgpCgphZmlzYXJlKGpvYykKCg==