Staredit Network > Forums > Technology & Computers > Topic: Card Simulation
Card Simulation
Apr 12 2010, 6:02 pm
By: CecilSunkure  

Apr 12 2010, 6:02 pm CecilSunkure Post #1



Here is my blog containing all my other coding project logs: http://cecilsunkure.blogspot.com/2010/04/card-simulation.html

So I just made a .py module that simulates a deck of cards!!

Here is the code:
Code
ACES_HIGH = True

class card:
   suits = ["Clubs",
            "Diamonds",
            "Hearts",
            "Spades" ]
   ranks = ["Ace",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10",
            "Jack",
            "Queen",
            "King" ]

   def __init__(self, suit=0, rank=0):
       self.suit = suit
       self.rank = rank

   def __str__(self):
       return (self.ranks[self.rank] + " of " + self.suits[self.suit])

   def __cmp__(self, other):
       if self.suit > other.suit:
           return 1
       if self.suit < other.suit:
           return -1

       if ACES_HIGH == True:
           m = 13
           if m > other.rank:
               return 1
           if m < other.rank:
               return -1

       if ACES_HIGH == False:
           if self.rank > other.rank:
               return 1
           if self.rank < other.rank:
               return -1
       return 0

class deck:
   def __init__(self):
       self.cards = []
       for suit in range(4):
           for rank in range(12):
               self.cards.append(card(suit, rank))

   def __str__(self):
       s = ''
       for i in range(len(self.cards)):
           s = s + str(self.cards[i]) + '\n'
       return s
           

   def shuffle(self):
       import random
       cards_length = len(self.cards)
       for i in range(cards_length):
           j = random.randrange(i, cards_length)
           self.cards[i], self.cards[j] = self.cards[j], self.cards[i]

   def remove(self, card):
       if card in self.cards:
           self.cards.remove(card)
           return True
       else:
           return False

   def draw(self):
       return self.cards.pop()

   def is_empty(self):
       return (len(self.cards) == 0)


new_Deck = deck()
new_Deck.shuffle()
print new_Deck


The last three lines of code can be deleted, but they use a couple class attributes to create a deck, shuffle it, then print it's contents.

So I posted this to get some suggestions; what sort of card game should I try to make first? I can use a graphical interface. Although, I would prefer the game to be playable in single player, and I can code the Ai if need be. I would also prefer the first game card game to be on the simple side. What do you think?

Post has been edited 1 time(s), last time on Apr 15 2010, 11:19 pm by CecilSunkure.



None.

Apr 12 2010, 7:09 pm Vrael Post #2



Seven card stud.



None.

Apr 12 2010, 7:12 pm MadZombie Post #3



It HAS to be a regular TCG? like MTG? Make it a battle card game instead of a trading card game. Their is a diffrence you know :O!. It's like playing with all planes walkers instead of regular MTG. Something like a deck of cards is your army and you have randomly generated field that you place the cards on so that it would play like risk ecept with card armies that move along killing shit so it would be like a RTS/BCG :3

Or just make a generic card game that revolves around SEN ;3

or poker :C



None.

Apr 12 2010, 7:52 pm NudeRaider Post #4

We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch

For starters I recommend Mau Mau. It has very few rules but is still entertaining. It served as a model for UNO.

Another simple game would be Blitz. It plays kinda like Black Jack.




Apr 12 2010, 8:09 pm CecilSunkure Post #5



I think I'll try making Mau Mau. The Ai should be pretty easy to code. Thanks Nude.

For now I'll not waste time making a graphical interface, I might do that for the next game :P



None.

Apr 12 2010, 8:12 pm Aristocrat Post #6



Pick a game that has already been solved, e.g. games that can be represented exactly by Markov chains. AIs for those tend to be a lot easier to make.

Easiest game that comes to mind: War.

