Skip to content

Instantly share code, notes, and snippets.

@dragstar328

dragstar328/bj2.py Secret

Created Apr 26, 2018
Embed
What would you like to do?
ブラックジャックシミュレーション
import random
suit = ["hart", "spade", "clover", "diamond"]
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def str_rank(self):
if self.rank == 11:
return "10(J)"
elif self.rank == 12:
return "10(Q)"
elif self.rank == 13:
return "10(K)"
else:
return str(self.rank)
def __str__(self):
s = "suit: %s \t rank: %d" % (self.suit, self.rank)
return s
class Deck:
def __init__(self):
self.deck = []
for s in suit:
for i in range(1, 14):
self.deck.append(Card(s, i))
random.shuffle(self.deck)
def pop(self):
return self.deck.pop()
def tail(self, count):
rev = self.deck[:-6:-1]
for c in rev:
print(c)
print("YOU ARE CHEATER!!")
class Strategy:
def __init__(self, player):
self.player = player
def think(self):
return "s"
def bet(self):
return 5
def continue_game(self):
return "n"
class BasicStrategy(Strategy):
def __init__(self, player):
super().__init__(player)
def think(self):
if self.player.count() >= 17:
return "s"
else:
return "h"
class UserStrategy(BasicStrategy):
def __init__(self, player):
super().__init__(player)
def continue_game(self):
if self.player.bet > 0:
return "y"
else:
return "n"
def bet(self):
return 20
class Player():
name = "player"
def __init__(self):
self.hands = []
self.strategy = None
self.call = False
self.gamecount = 0
self.wincount = 0
self.losecount = 0
self.evencount = 0
self.bet = 1000
def show(self):
s = ""
for c in self.hands:
s += c.str_rank()
s += ' '
return s
def count(self):
count = 0
aces = []
for c in self.hands:
if c.rank >= 10:
count += 10
elif c.rank == 1:
aces.append(c)
else:
count += c.rank
for c in aces:
if (count + 11) > 21:
count += 1
else:
count += 11
return count
def init_game(self):
self.hands = []
self.call = False
self.gamecount += 1
def isBurst(self):
return self.count() > 21
def draw(self, card):
self.hands.append(card)
def next(self):
if self.strategy != None:
return self.strategy.think()
else:
return "s"
def win(self, b):
self.wincount += 1
print("you get ", b*2)
self.bet += b*2
print("your bet ", self.bet)
def even(self, b):
self.evencount += 1
self.bet += b
print("your bet ", self.bet)
def lose(self):
self.losecount += 1
print("your bet ", self.bet)
def summary(self):
print("game count: ", self.gamecount)
print("win count: ", self.wincount)
print("even count: ", self.evencount)
print("lose count: ", self.losecount)
print("bet : ", self.bet ,"(", self.bet-1000, ")")
print("win rate : ", int(self.wincount/self.gamecount*100), "%")
def bets(self):
return 0
class User(Player):
name = "user"
def __init__(self):
super().__init__()
self.strategy = UserStrategy(self)
def bets(self):
return self.strategy.bet()
def continue_game(self):
return self.strategy.continue_game()
class Dealer(Player):
name = "dealer"
def __init__(self):
super().__init__()
self.strategy = BasicStrategy(self)
def show(self, openHand=False):
if openHand:
return super().show()
s = ""
for i, c in enumerate(self.hands):
if i == 0:
s += c.str_rank()
else:
s += "*"
s += " "
return s
def game(u=None):
# デッキの初期化
d = Deck()
# ユーザの初期化
if u == None:
u = User()
else:
u.init_game()
e = Dealer()
# init
u.draw(d.pop())
e.draw(d.pop())
u.draw(d.pop())
e.draw(d.pop())
print("\nUser\t", u.show(), "-> ", u.count())
print("Dealer\t", e.show(), "\n")
b = u.bets()
if u.bet - u.bets() < 0:
print("YOU CANT BET ANYMORE")
return
u.bet = u.bet - b
#user trun
print("USER TURN")
while(u.call == False):
# cheet
# d.tail(5)
hit = u.next()
if hit == 'h':
u.draw(d.pop())
print("\nUser\t", u.show(), "-> ", u.count())
if u.count() > 21:
print("BURST! YOU LOSE", "\n")
u.lose()
return
elif hit == "s":
u.call = True
else:
print("input again")
#dealer turn
print("DEALER TURN\n")
while(e.call == False):
hit = e.next()
if hit == 'h':
e.draw(d.pop())
print("Dealer\t", e.show())
if e.count() > 21:
print("User\t", u.show(), "-> ", u.count())
print("Dealer\t", e.show(True), "-> ", e.count())
print("BURST! YOU WIN\n")
u.win(b)
return
else:
e.call = True
print("OPEN")
print("You Called\t", u.show(), "-> ", u.count())
print("Dealer Called\t", e.show(True), "-> ", e.count())
if u.count() > e.count():
print("YOU WIN\n")
u.win(b)
elif u.count() == e.count():
print("EVEN\n")
u.even(b)
else:
print("YOU LOSE\n")
u.lose()
print("GAME END")
def game_start():
u = User()
game(u)
while(True):
if u.gamecount == 1000:
print("force finesh")
break;
ask = u.continue_game()
if ask == "y":
if u.bet == 0:
print("you can't continue cz you don't have bet")
break
game(u)
elif ask == "n":
again = False
break
else:
print("input y or n")
u.summary()
print("END")
game_start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.