so what i have is a list of lists
each individual list is a list of cards
cards1 = [AS, 10H, 3D, ...ect.]
cards2 = [4H, KS, 6S, 9D,....ect]

ColumnList = [cards1, cards2, cards3...etc]
It should print out a spider solitare tableau,
but as you play the game, this columns become different lengths

ex:
tableau:
1 2 3
AS 4H etc etc..
10H KS
3D 6S
9D
i need a way to keep print the tableau every time a card is moved

the tableau has 10 columns total
any help would be appreciated!

Recommended Answers

All 4 Replies

If you are using python >= 2.6, you could use the Tabular class from this post http://www.daniweb.com/code/snippet232375-2.html#post1025743 . Here is an example use

from tabular import Tabular
from random import randint

suits = "CDHS"
cvalues = list("AKQJ") + list(str(i) for i in range(2, 11))
deck = [cv + cs for cs in suits for cv in cvalues]
columns = [list() for i in range(10)]

def main():

    for c in deck:
       columns[randint(0,9)].append(c)
    height = max(len(c) for c in columns)

    table = Tabular(
                [("  %s  " % letter, "^", "{0}") for letter in "0123456789"],
                ([(c[i] if i < len(c) else "") for c in columns] for i in range(height))
            )

    print("{0}".format(table))

if __name__ == "__main__":
    main()

""" my output -->
-------------------------------------------------------------
|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |
-------------------------------------------------------------
| AD  | 4C  | 9D  | JC  | AC  | 2C  | JD  | QC  | KD  | 3D  |
| JS  | 8C  | JH  | 3C  | KC  | 7C  | 5D  | 5C  | QD  | 8D  |
|     | 4D  | 4H  | 6C  | 9S  | KH  | 5H  | 9C  | 6D  | 2H  |
|     | 7D  | KS  | 10C |     | 7H  | 4S  | 2D  | 10D | 6H  |
|     | AH  |     | 10H |     | 6S  | 7S  | 3H  | 8S  | 9H  |
|     | QH  |     | QS  |     | 10S |     | 8H  |     | 2S  |
|     |     |     | 5S  |     |     |     | AS  |     |     |
|     |     |     |     |     |     |     | 3S  |     |     |
-------------------------------------------------------------
"""

thanks a lot!
is there any way to do it without using tabular?

The escape character "\t" for tabs works wonders ;)

The escape character "\t" for tabs works wonders ;)

You mean like this:

import random

suits = "CDHS"
cvalues = list("AKQJ") + list(str(i) for i in range(2, 11))
deck = ([cv + cs for cs in suits for cv in cvalues])
columns =[list() for i in range(10)]

## real dealing with card shuffling and deck cards reducing
## can adapt for situation, where not all cards are delivered at once
random.shuffle(deck)
while deck:
    columns[random.randint(0,9)].append(deck.pop())
                    
def table():
    height = max(len(c) for c in columns)
    print "-"*8*10
    print '|',
    for i in range(10): print " %i\t|" % i,
    print
    print "-" * 8 * 10
    for i in range(height):
        for c in columns:
            if i < len(c): print "| ",c[i],"\t",
            else: print "| \t",
        print '|'
    print "-" * 8 * 10
    print
    
def move(i,j):
    columns[j].append(columns[i].pop())

if __name__ == "__main__":
    table()
    for i in range(2):
        while True:
            a,b=random.randint(0,9),random.randint(0,9)
            if a != b: break
        print "%i to %i" % (a,b)
        move(a,b)
        table()
        
""" Output in run:
>>> 
--------------------------------------------------------------------------------
|  0	|  1	|  2	|  3	|  4	|  5	|  6	|  7	|  8	|  9	|
--------------------------------------------------------------------------------
|  9D 	|  8S 	|  4C 	|  QD 	|  JD 	|  JH 	|  4S 	|  AD 	|  5C 	|  9S 	|
|  7H 	|  AS 	|  7S 	|  6S 	|  4D 	|  5D 	|  7C 	|  9H 	|  KS 	|  3S 	|
|  9C 	|  JC 	|  5S 	|  8D 	|  6D 	|  4H 	|  QS 	|  KD 	| 	|  3H 	|
|  8C 	|  10D 	|  KH 	|  KC 	|  10C 	|  2S 	|  6H 	|  JS 	| 	|  10S 	|
|  7D 	|  3D 	| 	| 	|  AH 	|  5H 	| 	|  QC 	| 	|  AC 	|
| 	| 	| 	| 	| 	|  6C 	| 	|  10H 	| 	|  8H 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  2C 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  QH 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  2D 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  3C 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  2H 	|
--------------------------------------------------------------------------------

9 to 7
--------------------------------------------------------------------------------
|  0	|  1	|  2	|  3	|  4	|  5	|  6	|  7	|  8	|  9	|
--------------------------------------------------------------------------------
|  9D 	|  8S 	|  4C 	|  QD 	|  JD 	|  JH 	|  4S 	|  AD 	|  5C 	|  9S 	|
|  7H 	|  AS 	|  7S 	|  6S 	|  4D 	|  5D 	|  7C 	|  9H 	|  KS 	|  3S 	|
|  9C 	|  JC 	|  5S 	|  8D 	|  6D 	|  4H 	|  QS 	|  KD 	| 	|  3H 	|
|  8C 	|  10D 	|  KH 	|  KC 	|  10C 	|  2S 	|  6H 	|  JS 	| 	|  10S 	|
|  7D 	|  3D 	| 	| 	|  AH 	|  5H 	| 	|  QC 	| 	|  AC 	|
| 	| 	| 	| 	| 	|  6C 	| 	|  10H 	| 	|  8H 	|
| 	| 	| 	| 	| 	| 	| 	|  2H 	| 	|  2C 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  QH 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  2D 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  3C 	|
--------------------------------------------------------------------------------

5 to 2
--------------------------------------------------------------------------------
|  0	|  1	|  2	|  3	|  4	|  5	|  6	|  7	|  8	|  9	|
--------------------------------------------------------------------------------
|  9D 	|  8S 	|  4C 	|  QD 	|  JD 	|  JH 	|  4S 	|  AD 	|  5C 	|  9S 	|
|  7H 	|  AS 	|  7S 	|  6S 	|  4D 	|  5D 	|  7C 	|  9H 	|  KS 	|  3S 	|
|  9C 	|  JC 	|  5S 	|  8D 	|  6D 	|  4H 	|  QS 	|  KD 	| 	|  3H 	|
|  8C 	|  10D 	|  KH 	|  KC 	|  10C 	|  2S 	|  6H 	|  JS 	| 	|  10S 	|
|  7D 	|  3D 	|  6C 	| 	|  AH 	|  5H 	| 	|  QC 	| 	|  AC 	|
| 	| 	| 	| 	| 	| 	| 	|  10H 	| 	|  8H 	|
| 	| 	| 	| 	| 	| 	| 	|  2H 	| 	|  2C 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  QH 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  2D 	|
| 	| 	| 	| 	| 	| 	| 	| 	| 	|  3C 	|
--------------------------------------------------------------------------------
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.