Skip to content

Instantly share code, notes, and snippets.

@ejherran
Last active May 30, 2016 19:16
Show Gist options
  • Save ejherran/aa2f1221cdd843f4ccba43df3794579b to your computer and use it in GitHub Desktop.
Save ejherran/aa2f1221cdd843f4ccba43df3794579b to your computer and use it in GitHub Desktop.
Go Test
import subprocess as sp
def genMat(r, c, v):
mat = []
row = [v]*c
for i in range(r):
mat.append(row[:])
return mat
def inMat(m, v):
flag = False
for r in m:
if v in r:
flag = True
break
return flag
def copyMat(m):
tm = []
for e in m:
tm.append(e[:])
return tm
def unir(g, lg):
for i in range(len(lg)-1):
for j in range(i+1, len(lg)):
if(lg[i] >= 0 and lg[j] >= 0):
g[lg[i]] = g[lg[i]]+g[lg[j]]
g[lg[j]] = []
res = []
for e in g:
if e != []:
res.append(e)
return res
def inGrupo(gs, t):
idx = -1
for i in range(len(gs)):
if t in gs[i]:
idx = i
break
return idx
def agrupar(m):
res = {'X': [], 'O':[]}
lv = len(m)
lh = len(m[0])
for i in range(lv):
for j in range(lh):
pd = m[i][j]
if(pd != '+'):
iup = inGrupo(res[pd], (i-1, j))
ido = inGrupo(res[pd], (i+1, j))
ile = inGrupo(res[pd], (i, j-1))
iri = inGrupo(res[pd], (i, j+1))
if(iup >= 0):
res[pd][iup].append((i, j))
elif(ido >= 0):
res[pd][ido].append((i,j))
elif(ile >= 0):
res[pd][ile].append((i,j))
elif(iri >= 0):
res[pd][iri].append((i,j))
else:
res[pd].append([(i, j)])
res[pd] = unir(res[pd], [iup, ido, ile, iri])
return res['X']+res['O']
def contar(m, g):
lv = len(m)
lh = len(m[0])
lib = []
for p in g:
if( p[0]-1 >= 0 and m[p[0]-1][p[1]] == '+' and (p[0]-1, p[1]) not in lib):
lib.append((p[0]-1, p[1]))
if( p[0]+1 < lv and m[p[0]+1][p[1]] == '+' and (p[0]+1, p[1]) not in lib):
lib.append((p[0]+1, p[1]))
if( p[1]-1 >= 0 and m[p[0]][p[1]-1] == '+' and (p[0], p[1]-1) not in lib):
lib.append((p[0], p[1]-1))
if( p[1]+1 < lh and m[p[0]][p[1]+1] == '+' and (p[0], p[1]+1) not in lib):
lib.append((p[0], p[1]+1))
return len(lib)
def libertades(m):
res = copyMat(m)
gs = agrupar(m)
for g in gs:
cnt = contar(m, g)
for t in g:
res[t[0]][t[1]] = cnt
return res
def ajuste(l):
r = ""
for e in l:
t = str(e)
if len(t) < 3:
t = t+("-"*(3-len(t)))
r = r+t
while(r[-1] == '-'):
r = r[0:-1]
return r
def colorize(v):
if(v == 'X'):
return '\033[91m'+v+'\033[0m'
elif(v == 'O'):
return '\033[92m'+v+'\033[0m'
else:
return v
def tablero(m, l):
num = list(range(1, len(m[0])+1))
dic = list("ABCDEFGHIJKLMNOPQRS")
sep = ['|']*len(m[0])
print(" "+" ".join(str(n) for n in num)+"\t\t"+" "+" ".join(str(n) for n in num))
for i in range(len(m)):
print(dic[i]+" "+"--".join(colorize(e) for e in m[i])+"\t\t"+dic[i]+" "+ajuste(l[i]))
if i < len(m)-1:
print(" "+" ".join(sep)+"\t\t"+" "+" ".join(sep))
def limpiar(m, l, o):
cc = 0
for i in range(len(m)):
for j in range(len(m[0])):
if(m[i][j] == o and l[i][j] == 0):
m[i][j] = '+'
cc += 1
return cc
def verificar(m, pi, pj, o):
to = ''
if o == 'X':
to = 'O'
else:
to = 'X'
tm = copyMat(m)
tm[pi][pj] = o
tl = libertades(tm)
limpiar(tm, tl, to)
tl = libertades(tm)
return tl[pi][pj]
def jugar():
m = genMat(9, 9, '+')
di = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18}
pt = {'X': 0, 'O': 0}
last = {'X': [], 'O': []}
pi = ['X', 'O']
c = 0
cp = 0
while(inMat(m, '+')):
alert = ""
while(True):
sp.call("clear", shell=True)
print("\t\tBlancas (O) ha capturado "+str(pt['O'])+" piedras!")
print("\t\tNegras (X) ha capturado "+str(pt['X'])+" piedras!")
print(alert+"\n")
l = libertades(m)
tablero(m, l)
try:
msj = ""
if(c % 2 == 0):
msj = "\nJugada De Negras("+pi[c % 2]+")?: "
else:
msj = "\nJugada De Blancas("+pi[c % 2]+")?: "
j = input(msj).upper()
if j == 'P':
cp += 1
break
else:
poi = di[ j[0] ]
poj = int(j[1])-1
if( m[poi][poj] == '+' ):
if(verificar(m, poi, poj, pi[c % 2]) > 0):
tm = copyMat(m)
tm[poi][poj] = pi[c % 2]
l = libertades(tm)
cc = limpiar(tm, l, pi[(c+1)%2])
if(tm != last[pi[c % 2]]):
m = tm
pt[pi[c % 2]] += cc
last[pi[c % 2]] = copyMat(m)
cp = 0
break
else:
alert = "\t\t\t\033[93mKo posicional infinito!.\033[0m"
else:
alert = "\t\t\t \033[93mJugada suicida!.\033[0m"
else:
alert = "\t\t\t \033[93mJugada repetida!.\033[0m"
except:
alert = "\t\t\t\033[93mJugada incorrecta!.\033[0m"
c = c+1
if(cp == 2):
break
sp.call("clear", shell=True)
print("\t\tBlancas (O) ha capturado "+str(pt['O'])+" piedras!")
print("\t\tNegras (X) ha capturado "+str(pt['X'])+" piedras!")
print("\t\t\t \033[94m¡FIN DEL JUEGO!\033[0m\n")
l = libertades(m)
tablero(m, l)
input("\nPulse enter...")
jugar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment