This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

じゃんけんプログラムを作ってみよう 模範解答例

Posted at

じゃんけんプログラムを作ってみよう <模範解答>

あなたの手を入力してください(グー:1 チョキ:2 パー:3)
じゃんけんプログラムを作るに当たってのヒントをいくつか提示します。
まずは
1)randomモジュールを使い、コンピュータが出す手を決めます。
2)ユーザーにどの手を出すか決めてもらいます。その際にじゃんけんの手(グー、チョキ、パー)を数字で管理すると作りやすいです。
3)コンピュータの手とユーザーの手で勝敗を判定する
これを繰り返します。
ゲームを終了する時には、終了する入力値をユーザーに入力してもらいましょう。

ソースコード

janken_game.py
import random
import sys

file_name = "result.txt"
hands = ["グー", "チョキ", "パー"]

# 勝敗にファイルに書き込む
def save(userid, result):
    data = str(userid) + " " + result
    f = open(file_name, "a")
    f.write(data)
    f.write("\n")

def game_end():
    print("ゲームを終了します")
    sys.exit()

"""
第一引数:s テキストファイルの全文字数
第二引数:search_str 検索する文字
"""
def countChar(s,search_str):
    # 検索する文字だけ一旦消すことで、全体から検索する文字を引けば、検索する文字の数を調べられる
    return len(s) - len(s.replace(search_str, ""))

# じゃんけんした回数と勝率を表示する
def show(userid):

    print("検索するUSER_ID:",userid)

    f = open(file_name, "r")
    result = f.read()
    f.close()

    uid = str(userid)
    print("これまでじゃんけんした数",countChar(result, uid))
    print("勝った回数:")
    print(countChar(result, "勝"))
    print("負けた回数:")
    print(countChar(result, "負"))
    print("あいこの回数:")
    print(countChar(result, "あ"))

while True:
    userid = int(input("USER_IDを入力してください: "))
    print("1:じゃんけんをする 2: 勝敗を見る 3:ゲーム終了")
    op = int(input())

    if op == 1:
        while True:
            # 2の時
            print("じゃんけんを開始します。")
            comp_hands = random.randint(0,2)

            # オプション追加

            print("何を出しますか?0:グー 1: チョキ 2: パー 3:ゲーム終了")
            my_hand = int(input())

            if my_hand == 3:
                game_end()
            elif my_hand < 0 or my_hand > 2:
                print("0〜2の間で手を選んでください")
                continue

            print("自分: ", hands[my_hand])
            print("コンピュータ: ", hands[comp_hands])

            jadge = (my_hand - comp_hands + 3) % 3
            if jadge == 0:
                print("結果は...あいこ")
                # あいこの時は「あ」と書き込む
                save(userid, result="あ")
            elif jadge == 1:
                print("結果は...負け")
                # ユーザー名orIDと勝敗にファイルに書き込む
                save(userid, result="負")
            else:
                print("結果は...勝ち!!")
                # ユーザー名orIDと勝敗にファイルに書き込む
                save(userid, result="勝")
    elif op == 2:
        show(userid)
    elif op == 3:
        end()
    else:
        # エラーメッセージ
        print("0〜3の数字を入力してください")

じゃんけんプログラムを作ってみよう

あなたの手を入力してください(グー:1 チョキ:2 パー:3)
じゃんけんプログラムを作るに当たって、
randomモジュールを使います。

じゃんけんプログラム仕様

じゃんけんの勝敗を判定する方法

以下の式ですが、じゃんけんの勝敗を判定する一例の方法です。

jadge.py
my_hand = ["グー", "チョキ", "パー"]

jadge = (my_hand - player_hand + 3) % 3

上記以外にもあれば、見つけた方法で記述しても構いません。

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