-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
124 lines (104 loc) · 4.58 KB
/
bot.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
"""
This file handels all of the betting that the bot does.
"""
import card as cardsets
from winning_hand import winner
from random import sample
MONTE_CARLO_ITERATIONS = 10000
def bot_bet(current_call, bot, table, can_raise=True):
"""
Handelling of the betting done by the bot. This function calls on our
Monte Carlo algorithm to determine the probability of the bot winning, Then
based off the probability and some random chance we determine whether or not
to either raise, bet, call, check, or fold. If the bot raises or bets then
how much chips it puts in is proportional to its chance of winning the round.
Inputs:
current call - the minimum amount of chips the bot must put in.
bot - an object of the bot class, essentially our bot we will be playing against
table - an object of the Table class
can_raise=True - whether or not the bot is allowed to raise, default is True
Output: The call the player must make, or 'Fold' if the bot folds.
Runtime: O(p*xlog(x) + plog(p)) where x=n+m where n is the the number of cards that the player
has and m is the number of cards on the table and p is the number of players
"""
print('bot turn: ', end=' ')
chance = Monte_Carlo(bot, table) # O(p*xlog(x) + plog(p))
chance = chance // 100 # chance=(chance/10,000)*100%
print("Chance BEFORE: ", chance)
chance = chance + bot.rand_number_in_range(-15, 15) # gives the bot a bit of randomness
if chance <= 0:
chance = 0
if chance >= 100:
chance = 100
print("Chance AFTER: ", chance)
if type(current_call) is int and current_call > 0:
if chance >= 50 and bot.get_chips() - current_call > 0 and can_raise is True:
base = (chance - 50) / 50
value = int(base * (bot.get_chips() - current_call))
print('Raising by: ', value)
bot.remove_chips(value + current_call) #O(n)
table.add_chips(value + current_call) #O(m)
return value
if chance >= 50 and (bot.get_chips() - current_call <= 0
or can_raise is False):
print('calling')
chip = min(current_call, bot.get_chips())
bot.remove_chips(chip) #O(n)
table.add_chips(chip) #O(m)
return -1
if 30 <= chance < 50:
print('calling')
if current_call > bot.get_chips():
print('Bot goes all in')
chip = bot.get_chips()
bot.remove_chips(chip) #O(n)
table.add_chips(chip) #O(m)
return -1
else:
bot.remove_chips(current_call) #O(n)
table.add_chips(current_call) #O(m)
return -1
if chance < 30:
print('folding')
return 'Fold'
else:
if chance >= 50:
base = (chance - 50) / 50
value = int(base * bot.get_chips())
print('betting by', value)
bot.remove_chips(value) #O(n)
table.add_chips(value) #O(m)
return value
else:
print('checking')
return 0
def Monte_Carlo(bot, table):
"""
A implimentation of the Monte Carlo algorithm, this algorithm works by taking
a sample size of the different possibilities of what the remaining cards on the
table will be and what cards the player has in their hand. It takes 10,000
different samples and calculates if it will win in each scenario and returns
how many times it won out of 10,000
Inputs:
bot - an object of the bot class, essentially our bot we will be playing against
table - an object of the Table class
Output: How many times the bot won in 10,000 scenarios
Runtime: O(p*xlog(x) + plog(p)) where x=n+m where n is the the number of cards that the player
has and m is the number of cards on the table and p is the number of players
"""
count = 0
for i in range(MONTE_CARLO_ITERATIONS): #O(10,000)=O(1)
my_deck = cardsets.Deck()
my_deck.remove_card(bot.get_cards()[0]) #O(n)
my_deck.remove_card(bot.get_cards()[1]) #O(n)
for card in table.get_cards():
my_deck.remove_card(card)
listed_deck = my_deck.get_cards()
x = sample(listed_deck, 7 - len(table.get_cards()))
person = cardsets.Player(0, x[0:2])
players = [person, bot]
new_table = cardsets.Table(table.get_cards() + x[2:])
if type(winner(new_table, players,
printing=False)) is cardsets.Bot: #O(p*xlog(x) + plog(p))
count += 1
return count