前置き
今日は、Pythonを使い、1から10000までの整数を用いて、ユーザーとコンピューターが考えた数の大小を競うゲームを作りたくなった。
プログラムコード
from random import randint
# Define the winning conditions for each difficulty level
EASY_WINS = 3
MEDIUM_WINS = 5
HARD_WINS = 10
# Function to set the difficulty level
def set_difficulty():
level = input("Choose a difficulty. Type 'easy', 'medium', or 'hard': ")
if level == "easy":
return EASY_WINS
elif level == "medium":
return MEDIUM_WINS
elif level == "hard":
return HARD_WINS
else:
print("Invalid choice. Defaulting to easy level.")
return EASY_WINS
def game():
print("Welcome to the Number Guessing Game!")
print("Both you and the computer will choose a number between 1 and 10000.")
winning_condition = set_difficulty()
user_wins = 0
computer_wins = 0
while user_wins < winning_condition and computer_wins < winning_condition:
user_number = int(input("Choose a number between 1 and 10000: "))
if not 1 <= user_number <= 10000:
print("Invalid number. Please choose a number between 1 and 10000.")
continue
computer_number = randint(1, 10000)
print(f"Computer chose: {computer_number}")
if user_number > computer_number:
user_wins += 1
print(f"You win this round! Current score - You: {user_wins}, Computer: {computer_wins}")
elif user_number < computer_number:
computer_wins += 1
print(f"Computer wins this round! Current score - You: {user_wins}, Computer: {computer_wins}")
else:
print("It's a tie! No points awarded. Current score remains the same.")
if user_wins == winning_condition:
print(f"Congratulations! You won the game with {user_wins} wins.")
else:
print(f"Computer wins the game with {computer_wins} wins. Better luck next time!")
# Start the game
game()
プログラムコードの説明
ここでは、set_difficulityという関数を使い、easy levelでは、3勝が勝者、medium levelでは、5勝が勝者、hard levelでは、10勝が勝者と定義している。
公平な試合にするため、randomint関数を用いている。
出力結果 (Easy Level)
Welcome to the Number Guessing Game!
Both you and the computer will choose a number between 1 and 10000.
Choose a difficulty. Type 'easy', 'medium', or 'hard': easy
Choose a number between 1 and 10000: 8500
Computer chose: 1657
You win this round! Current score - You: 1, Computer: 0
Choose a number between 1 and 10000: 2850
Computer chose: 4889
Computer wins this round! Current score - You: 1, Computer: 1
Choose a number between 1 and 10000: 7777
Computer chose: 8956
Computer wins this round! Current score - You: 1, Computer: 2
Choose a number between 1 and 10000: 9999
Computer chose: 5887
You win this round! Current score - You: 2, Computer: 2
Choose a number between 1 and 10000: 8888
Computer chose: 3627
You win this round! Current score - You: 3, Computer: 2
Congratulations! You won the game with 3 wins.
ここでは、私が3-2で勝利した。ついでに、hard levelの時の出力結果も入れておく。
出力結果 (Hard Level)
Welcome to the Number Guessing Game!
Both you and the computer will choose a number between 1 and 10000.
Choose a difficulty. Type 'easy', 'medium', or 'hard': hard
Choose a number between 1 and 10000: 8988
Computer chose: 3329
You win this round! Current score - You: 1, Computer: 0
Choose a number between 1 and 10000: 5678
Computer chose: 1074
You win this round! Current score - You: 2, Computer: 0
Choose a number between 1 and 10000: 3456
Computer chose: 1175
You win this round! Current score - You: 3, Computer: 0
Choose a number between 1 and 10000: 2345
Computer chose: 1145
You win this round! Current score - You: 4, Computer: 0
Choose a number between 1 and 10000: 8214
Computer chose: 4640
You win this round! Current score - You: 5, Computer: 0
Choose a number between 1 and 10000: 1312
Computer chose: 7069
Computer wins this round! Current score - You: 5, Computer: 1
Choose a number between 1 and 10000: 2568
Computer chose: 5811
Computer wins this round! Current score - You: 5, Computer: 2
Choose a number between 1 and 10000: 6754
Computer chose: 8808
Computer wins this round! Current score - You: 5, Computer: 3
Choose a number between 1 and 10000: 7272
Computer chose: 9984
Computer wins this round! Current score - You: 5, Computer: 4
Choose a number between 1 and 10000: 9950
Computer chose: 8623
You win this round! Current score - You: 6, Computer: 4
Choose a number between 1 and 10000: 9682
Computer chose: 8873
You win this round! Current score - You: 7, Computer: 4
Choose a number between 1 and 10000: 3259
Computer chose: 8621
Computer wins this round! Current score - You: 7, Computer: 5
Choose a number between 1 and 10000: 5418
Computer chose: 8646
Computer wins this round! Current score - You: 7, Computer: 6
Choose a number between 1 and 10000: 6710
Computer chose: 1765
You win this round! Current score - You: 8, Computer: 6
Choose a number between 1 and 10000: 8200
Computer chose: 6917
You win this round! Current score - You: 9, Computer: 6
Choose a number between 1 and 10000: 1235
Computer chose: 6916
Computer wins this round! Current score - You: 9, Computer: 7
Choose a number between 1 and 10000: 2341
Computer chose: 7760
Computer wins this round! Current score - You: 9, Computer: 8
Choose a number between 1 and 10000: 1009
Computer chose: 3783
Computer wins this round! Current score - You: 9, Computer: 9
Choose a number between 1 and 10000: 9999
Computer chose: 4173
You win this round! Current score - You: 10, Computer: 9
Congratulations! You won the game with 10 wins.
ここでも、私が10-9で勝利した。しかし、疑問に思った。Deuceを導入するのと、同じ数字を連続して入力するのは禁止にしたら、もっと試合が公平になると感じた。
改良したプログラムコード
from random import randint
# Define the winning conditions for each difficulty level
EASY_WINS = 3
MEDIUM_WINS = 5
HARD_WINS = 10
# Function to set the difficulty level
def set_difficulty():
level = input("Choose a difficulty. Type 'easy', 'medium', or 'hard': ")
if level == "easy":
return EASY_WINS
elif level == "medium":
return MEDIUM_WINS
elif level == "hard":
return HARD_WINS
else:
print("Invalid choice. Defaulting to easy level.")
return EASY_WINS
def game():
print("Welcome to the Number Guessing Game!")
print("Both you and the computer will choose a number between 1 and 10000.")
winning_condition = set_difficulty()
user_wins = 0
computer_wins = 0
last_user_number = None
last_computer_number = None
while user_wins < winning_condition and computer_wins < winning_condition:
while True:
user_number = int(input("Choose a number between 1 and 10000 (not the same as last time): "))
if user_number == last_user_number:
print("You cannot choose the same number as last time. Please try again.")
elif not 1 <= user_number <= 10000:
print("Invalid number. Please choose a number between 1 and 10000.")
else:
break
while True:
computer_number = randint(1, 10000)
if computer_number != last_computer_number:
break
print(f"Computer chose: {computer_number}")
if user_number > computer_number:
user_wins += 1
print(f"You win this round! Current score - You: {user_wins}, Computer: {computer_wins}")
elif user_number < computer_number:
computer_wins += 1
print(f"Computer wins this round! Current score - You: {user_wins}, Computer: {computer_wins}")
else:
print("It's a tie! No points awarded. Current score remains the same.")
last_user_number = user_number
last_computer_number = computer_number
if user_wins == winning_condition:
print(f"Congratulations! You won the game with {user_wins} wins.")
else:
print(f"Computer wins the game with {computer_wins} wins. Better luck next time!")
# Start the game
game()
元のプログラムコードと改良したプログラムコードの違い
元のプログラムコードと改良したプログラムコードでは、引き分けの処理方法と、ゲームの終了条件が異なっている。元のコードでは、ユーザーまたはコンピュータが指定された勝利数に達した時点でゲームが終了する。しかし、改良したコードでは、ユーザーまたはコンピュータが指定された勝利数に達しているかつ、2ポイント以上の差がついている場合にゲームが終了します。これにより、4-4のような同点の状況(deuce)でも、勝者が決まるまでゲームが続行されます。
改良したプログラムコードを使用した実際のゲームプレイ
Welcome to the Number Guessing Game!
Both you and the computer will choose a number between 1 and 10000.
Choose a difficulty. Type 'easy', 'medium', or 'hard': medium
Choose a number between 1 and 10000 (not the same as last time): 4305
Computer chose: 1577
You win this round! Current score - You: 1, Computer: 0
Choose a number between 1 and 10000 (not the same as last time): 0814
Computer chose: 6005
Computer wins this round! Current score - You: 1, Computer: 1
Choose a number between 1 and 10000 (not the same as last time): 1024
Computer chose: 200
You win this round! Current score - You: 2, Computer: 1
Choose a number between 1 and 10000 (not the same as last time): 2553
Computer chose: 8412
Computer wins this round! Current score - You: 2, Computer: 2
Choose a number between 1 and 10000 (not the same as last time): 5809
Computer chose: 1781
You win this round! Current score - You: 3, Computer: 2
Choose a number between 1 and 10000 (not the same as last time): 3056
Computer chose: 200
You win this round! Current score - You: 4, Computer: 2
Choose a number between 1 and 10000 (not the same as last time): 111
Computer chose: 2380
Computer wins this round! Current score - You: 4, Computer: 3
Choose a number between 1 and 10000 (not the same as last time): 143
Computer chose: 5481
Computer wins this round! Current score - You: 4, Computer: 4
Choose a number between 1 and 10000 (not the same as last time): 8403
Computer chose: 6155
You win this round! Current score - You: 5, Computer: 4
Choose a number between 1 and 10000 (not the same as last time): 1253
Computer chose: 5994
Computer wins this round! Current score - You: 5, Computer: 5
Choose a number between 1 and 10000 (not the same as last time): 7845
Computer chose: 2486
You win this round! Current score - You: 6, Computer: 5
Choose a number between 1 and 10000 (not the same as last time): 1891
Computer chose: 7964
Computer wins this round! Current score - You: 6, Computer: 6
Choose a number between 1 and 10000 (not the same as last time): 8037
Computer chose: 1512
You win this round! Current score - You: 7, Computer: 6
Choose a number between 1 and 10000 (not the same as last time): 2048
Computer chose: 6350
Computer wins this round! Current score - You: 7, Computer: 7
Choose a number between 1 and 10000 (not the same as last time): 9146
Computer chose: 2091
You win this round! Current score - You: 8, Computer: 7
Choose a number between 1 and 10000 (not the same as last time): 4301
Computer chose: 5298
Computer wins this round! Current score - You: 8, Computer: 8
Choose a number between 1 and 10000 (not the same as last time): 8569
Computer chose: 2797
You win this round! Current score - You: 9, Computer: 8
Choose a number between 1 and 10000 (not the same as last time): 3410
Computer chose: 6381
Computer wins this round! Current score - You: 9, Computer: 9
Choose a number between 1 and 10000 (not the same as last time): 7296
Computer chose: 7395
Computer wins this round! Current score - You: 9, Computer: 10
Choose a number between 1 and 10000 (not the same as last time): 9999
Computer chose: 6253
You win this round! Current score - You: 10, Computer: 10
Choose a number between 1 and 10000 (not the same as last time): 6707
Computer chose: 4440
You win this round! Current score - You: 11, Computer: 10
Choose a number between 1 and 10000 (not the same as last time): 3263
Computer chose: 5412
Computer wins this round! Current score - You: 11, Computer: 11
Choose a number between 1 and 10000 (not the same as last time): 8907
Computer chose: 5617
You win this round! Current score - You: 12, Computer: 11
Choose a number between 1 and 10000 (not the same as last time): 2431
Computer chose: 821
You win this round! Current score - You: 13, Computer: 11
Congratulations! You won the game with 13 wins.
ここでは、私は"Medium" Levelを選択したが、スコアが4-4になったため、2点差がつくまで勝負が決さないDeuceに突入した。そして、私は13-11で見事勝利した。
終わりに
今回は、範囲を1から10000に絞り、レベルもEasy,Medium, Hardの3つとした。このプログラムコードを参考にしながら、皆さんも時間がある時に、コンピュータと、"Number Guessing Game"を遊んでみてはいかがでしょうか。