C,
pasted
on Oct 13:
|
# -*- coding: utf-8 -*-
###########################
# Salto del Caballo
# Versión: 0.2
# Autor: Adrigm
# Email: adrigm.admin@gmail.com
# Web: www.adrigm.es
# Licencia: GPL
###########################
# Bibliotecas.
import random
import sys
# Funciones.
# ---------------------------------------------------------------------
# Dibuja el tablero.
def dibujar(tablero):
tab = ""
for f in range(8):
for c in range(8):
if tablero[f]1 < 10:
celda = " " + str(tablero[f]1)
else:
celda = str(tablero[f]1)
tab = tab + celda + " "
tab = tab + "\n"
print tab
# Devuelve una lista con las coordenadas del caballo.
def pos_c(tablero):
for f in range(8):
for c in range(8):
if tablero[f]1 == "CC":
pos = [f, c]
return pos
# Devuelve 1 si el tablero está lleno.
def lleno(tablero):
for f in range(8):
for c in range(8):
if tablero[f]1 == "__":
return 0
return 1
# Mueve desde CC desde inicial hasta final.
def mover(inicial, final, tablero, contador):
tablero[inicial[0]][inicial[1]] = contador
tablero[final[0]][final[1]] = "CC"
# Retrocede una posición.
def retroceder(inicial, final, tablero):
tablero[inicial[0]][inicial[1]] = "__"
tablero[final[0]][final[1]] = "CC"
# devuelve una lista con los movimientos posibless.
def posibles(mov, pos, tablero):
posibles = []
for n in range(8):
if (pos[0] + mov[n][0]) in range(8) and (pos[1] + mov[n][1]) in range(8):
if tablero[pos[0] + mov[n][0]][pos[1] + mov[n][1]] == "__":
v = [pos[0]+mov[n][0], pos[1]+mov[n][1]]
posibles.append(v)
return posibles
# Ordena dos listas en función de la primera.
def ordenar(lista, listb):
if listb == []:
return listb
else:
for i in range(len(lista)):
for j in range(len(lista) -1):
if lista[j] > lista[j+1]:
listb[j], listb[j+1] = listb[j+1], listb[j]
lista[j], lista[j+1] = lista[j+1], lista[j]
return listb
# Ordena los valores de manera que primero se mueva a las menos probables.
def heuristica(pos_mov, tablero, mov):
n_pos = []
for n in range(len(pos_mov)):
pos = len(posibles(mov, pos_mov[n], tablero))
n_pos.append(pos)
return ordenar(n_pos, pos_mov)
# Función recursiva principal.
def caballo(contador):
contador += 1
pos = pos_c(tablero)
mov = [[2, 1], [1, 2], [-2, 1], [2, -1], [-1, 2], [1, -2], [-2, -1], [-1, -2]]
pos_mov = posibles(mov, pos, tablero)
pos_mov = heuristica(pos_mov, tablero, mov)
for i in range(len(pos_mov)):
pos_ant = pos
mover(pos, pos_mov[i], tablero, contador)
dibujar(tablero)
if lleno(tablero):
sys.exit()
pos = pos_c(tablero)
caballo(contador)
retroceder(pos, pos_ant, tablero)
pos = pos_c(tablero)
dibujar(tablero)
# ---------------------------------------------------------------------
# Crea un tablero de 8x8.
tablero = range(8)
for n in tablero:
tablero[n] = range(8)
# Pone el tablero a 0.
for f in range(8):
for c in range(8):
tablero[f]1 = "__"
# Posición inicial caballo.
tablero[random.randint(0,7)][random.randint(0,7)] = "CC"
# Comienza el programa.
dibujar(tablero)
contador = 0
caballo(contador)
|
Output:
|
Line 2: error: invalid preprocessing directive #-
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '##' in program
Line 3: error: stray '#' in program
Line 2: error: invalid preprocessing directive #Salto
Line 2: error: invalid preprocessing directive #Versi
Line 2: error: invalid preprocessing directive #Autor
Line 2: error: invalid preprocessing directive #Email
Line 2: error: invalid preprocessing directive #Web
Line 2: error: invalid preprocessing directive #Licencia
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '##' in program
Line 10: error: stray '#' in program
Line 2: error: invalid preprocessing directive #Bibliotecas
Line 13: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'random'
Line 2: error: invalid preprocessing directive #Funciones
Line 2: error: invalid preprocessing directive #--
Line 2: error: invalid preprocessing directive #Dibuja
Line 21: error: expected identifier or '(' before '&' token
Line 22: error: expected identifier or '(' before 'for'
Line 24: error: expected identifier or '(' before numeric constant
Line 25: error: expected identifier or '(' before '&' token
Line 25: error: expected identifier or '(' before '+' token
Line 28: error: expected identifier or '(' before '&' token
Line 29: warning: data definition has no type or storage class
Line 29: error: 'quot' undeclared here (not in a function)
Line 29: error: stray '\' in program
Line 29: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 30: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'tab'
Line 2: error: invalid preprocessing directive #Devuelve
Line 36: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 36: error: expected identifier or '(' before ':' token
Line 2: error: invalid preprocessing directive #Devuelve
Line 44: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 44: error: expected identifier or '(' before ':' token
Line 2: error: invalid preprocessing directive #Mueve
Line 51: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 2: error: invalid preprocessing directive #Retrocede
Line 54: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'retroceder'
Line 55: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 56: error: 'final' undeclared here (not in a function)
Line 56: warning: data definition has no type or storage class
Line 56: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 2: error: invalid preprocessing directive #devuelve
Line 59: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'posibles'
Line 63: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 63: error: expected identifier or '(' before ':' token
Line 2: error: invalid preprocessing directive #Ordena
Line 75: error: 'j' undeclared here (not in a function)
Line 75: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token
Line 2: error: invalid preprocessing directive #Ordena
Line 2: error: invalid preprocessing directive #Funci
Line 2: error: invalid preprocessing directive #--
Line 2: error: invalid preprocessing directive #Crea
Line 2: error: invalid preprocessing directive #Pone
Line 119: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 2: error: invalid preprocessing directive #Posici
Line 122: error: 'random' undeclared here (not in a function)
Line 122: warning: data definition has no type or storage class
Line 122: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Line 2: error: invalid preprocessing directive #Comienza
In function 'dibujar':
Line 126: error: expected declaration specifiers before 'contador'
|
|