-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetrisBruteForce.py
76 lines (70 loc) · 2.34 KB
/
TetrisBruteForce.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
from Tetris import Game
import copy
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
#%% Playing the game in console
#width = 10
#height = 20
#score = 0
#print("Starting game!")
#game = Game(width,height,display=True)
#while not game.gameOver:
# bestAction = 0
# bestReward = -1000000
# for i in range(4*width):
# test = copy.deepcopy(game)
# test.showGame = False
# test.makeMove(i)
# if test.reward > bestReward:
# bestReward = test.reward
# bestAction= i
# game.makeMove(bestAction)
#print("Game over! Final score:", game.score)
#%% Animating the game
width = 10
height = 20
fig, ax = plt.subplots()
boards = []
lines = []
# Creating colormap
colors = [(255, 255, 255), (93, 245, 230), (0, 0, 155), (255, 165, 0), (255, 255, 30), (0, 200, 0), (222, 34, 34), (138, 43, 226)]
for i,color in enumerate(colors):
colors[i] = (color[0]/255, color[1]/255, color[2]/255)
cm = LinearSegmentedColormap.from_list('tetrisColormap', colors, N=8)
# Initializing board and label
im = ax.imshow(np.zeros([height,width]), vmin=0, vmax=7, cmap=cm)
label = ax.text(0,0.5, "Lines Cleared: 0", bbox={'facecolor':'w', 'alpha':0.75, 'pad':1, 'edgecolor':'white'})
# Removing axis labels
plt.xticks([])
plt.yticks([])
# Playing game and copying each state to display
game = Game(width,height,display=False)
while not game.gameOver:
bestAction = 0
bestReward = -1000000
for i in range(4*width):
test = copy.deepcopy(game)
test.showGame = False
test.makeMove(i)
if test.reward > bestReward:
bestReward = test.reward
bestAction= i
game.makeMove(bestAction)
boards.append(copy.deepcopy(game.board))
lines.append( game.totalLines)
boards.append(copy.deepcopy(game.board))
lines.append(game.totalLines)
# Function to animate
def animate(i):
im.set_data(boards[i])
label.set_text("Lines Cleared: " + str(lines[i]))
return im,label
# Animating
numFrames = len(boards)
ani = animation.FuncAnimation(fig, func=animate, frames=numFrames, interval=100, blit=True, repeat=False,)
plt.show(block=False)
#%% To save the animation as a video file
#FFwriter = animation.FFMpegWriter(fps=10, extra_args=['-vcodec', 'libx264'])
#ani.save('Tetris.mp4', writer = FFwriter)