This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_test_expectimax.py
More file actions
62 lines (47 loc) · 1.83 KB
/
game_test_expectimax.py
File metadata and controls
62 lines (47 loc) · 1.83 KB
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
from game import Game, Direction
import pygame
import constants
from grid_window import draw_grid, draw_popup
import expectimax
def main():
pygame.init()
screen = pygame.display.set_mode((constants.WINDOW_SIZE, constants.WINDOW_SIZE))
engine_clock = pygame.time.Clock()
# initialize fonts
FONT_30 = pygame.font.SysFont('Consolas', 30)
FONT_36 = pygame.font.SysFont('Consolas', 36)
game = Game()
while True:
while not game.hasEnded():
pygame.display.set_caption(f"2048-Python | [R]estart | Moves = {len(game.getMoves())} | Score = {game.getScore()}")
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
# get best move
best_move = expectimax.getBestMove(game, depth=3)
print(f'step={len(game.getMoves())} best_move={best_move}')
# enact move
game.attempt_move(best_move)
# Draw the game grid
grid_surface = draw_grid(FONT_30, game.getGrid())
screen.blit(grid_surface, (0, 0))
# draw game over screen if game is over
if game.hasEnded():
popup, x, y = draw_popup(FONT_36)
screen.blit(popup, (x ,y))
engine_clock.tick(constants.FPS_CAP)
pygame.display.flip()
# game has ended
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYUP:
# reset
if event.key == pygame.K_r:
game.restartGame()
engine_clock.tick(constants.FPS_CAP)
pygame.display.flip()
if __name__ == "__main__":
main()