1

私は現在、2 人のプレーヤーが 20 枚のカードのデッキ (2,10 のハートのみの 2,10) を一致させようとする、5x4 (行 * 列) グリッドを使用して、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|
|     |  |     |  |     |  |     |  
-------  -------  -------  -------  
4

1 に答える 1

1

問題は def main() にあります:

デフメイン():

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

* 4 は、次のことを意味します。

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

これになります:

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

次のように入力することもできます。

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

それで。あなたの問題はここにあります:

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

これは次のように出力されます。

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

次のように配置する必要があります。

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

そのため、希望どおりに1行で印刷されます。

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

ここに私が少しクリーンアップしたコードがあります。正常に動作する場合は、動作するはずです。

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)

ああ、John Y のように (コピーして貼り付けてください):

メイン関数にはぶら下がっている 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 |  
 |     |  |     |  |     |  |     | 
 -------  -------  -------  ------- 
>>> 
于 2013-02-04T00:29:13.327 に答える