-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
183 lines (168 loc) · 4.7 KB
/
game.py
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
171
172
173
174
175
176
177
178
179
180
181
182
import numpy as np
from time import sleep
from BO import BO
import os
class Game():
def __init__(self, tmn,RN):
self.tmn = tmn
self.CriarTabuleiro()
self.RN = RN
def CriarTabuleiro(self):
self.tabuleiro = np.zeros((self.tmn,self.tmn))
self.pontos = 0
self.rodadas = 0
self.Colocar_Aleatrio()
def Colocar_Aleatrio(self):
while True:
i = np.random.randint(0,self.tmn)
j = np.random.randint(0,self.tmn)
if self.tabuleiro[i][j] == 0:
self.tabuleiro[i][j] = 1
break
def Pontos(self):
self.pontos = (np.sum(self.tabuleiro) + np.max(self.tabuleiro)*4)
def Print(self):
print(self.tabuleiro)
print(self.pontos)
def Teclado(self):
aux=self.RN.predict(self.tabuleiro)
if aux == 0 :
return 'c'
elif aux == 1:
return 'b'
elif aux == 2:
return 'e'
else :
return 'd'
def esqdit(self,tabu):
novo_tabuleiro = []
for linha in tabu:
l = []
mem = []
zeros = []
for item in linha:
if not item == 0:
mem.append(item)
else:
zeros.append(0)
aux = 0
aux2 = []
for item in mem:
if aux == 0:
aux = item
else:
if aux == item:
zeros.append(0)
aux += 1
else:
aux2.append(aux)
aux = item
if not aux == 0:
aux2.append(aux)
l = zeros + aux2
novo_tabuleiro.append(l)
self.tabuleiro = np.array(novo_tabuleiro)
def diresq(self,tabu):
novo_tabuleiro = []
for linha in tabu:
l = []
mem = []
zeros = []
for item in reversed(linha):
if not item == 0:
mem.insert(0,item)
else:
zeros.append(0)
aux = 0
aux2 = []
for item in reversed(mem):
if aux == 0:
aux = item
else:
if aux == item:
zeros.insert(0,0)
aux += 1
else:
aux2.insert(0,aux)
aux = item
if not aux == 0:
aux2.insert(0,aux)
l = aux2 +zeros
novo_tabuleiro.append(l)
self.tabuleiro = np.array(novo_tabuleiro)
def cimabaixo(self, tabu):
self.esqdit(tabu.transpose())
self.tabuleiro = self.tabuleiro.transpose()
def baixocima(self, tabu):
self.diresq(tabu.transpose())
self.tabuleiro = self.tabuleiro.transpose()
def Calcular_novo_tabuleiro(self,tecla):
if (tecla == 'c'):
self.baixocima(self.tabuleiro)
elif (tecla == 'b'):
self.cimabaixo(self.tabuleiro)
elif (tecla == 'e'):
self.diresq(self.tabuleiro)
else:
self.esqdit(self.tabuleiro)
def LoopGame(self,i):
while True:
if not 0 in self.tabuleiro or 11 in self.tabuleiro:
break
self.Colocar_Aleatrio()
tecla = self.Teclado()
self.Calcular_novo_tabuleiro(tecla)
self.rodadas += 1
sleep(i)
self.Pontos()
po = []
def sunss(num):
return num[1]
def me (array):
todos = 0
for x in array:
aux=sunss(x)
todos = aux + todos
return todos/len(array)
epocas = 100
bo = BO()
array = bo.Criar_inicio()
melhor=0
tabu=[]
Elite = [0,0,0]
z=0
meme = 0
while True:
y=0
z = z+1
for x in array:
y = y+ 1
print("epoca")
print(z)
print ("Porcentagem do array")
print ((y/110)*100)
print ("Rodada anterior")
print ("Melhor Resultado")
print (melhor)
print ("Tabuleiro")
print (tabu)
print ("ELITE")
print ("Melhor Resultado")
print (Elite[1])
print ("Tabuleiro")
print (Elite[2])
print ("media anterior")
print (meme)
game = Game(4,x[0])
game.LoopGame(0)
x[1] = game.pontos
x.append(game.tabuleiro)
os.system('clear')
array = sorted(array,key=sunss,reverse=True)
melhor = array[0][1]
tabu =array[0][2]
meme = me(array)
Elite=array[0]
bo.salvar(Elite[0],z)
array = bo.recalcular(array)
array.append([Elite[0],0])