Python - 8 puzzle - contar cada movimiento

 
Vista:

8 puzzle - contar cada movimiento

Publicado por frank (1 intervención) el 02/09/2019 06:22:11
Hola
tengo problemas con mi código es un juego 8 puzzle pero quiero hacer que cuente cada movimiento que hace.
soy nuevo en pyhthon

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class Node(object):
    def __init__(fin, dato, prev, next):
        fin.dato = dato
        fin.prev = prev
        fin.next = next
 
class DoubleList(object):
    head = None
    tail = None
    best = None
    explorado = 1
 
    def append(fin, dato):
        mov = Node(dato, None, None)
        if fin.head is None:
            fin.head = fin.tail = fin.best = mov
        else:
            mov.prev = fin.best
            mov.next = None
            fin.tail.next = mov
            fin.tail = mov
 
    def recover(fin):
        current_node = fin.best
        while current_node is not None:
            yield current_node.dato
            current_node = current_node.prev
 
    def bestNode(fin):
        f  = 0
        m  = 2000
        h  = 0
        nm = m
 
        i = fin.head
        while i is not None:
           f = i.dato.h + i.dato.g
           if(i.dato.explorado==0 and f < m):
              m = f
           i = i.next
 
        i = fin.head
        while i is not None:
           f = i.dato.h + i.dato.g
           h = i.dato.h
           if(i.dato.explorado==0 and f==m and h < nm):
              fin.best = i
              nm = h
           i = i.next
 
        fin.explorado = fin.explorado + 1
        fin.best.dato.explorado = fin.explorado
 
 
# JUEGO
class Puzzle(object):
  coordenadas_x = [1,1,1,2,2,2,3,3,3]
  coordenadas_y = [1,2,3,1,2,3,1,2,3]
  elementos     = ["1","2","3","4","5","6","7","8"]
  intercambios  = [[1,3,9,9],
                   [0,2,4,9],
                   [1,5,9,9],
                   [0,4,6,9],
                   [1,3,5,7,9],
                   [2,4,8,9],
                   [3,7,9,9],
                   [4,6,8,9],
                   [5,7,9,9]]
 
  def __init__(fin):
    fin.estado = None
    fin.h = 0
    fin.g = 0
    fin.explorado = 0
    fin.ascendiente = None
 
  def posx(fin,c):
    return fin.coordenadas_x[fin.estado.index(c)]
 
  def posy(fin,c):
    return fin.coordenadas_y[fin.estado.index(c)]
 
  def distanciaManhatan(fin,meta):
    suma = 0
    for j in fin.elementos:
      dmx = abs(fin.posx(j) - meta.posx(j))
      dmy = abs(fin.posy(j) - meta.posy(j))
      suma = suma + dmx + dmy
    return suma
 
  def descendientes(fin):
    b = fin.estado.index(" ")
    a = fin.ascendiente
    r = list()
    j = 0
    while(fin.intercambios[b][j] != 9):
      d = fin.estado[:]
      d[b] = fin.estado[fin.intercambios[b][j]]
      d[fin.intercambios[b][j]] = " "
      j = j + 1
      if( d != a ):
        r.append(d)
    return r
 
  def printPuzzle(fin):
    print("_____")
    print(' '.join(fin.estado[0:3]))
    print(' '.join(fin.estado[3:6]))
    print(' '.join(fin.estado[6:9]))
    print('')
 
 
# INICIO
if __name__ == "__main__":
 
  estados = []
 
  lista = DoubleList()
  estado_inicial = Puzzle()
  estado_meta    = Puzzle()
 
  # ESTADO META
  estado_meta.estado   = list("12345678 ")
 
  # ESTADO INICIAL
  estado_inicial.estado = list("4531 2678")
  estado_inicial.h = estado_inicial.distanciaManhatan(estado_meta)
  estado_inicial.g = 0
  estado_inicial.explorado = 1
 
  # IMPRIME
  print("8 puzzle")
  print("")
  print("Estado inicial")
  estado_inicial.printPuzzle()
  print("Estado final ")
  estado_meta.printPuzzle()
 
  print("Inicia ")
 
  # Lista
  lista.append(estado_inicial)
 
  lista.bestNode()
  mejorNodo =  lista.best.dato
 
  j=0
  while (j < 2000 and mejorNodo.estado != estado_meta.estado):
 
    for hijo in mejorNodo.descendientes():
      elem = Puzzle()
      elem.estado = hijo
      elem.h = elem.distanciaManhatan(estado_meta)
      elem.g = mejorNodo.g + 1
      elem.explorado = 0
      elem.ascendiente = mejorNodo.estado
      lista.append(elem)
 
    lista.bestNode()
    mejorNodo =  lista.best.dato
    j=j+1
 
  #RESULTADO
  for v in lista.recover():
      estados.append(v)
 
  estados.reverse()
 
  for e in estados:
      e.printPuzzle()
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

8 puzzle - contar cada movimiento

Publicado por Gnommo (1 intervención) el 16/09/2020 22:19:45
Hola, tienes la documentación de tu programa, me fue de bastante utilidad, pero no entiendo para qué sirven algunas líneas.
Saludos!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar