-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048AI.py
97 lines (92 loc) · 2.78 KB
/
2048AI.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
from twenty48 import Board_2048
from expectimax2048 import ExpectimaxAgent
def main():
board = Board_2048()
agent = ExpectimaxAgent()
game = True
score = 0
print "Game with Expectimax depth 2 with moves shown:"
#board.print_board()
#print score
while game:
move = agent.getAction(board)
if move == 'right':
new_board,moves,s = board.right()
elif move == 'left':
new_board,moves,s = board.left()
elif move == 'up':
new_board,moves,s = board.up()
elif move == 'down':
new_board,moves,s = board.down()
board = new_board
score += s
#print move
#board.print_board()
#print score
if board.check_end():
game = False
print
board.print_board()
print "Final Score: " + str(score)
print
print
analysis()
def analysis():
total = 0
maxScore = 0
for i in range(100):
board = Board_2048()
agent = ExpectimaxAgent(1)
game = True
score = 0
while game:
move = agent.getAction(board)
if move == 'right':
new_board,moves,s = board.right()
elif move == 'left':
new_board,moves,s = board.left()
elif move == 'up':
new_board,moves,s = board.up()
elif move == 'down':
new_board,moves,s = board.down()
board = new_board
score += s
if board.check_end():
game = False
#board.print_board()
#print "Final Score: " + str(score)
total += score
if score > maxScore:
maxScore = score
print "Average Score After 100 Games Depth 1: " + str(float(total/100))
print "Max(Depth 1): " + str(maxScore)
maxScore=0
total = 0
for i in range(100):
board = Board_2048()
agent = ExpectimaxAgent()
game = True
score = 0
while game:
move = agent.getAction(board)
if move == 'right':
new_board,moves,s = board.right()
elif move == 'left':
new_board,moves,s = board.left()
elif move == 'up':
new_board,moves,s = board.up()
elif move == 'down':
new_board,moves,s = board.down()
board = new_board
score += s
if board.check_end():
game = False
#board.print_board()
#print "Final Score: " + str(score)
total += score
if score > maxScore:
maxScore = score
print "Average Score After 100 Games Depth 2: " + str(float(total/100))
print "Max(Depth 2): " + str(maxScore)
if __name__ == "__main__":
main()