codeが長くなったので稿を改めました。
前回迄でplayer、dealerのカード配布が終わりましたが、見やすくするためdealerのカード引きの所を関数化しました。
でもまだ一回だけ引く所迄です。 dealerの得点は見えるようにしてあります。
ブラックジャックを作る – 11
1 | import random Tak-BJ 2-5list13.py | |
2 | ||
3 | RANK, SUIT = 0, 1 | |
4 | ||
5 | ||
6 | def player_op(deck, player_hand, op): | |
7 | ending =False | |
8 | if op == ‘1’: | |
9 | print(‘[ プレイヤー:スタンド ]’) | |
10 | ending = True | |
11 | elif op == ‘2’: | |
12 | print(‘[ プレイヤー:ヒット ]’) | |
13 | player_hand.append(deck.pop()) | |
14 | print_player_hand(player_hand) | |
15 | ending = False | |
16 | ||
17 | ||
18 | if get_point(player_hand) > 21: # バスト判定 | |
19 | print(‘[ プレイヤーはバストした! ]’) | |
20 | ending = True | |
21 | elif get_point(player_hand) == 21: | |
22 | print(’21です!’) | |
23 | ending = True | |
24 | ||
25 | return ending | |
26 | ||
27 | ||
28 | def dealer_op(deck, player_hand, dealer_hand): | |
29 | while get_point(player_hand) <= 21: | |
30 | if get_point(dealer_hand) >= 17: | |
31 | print(‘[ ディーラー:スタンド ]’) | |
32 | break | |
33 | else: | |
34 | print(‘[ ディーラー:ヒット ]’) | |
35 | dealer_hand.append(deck.pop()) | |
36 | print_dealer_hand(dealer_hand, False) | |
37 | ||
38 | ||
39 | def get_point(hand): | |
40 | result = 0 | |
41 | ace_flag = False | |
42 | for card in hand: | |
43 | if card[RANK] == 1: | |
44 | ace_flag = True | |
45 | if card[RANK] > 10: | |
46 | num = 10 | |
47 | else: | |
48 | num = card[RANK] | |
49 | result = result + num | |
50 | if ace_flag and result <= 11: | |
51 | result += 10 | |
52 | return result | |
53 | ||
54 | # print_player_hand関数 | |
55 | ||
56 | ||
57 | def print_player_hand(player_hand): | |
58 | print(‘プレイヤー (‘, get_point(player_hand), ‘): ‘) | |
59 | for card in player_hand: | |
60 | print(‘[‘, card[SUIT], card[RANK], ‘]’) | |
61 | print() | |
62 | ||
63 | # print_dealer_hand関数 | |
64 | def print_dealer_hand(dealer_hand, uncovered): | |
65 | if uncovered: | |
66 | print(‘ディーラー (‘, get_point(dealer_hand), ‘): ‘) | |
67 | else: | |
68 | print(‘ディーラー ( ?? ): ‘) | |
69 | flag = True | |
70 | for card in dealer_hand: | |
71 | if flag or uncovered: | |
72 | print(‘[‘, card[SUIT], card[RANK], ‘]’) | |
73 | flag = True #False | |
74 | else: | |
75 | print(‘[ * * ]’) | |
76 | print() | |
77 | ||
78 | ||
79 | def make_deck(): | |
80 | suits = [‘S’, ‘H’, ‘D’, ‘C’] # スート(記号)の定義 | |
81 | ranks = range(1, 14) # ランク(数字)の定義 | |
82 | deck = [(x, y) for x in ranks for y in suits] | |
83 | random.shuffle(deck) # シャッフルする | |
84 | return deck | |
85 | ||
86 | ||
87 | def main(): | |
88 | turn = 1 | |
89 | player_money = 100 | |
90 | while player_money > 0: | |
91 | print(‘ターン:’, turn) | |
92 | print(‘所持金:’, player_money) | |
93 | ||
94 | player_hand = [] # プレイヤーの手札を格納するリスト | |
95 | dealer_hand = [] # ディーラーの手札を格納するリスト | |
96 | deck = make_deck() | |
97 | bet = 10 | |
98 | player_money -= bet | |
99 | ||
100 | for i in range(2): # お互いに2枚ずつ引く | |
101 | player_hand.append(deck.pop()) # デッキからプレイヤーの手札へ | |
102 | dealer_hand.append(deck.pop()) # デッキからディーラーの手札へ | |
103 | ||
104 | # print(player_hand) | |
105 | print_player_hand(player_hand) | |
106 | ||
107 | # print(dealer_hand) | |
108 | print_dealer_hand(dealer_hand, False) # 本来はTrueですが、ここではFalseにしておく | |
109 | ||
110 | # プレイヤーターン | |
111 | while True: | |
112 | op = input(‘スタンド : 1, ヒット : 2 > ‘) | |
113 | ending = player_op(deck, player_hand, op) | |
114 | if ending: | |
115 | break | |
116 | ||
117 | # ディーラーターン | |
118 | dealer_op(deck, player_hand, dealer_hand) | |
119 | ||
120 | turn += 1 | |
121 | input(‘次のターンへ’) | |
122 | print(‘ゲームオーバー’) | |
123 | ||
124 | ||
125 | if __name__ == ‘__main__’: | |
126 | main() |
ブラックジャックを作る – 12
・playerの掛け金の処理を追加する(*印)
1 | import random | Tak-BJ 2-5list16.py | |
2 | |||
3 | RANK, SUIT = 0, 1 | ||
4 | |||
5 | |||
6 | def player_op(deck, player_hand, op): | ||
7 | doubled, ending = False, False | ||
8 | if op == ‘1’: | ||
9 | print(‘[ プレイヤー:スタンド ]’) | ||
10 | ending = True | ||
11 | elif op == ‘2’: | ||
12 | print(‘[ プレイヤー:ヒット ]’) | ||
13 | player_hand.append(deck.pop()) | ||
14 | print_player_hand(player_hand) | ||
15 | ending = False | ||
16 | |||
17 | |||
18 | if get_point(player_hand) > 21: | # バスト判定 | |
19 | print(‘[ プレイヤーはバストした! ]’) | ||
20 | ending = True | ||
21 | elif get_point(player_hand) == 21: | ||
22 | print(’21です!’) | ||
23 | ending = True | ||
24 | |||
25 | return ending | ||
26 | |||
27 | |||
28 | def dealer_op(deck, player_hand, dealer_hand): | ||
29 | while get_point(player_hand) <= 21: | ||
30 | if get_point(dealer_hand) >= 17: | ||
31 | print(‘[ ディーラー:スタンド ]’) | ||
32 | break | ||
33 | else: | ||
34 | print(‘[ ディーラー:ヒット ]’) | ||
35 | dealer_hand.append(deck.pop()) | ||
36 | print_dealer_hand(dealer_hand, False) | ||
37 | |||
38 | |||
39 | def get_point(hand): | ||
40 | result = 0 | ||
41 | ace_flag = False | ||
42 | for card in hand: | ||
43 | if card[RANK] == 1: | ||
44 | ace_flag = True | ||
45 | if card[RANK] > 10: | ||
46 | num = 10 | ||
47 | else: | ||
48 | num = card[RANK] | ||
49 | result = result + num | ||
50 | if ace_flag and result <= 11: | ||
51 | result += 10 | ||
52 | return result | ||
53 | |||
54 | # print_player_hand関数 | ||
55 | |||
56 | |||
57 | def print_player_hand(player_hand): | ||
58 | print(‘プレイヤー (‘, get_point(player_hand), ‘): ‘) | ||
59 | for card in player_hand: | ||
60 | print(‘[‘, card[SUIT], card[RANK], ‘]’) | ||
61 | print() | ||
62 | |||
63 | # print_dealer_hand関数 | ||
64 | def print_dealer_hand(dealer_hand, uncovered): | ||
65 | if uncovered: | ||
66 | print(‘ディーラー (‘, get_point(dealer_hand), ‘): ‘) | ||
67 | else: | ||
68 | print(‘ディーラー ( ?? ): ‘) | ||
69 | flag = True | ||
70 | for card in dealer_hand: | ||
71 | if flag or uncovered: | ||
72 | print(‘[‘, card[SUIT], card[RANK], ‘]’) | ||
73 | flag = True #False | ||
74 | else: | ||
75 | print(‘[ * * ]’) | ||
76 | print() | ||
77 | |||
78 | |||
79 | def make_deck(): | ||
80 | suits = [‘S’, ‘H’, ‘D’, ‘C’] | # スート(記号)の定義 | |
81 | ranks = range(1, 14) | # ランク(数字)の定義 | |
82 | deck = [(x, y) for x in ranks for y in suits] | ||
83 | random.shuffle(deck) | # シャッフルする | |
84 | return deck | ||
85 | |||
86 | |||
87 | def main(): | ||
88 | turn = 1 | ||
89 | player_money = 100 | ||
90 | while player_money > 0: | ||
91 | print(‘ターン:’, turn) | ||
92 | print(‘所持金:’, player_money) | ||
93 | |||
94 | player_hand = [] | # プレイヤー手札を格納するリスト | |
95 | dealer_hand = [] | # ディーラー手札を格納するリスト | |
96 | deck = make_deck() | ||
97 | |||
* | 98 | try: | |
* | 99 | bet = int(input(‘bet > ‘)) | |
* | 100 | except: | |
* | 101 | print(‘Enter intiger’) | |
* | 102 | continue | |
* | 103 | ||
* | 104 | #入力値が所持金を越えていたらやり直し | |
* | 105 | if bet > player_money: | |
* | 106 | print(‘Not enough money you bet!’) | |
* | 107 | continue | |
* | 108 | #入力値が0より小さかったらやり直し | |
* | 109 | elif bet <= 0: | |
* | 110 | print(‘No joke, please!’) | |
* | 111 | continue | |
112 | |||
113 | player_money -= bet | ||
114 | |||
115 | for i in range(2): # お互いに2枚ずつ引く | ||
116 | player_hand.append(deck.pop()) | # デッキからプレイヤーの手札へ | |
117 | dealer_hand.append(deck.pop()) | # デッキからディーラーの手札へ | |
118 | |||
119 | # print(player_hand) | ||
120 | print_player_hand(player_hand) | ||
121 | |||
122 | # print(dealer_hand) | ||
123 | print_dealer_hand(dealer_hand, False) | # 最終的にはTrueにする | |
124 | |||
125 | # プレイヤーターン | ||
126 | while True: | ||
127 | op = input(‘スタンド : 1, ヒット : 2 > ‘) | ||
128 | ending = player_op(deck, player_hand, op) | ||
129 | if ending: | ||
130 | break | ||
131 | |||
132 | # ディーラーターン | ||
133 | dealer_op(deck, player_hand, dealer_hand) | ||
134 | |||
135 | turn += 1 | ||
136 | input(‘次のターンへ’) | ||
137 | print(‘ゲームオーバー’) | ||
138 | |||
139 | |||
140 | if __name__ == ‘__main__’: | ||
141 | main() |
ブラックジャックを作る – 13
・playerの掛け金の処理を追加する
・deckの残りが10枚以下になったら、deckを元へ戻す
・dealerの得点計算はこれから
1 | import random | Tak-BJ 2-5list18.py | |
2 | |||
3 | RANK, SUIT = 0, 1 | ||
4 | |||
5 | |||
6 | def player_op(deck, player_hand, op): | ||
7 | doubled, ending = False, False | ||
8 | if op == ‘1’: | ||
9 | print(‘[ プレイヤー:スタンド ]’) | ||
10 | ending = True | ||
11 | elif op == ‘2’: | ||
12 | print(‘[ プレイヤー:ヒット ]’) | ||
13 | player_hand.append(deck.pop()) | ||
14 | print_player_hand(player_hand) | ||
15 | ending = False | ||
16 | |||
17 | |||
18 | if get_point(player_hand) > 21: | # バスト判定 | |
19 | print(‘[ プレイヤーはバストした! ]’) | ||
20 | ending = True | ||
21 | elif get_point(player_hand) == 21: | ||
22 | print(’21です!’) | ||
23 | ending = True | ||
24 | |||
25 | return ending | ||
26 | |||
27 | |||
28 | def dealer_op(deck, player_hand, dealer_hand): | ||
29 | while get_point(player_hand) <= 21: | ||
30 | if get_point(dealer_hand) >= 17: | ||
31 | print(‘[ ディーラー:スタンド ]’) | ||
32 | break | ||
33 | else: | ||
34 | print(‘[ ディーラー:ヒット ]’) | ||
35 | dealer_hand.append(deck.pop()) | ||
36 | print_dealer_hand(dealer_hand, False) | ||
37 | |||
38 | |||
39 | def get_point(hand): | ||
40 | result = 0 | ||
41 | ace_flag = False | ||
42 | for card in hand: | ||
43 | if card[RANK] == 1: | ||
44 | ace_flag = True | ||
45 | if card[RANK] > 10: | ||
46 | num = 10 | ||
47 | else: | ||
48 | num = card[RANK] | ||
49 | result = result + num | ||
50 | if ace_flag and result <= 11: | ||
51 | result += 10 | ||
52 | return result | ||
53 | |||
54 | # print_player_hand関数 | ||
55 | |||
56 | |||
57 | def print_player_hand(player_hand): | ||
58 | print(‘プレイヤー (‘, get_point(player_hand), ‘): ‘) | ||
59 | for card in player_hand: | ||
60 | print(‘[‘, card[SUIT], card[RANK], ‘]’) | ||
61 | print() | ||
62 | |||
63 | # print_dealer_hand関数 | ||
64 | def print_dealer_hand(dealer_hand, uncovered): | ||
65 | if uncovered: | ||
66 | print(‘ディーラー (‘, get_point(dealer_hand), ‘): ‘) | ||
67 | else: | ||
68 | print(‘ディーラー ( ?? ): ‘) | ||
69 | flag = True | ||
70 | for card in dealer_hand: | ||
71 | if flag or uncovered: | ||
72 | print(‘[‘, card[SUIT], card[RANK], ‘]’) | ||
73 | flag = True #False | ||
74 | else: | ||
75 | print(‘[ * * ]’) | ||
76 | print() | ||
77 | |||
78 | |||
79 | def make_deck(): | ||
80 | suits = [‘S’, ‘H’, ‘D’, ‘C’] | # スート(記号)の定義 | |
81 | ranks = range(1, 14) | # ランク(数字)の定義 | |
82 | deck = [(x, y) for x in ranks for y in suits] | ||
83 | random.shuffle(deck) | # シャッフルする | |
84 | return deck | ||
85 | |||
86 | |||
87 | def main(): | ||
88 | turn = 1 | ||
89 | player_money = 100 | ||
90 | deck = make_deck() | #山札の不足を解消する | |
91 | |||
92 | while player_money > 0: | ||
93 | print(‘ターン:’, turn) | ||
94 | print(‘所持金:’, player_money) | ||
95 | |||
96 | player_hand = [] | # プレイヤー手札を格納するリスト | |
97 | dealer_hand = [] | # ディーラー手札を格納するリスト | |
98 | |||
99 | try: | ||
100 | bet = int(input(‘bet > ‘)) | ||
101 | except: | ||
102 | print(‘Enter intiger’) | ||
103 | continue | ||
104 | |||
105 | #入力値が所持金を越えていたらやり直し | ||
106 | if bet > player_money: | ||
107 | print(‘Not enough money you bet!’) | ||
108 | continue | ||
109 | #入力値が0より小さかったらやり直し | ||
110 | elif bet <= 0: | ||
111 | print(‘No joke, please!’) | ||
112 | continue | ||
113 | |||
114 | player_money -= bet | ||
115 | |||
116 | # deckの残りが10枚以下ならdeckを再構築 | ||
117 | if len(deck) < 10: | ||
118 | deck = make_deck() | ||
119 | |||
120 | for i in range(2): # お互いに2枚ずつ引く | ||
121 | player_hand.append(deck.pop()) | # デッキからプレイヤーの手札へ | |
122 | dealer_hand.append(deck.pop()) | # デッキからディーラーの手札へ | |
123 | |||
124 | # print(player_hand) | ||
125 | print_player_hand(player_hand) | ||
126 | |||
127 | # print(dealer_hand) | ||
128 | print_dealer_hand(dealer_hand, False) | # 最終的にはTrueにする | |
129 | |||
130 | # プレイヤーターン | ||
131 | while True: | ||
132 | op = input(‘スタンド : 1, ヒット : 2 > ‘) | ||
133 | ending = player_op(deck, player_hand, op) | ||
134 | if ending: | ||
135 | break | ||
136 | |||
137 | # ディーラーターン | ||
138 | dealer_op(deck, player_hand, dealer_hand) | ||
139 | |||
140 | turn += 1 | ||
141 | input(‘次のターンへ’) | ||
142 | print(‘ゲームオーバー’) | ||
143 | |||
144 | |||
145 | if __name__ == ‘__main__’: | ||
146 | main() |
次が動作一例:
Playerが最初の配布で20だったのでstandした。
dealerも17以上なので自動的にstand。 勝敗判定は未完成。
ターン: 1
所持金: 100
bet > 10
プレイヤー ( 20 ):
[ D 11 ]
[ D 10 ]
ディーラー ( ?? ):
[ S 8 ]
[ C 10 ]
スタンド : 1, ヒット : 2 > 1 <– playerはstandした
[ プレイヤー:スタンド ] <– dealerは17以上になっているのでstandした
[ ディーラー:スタンド ]
次のターンへ