as I am learning the Ruby language, I am getting closer to actual programming. I was thinking of creating a simple card game. My question isn't Ruby oriented, but I do know want to learn how to solve this problem with a genuine OOP approach. In my card game, I want to have four players, using a standard deck with 52 cards, no jokers/wildcards. In the game, I won't use the ace as a dual card, it is always the highest card.
So, the programming problems I wonder about are the following:
How can I sort/randomize the deck of cards? There are four types, each having 13 values. Eventually there can be only unique values, so picking random values could generate duplicates.
How can I implement a simple AI? As there are tons of card games, someone would have figured this part out already, so references would be great.
I am a true Ruby nuby, and my goal here is to learn to solve problems, so pseudo code would be great, just to understand how to solve the problem programmatically. I apologize for my grammar and writing style if it's unclear, for it is not my native language.
Also, pointers to sites where such challenges are explained would be a great resource!
Thank you for your comments, answers and feedback!
Something to get you started
You can ensure unique cards very easily by using numbers from 0 to 51.
The
Array#shuffle
method is based off the Knuth-Fisher-Yates shuffle algorithm. http://en.wikipedia.org/wiki/Fisher–Yates_shuffletest
output
Don't bother looking for an AI package
You will learn more and get greater satisfaction by coding the "AI" yourself.
Start simple, just consider:
once you have that working, you can get develop more sophisticated strategies:
then if you want to get really sophisticated, you can start looking into opponent modeling
Macek's answer is good as far as for setting up a deck.
You also asked about other entities.
You probably want four "Players". Each player might be either human or machine controlled.
To implement a human player, you interface with the screen/mouse/keyboard; to implement the machine controlled players, you have a hand and you can see some central cards on a table (all the players need to know of a central table that holds any cards that would be on the table).
From there the logic is based on what game you are playing.
Once your "Player" (AI) gets the turn (for instance, a
takeTurn
method is called on your player object), it should examine its cards and make the proper decisions--taking cards from stacks on the table or placing cards from its hand onto the table. (The table almost certainly has at least two stacks a player can access--"Draw" and "Discard".)When a Human player has his
takeTurn
method called, it should interface with the screen--updating the player's hand, allowing him to draw and discard.When each player is done with his turn, it should return. It can't directly call the next player (otherwise you'd start to build up a stack), so you need some form of turn control that can call the players in order. This centralized control also prevents players from knowing about each other, they shouldn't really need to (one of the best OO design tactics is that each object should know as little about other objects as possible).
...Still thinking... I may add more...
Rather than cramming this all in a comment, I'm adding this as a note for people that might find it useful. Ruby 1.9's native
Array#shuffle!
andArray#shuffle
does in fact use the Knuth-Fisher-Yates shuffle algorithm.ruby-1.9.1-p376/array.c
I'm not sure what sort of card game you want to build but the most common way of building this sort of AI is generating a tree of possible options. I don't think there's a library to do it as such but ruby can do trees easily.
The aim is to have a root node which is the present time and then each child node is a possible action. Then the child of each possible action is the next possible action. From there you can build a tree of every possible outcome. All that remains is to select the outcome you like.
Where you don't have all the information (ie can't see your opponents cards) you simulate it. By simulate I mean guess. The average of all the simulations/guesses will give you a good idea of which tree branches are 'likely to be the best'.
If you can do all that you're well on the way (and it's a really good exercise), there's hundreds of AI articles about, google will be your friend. The only problem with the approach I described is it can be desperately slow but there are many clever techniques to speed it up like transposition tables, alpha-beta pruning etc... which I don't suggest you look up quite yet.
Something very simple to get you started: