Skip to content

Instantly share code, notes, and snippets.

@zeitan
Created February 12, 2013 00:56
Show Gist options
  • Save zeitan/4759091 to your computer and use it in GitHub Desktop.
Save zeitan/4759091 to your computer and use it in GitHub Desktop.
4 en linea
private
def calcula_jugada(tablero)
cols_ganadora = Array.new(7)
fila = tablero.column(index).to_a.rindex(0)
cols_ganadora.each_with_index{|value,index| cols_ganadora[index]] = ganar_horizontal(tablero,index, fila) + ganar_vertical(tablero,index,fila) + ganar_cruzado(tablero,index,fila)}
end
def ganar_horizontal(tablero,col,fila)
fila_checar = tablero.row(fila)
izq = der = 0
if col > 0
for i in col-1..0
if fila_checar[i] == self.tipo.to_tipo + "" + self.id.to_s then
izq = izq + 1
else
break
end
end
end
if col + 1 < fila_checar.size
for i in col+1..fila_checar.size-1
if fila_checar[i] == self.tipo.to_tipo + "" + self.id.to_s then
der = der + 1
else
break
end
end
end
(izq + der +1) % 4
end
def ganar_vertical(tablero,col,fila)
if tablero.rowsize - fila >= 3 then
fila_checar = tablero.column(col)
abajo = 1
for i in fila + 1..fila_checar.size
if fila_checar[i] == self.tipo.to_tipo + "" + self.id.to_s then
abajo = abajo + 1
else
break
end
end
abajo % 4
else
0
end
end
def ganar_cruzado(tablero,col,fila)
abajo = arriba = 0
if col > 0
for i in col-1..0
if tablero.column(i)[fila-1] == self.tipo.to_tipo + "" + self.id.to_s then
abajo = abajo + 1
fila = fila - 1
else
break
end
end
end
if
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment