forked from l2blackbelt/GameOfPureStrategy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJamesBots.py
More file actions
171 lines (138 loc) · 6.95 KB
/
JamesBots.py
File metadata and controls
171 lines (138 loc) · 6.95 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python
from utils.log import log
from bots.simpleBots import BasicBot
import random
def get_Chosen(nums):
chosen = []
while True:
if len(nums) == 0:
break
idx = random.randint(0,len(nums)-1)
if not nums[idx] in chosen:
chosen.append(nums[idx])
total = 0
for x in chosen:
total = total + x
total += nums[idx]
nums.remove(nums[idx])
if total >= 46:
break
chosen.sort(reverse = True)
return chosen
class HalfPointsBot(BasicBot): #can extend one of the simple bots, BasicBot, ObviousBot, RandomBot
#these are the three methods called by GameArena. If your bot doesn't need one, you can delete it.
def __init__(self, player_num, num_players, num_cards, num_games):
#Bot is initialized once at the beginning of the competition, and persists between games.
nums = list(range(1,num_cards))
self.chosen = get_Chosen(nums)
self.player_num = player_num #your player number
self.num_players = num_players #normally 2, but ideally, you should allow your bot to gracefully handle more
self.num_cards = num_cards
return
def end_game(self, result):
#Called by GameArena upon game end. Result is the number of the winning bot previous game, -1 if tie
#Likely want to reset any tracking variables that persist between rounds here.
nums = list(range(1,self.num_cards))
self.chosen = get_Chosen(nums)
return
def take_turn(self, game_state, verbose = False):
"""
Called by GameArena when it's time to take your turn. You are passed a "game" object with this info to work with:
card = (int) value 1 thru num_cards
variables available to your bot:
self.player_num = your player number
self.num_players = normally 2, but ideally, you should allow your bot to gracefully handle more
self.num_cards = normally 13, but ideally, you should allow your bot to gracefully handle any amount
game_state.current_won_cards[player_num][cards] = list of cards each player has won so far
game_state.current_scores[player_num] = current score of each each player
game_state.current_hands[player][cards] = list of cards currently in each player's hand
game_state.current_prizes[cards] = list of prizes remaining
game_state.prize_this_round (int) = current prize showing for this round
"""
#use log(self,"text") instead of print("text") so we know what module is printing, for cleaner output
#log(self,"This is how I do a print statement!")
#a completed bot should wrap all log statments in verbosity checks, so we don't get a flooded console if running 1000 iterations
if verbose:
log(self, str(self.chosen) )
#do fun logic
if not game_state.prize_this_round in self.chosen:
return min(game_state.current_hands[self.player_num])
else:
finder = self.chosen.index(game_state.prize_this_round)
return game_state.current_hands[self.player_num][-finder:][0]
class HalfPointsAdaptBot(BasicBot): #can extend one of the simple bots, BasicBot, ObviousBot, RandomBot
#these are the three methods called by GameArena. If your bot doesn't need one, you can delete it.
def __init__(self, player_num, num_players, num_cards, num_games):
#Bot is initialized once at the beginning of the competition, and persists between games.
nums = list(range(1,num_cards-1))
self.abort_BotUp = 0
self.abort_Obvious = 0
self.chosen = get_Chosen(nums)
self.player_num = player_num #your player number
self.num_players = num_players #normally 2, but ideally, you should allow your bot to gracefully handle more
self.num_cards = num_cards
self.last_wins = 0
return
def end_game(self, result):
#Called by GameArena upon game end. Result is the number of the winning bot previous game, -1 if tie
#Likely want to reset any tracking variables that persist between rounds here.
nums = list(range(1,self.num_cards-1))
self.chosen = get_Chosen(nums)
self.abort_Obvious = 0
self.abort_BotUp = 0
self.last_wins = 0
return
def take_turn(self, game_state, verbose = False):
"""
Called by GameArena when it's time to take your turn. You are passed a "game" object with this info to work with:
card = (int) value 1 thru num_cards
variables available to your bot:
self.player_num = your player number
self.num_players = normally 2, but ideally, you should allow your bot to gracefully handle more
self.num_cards = normally 13, but ideally, you should allow your bot to gracefully handle any amount
game_state.current_won_cards[player_num][cards] = list of cards each player has won so far
game_state.current_scores[player_num] = current score of each each player
game_state.current_hands[player][cards] = list of cards currently in each player's hand
game_state.current_prizes[cards] = list of prizes remaining
game_state.prize_this_round (int) = current prize showing for this round
"""
#use log(self,"text") instead of print("text") so we know what module is printing, for cleaner output
#log(self,"This is how I do a print statement!")
#a completed bot should wrap all log statments in verbosity checks, so we don't get a flooded console if running 1000 iterations
if verbose:
log(self, str(self.chosen) )
#do fun logic
safe_to_use = 1
for i in range(0,self.num_players-1):
if i != self.player_num:
if self.num_players == 2:
if sum(game_state.current_prizes) == sum(game_state.current_hands[i]):
self.abort_Obvious += 1
if len(game_state.current_prizes) == (self.num_cards - int(round(float(self.num_cards)*0.3))) and sum(game_state.current_hands[i]) >= (sum(range(1,self.num_cards)) - 13):
self.abort_BotUp = 1
if max(game_state.current_hands[self.player_num]) > max(game_state.current_hands[i]):
safe_to_use = 1
else:
safe_to_use = 0
#if len(game_state.current_prizes) >= 5 and (game_state.prize_this_round + game_state.current_scores[self.player_num]) > max(game_state.current_scores):
# return max(game_state.current_hands[self.player_num])
if self.abort_Obvious > 4:
if (game_state.prize_this_round + 1) in game_state.current_hands[self.player_num]:
return (game_state.prize_this_round + 1)
else:
return min(game_state.current_hands[self.player_num])
if self.abort_BotUp == 1:
idx = len(game_state.current_hands[self.player_num])/2
return game_state.current_hands[self.player_num][idx]
if safe_to_use == 1 and max(game_state.current_prizes) == game_state.prize_this_round:
return max(game_state.current_hands[self.player_num])
if not game_state.prize_this_round in self.chosen:
return min(game_state.current_hands[self.player_num])
else:
finder = self.chosen.index(game_state.prize_this_round)
if len(game_state.current_hands[self.player_num]) == 1:
return game_state.current_hands[self.player_num][0]
elif game_state.current_hands[self.player_num][-(finder):][0] == 13:
return game_state.current_hands[self.player_num][-(finder+1):][0]
else:
return game_state.current_hands[self.player_num][-(finder):][0]