2013-01-31 52 views
1

我目前在python中構建了一個簡單的卡匹配遊戲,有一個5x4(行*列)網格,其中兩個玩家試圖匹配一副20張卡片)* 2。Python上的卡匹配遊戲

我遇到的問題是在通過橋面迭代,打印的卡片出一格的時尚所以它看起來像這樣:

-----  ----- -----  ----- 
- -  - - - -  - - 
4-H  6-H  7-H  8-H 
- -  - - - -  - - 
-----  ----- -----  ----- 

我目前擁有的代碼如下:

#needed import for shuffle function 
from random import shuffle 

#class for my deck 
class Deck: 
    #constructor starts off with no cards 
    def __init__(self): 
     self._deck = [] 

    #populate the deck with every combination of suits and values  
    def Populate(self): 
     #Heart, Diamond, Spades, Clubs 
     for suit in 'HDSC': 
      #Jack = 11, Queen = 12, King = 13, Ace = 14 
      for value in range(2, 15): 
       if value == 11: 
        value = 'J' 
       elif value == 12: 
        value = 'Q' 
       elif value == 13: 
        value = 'K' 
       elif value == 14: 
        value = 'A' 
       #add to deck list 
       self._deck.append(str(value) + '-' + suit) 

    #populate the deck with only hears hearts and all cards except face cards and aces (2, 3, 4, 5, 6, 7, 8, 9, 10) twice 
    def gamePop(self): 
     suit = 'H' 
     for x in range(2): 
      for value in range(2, 11): 
       self._deck.append(str(value) + '-' + suit) 

    #shuffle the deck with the random import    
    def Shuffle(self): 
     shuffle(self._deck) 

    #length of the deck 
    def len(self): 
     return len(self._deck) 

    def stringIt(self): 
     #Returns the string representation of a deck 
     result = '' 
     for c in self._deck: 
      result = result + str(c) + '\n' 
     return result 

#class for a single card 
class Card: 
    #constructor for what type of card it is 
    def __init__(self, value, suit): 
     self._value = value 
     self._suit = suit 
     self._card = self._value + self._suit 

    #print the type of card  
    def Description(self): 
     return (self._card) 

    #overloaded == 
    def __eq__(self, another): 
     if (self._card == another.Description()): 
      return True 
     else: 
      return False 
#main function which plays the game 
def main(): 

    #sets player counters to zero, 
    pOneCount = 0 
    pTwoCount = 0 

    #creates the deck to be put on the board 
    gameDeck = Deck() 
    gameDeck.gamePop() 
    gameDeck.Shuffle() 

    print(gameDeck._deck) 
    currentCard = 0 
    for row in range(5): 
     for card in range(0,4+i): 
      mystring = 
     print ('------- ' * 4) 
     print ('|  | ' * 4) 
     for x in range(4): 
      print ('| ' +gameDeck._deck[currentCard]+'|'), 
      currentCard += 1 
     print ('|  | ' * 4) 
     print ('------- ' * 4) 

編輯:我清理了我試過的代碼。

輸出的電流是這樣的:

------- ------- ------- ------- 
|  | |  | |  | |  | 
| 7-H| 
| 5-H| 
| 7-H| 
| 9-H| 
|  | |  | |  | |  | 
------- ------- ------- ------- 
+0

他試過的是他發佈的代碼。他所缺少的是這個問題的一個很好的解釋。 – Phil

+0

對不起,我編輯過它以包含我試過的代碼。 –

+0

你能舉一個你當前輸出的例子嗎? – jknupp

回答

1

問題是在DEF主():

DEF主():

print ('------- ' * 4) 
    print ('|  | ' * 4) 
    for x in range(4): 
     print ('| ' +gameDeck._deck[currentCard]+'|'), 
     currentCard += 1 
    print ('|  | ' * 4) 
    print ('------- ' * 4) 

的* 4只是意味着這:

print ('------- ' * 4) 

will become此:

print ('------- ' + '------- ' + '------- ' + '------- ') 

它也可以作爲輸入:

print ('------- ------- ------- ------- ') 

如此。你的問題是在這裏:

| 7-H| 
| 5-H| 
| 7-H| 
| 9-H| 

你需要把它當作是這樣的:

 print ('| ' +gameDeck._deck[currentCard]+'|'+'| ' +gameDeck._deck[currentCard+1]+'|'+'| ' +gameDeck._deck[currentCard+2]+'|'+'| ' +gameDeck._deck[currentCard+3]+'|') 

所以它會在同一行打印怎麼樣

for x in range(4): 
     print ('| ' +gameDeck._deck[currentCard]+'|'), 
     currentCard += 1 

,因爲這將打印你想要它:

| 7-H| | 5-H| | 7-H| | 9-H| 

h ere是我清理一下的代碼。如果它喜歡它應該工作,它應該工作:

def main(): 

    #sets player counters to zero, 
    pOneCount = 0 
    pTwoCount = 0 

    #creates the deck to be put on the board 
    gameDeck = Deck() 
    gameDeck.gamePop() 
    gameDeck.Shuffle() 

    print(gameDeck._deck) 
    currentCard = 0 
    for row in range(5): 
     for card in range(0,4+i): 
     print (' ------- ' * 4) 
     print (' |  | ' * 4) 
     print (' | ' +gameDeck._deck[currentCard]+' | '+' | ' +gameDeck._deck[currentCard+1]+' | '+' | ' +gameDeck._deck[currentCard+2]+' | '+' | ' +gameDeck._deck[currentCard+3]+' | ') 
     print (' |  | ' * 4) 
     print (' ------- ' * 4) 

哦,像約翰Ÿ說(複製和粘貼):

主要功能有懸空的MyString =,這是赤裸裸的語法錯誤

這裏我用它來測試,因爲整個代碼不工作對我來說,我只是測試打印部分:

print (' ------- ' * 4) 
print (' |  | ' * 4) 
print (' | ' +"1-H"+' | '+' | ' +"2-H"+' | '+' | ' +"3-H"+' | '+' | ' +"4-H"+' | ') 
print (' |  | ' * 4) 
print (' ------- ' * 4) 

這讓我:

------- ------- ------- ------- 
|  | |  | |  | |  | 
| 1-H | | 2-H | | 3-H | | 4-H | 
|  | |  | |  | |  | 
------- ------- ------- ------- 
>>> 
+0

看起來像OP可能一直試圖在'print'上使用Python 2尾隨的逗號來壓制換行符,但實際上使用的是Python 3,它不會以這種方式工作。 (即使他壓制換行符,他也不得不再發行一個'print'來實際打印換行符。) –

+0

好吧,我的打印作品。只是測試了打印部分,因爲整個codez不適合我,請參閱答案 –