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

数字当てゲーム(Python)+独自ルール

Posted at

Pythonで数字当てゲームを作ってみようかという遊びです。
数字当てだけでは、面白くないので、コンピュータと競わせるように
独自ルールを作りました。

  • 1〜9の間の数字を入力
  • 1度使った数字は、使えない
  • コンピュータと同じ数字を使った場合は、スコアーに加算されない
  • コンピュータと違う数字を使った場合は、スコアーに加算される

技術解説

import random

# 1-9の数字を用意
raw_list = [1, 2, 3, 4, 5, 6, 7, 8 , 9]
score = 0

# ゲームスタート
print('Please enter')

while True:
  print(raw_list)

  # PCの出す手を用意
  pc_card_idx = random.randrange(len(raw_list))
  pc_card = raw_list[pc_card_idx]

  # 入力待ち
  while True:
    s = input('>> ')
    if str.isdecimal(s):
      your_card = int(s)
      if your_card in raw_list:
        your_card_idx = raw_list.index(your_card)
        break

  # 判定
  if your_card == pc_card:
    raw_list.remove(pc_card)
  else:
    raw_list.remove(pc_card)
    raw_list.remove(your_card)
    score += your_card

 # 手札を見せ合う
  print('your card: {0}. pc card: {1}. Now Score: {2}'.format(your_card, pc_card, score))


  if len(raw_list) <= 1:
    break

print('Your Score Is {}'.format(score))

さいごに

次回は、数字を学習するAIを作成してみたいです。

1
0
2

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
1
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?