Skip to content

Instantly share code, notes, and snippets.

@altrlz
Created July 28, 2018 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altrlz/c6a94b4a01ab9642815f18e4359b40e8 to your computer and use it in GitHub Desktop.
Save altrlz/c6a94b4a01ab9642815f18e4359b40e8 to your computer and use it in GitHub Desktop.
main.py
import random
RANK, SUIT = 0, 1
#デッキを作る
def make_deck():
rank = range(1,14)
suit = ('S','H','C','D')
deck = [(i,j) for i in rank for j in suit]
random.shuffle(deck)
return deck
#手札のポイントを計算する
def get_point(hand):
result = 0
ace_flag = False
for card in hand:
if card[RANK] == 1:
ace_flag = True
if card[RANK] > 10:
num = 10
else:
num = card[RANK]
result = result + num
if ace_flag and result <= 11:
result += 10
return result
#プレイヤーの手札を表示する
def print_player_hand(player_hand):
print('プレイヤー(', get_point(player_hand),'): ')
for card in player_hand:
print('[',card[SUIT],card[RANK],']')
print()
#ディーラーの手札を表示する
def print_dealer_hand(dealer_hand,uncovered):
if uncovered :
print('ディーラー(', get_point(dealer_hand),'): ')
else :
print('ディーラー(??)')
flag = True
for card in dealer_hand:
if flag or uncovered :
print('[',card[SUIT],card[RANK],']')
flag = False
else :
print('[ * * ]')
print()
#勝ち負けの判定をする
def win_lose(dealer_hand, player_hand, bet, player_money):
player_point = get_point(player_hand)
dealer_point = get_point(dealer_hand)
if player_point <= 21:
if player_point > dealer_point or dealer_point > 21:
if player_point == 21:
return ('<<プレイヤーの勝ち>>', player_money + int(bet*2.5))
else:
return ('<<プレイヤーの勝ち>>', player_money + bet*2)
elif player_point == dealer_point:
return ('<<プッシュ>>', player_money + bet)
else:
return ('<<プレイヤーの負け>>', player_money)
else :
return ('<<プレイヤーの負け>>', player_money)
return 0
#プレイヤーのターン処理
def player_op(deck, player_hand, op):
doubled, ending = False, False
if op == '1':
print('[プレイヤー:スタンド]')
doubled, ending = False, True
elif op == '2':
print('[プレイヤー:ヒット]')
player_hand.append(deck.pop())
print_player_hand(player_hand)
doubled, ending = False, False
elif op == '3':
print('[プレイヤー:ダブル]')
if len(player_hand) == 2:
player_hand.append(deck.pop())
print_player_hand(player_hand)
doubled, ending = True, True
else :
print('ダブルはできません。')
if get_point(player_hand) > 21:
print('[プレイヤーはバストした!]')
ending = True
elif get_point(player_hand) == 21:
print('[ブラックジャック!]')
ending = True
return doubled, ending
#ディーラーのターン処理
def dealer_op(deck, player_hand, dealer_hand):
while get_point(player_hand) <= 21:
if get_point(dealer_hand) >= 17:
print('[ディーラー:スタンド]')
break
else:
print('[ディーラー:ヒット]')
dealer_hand.append(deck.pop())
print_dealer_hand(dealer_hand, False)
def main():
turn = 1
player_money = 100
deck = make_deck()
while(player_money > 0):
print('-'*20)
print('ターン:',turn)
print('所持金:',player_money)
print('-'*20)
player_hand = []
dealer_hand = []
#ベット
try :
bet = int(input('ベット額 > '))
except :
print('整数を入力してください')
continue
if bet > player_money:
print('所持金が不足しています')
continue
elif bet <= 0:
print('ベットできる額は1以上です。')
continue
#所持金からベット額を引く
player_money -= bet
#山札が10枚以下なら再構築する&シャッフル
if len(deck) < 10:
deck = make_deck()
#カードを2枚づつ引く
for i in range(2):
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
uncovered = False
print('-'*20)
print_player_hand(player_hand)
print_dealer_hand(dealer_hand, uncovered)
print('-'*20)
#プレイヤーのターン
while True:
op = input('スタンド:1, ヒット:2, ダブル:3 > ')
doubled, ending = player_op(deck, player_hand, op)
if doubled :
bet += bet
player_money -= bet
break
if ending :
break
#ディーラーのターン
dealer_op(deck,player_hand,dealer_hand)
#勝敗の判定
uncovered = True
print('-'*20)
print_player_hand(player_hand)
print_dealer_hand(dealer_hand, uncovered)
print('-'*20)
message, player_money = win_lose(dealer_hand, player_hand, bet, player_money)
print(message)
turn += 1
input('次のターンへ')
print('ゲームオーバー')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment