-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.py
53 lines (38 loc) · 1.34 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
import random
import time
import numpy as np
from copy import deepcopy
from envRL import Game2048
game = Game2048(theme='light')
def playGame(theme, difficulty, model):
"""
Main game loop function.
"""
board = game.new_game()
time.sleep(0.2)
status = "PLAY"
# main game loop
while status == "PLAY":
state = deepcopy(board)
state = game.change_values(state)
state = np.array(state, dtype=np.float32).reshape((1, 4, 4, 16))
control_scores = model(state)
control_buttons = np.flip(np.argsort(control_scores), axis=1)
key = control_buttons[0][0]
legal_moves = game.findLegalMoves(board)
while key not in legal_moves:
key = random.randint(0, 3)
if random.random() < 0.2:
key = random.randint(0, 3)
print(f"applying move: {key}")
new_board, score = game.move(key, board)
print(new_board)
if not game.checkSame(board, new_board):
board = game.fillTwoOrFour(new_board)
game.display(board, theme)
# update game status
status = game.checkGameStatus(board, difficulty)
# check if the game is over
board, status = game.winCheck(board, status)
else:
board = new_board