473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

just need help with a card game

8
I got my code to work be the print out go's down on a angle is there anyway to stop this from happing. P.S sorry for taking up your time


Expand|Select|Wrap|Line Numbers
  1. class Card:
  2.  
  3.     suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
  4.     rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7",
  5.                 "8", "9", "10", "Jack", "Queen", "King"]
  6.  
  7.     def __init__ (self, suit=0, rank=1):
  8.         if suit >= 0 and suit <= 3:
  9.             self.suit = suit
  10.         else:
  11.             self.suit = 0
  12.         if rank >= 1 and rank <= 13:
  13.             self.rank = rank
  14.         else:
  15.             self.rank = 1
  16.  
  17.     def __str__(self):
  18.         return self.rankList[self.rank] + " of " + self.suitList[self.suit]
  19.  
  20.     def __cmp__(self, other):
  21.         if self.rank > other.rank:
  22.             return 1
  23.         if self.rank < other.rank:
  24.             return -1
  25.         if self.suit > other.suit:
  26.             return 1
  27.         if self.suit < other.suit:
  28.             return -1
  29.         return 0
  30.  
  31. class Deck:
  32.  
  33.     def __init__(self):
  34.         self.cards = [] 
  35.         for suit in range(4):
  36.             for rank in range(1,14):
  37.                 self.cards.append(Card(suit, rank))
  38.  
  39.     def __str__(self):
  40.         s = ""
  41.         for i in range(len(self.cards)):        
  42.             s = s + (" " * i) + str(self.cards[i]) + "\n"
  43.         return s
  44.  
  45.     def printDeck(self):
  46.         for card in self.cards:
  47.             print card
  48.  
  49.  
  50. def main():
  51.  
  52.     deck1 = Deck()
  53.     print "Original deck:"
  54.     print deck1
  55.  
  56.  
  57. main()
if some one can help me that will be gr8
May 22 '07 #1
5 1931
codie
8
o sorry and i need to add spaces between them as well :-(
May 22 '07 #2
bvdet
2,851 Expert Mod 2GB
'Deck' method:
Expand|Select|Wrap|Line Numbers
  1.     def __str__(self):
  2.         return '\n'.join([str(self.cards[i]) for i in range(len(self.cards))])
May 22 '07 #3
codie
8
thank you so much i need that help
May 23 '07 #4
bvdet
2,851 Expert Mod 2GB
thank you so much i need that help
You are welcome. I am pleased to help.
May 23 '07 #5
bvdet
2,851 Expert Mod 2GB
Actually, I like this better:
Expand|Select|Wrap|Line Numbers
  1. ....def __iter__(self):
  2.         for card in self.cards:
  3.             yield card
  4.  
  5.     def __str__(self):
  6.         return '\n'.join([str(c) for c in self])
May 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gary Camblin | last post by:
Has anyone got a script to run a higher or lower card game on a web page. Like the game show play you cards right.
7
by: Ken Smith | last post by:
I have a little video poker game I created in Javascript. It uses Tables and inner html stuff - for example: (psudo-code) imagePicked=random card image (2h.gif);...
13
by: lane straatman | last post by:
I'm trying to figure out what data type is appropriate to represent a card in a game. The idea that I thought was going to work was a struct, foo, with two integer fields and two fields of char...
7
by: Gladen Blackshield | last post by:
Hello All! Still very new to PHP and I was wondering about the easiest and simplest way to go about doing something for a project I am working on. I would simply like advice on what I'm asking...
1
by: Olmar | last post by:
Hi, I am trying to write a version of the Hears card game. I can't figue out what is the best way to detect clicks on the cards. Using the cards.dll interface as described in tutorials like...
3
by: noob2008 | last post by:
this code works but theres a problem wif it. wen it generates for 4 players, it has repeated values i.e 2 of diamond appear twice. how do i avoid this? and i hav no idea how to program the...
2
by: DesiShaddy | last post by:
Hi there, I am trying to create card game, that shuffles the cads in deck Draws hands and sorts........I am really struck here.....I get all compile errors with Vector<> Any help would be...
12
by: hollis092 | last post by:
I am writing code for the game of BINGO. The code below shows a function that fills the card with random numbers. A random number in each location, 0-15 for B column; 16-30 for I column, 31-45 for...
32
by: falconsx23 | last post by:
I am making a game called Set, it is a card game: here is a brief description of the rules: The game of Set is played with 81 cards, each of which has 4 properties (number, pattern, color and...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.