2016-12-16 8 views
0

私はカードゲームの戦争をしていますが、10を除いてすべてのカードの値を正確に読み取る問題があります.10を除いてゲームのすべてに負けてしまいますが、ゲームは勝つために1つを選びます。理由は分かりません。私は以下のコードを書かなければなりません、そして、私はそれを一度参照しますが、おそらくそれはコード内の別の場所です。ウォーカードのゲーム値のエラーです。 Python

import random, csv 

global wars 

global counter 


wars = 0 

counter= 0     
def buildDeck():#builds deck 
       suits=[] 
       ranks=[] 
       ranks = [str(x) for x in range(2, 11)] + ['J', 'Q', 'K', 'A'] 
       suits = ['D', 'H', 'S', 'C'] 
       deck = [rank + ' of ' + suit for rank in ranks for suit in suits] 
    random.shuffle(deck) 

     deck = [rank + ' of ' + suit for rank in ranks for suit in suits] 
     random.shuffle(deck) 

     return deck 

def values(card):#gives cards value 
     carddict={'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'J':11, 'Q':12, 'K':13, 'A':14} 
     return (card) 


def Choice(pile): 
    if pile[-1] == "": 
     return random.choice(pile[-2]) 
    if pile[-2] == "": 
     return random.choice(pile[-3]) 
    if pile[-3] == "": 
     return random.choice(pile[-4]) 
    else: 
     return pile[-1] 



def mixdecks(deck1, pileOne, pileTwo):# takes pile mixes them together and adds to bottom of winning deck 
    pileOne.extend(pileTwo) 
    random.shuffle(pileOne) 
    deck1.extend(pileOne) 

def playGame(): 

    deck = buildDeck()#makes deck 

    deckOne = [deck[2 * i + 1] for i in range(26)]#deals deck 
    deckTwo = [deck[2 * i]  for i in range(26)] 

    while len(deckOne) != 0 or len(deckTwo) != 0:#ends when deck runs out 

     global wars 


     global counter 


     if len(deckOne)==0:#wins 
       print('Payer two wins.') 
       print('player one had', len(deckOne), 'player two had', len(deckTwo)) 
       counter +=1 
       break 

     if len(deckTwo)==0: 
       print('Payer one wins.') 
       print('player one had', len(deckOne), 'player two had', len(deckTwo)) 
       counter +=1 
       break 

     pileOne = [deckOne.pop(0)]#plays cards 
     pileTwo = [deckTwo.pop(0)] 
     print('Player one', len(deckOne), 'used', pileOne[0], 'Player two', len(deckTwo), 'used', pileTwo[0]) 

     if values(pileOne[0]) > values(pileTwo[0]):#player one win condition 
       print("Player one wins with", pileOne[0]) 
       mixdecks(deckOne, pileOne, pileTwo) 

     elif values(pileTwo[0]) > values(pileOne[0]):#player two win condition 
       print("Player two wins with", pileTwo[0]) 
       mixdecks(deckTwo, pileOne, pileTwo) 

     elif values(pileTwo[0]) == values(pileOne[0]): 
       #war start 
       war = True 
       print('I Declare War') 

       while war==True: 


         for i in range(3):   

           if deckOne: 
             pileOne.append(deckOne.pop(0)) 
           elif pileOne[-1]: 
             pileOne.append('') 

           if deckTwo: 
             pileTwo.append(deckTwo.pop(0)) 
           elif pileTwo[-1]: 
             pileTwo.append('') 


         print('Player one', len(deckOne), 'used', pileOne, 
           'Player two', len(deckTwo), 'used', pileTwo) 


         if values(Choice(pileOne)) > values(Choice(pileTwo)):#player one win condition 
          print("Player one", len(deckOne), "wins the war with", pileOne) 
          mixdecks(deckOne, pileOne, pileTwo) 
          war = False 


         elif values(Choice(pileTwo)) > values(Choice(pileOne)):#player two win condition 
          print("Player two", len(deckTwo), "wins the war with", pileTwo) 
          mixdecks(deckTwo, pileOne, pileTwo) 
          war = False 


         wars += 1 

x=str(input('Enter any Key To Run ("n" is to quit): ')) # start loop 

if x != "n": #set value to true 

    play = True 

    while play == True: 
     playGame() 

     again=input('Enter any Key To Run again ("n" is quit): ') #asks to go again 

     if again == "n": 

      print("Thank you for playing.") 
      print(" You played", counter, "rounds.") 
      print(" You had", wars, "wars.")  
      play = False 
      x = "n" 

else: # terminates inital go 

    print ("Thank you anyway.") 
    print (counter,wars) 
+0

スタックオーバーフローへようこそ。http://stackoverflow.com/help/how-to-askを読んで質問を修正してください。あなたのコードを投げ捨てて、「なぜこれは機能しないのですか?」と尋ねるだけです。何の反応も得られません。試したこと、エラーメッセージなどを説明する必要があります – Mikkel

答えて

0

この質問は、「私のコードは壊れていますが、修正できますか?」というスタックオーバーフローの目的ではありません。より具体的な質問は、より具体的な回答を得るでしょう。たとえば、「すべてのカードの値を正しく読み込みます...」と言います。正確には、すべてのカードの価値を正確に読み取るものは何ですか?スクリプト全体?特定の機能?さらに、カードゲーム戦争とは何ですか、そしてそれはどのように動作するはずですか?質問の範囲を狭めると、誰かが喜んで助けてくれると確信しています。

0

問題は、誤って数字を文字列として扱っていることです。さらに、values(card)メソッドでは、carddictメソッドを使用していません。代わりにreturn (card)cardという文字列を使用するだけです。

これらの文字列を整数に割り当てるcarddict辞書を使用します。しかし、cardには'10 of H'のような文字列が含まれています。str.split()をリストに入れ、最初のインデックス'10'を取得し、そのキーで辞書を呼び出して、最後にその値を返します。

コードで合計:
変更return (card)からreturn carddict[card.split()[0]]です。

その後、すべてのカードがカードアレイで使い切られたら、私はlist index out of rangeを得て、try/catchIndexErrorで修正することができます。

しかし、これは少なくともあなたのカードの値のインデックスを正しく取得します。

これが役に立ちます。これは楽しい質問だったし、私は戦争ゲームを楽しんだ!乾杯。

関連する問題