1

I am current building a simple card matching game in python, with a 5x4 (row*column) grid, in which two players try to match a deck of twenty cards (2,10 of only suit Hearts) * 2.

The problem I am running into is in iterating through the deck, printing the cards out in a grid fashion so it would look like this:

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

The code I currently have is below:

#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)

Edit: I cleared up the code which I've tried.

The current output is this:

-------  -------  -------  -------  
|     |  |     |  |     |  |     |  
|  7-H|
|  5-H|
|  7-H|
|  9-H|
|     |  |     |  |     |  |     |  
-------  -------  -------  -------  
4
  • What he has tried is the code he posted. What he's missing is a good explanation of the question. Commented Jan 31, 2013 at 22:55
  • Sorry, I've edited it to include the code I've tried. Commented Jan 31, 2013 at 23:18
  • Can you give an example of what your current output is? Commented Feb 1, 2013 at 14:04
  • The code as presented won't produce any output at all. The main function has a dangling mystring =, which is a blatant syntax error. Also, there is a reference to i which isn't defined anywhere. Commented Feb 1, 2013 at 22:46

1 Answer 1

1

the problem is in the def main():

def main():

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

the * 4 just mean that this:

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

will become this:

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

it can also be type as:

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

so. your problem is here:

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

this would print as:

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

you need to put it as something like this:

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

so it would print in one line like how you want it:

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

here is the code that i clean up a little. if it work like it should, it should work:

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)

oh, and like John Y say (copy and paste):

The main function has a dangling mystring =, which is a blatant syntax error

here what i use to test, because the whole code don't work for me, i just tested the print part:

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

that got me:

 -------  -------  -------  ------- 
 |     |  |     |  |     |  |     | 
 | 1-H |  | 2-H |  | 3-H |  | 4-H |  
 |     |  |     |  |     |  |     | 
 -------  -------  -------  ------- 
>>> 
Sign up to request clarification or add additional context in comments.

2 Comments

Looks like OP may have been trying to use the Python 2 trailing comma on the print to suppress the newline, but is actually using Python 3, which doesn't work that way. (And even if he were suppressing the newline, he'd then have to issue another print to actually print the newline.)
well, my print works. just tested the print part because the whole codez don't work for me, see answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.