Sei sulla pagina 1di 7

(setf *estados* '(1 3 5 7 60))

(setq *jerry* 2 *tom1* 11 *tom2* 12 *tom3* 13 *tom4* 14)

(defun iniciar-juego()
(setf estados (copy-list *estados*))
;;(setf '(1 3 5 7 60)
;;(setq *jerry* 2 *tom1* 11 *tom2* 12 *tom3* 13 *tom4* 14)
(imprimir-tablero estados)
(movimiento-oponente estados)
)

(defun crear-tablero ()
(setf tablero (list '0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 ))
)

(defun convertir-a-letras (v)


(cond ((equal v 11) "G1")
((equal v 12) "G2")
((equal v 13) "G3")
((equal v 14) "G4")
((equal v 2) "R")
(t " "))
)

(defun imprimir-fila (a b c d e f g h)
(format t "~&| ~A | ~A | ~A | ~A | ~A | ~A | ~A | ~A |"
(convertir-a-letras a)
(convertir-a-letras b)
(convertir-a-letras c)
(convertir-a-letras d)
(convertir-a-letras e)
(convertir-a-letras f)
(convertir-a-letras g)
(convertir-a-letras h)
)
)

(defun imprimir-tablero (estados)


(setq tablero (crear-tablero))
(dotimes (i 4)
(setf (nth (nth i estados) tablero) (+ 11 i))
)
(setf (nth (nth 4 estados) tablero) 2)
(format t "~%")
(imprimir-fila (nth 1 tablero) (nth 2 tablero) (nth 3 tablero) (nth 4 tablero)
(nth 5 tablero) (nth 6 tablero) (nth 7 tablero) (nth 8 tablero))
(format t "~& ---------------------------------------------------------")
(imprimir-fila (nth 9 tablero) (nth 10 tablero) (nth 11 tablero) (nth 12
tablero) (nth 13 tablero) (nth 14 tablero) (nth 15 tablero) (nth 16 tablero))
(format t "~& ---------------------------------------------------------")
(imprimir-fila (nth 17 tablero) (nth 18 tablero) (nth 19 tablero) (nth 20
tablero) (nth 21 tablero) (nth 22 tablero) (nth 23 tablero) (nth 24 tablero))
(format t "~& ---------------------------------------------------------")
(imprimir-fila (nth 25 tablero) (nth 26 tablero) (nth 27 tablero) (nth 28
tablero) (nth 29 tablero) (nth 30 tablero) (nth 31 tablero) (nth 32 tablero))
(format t "~& ---------------------------------------------------------")
(imprimir-fila (nth 33 tablero) (nth 34 tablero) (nth 35 tablero) (nth 36
tablero) (nth 37 tablero) (nth 38 tablero) (nth 39 tablero) (nth 40 tablero))
(format t "~& ---------------------------------------------------------")
(imprimir-fila (nth 41 tablero) (nth 42 tablero) (nth 43 tablero) (nth 44
tablero) (nth 45 tablero) (nth 46 tablero) (nth 47 tablero) (nth 48 tablero))
(format t "~& ---------------------------------------------------------")
(imprimir-fila (nth 49 tablero) (nth 50 tablero) (nth 51 tablero) (nth 52
tablero) (nth 53 tablero) (nth 54 tablero) (nth 55 tablero) (nth 56 tablero))
(format t "~& ---------------------------------------------------------")
(imprimir-fila (nth 57 tablero) (nth 58 tablero) (nth 59 tablero) (nth 60
tablero) (nth 61 tablero) (nth 62 tablero) (nth 63 tablero) (nth 64 tablero))
(format t "~& ---------------------------------------------------------")
)

(defun primera-fila (pos)


(if (<= pos 8) t nil)
)

(defun ultima-fila (pos)


(if (>= pos 57) t nil)
)
(defun primera-colum (pos)
(if (= (mod pos 8) 1) t nil)
)

(defun ultima-colum(pos)
(if (= (mod pos 8) 0) t nil)
)

(defun jugador-estado (jugador)


(cond
((= jugador 11) 0)
((= jugador 12) 1)
((= jugador 13) 2)
((= jugador 14) 3)
((= jugador 2) 4)
)
)

(defun validar-jugar-subir-izq (jugador pos tablero)


(if(and (and (not (primera-fila pos))(not (primera-colum pos)))(= (nth (- pos 9)
tablero)0)) t nil)
)
(defun validar-jugar-subir-der (jugador pos tablero)
(if(and (and (not (primera-fila pos))(not (ultima-colum pos)))(= (nth (- pos 7)
tablero)0)) t nil)
)

(defun validar-jugar-bajar-izq (jugador pos tablero)


(if(and (and (not (ultima-fila pos))(not (primera-colum pos)))(= (nth (+ pos 7)
tablero)0)) t nil)
)

(defun validar-jugar-bajar-der (jugador pos tablero)


(if(and (and (not (ultima-fila pos))(not (ultima-colum pos)))(= (nth (+ pos 9)
tablero)0)) t nil)
)

(defun movimiento-oponente(estados)
(let* ((nuevo-estados nil)
(rpt nil)
(jugador nil))
(format t "~& ~A ~&" estados)
(format t "~&�Cual gato desea mover? : ")
(setf rpt (read))
(cond
((= rpt 1) (setf jugador *tom1*))
((= rpt 2) (setf jugador *tom2*))
((= rpt 3) (setf jugador *tom3*))
((= rpt 4) (setf jugador *tom4*))
)
(setf nuevo-estados (copy-list (validar-movimiento jugador tablero estados)) )
(format t "~A ~&" nuevo-estados)
(imprimir-tablero nuevo-estados)
(if(ganadorGatos nuevo-estados) (format t "~&TU GANAS!!!")
(movimiento-maquina 2 tablero nuevo-estados)
)
)
)

(defun ganadorGatos (estados)


(let ((posRat (nth 4 estados))
(posGatos (pos-gatos estados)))
(cond
((and (not (primera-fila posRat)) (not (primera-colum posRat)) (not (ultima-
fila posRat)) (not (ultima-colum posRat)))
(if (and (member (- posRat 9) posGatos)(member (- posRat 7) posGatos)(member
(+ posRat 7) posGatos)(member (+ posRat 9) posGatos)) t))
((and (primera-fila posRat) (primera-colum posRat))
(if (member (+ posRat 9) posGatos) t))
((and (ultima-fila posRat) (ultima-colum posRat))
(if (member (- posRat 9) posGatos) t))
((primera-fila posRat)
(if (and (member (+ posRat 7) posGatos) (member (+ posRat 9) posGatos)) t))
((ultima-fila posRat)
(if (and (member (- posRat 7) posGatos) (member (- posRat 9) posGatos)) t))
((primera-colum posRat)
(if (and (member (- posRat 7) posGatos) (member (+ posRat 9) posGatos)) t))
((ultima-colum posRat)
(if (and (member (- posRat 9) posGatos) (member (+ posRat 7) posGatos)) t))
(t nil)
)
)
)

;; posicion del raton


(defun pos-raton (estado )
(
(lambda (x)
(setq x
(nth 4 estado))
(list x)
) 0) )

;; posicion del gato


(defun pos-gatos(estado)
(
(lambda (y)
(dotimes (i 4)
(setq y (append y (list (nth i estado))))
)
y )
nil )
)

;; coordenadas de las posicion


(defun coordenadas(pos)
(list (floor (/ pos 8)) (mod pos 8)
)
)

;; coordenadas posicion del gato


(defun pos-gatos-coor(estado)
(
(lambda (y)
(dotimes (i 4)
(setq y (append y (list (coordenadas (nth i (pos-gatos
estado))))))
)
y )
nil )
)

(defun distancia(coordenada1 coordenada2)


(let ((x1 (car coordenada1))(x2 (car coordenada2)) (y1 (cadr coordenada1)) (y2
(cadr coordenada2)))
(sqrt( + ( * ( - x1 x2)( - x1 x2)) ( * ( - y1 y2)( - y1 y2))))
)
)

(defun distGatoRaton (estado)


(let ((posRaton (pos-raton estado)) (posGato (pos-gatos estado)))
(+
(distancia (coordenadas (nth 0 posRaton))(coordenadas (nth 0 posGato)))
(distancia (coordenadas (nth 0 posRaton))(coordenadas (nth 1 posGato)))
(distancia (coordenadas (nth 0 posRaton))(coordenadas (nth 2 posGato)))
(distancia (coordenadas (nth 0 posRaton))(coordenadas (nth 3 posGato)))
)
)
)

(defun posicion-evaluacion (p1 p2)


(if (> (- (nth 0 p2) (nth 0 p1)) 0)
1
-1
)
)

(defun posi-inicio(estados)
(let ((dist-inic nil))
(setf dist-inic ( distancia (coordenadas (nth 4 estados))(coordenadas
(mod (nth 4 estados) 8))) )
dist-inic
)
)

(defun pasar-gato(p1 p2)


(if (<= (- (nth 0 p2) (nth 0 p1)) 0)
5
-1
)
)

(defun evaluacion (estado)


(let ((dist-gato-raton 0) (p-raton ( coordenadas (nth 0 ( pos-raton estado)))))
(mapcar #'
(lambda (p-gato)
(setq dist-gato-raton (+ dist-gato-raton (* dist-gato-raton (posicion-
evaluacion p-gato p-raton) (distancia p-gato p-raton))(pasar-gato p-gato p-raton)))
)
(pos-gatos-coor estado)
)
dist-gato-raton
)
)
(defun sucesores-por-turno (tablero jugador)
(setf temptablero (copy-list tablero))
(let ((lista-sucesores nil))
(cond
((= jugador 1)
(dolist (p-gato (pos-gatos (nth 0 estado)))
(setq lista-sucesores (append lista-sucesores (gen-sucesores temptablero
jugador p-gato)))
)
)
((= jugador 2)
(setq lista-sucesores (gen-sucesores temptablero jugador (nth 4 estados) ))
)
)
lista-sucesores
)
)
(setq cantidad 1)

(defun gen-sucesores (tablero jugador p-ficha-mov estados)


(setq sucesores nil)

(cond
((= jugador 2)
(setf tempestados (copy-list estados))
(setf temptablero (copy-list tablero))
(if (validar-jugar-subir-izq jugador p-ficha-mov tablero)
(setq sucesores (append sucesores (list ( jugar-subir-izq jugador p-
ficha-mov temptablero tempestados))))(+ cantidad 1))
( setf tempestados (copy-list estados))
( setf temptablero (copy-list tablero))

(if (validar-jugar-subir-der jugador p-ficha-mov tablero )


(setq sucesores (append sucesores (list ( jugar-subir-der jugador p-
ficha-mov temptablero tempestados) )))(+ cantidad 1))
(setf tempestados (copy-list estados))
(setf temptablero (copy-list tablero))

(if (validar-jugar-bajar-izq jugador p-ficha-mov tablero )


(setq sucesores (append sucesores (list ( jugar-bajar-izq jugador p-
ficha-mov temptablero tempestados) )))(+ cantidad 1))
(setf tempestados (copy-list estados))
(setf temptablero (copy-list tablero))
(if (validar-jugar-bajar-der jugador p-ficha-mov tablero )
(setq sucesores (append sucesores (list ( jugar-bajar-der jugador p-
ficha-mov temptablero tempestados) )))(+ cantidad 1))
)
)
sucesores
)
(defun ordenar-sucesores (sucesores)
(let ((ltemp nil))
(mapcar #'
(lambda (sucesor)
(setq ltemp (append ltemp (list (list sucesor (evaluacion sucesor)))))
)
sucesores
)
(sort ltemp #'> :key #'cadr)
)
)

(defun no-determinista (jugador pos tablero estados)


(setq posicion-no-determinista (random cantidad))
(let ((jugar-jugada nil))
( setf jugar-jugada (nth posicion-no-determinista ( ordenar-sucesores ( gen-
sucesores tablero jugador (nth (jugador-estado jugador) estados) estados ))))
jugar-jugada
)
)
(defun movimiento-maquina(jugador tablero estados)
(let* ((nuevo-estados nil))
(setf nuevo-estados (copy-list (no-determinista jugador (nth (jugador-estado
jugador) estados) tablero estados)))
(format t "~& ~A" nuevo-estados)
(imprimir-tablero nuevo-estados)
(movimiento-oponente nuevo-estados)
)
)

Potrebbero piacerti anche