0
0

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 3 years have passed since last update.

Pythonで楽しいジャンケンポイ 初心者向けを参考にしてみた

Posted at

現場があまりに暇で、知人がジャンケンゲーム作ったのを思い出したので

参考にした記事

@sandreamさんの記事「Pythonでじゃんけんポイっ 初心者向け(回答と解説)
」を参考に、
ジャンケン小僧を相手に3回先勝ルールにしたものです。
@sandreamさんがジャンケンの記事を書くに至った経緯や、
全体機能の詳しい解説は上記の記事を参考にしてください。

コード

janken.py
# _*_ coding: utf-8 _*_

import random
import time

dic = {'a':'ぐー','b':'ちょき','c':'ぱー'}

# 出せる手リストの取得
def clist(hands):
    ls =[]
    for i in hands:
        ls.append(i)
    return ls

# 出せる手メッセージ
comm_mess = str(','.join(dic))+' のどれかを入力してね!'

# 勝利エンドメッセージ
def good_end():
    mess = str(\
        'ばかな~!!\n'\
        'じゃんけんこぞうが じゃんけんで まけただとぉぉー?!\n'\
        'ひでぶー!\n'\
    )
    return mess

# 敗北エンドメッセージ
def bad_end():
    mess = str(\
        'ざんねん きみの まけだよ!!\n'\
        '明日の朝10時までに原因を解明して敗北抑止チェックリストを提出してください。\n'\
    )
    return mess

# ジャンケン小僧エスパーモード
def kozou_tsuyoi(uch,pch,uco,pco):
    #ジャンケン小僧エスパーモードのメッセージ
    kozou_mess1 = ('クソ~!じゃんけんこぞうをナメるなよ~!!')
    #ジャンケン小僧超エスパーモードのメッセージ
    kozou_mess2 = ('みえる!おまえの てが みえるぞ!!')
    _random =random.random()
    #プレイヤーの勝ち星、小僧の勝ち星、手を条件に分岐する
    if uco == 2 and pco <= 1 and uch == dic['a']:
        print(kozou_mess1)
        hands = dic['c']
    elif uco == 2 and pco <= 1 and uch == dic['b']:
        print(kozou_mess1)
        hands = dic['a']
    elif uco == 2 and pco <= 1 and uch == dic['c']:
        print(kozou_mess1)
        hands = dic['b']
    #ジャンケン小僧が超エスパーモードになる
    elif _random < 0.01 and uch == dic['a']:
        print(kozou_mess2)
        hands = dic['c']
    elif _random < 0.01 and uch == dic['b']:
        print(kozou_mess2)
        hands = dic['a']
    elif _random < 0.01 and uch == dic['c']:
        print(kozou_mess2)
        hands = dic['b']
    else:
        hands = pch
    return hands

choice_list = clist(dic)
judge = None
player_win ='You WIN !!\n'
player_lose ='You LOSE !!\n'
player_draw ='DRAW !!\n'

user_co = 0
pc_co = 0
print(\
'ぼくは じゃんけん こぞう\n'\
'じゃんけん しようよ\n'\
'さきに 3かい かったほうが かち\n'\
)

for i in clist(dic):
    print (i+''+' '+dic[i])
time.sleep(2)
print(comm_mess+'\n')

while True:
    if judge == player_draw:
        print('アイッ!コデッッ!!')
    else:
        print('ジャンッ!ケンッッ!!')
    
    #プレイヤーが手を選ぶ
    print('(何を出す!?')
    user = input('')
    user_choice = user.lower()
    try:
        user_choice = dic[user]
    except:
        print(comm_mess)
        continue
    
    #ジャンケン小僧が手を選ぶ
    pc_choice = dic[str(random.choice(clist(dic)))]
    
    #ジャンケン小僧エスパーモード
    _random = random.random()
    if _random < 0.5:
        pc_choice = kozou_tsuyoi(user_choice,pc_choice,user_co,pc_co)
    
    if judge == player_draw:
        print('ショッッッ!!')
    else:
        print('ポイッッッ!!')
    time.sleep(1.5)
    
    #手の確認をする
    print(\
    'プレイヤーの選んだ手:'+str(user_choice)+'\n'\
    'じゃんけんこぞうの手:'+str(pc_choice)+'\n'\
    )
    time.sleep(1.5)
    
    #勝敗判定
    if user_choice == pc_choice:
        judge = player_draw
        print(player_draw)
    elif user_choice == dic['a']:
        if pc_choice ==dic['b']:
            judge = player_win
            print(player_win)
            user_co += 1
        elif pc_choice == dic['c']:
            judge = player_lose
            print(player_lose)
            pc_co += 1
        else:
            print('ジャンケンの法則が乱れる!A')
    elif user_choice == dic['b']:
        if pc_choice ==dic['c']:
            judge = player_win
            print(player_win)
            user_co += 1
        elif pc_choice == dic['a']:
            judge = player_lose
            print(player_lose)
            pc_co += 1
        else:
            print('ジャンケンの法則が乱れる!B')
    elif user_choice == dic['c']:
        if pc_choice ==dic['a']:
            judge = player_win
            print(player_win)
            user_co += 1
        elif pc_choice == dic['b']:
            judge = player_lose
            print(player_lose)
            pc_co += 1
        else:
            print('ジャンケンの法則が乱れる!C')
    else:
        print('ジャンケンの法則が乱れる!D')
    
    print(\
    'プレイヤーの勝利数:'+str(user_co)+'\n'\
    'じゃんけんこぞうの勝利数:'+str(pc_co)+'\n'\
    )
    time.sleep(1.5)
    
    #進捗別イベント(考え中)
    
    #エンディング判定
    if user_co == 3:
        print(good_end())
        break
    elif pc_co == 3:
        print(bad_end())
        break

print('THE END')

Repl.itさんで試したところ、なんとなくイメージ通りに動いたっぽいのでヨシ!
無題.png

とくちょう

単純なchoiceの選出は十分カオスで、これはこれで面白いのですが
やはりゲームセンターにあったジャンケンゲームと言えば相手の理不尽な強さですかね
昔よくメダルゲーで巻き上げられたモノです。
そんな機能を付け足しました。

エスパー機能

名付けてエスパー機能
ランダムで関数を実行し、特定の条件になると発動します。
↓のあたり

def kozou_tsuyoi(uch,pch,uco,pco):
    _random =random.random()
    #プレイヤーの勝ち星、小僧の勝ち星、手を条件に分岐する
    if uco == 2 and pco <= 1 and uch == dic['a']:
        hands = dic['c']
    #ランダムの値
    elif _random < 0.01 and uch == dic['a']:
        hands = dic['c']
    else:
        hands = pch
    return hands

条件は分かれていますが、設計としては次の通りです。

  • 負け気味だと勝ちに行く機能
  • ランダム発動で絶対に勝ちに行く機能

発動条件、分岐の条件を甘くすることで理不尽な強さにできると思います。

プレイヤーの手に合わせて100%変えてくるんじゃ面白くないですからね。
ちょっと変えてみた次第です。

気にした部分

while True爆弾

ループ抜けの条件が勝つか、負けるかという男気溢れた仕様なので
参考にした記事のまんま、入力制限は残しました。

if分岐のelse逃げ

分岐が多めになってしまったので、ifにelseを持たせるようにして例外処理っぽいことをしています。
まぁそれでも勝つか、負けるかしないとゲームが終わらないんですけどね。

最後に

ジャンケン小僧にジャンケンで負けた方は
明日の朝10時までに原因を解明して敗北抑止チェックリストを提出してください。

0
0
4

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?