2017-01-20 8 views
-1

私はFarkle(ダイスゲーム)のための簡単なスコアリングシステムを作った。私はPythonの初めの生徒で、特定のことをする方法についていくつかの質問があります。まず第一に、どのようにして一致するシーケンスがあるかについて、私のコードが自分のリスト(ダイスグループ)をチェックする方法を見つけるのが難しいです。私はそれをチェックしてみたいと思います。具体的には、私が入っていなくても、手動で各番号の番号があるときに何が起こるかを設定することなく、4種類あります。これを行う最も簡単な方法は何ですか?4種類のダイスゲーム

コードに関する一般的なご意見もありがとうございます。種類の4をチェックする

from __future__ import print_function 
from collections import Counter 
import random 


onescore = 0 
twoscore = 0 
threescore = 0 
fourscore = 0 
fivescore = 0 
fivesinglesscore = 0 
sixscore = 0 
fourofakind = 0 
scorelist = [] 

##ROLLING THE DICE## 
def roll(): 
    dice = (1,2,3,4,5,6) 
    dice1 = random.choice(dice) 
    dice2 = random.choice(dice) 
    dice3 = random.choice(dice) 
    dice4 = random.choice(dice) 
    dice5 = random.choice(dice) 
    dice6 = random.choice(dice) 

    global onescore,twoscore,threescore,fourscore,fivescore,fivesinglesscore,sixscore,fourofakind 
    onescore = 0 
    twoscore = 0 
    threescore = 0 
    fourscore = 0 
    fivescore = 0 
    fivesinglesscore = 0 
    sixscore = 0 
    fourofakind = 0 

    dicegroup = [dice1,dice2,dice3,dice4,dice5,dice6] 
    print ('Your rolls are',dicegroup) 

    dicenum = Counter(dicegroup) 
    onescore = dicenum[1] * 100 
    print ('There are',dicenum[1],'ones. This gives you', onescore, 'points.') 

    fivesinglesscore = dicenum[5] * 50 
    print ('There are',dicenum[5],'fives. This gives you', fivesinglesscore, 'points.') 

    if dicenum[2] == 3: 
     twoscore = 200 
     print ('There are',dicenum[2],'twos. This gives you', twoscore, 'points.') 
    if dicenum[3] == 3: 
     threescore = 300 
     print ('There are',dicenum[3],'threes. This gives you', threescore, 'points.') 
    if dicenum[4] == 3: 
     fourscore = 400 
     print ('There are',dicenum[4],'fours. This gives you', fourscore, 'points.') 
    if dicenum[5] == 3: 
     fivescore = 500 
     print ('There are',dicenum[5],'fives. This gives you', fivescore, 'points.') 
    if dicenum[6] == 3: 
     sixscore = 600 
     print ('There are',dicenum[6],'sixes. This gives you', sixscore, 'points.') 
    if dicenum[any] == 4: 
     fourofakind = 1000 
     print ('You have four of a kind. This is worth', fourofakind, 'points.') 

roll() 

##SCORE PROCESSING 
finalscore = onescore + twoscore + threescore + fivescore + fivesinglesscore + sixscore + fourofakind 
print (finalscore) 
scorelist.append(finalscore) 
print ('') 
print ('Score list is:') 
print (scorelist) 
+0

StackOverflowは、コードレビューサイトではなく、Q&Aサイトです。具体的な回答がある特定の質問をする必要があります。コードレビューを求める他のフォーラムがあります(たとえばreddit.com/r/learnprogramming) – Metropolis

+0

@ Metropolis Fair十分です。私はサイトの目的を少し誤解していました(しかし、私は質問しましたが、それにもかかわらず関連していると思います)。私に知らせてくれてありがとう。 – Andrew

答えて

0

:今

from collections import Counter 

x = [1,2,3,4,5,6,76,5,234,123,4,123,312,3,5,245,123,123,1,51,35,123,123,1,23,1,1,1,1,1,12,32,31,23] 

print(Counter(x)) 
#Counter({1: 8, 123: 6, 5: 3, 3: 2, 4: 2, 23: 2, 32: 1, 2: 1, 6: 1, 31: 1, 12: 1, 234: 1, 76: 1, 51: 1, 245: 1, 312: 1, 35: 1}) 

Counterを使用してcollectionsモジュールから、私たちは、リスト中の私たちのためにすべてのユニークなアイテムをカウントした辞書を作成することができます私たちがしなければならないのはフィルタリングだけですが、多くはx of a kindしています。私たちは、今、私たちはそのすべての項目のリストを取り戻すのすべての値をフィルタリングリストの内包表記を作成することによって、これを達成し、カウントが4

from collections import Counter 

x = [1,2,3,4,5,6,76,5,234,123,4,123,312,3,5,245,123,123,1,51,35,123,123,5,1,23,1,1,1,1,1,12,32,31,23] 

answer = [a for a,b in Counter(x).items() if b == 4] 
print(answer) 
#[5] 

と等しい場合にのみ、この場合には、我々が望むものを保つことができますこれは、リストに一列に4をチェックします

x = [1,2,3,4,5,6,76,5,234,123,4,123,312,3,5,245,123,123,1,51,35,123,123,1,23,1,1,1,1,1,12,32,31,23] 

for i in range(len(x)): 
    to_check = x[i:i+4] 
    if to_check.count(to_check[0]) == 4: 
     print('found it {0}, position {1} to position {2}'.format(to_check[0], i, i+4)) 

:行の4をチェックする4.


の数を持っています。一度にリストからサイズ4のチャンクを取り出し、そのリスト内のすべてが同じであるかどうかをチェックします。そうであれば、アイテムと位置を出力します。プログラムに合わせて変更することができます。

count()はリスト上で動作し、リストに数えたいものを渡します。このケースでは、スライスされたリストの最初の項目をカウントしたいので、カウントが4であるかどうかをチェックします。

注:これは、[1,2,2,2,2,2]を2,4として扱います。 2番目のセットと2の2番目のセットであるため、行項目に表示されます。あなただけの行の4の1セットとしてそれを扱いたい場合は、次のようにコードを変更することができます

x = [1,2,3,4,5,6,76,5,234,123,4,123,312,3,5,245,123,123,1,51,35,123,123,1,23,1,1,1,1,1,12,32,31,23] 

i = 0 
while i < len(x): 
    to_check = x[i:i+4] 
    if to_check.count(to_check[0]) == 4: 
     print('found it {0}, position {1} to position {2}'.format(to_check[0], i, i+4)) 
     i += 4 
    else: 
     i += 1 

while loopたちはfor loopよりiより良いの流れを制御することができます使用します。 4を連続して見つけると、4の最後までスキップします。