( http://en.wikipedia.org/wiki/War_%28card_game%29 )



None.

Apr 12 2010, 8:14 pm CecilSunkure Post #7



Oh I actually thought about making War. I don't even know what a Markov chain is :O

I'll Wikipedia it.



None.

Apr 12 2010, 8:29 pm NudeRaider Post #8

We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch

I think an AI for Mau Mau should be fairly easy. You just have to check if the computer has a matching card on his hand. If there's multiple, pick any non-7 card so you can counter enemy 7s. And pick 8s if you can chain them with another card on your hand.
Other than that there's not much strategy involved.

I'm sure you can improve the AI when you make it remember all played cards so the probabilites for certain cards can influence its color choice for jacks or something, but that's really not necessary.




Apr 12 2010, 10:26 pm CecilSunkure Post #9



Sounds good. I'll stick with Mau Mau.

Here is a code update. I am unsure if I will do anymore today, depends on how quickly I get my homework done tonight.

Updated Code


Output from Code


Now there is a hand class which is an inherited class from my deck class. I however defined a print function (__str__) for the hand class, as I wanted the printing of a hand to yield a slightly different string than the print function of the original deck class. As you can see by the last 8 lines of code, it is easy to create multiple instances of hands, and if I wanted to I could also create multiple instances of decks quite easily. OOP FTW

Note: The output is a randomized hand, those are not set in stone hands that are dealt. Also, I am actually dealing from the "bottom" of the deck since it is easier to code. I was having trouble using the popleft() method, so I just used pop.

I am also using ACES_HIGH as a constant for determining if the rank of an Ace should be considered 13 or 1.

Post has been edited 1 time(s), last time on Apr 12 2010, 10:32 pm by CecilSunkure.



None.

Apr 13 2010, 2:12 am EzDay281 Post #10



Seems you've already got your answer, but just to throw a link out:
http://www.dvorakgame.co.uk/index.php/Main_Page
may be good for inspiration when it comes to card games.



None.

Apr 13 2010, 10:07 am NudeRaider Post #11

We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch

Can you do color in your text output? Then I'd recommend a much more intuitive display of your hand, possibly even sorted by color:
Colors: Spades, Clubs, Diamonds, Hearts on white background
283810
AKQJ for Aces, Kings, Queens and Jacks.

Maybe replace 10 with 0 so each card only takes only 1 column.




Apr 13 2010, 10:17 pm CecilSunkure Post #12



Code update for the day:

Current Code


Output Code


At the moment, I have no code for taking player turns or simulating Ai turns. I will start work on that tomorrow. Today I added in class Mau Mau and a main function, as well as a loop within the main function for alternating between player and CPU turns, which is just two if statements.

I couldn't figure out color coding, so I just went with the current layout. The "///" would be the deck, and the card next to the deck is the current face-up card.

Progress is going well. Thanks for all the suggestions you guys. I'll post up another update tomorrow.

I should be able to easily allow an option, later on, for multiple computer Ai players by creating an Ai class, and instantiating multiple instances of the Ai class. At the beginning I can let the player choose between like 1-3 Ai players or something. Sound good?

Any suggestions are welcome.



None.

Apr 14 2010, 7:28 pm O)FaRTy1billion[MM] Post #13

👻 👾 👽 💪

http://msdn.microsoft.com/en-us/library/ms686047(VS.85).aspx

Code
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hOut, color);




TinyMap2 - Latest in map compression! ( 7/09/14 - New build! )
EUD Action Enabler - Lightweight EUD/EPD support! (ChaosLauncher/MPQDraft support!)
EUDDB - topic - Help out by adding your EUDs! Or Submit reference files in the References tab!
MapSketch - New image->map generator!
EUDTrig - topic - Quickly and easily convert offsets to EUDs! (extended players supported)
SC2 Map Texture Mask Importer/Exporter - Edit texture placement in an image editor!
\:farty\: This page has been viewed [img]http://farty1billion.dyndns.org/Clicky.php?img.gif[/img] times!

Apr 15 2010, 10:57 pm CecilSunkure Post #14



Here is another code update. I didn't work on this yesterday because I was short on time.

Current Build


Output Sample


As you can see from the output code, I added in a function that checks to see if a move is valid. I input an invalid move rule wise, as in the card in my hand didn't have the same rank as the face up card. The error I received was "Valid move check failed.". After this, I entered in a non-integer, which resulted in a error message telling me my move was not an integer. Finally, I chose to draw a card from the deck. This added another card into my hand.

Since you can draw cards, the only downside to this game is that a person's hand could in theory become really large, making it a bit annoying to scroll up and down.

I expanded the main function, and modified the MauMau class to allow for checking to see if a move is valid.

I also deleted the __cmp__ method of my card class, because it was screwing with my == comparison, and I was a bit unsure of how to use the 'rich' methods. Things were getting over-complicated with this method. I might bring it back if it turns out to be easier to use with <, >, <=, >=, != later on.

Now I need to implement an actual stack of played cards (the face up ones next to the deck). I already have a test file that creates a stack and pops (pop means removing and returning the last value of a list) values that were recently laid into the stack. Here is my test file for the stack, which will be added into the card game simulation sometime soon:
StackTest.py


The stack code is fairly simple and I don't think I really need to explain it. If you want me to I can however.

As always, any comments questions or suggestions are appreciated.

Here is a .exe of my current build: http://download503.mediafire.com/m5ydqyl9zzng/nyfzyamykzi/Cards.7z

Post has been edited 2 time(s), last time on Apr 16 2010, 6:47 pm by CecilSunkure. Reason: Added .exe link.



None.

Apr 16 2010, 12:29 am Apos Post #15

I order you to forgive yourself!

This inspired me in making the same game using java (Since that's what I'm learning.). I currently have 140 lines of code including comments (Without copying the code you made.)

All it can do so far is generate a deck of card, randomize it, give cards to player 1 and 2.
Maybe once it's done, we could compare both code and see how different they are from each other.




Apr 16 2010, 4:43 am CecilSunkure Post #16



Oh awesome, sounds cool.

Java is actually a better language than Python, or so I've heard, so I wish I could transfer my current syntactical knowledge to Java :P

I have 198 lines of code currently. Over the weekend I should be working on the face up stack, and making the barebones of the Ai.

That's strange. I created a deck, shuffled, and handed out cards to two hands with 86 lines. Does it really take that many in Java? Oh, you also have curly brackets, which Python doesn't. Depending on the style that could add like 30% more lines.

