0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonポーカーゲームを修正してみる

0
Last updated at Posted at 2021-03-20

感想

学んだとは思う。こういうことと併用してPyQなどで補足すればPythonのプラクティスとしてはいいかも。

背景

Pythonでポーカーゲームを作るという課題が購入した本にありました。
それを多少変えてみるという企画です。

インプット情報

1週間でPythonの基礎が学べる本 (1週間シリーズ) 亀田 健司

https://www.amazon.co.jp/dp/4295008532/ref=cm_sw_r_tw_dp_AKRRVWE44PCVADC1FXCQ via @amazonJP

改修方針

・役を減らす。
・スペシャル会員の場合は「ロイヤル・ストレートフラッシュ」にする。

コード 部分 

import random
import os
import itertools




# カードの種類の取得
def get_kind(index):
    kind = ["", "", "", ""]
    return kind[index - 1]


# 数字の種類の取得
def get_num(index):
    nums = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
    return nums[index - 1]


# 手札を小さい順に並べ替え
def sort_card(hand):
    for i in range(0, 4):
        for j in range(i + 1, 5):
            if hand[i]["num"] > hand[j]["num"]:
                tmp = hand[j]
                hand[j] = hand[i]
                hand[i] = tmp
    return hand


# ゲームの初期化
def init():
    # トランプのカード
    cards = [{"kind": kind, "num": num} for kind, num in itertools.product(range(1, 4 + 1), range(1, 13 + 1))]

    # カードのシャッフル
    random.shuffle(cards)
    return take(cards, 5)


# 手札の表示
def show_hand(hand,selects, acter):
    for i in range(1,5+1):
        num_string = str(i)
        if i in selects:
            num_string = "*"+num_string
        print("{:>5}  ".format(num_string),end="")
    print()
    i = 0
    if acter == "S":

        for i in range(5):
            hand[i]["kind"] = 1
        hand[0]["num"] = 0
        hand[1]["num"] = 9
        hand[2]["num"] = 10
        hand[3]["num"] = 11
        hand[4]["num"] = 12
    else:
        pass

    for card in hand:
        card_kind = get_kind(card["kind"])
        card_number = get_num(card["num"])
        print("[{}{:>2}] ".format(card_kind,card_number),end=" ")
    print()



# メインプログラム
def main():
    acter = "S"
    # カードの初期化
    hand, cards = init()
    hand = sort_card(hand)
    # ゲームプレイのループ
    for i in range(1, 3 + 1):
        hand, cards, end_flag = game_main(i, hand, cards,acter)
        if end_flag == True:
            break
    game_end(hand, acter)

# 実行例
ゲーム終了
ゲーム終了
    1      2      3      4      5  
[10]  [11]  [12]  [13]  [ A]  
ロイヤルストレートフラッシュ score:10000
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?