私はブラックジャックゲームを構築してフラスコを学んでいます。フラスコ - オブジェクトインスタンスを維持する
ゲームロジックを含むgame.py
モジュールを作成しました。その中で、私は、次のようなディーラーの靴を定義します(これは抜粋です)
class Blackjack(object):
def __init__(self):
pass
NO_OF_DECKS = 1
SUITS = ['clubs', 'spades', 'diamonds', 'hearts']
RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
VALUES = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10}
class Shoe(Blackjack):
""" Creates a new shoe object from which cards are dealt
to the player and the dealer.
Input arguments:
decks :: the number of decks the player wishes to play with.
"""
def __init__(self, decks=1):
self.cards_in_shoe = {}
self.total_cards = decks * 52
for i in Blackjack.SUITS:
self.cards_in_shoe[i] = Blackjack.RANKS * decks
def get_random_card(self):
random_suit = random.choice(self.cards_in_shoe.keys())
cards_in_suit = self.cards_in_shoe[random_suit]
random_card = random.choice(cards_in_suit)
cards_in_suit.remove(random_card)
# deleting suit if cards exhausted.
if len(self.cards_in_shoe[random_suit]) == 0:
del self.cards_in_shoe[random_suit]
return (random_suit, random_card)
def remaining_cards_in_shoe(self):
""" Returns the total number of cards remaining to be
drawn for both player and dealer.
"""
return sum(len(v) for v in self.cards_in_shoe.itervalues())
さて、私の「trygame.html」テンプレートで、私はremaining_cards_in_shoe
プロパティで、新しいカードを対処できるようにしたいと思います新しいカードが発行されるたびに減少します。残念ながら、これは現在機能していません。 1つのデッキでは、カウンターは51のままです(最初のカードが処理された後)。
@app.route('/trygame', methods=['GET', 'POST'])
def trygame():
shoe = game.Shoe()
card = shoe.get_random_card()
remaining_cards = shoe.remaining_cards_in_shoe()
return render_template('trygame.html',
card=card,
remaining_cards=remaining_cards)
は今、私は問題が常に靴のオブジェクトを再初期化が付属してい疑う:
は、ここに私の現在の@app.route('/trygame')
図です。私は問題がある一定の100%ではないんだと、私は、この時点で進行するかどうかはわからないよ
<p>Here we will insert a card: {{ card }}</p>
<p>Remaining cards in shoe: {{ remaining_cards }}</p>
<form action="/trygame" method="post">
<button type="submit" class="btn btn-primary">Deal card</button>
</form>
:テンプレート上のボタンは、単にとして定義されます。私はセッションを作成しようとしましたが、メソッドを使用できるように、クラスインスタンスを格納する方法がわかりません。さらに、最初にこれを行うことが賢明であるかどうかはわかりません。私は、ユーザーがカード辞書の内容を見ることができないようにしたいと思います。
@app.route('/trygame', methods=['GET', 'POST'])
def trygame():
# I believe this is the way to go, but it won't work
print session
if request.method == 'POST':
card = session['shoe'].get_random_card()
remaining_cards= session['shoe'].remaining_cards_in_shoe()
return render_template('trygame.html',
card=card,
remaining_cards=remaining_cards)
session['shoe'] = game.Shoe()
card = session['shoe'].get_random_card()
remaining_cards = session['shoe'].remaining_cards_in_shoe()
return render_template('trygame.html',
card=card,
remaining_cards=remaining_cards)
をしかし、それは働いていない:
私はこのような何かを行うことができ期待していました。 問題はなんですか? 私のオプションは何ですか? current_app
,session
,redis
? このシナリオでは、どのようなベストプラクティスがあり、実際の例を提示できますか?
これは私の問題を解決するものではありません。私はそれを見る方法で、メソッド呼び出しを実行する可能性を含めてオブジェクトを格納する必要があります。私の現在の問題は 'cards_remaining_in_shoe()'がリセットされていることです。 – mmenschig
それとも、私はまだそれを完全に理解していません。リソースをありがとう、私はいくつかの研究を行います。おそらく、レディスによるサーバー側のセッションが必要になるでしょう。また、HTTP – mmenschig
で読み上げます。Pythonでクラス内でリセットを取得している場合は、[Python Garbage Collecting。](https://docs.python.org/3/library/gc)を参照してください。 .html)Pythonのガベージコレクションは、あまりにも多くのストレージが使用されていると思うときはいつでも自動的に収集します。これはあなたの問題の原因かもしれません。 –