http://cecilsunkure.blogspot.com/2010/04/card-simulation.html

Post has been edited 2 time(s), last time on Apr 16 2010, 5:47 am by CecilSunkure.



None.

Apr 17 2010, 2:36 am Apos Post #17

I order you to forgive yourself!

As I said, I wanted to try to do it using Java. Here is what I got so far.

Current Build

Output Sample


Note, since I first made my code in French, I translated it before posting, this is also why some of the variables are written in French.
I tried to make the output as close as possible to CecilSunkure's version.
Also, I removed almost all comments so it takes up less space. (With comments, the code is 295 lines, without them, it's only 225.)




Apr 17 2010, 11:22 pm CecilSunkure Post #18



Here is a code update for the day. I have to spend the rest of my day doing homework, so I don't know what progress will be made throughout the weekend. I may work on this more tomorrow.

CurrentBuild


Output Sample


As you can see, I have an Ai working to play against me. I currently don't have anything to have a player win the game, so there should be an exception thrown during one of the Ai's methods or during the makeMove method when either player has no cards in their hand.

The Ai works by creating a list of cards available to play, then randomly choosing one to play. If none of the cards in the Ai's are playable then it will draw a card from the deck. This reminds me, if the deck empties then there should be a thrown exception as well, as I haven't coded switching the stack with the deck when the deck is empty.

Once I work on this some more, I plan to handle the mentioned exceptions that should be thrown, as well as begin working on creating some unique cards. For now I like the "make next player skip their turn" and "wild card". The wild card would allow the player to switch the current suit to any suit desired.

Here is a .exe in case you wanted to play the game ^^
http://download510.mediafire.com/13dumzc1bodg/awtwokn2wmt/Cards1.7z

Source .py available here:
http://cecilsunkure.blogspot.com/2010/04/card-simulation-continued.html

Currently about 300 lines.

Post has been edited 2 time(s), last time on Apr 18 2010, 12:07 am by CecilSunkure.



None.

Apr 20 2010, 4:15 am CecilSunkure Post #19



Here is my final build (unless bugs are found):

Current Build


There are now two "unique" cards. Playing a seven makes the next player draw a card, and playing an Ace makes the next player lose a turn. The face up stack now sets itself as the deck and shuffles when the deck empties. If a player plays all their cards in their hand they win, and the program exits using sys.exit().

I may in the future add in support for multiple Ai players, but for now I shall not. I want to start on a new and more complicated project, and I don't feel that working on this much more will help me to learn much more.

Currently 333 lines of code.

Here is a link to the .exe file in case you want to play it:
http://www.mediafire.com/?jjymynjngdz

Here is a link to the .py source file:
http://www.mediafire.com/?jnzum2zdrdv

Blogpost:
http://cecilsunkure.blogspot.com/2010/04/card-simulation-complete.html

Hope you enjoy!

Post has been edited 1 time(s), last time on Apr 20 2010, 6:22 pm by CecilSunkure.



None.

Apr 21 2010, 8:55 am NudeRaider Post #20

We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch

Actually, if you want to stay true to Mau Mau rules then here's the unique cards:
7 forces drawing 2 cards but can be countered by another 7 so the one playing the first 7 has to draw 4 cards.
8 skips the next player's turn. (Not as useful with more than 2 players, but still good to annoy your neighbor :P)
Jack is a wildcard and can be played on anything, but it doesn't counter 7 or 8. After playing a Jack you can choose the suit which the next player has to play.

Whenever you can't play a card you have to draw a card, except if you just drew 2/4/6 or 8 due to 7s.




Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[2024-10-10. : 7:46 pm]
Vrael -- NudeRaider
NudeRaider shouted: everyone gets their own message! How thoughtful! :wub:
ill thinkful your message
[2024-10-09. : 7:27 pm]
NudeRaider -- everyone gets their own message! How thoughtful! :wub:
[2024-10-09. : 12:42 pm]
Vrael -- do these guys realize the effort they're putting to advertise to like 15 people?
[2024-10-09. : 12:25 pm]
Zoan -- hard at work for dem minerals
[2024-10-08. : 7:53 pm]
Zoan -- I'm in Georgia; somehow it seems to have been a lot worse for you guys even though you're all more north
[2024-10-08. : 7:52 pm]
Zoan -- Ultraviolet
Ultraviolet shouted: My town right now (a few days ago at this point). It's crazy here, day 12 with no power or running water.
daaaang man
[2024-10-08. : 6:40 pm]
Ultraviolet -- My town right now (a few days ago at this point). It's crazy here, day 12 with no power or running water.
[2024-10-07. : 11:00 pm]
Oh_Man -- https://youtu.be/h6ORd1FA6V8 we saved the gays (this map was exceedingly korean)
[2024-10-07. : 3:18 pm]
Zoan -- Oh_Man
Oh_Man shouted: and then he replaced every time i said free will or the self with "gubble gubble gubble" XD
lool
[2024-10-06. : 3:32 pm]
Vrael -- everyone knows that
Please log in to shout.


Members Online: NudeRaider