Differences between revisions 2 and 3
Revision 2 as of 2018-10-10 14:21:23
Size: 826
Comment:
Revision 3 as of 2018-10-10 15:25:25
Size: 3488
Comment:
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
 * 解答は基本的に,下記 行です.
  * 問題の回答としては,これだけで十分です.
  * 自分自身のソフトウェア開発のためには,できるだけコメントをつける習慣をもってください.
 * 解答は基本的に,下記の二つの関数です.
Line 21: Line 20:
import random
Line 22: Line 22:
def janken_random_hand():
    return( random.randint( 0, 2 ) )
}}}

{{{#!highlight python
def janken_judge( x, y ):
    return( ((y-x)+3)%3 )
Line 27: Line 34:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==============================================================================
# * Copyright (c) 2018 IIJIMA, Tadashi
# * (IIJIMA Laboratory, Dept. of Science and Technology, Keio University).
# ==============================================================================
# ソフトウェア工学[02] 拡張課題[02]-(000a) EP_02_000a_.py
# EP(Extended Problem) 02-000a:
# 2018-10-03 飯島 正 (iijima@ae.keio.ac.jp)
# ==============================================================================
# ----- 乱数を取り扱うrandomモジュールをインポートする -----
import random
# ==============================================================================
# ===== 【関数定義】 乱数で「じゃんけんの手」を求める関数 (0,1,2の整数で返す) =====
def janken_random_hand():
    return( random.randint( 0, 2 ) )
# ==============================================================================
# ===== 【関数定義】 「じゃんけんの勝敗」を0,1,2の整数で返す関数 =====
def janken_judge( x, y ):
    return( ((y-x)+3)%3 )
# ==============================================================================
# ===== 【関数定義】 「じゃんけんの勝敗」の文字列を,対応する整数0,1,2から返す関数 =====
# リスト(配列)もしくは辞書を使うともっと簡潔にできる → 後日
def judge_str( x ):
    if x == 0:
        return( "あいこ" )
    elif x == 1:
        return( "勝ち" )
    else:
        return( "負け" )
# ==============================================================================
# ===== 【メイン・プログラム】  =====
# ----- オープニングメッセージ -----
print( "じゃんけんをしましょう.コンピュータはランダムに手を決めます: " )

# ----- じゃんけんゲーム -----
hand = int()
judge = int()
while True:
    computer_hand = janken_random_hand()
    print( "あなたの手は何ですか" )
    your_hand = input( "(グーならg, チョキならc, パーならp,終了ならXを入力してください)>>> " )
    print( "コンピュータの手は", computer_hand, "でした" )
    if your_hand == "g":
        judge = janken_judge( 0, computer_hand )
    elif your_hand == "c":
        judge = janken_judge( 1, computer_hand )
    elif your_hand == "p":
        judge = janken_judge( 2, computer_hand )
    elif your_hand == "X":
        break
    
    print( judge_str( judge ) )
# ==============================================================================

拡張問題(4)

  • 演習(4)

    拡張課題(4a)

    【関数定義】

    [じゃんけん]対戦ゲーム

    EP_02_004a_janken_game.py

  • 解答は基本的に,下記の二つの関数です.

   1 import random
   2 
   3 def janken_random_hand():
   4     return( random.randint( 0, 2 ) )

   1 def janken_judge( x, y ):
   2     return( ((y-x)+3)%3 )

   1 #!/usr/bin/env python
   2 # -*- coding: utf-8 -*-
   3 # ==============================================================================
   4 # * Copyright (c) 2018 IIJIMA, Tadashi
   5 # *       (IIJIMA Laboratory, Dept. of Science and Technology, Keio University).
   6 # ==============================================================================
   7 # ソフトウェア工学[02] 拡張課題[02]-(000a)  EP_02_000a_.py
   8 # EP(Extended Problem) 02-000a: 
   9 #        2018-10-03 飯島 正 (iijima@ae.keio.ac.jp)
  10 # ==============================================================================
  11 # ----- 乱数を取り扱うrandomモジュールをインポートする -----
  12 import random
  13 # ==============================================================================
  14 # ===== 【関数定義】 乱数で「じゃんけんの手」を求める関数 (0,1,2の整数で返す) =====
  15 def janken_random_hand():
  16     return( random.randint( 0, 2 ) )
  17 # ==============================================================================
  18 # ===== 【関数定義】 「じゃんけんの勝敗」を0,1,2の整数で返す関数 =====
  19 def janken_judge( x, y ):
  20     return( ((y-x)+3)%3 )
  21 # ==============================================================================
  22 # ===== 【関数定義】 「じゃんけんの勝敗」の文字列を,対応する整数0,1,2から返す関数 =====
  23 #     リスト(配列)もしくは辞書を使うともっと簡潔にできる → 後日
  24 def judge_str( x ):
  25     if x == 0:
  26         return( "あいこ" )
  27     elif x == 1:
  28         return( "勝ち" )
  29     else:
  30         return( "負け" )
  31 # ==============================================================================
  32 # ===== 【メイン・プログラム】  =====
  33 # ----- オープニングメッセージ -----
  34 print( "じゃんけんをしましょう.コンピュータはランダムに手を決めます: " )
  35 
  36 # ----- じゃんけんゲーム -----
  37 hand = int()
  38 judge = int()
  39 while True:
  40     computer_hand = janken_random_hand()
  41     print( "あなたの手は何ですか" )
  42     your_hand = input( "(グーならg, チョキならc, パーならp,終了ならXを入力してください)>>> " )
  43     print( "コンピュータの手は", computer_hand, "でした" )
  44     if your_hand == "g":
  45         judge = janken_judge( 0, computer_hand )
  46     elif your_hand == "c":
  47         judge = janken_judge( 1, computer_hand )
  48     elif your_hand == "p":
  49         judge = janken_judge( 2, computer_hand )
  50     elif your_hand == "X":
  51         break
  52     
  53     print( judge_str( judge ) )
  54 # ==============================================================================


SoftEng: Python/Prog/Practice/Extended/02/EP_004a (last edited 2018-10-28 15:20:52 by TadashiIijima)