私はPythonを書く初心者です。私はユーザに動的な選択肢を作成しようとしています。彼らが望むオプションを選択し、それに基づいて機能を実行します。条件に基づいて変更されるオプションのリストを表示し、ユーザにそれらのオプションの1つを選択させてください
私の「ゲーム」は、5ガロンの樽に4ガロンを入れなければならないダイ・ハードから場面を再現しています。
ユーザーは、何も入っていない2本のボトルと水を利用して始めます。彼らが始めているので、彼らは2つのだけのオプションが必要です。
[1] Fill bottle A
[2] Fill bottle B
Select Option:
コードが正しかった、とオプション1を選択したと仮定し、満たされたボトルAを、彼らが持っている次のオプションは次のようになります。
[1] pour bottle A into bottle B
[2] Fill bottle B
[3] Empty bottle A
Select Option:
ここに私の(おそらく恐ろしい)のコードは、これまでのところです:私は把握に苦労しています何
class Bottles(object):
amount = 0
def __init__(self,name,capacity,amount):
self.name = name
self.capacity = capacity
self.amount = amount
def AddWater(self,increase):
if (self.amount + increase) > self.capacity:
self.amount = self.capacity
print("Overflow! {0}'s at max capacity ({1} gallons)").format(self.name,self.capacity)
else:
self.amount = self.amount + increase
def RemWater(self,decrease):
if (self.amount - decrease) < 0:
self.amount = 0
print("Empty! {0} is now empty!").format(self.name)
else:
self.amount = self.amount - decrease
def ShowOptions():
available_options = []
option_value = 1
print("Bottle A Amount: {0}").format(bottle_a.amount)
print("Bottle B Amount: {0}").format(bottle_b.amount)
print("Your options are as follows:")
if bottle_a.amount != bottle_a.capacity:
print("[{0}] Fill bottle A").format(option_value)
available_options.append(str(option_value))
option_value += 1
if bottle_b.amount != bottle_b.capacity:
print("[{0}] Fill bottle B").format(option_value)
available_options.append(str(option_value))
option_value += 1
if bottle_a.amount != bottle_a.capacity and bottle_b.amount > 0:
print("[{0}] Pour water in Bottle B into Bottle A").format(option_value)
option_value += 1
if bottle_b.amount != bottle_b.capacity and bottle_a.amount != 0:
print("[{0}] Pour water in Bottle A into Bottle B").format(option_value)
option_value += 1
if bottle_a.amount == 4 or bottle_b.amount == 4:
print("{0}] Defuse bomb.").format(option_value)
option_value += 1
bottle_a = Bottles("Bottle A",5,3) # 5 gallon bottle
bottle_b = Bottles("Bottle B",3,0) # 3 gallon bottle
ShowOptions()
は、その選択をお願いして、余分なOPTの全体の束を追加することなく、その機能を実行するために、両方の方法です毎回イオンチェック。
図をあなたは、余分なチェックを回避し、その後構築し、それを使用する必要があるデータの種類:
はここでゲームが上記のアプローチで見ることができる方法の例です。 – cco