-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
156 lines (119 loc) · 3.86 KB
/
utilities.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
#Part 2 of the lab
import random
from transition import *
#Utility function takes a board state as input
#and returns a value between 1 and 10 indicating
#how desirable the state is
#Evasive uses the number of pieces remaining for the player
#and values it based on how many is left
def evasive(input_state, player):
p_remaining = 0. #Number of pieces remaining
for row in input_state: #Check which player it is and calculate the number
for column in row: #of pieces remaining for them
if player == "X":
if column == "X":
p_remaining += 1
if player == "O":
if column == "O":
p_remaining += 1
percentange = random.uniform(0,1) #Generates random number between 0-1
Num = round(percentange , 2) #round it down to one decimal
utility = p_remaining + Num #utility is evasive
return utility
def conqueror(input_state, player):
p_remaining = 0. #Number of pieces remaining
for row in input_state: #Check which player it is and calculate the number
for column in row: #of pieces remaining for them
if player == "X":
if column == "O":
p_remaining += 1
if player == "O":
if column == "X":
p_remaining += 1
percentange = random.uniform(0,1) #Generates random number between 0-1
Num = round(percentange , 2) #round it down to one decimal
utility = (0 - p_remaining) + Num #utility is conquerer
return utility
def winningscore(player, input_state):
board = Board(state, player)
winningvalue = 200
if player == "X":
if board.terminal_state() == "X":
return winningvalue
elif board.terminal_state() == "O":
return (-1) * winningvalue
else:
return 0
elif player == "O":
if board.terminal_state() == "O":
return winningvalue
elif board.terminal_state() == "X":
return (-1) * winningvalue
else:
return 0
else:
return 0
def myscore (player, input_state):
Num_pieces = 0
player_X = []
player_O = []
total = 0
row_num = 0
for row in input_state:
row_num = row_num + 1
column_num = 0
for column in row:
column_num = column_num + 1
piece = (row_num - 1, column_num - 1)
if column == "X":
player_X.append(piece)
if column == "O":
player_O.append(piece)
if player == "X":
for pos in player_X:
total = pos[0] + total
Num_player = len(player_X)
return Num_player + total + winningscore(player, input_state)
elif player == "O":
for pos in player_O:
total = pos[0] + total
Num_player = len(player_X)
return Num_player + (row_num - total) + winningscore(player, input_state)
def enemyscore(player, input_state):
Num_pieces = 0
player_X = []
player_O = []
total = 0
row_num = 0
for row in input_state:
row_num = row_num + 1
column_num = 0
for column in row:
column_num = column_num + 1
piece = (row_num - 1, column_num - 1)
if column == "X":
player_X.append(piece)
if column == "O":
player_O.append(piece)
if player == "X":
for pos in player_O:
total = pos[0] + total
Num_player = len(player_X)
return Num_player + (row_num - total) + winningscore("O", input_state)
elif player == "O":
for pos in player_X:
total = pos[0] + total
Num_player = len(player_X)
return Num_player + total + winningscore("X", input_state)
def house_lannister(input_state, player):
result = 2 * (myscore(player, input_state) - 1) * enemyscore(player, input_state)
percentange = random.uniform(0,1) #Generates random number between 0-1
Num = round(percentange , 2)
result = result + Num
return result
def house_stark(input_state, player):
result = 1 * (myscore(player, input_state) - 2) * enemyscore(player, input_state)
percentange = random.uniform(0,1) #Generates random number between 0-1
Num = round(percentange , 2)
result = result + Num
return result