Card game

I am working on a card game in java and I'm going to need my program to have access to, as well as store thousands of unique cards. could anyone point me in a good direction for what I should use to store my cards? any and all help will be appreciated. Thank you.
[278 byte] By [IIIIIIll] at [2007-9-30 7:12:58]
# 1
Vector, ArrayList, Hashtable... there are a lot of options...
luisoft at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 2

Is there any inherent structure to the cards?

Take typical playing cards suitable for Poker. All cards fall into one of four suits. Alternatively, there are 13 cards of each suit. Thus, for some games (such as Concentration), a two-dimensional array can be the optimal way to store the cards. I have seen people resort to a 4-D array as a way of simplifying search and comparison algorithms.

At a bare minumum, use an array with each distinct card having a point value, and those cards sorted appropriately. You will find that, depending on the rules of the game and particular properties of the cards, you may find it more efficient to break it up into multiple arrays, sets, trees, stacks, queues, whatever.

Unless you tell us what the rules of the game are, we can only suggest that you play around with it.

TheDavid at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 3

fair enough. Well, I dont know how many of you are familiar with the game Magic: The Gathering by Wizards of the Coast, but thats the general idea here. there are 7 basic colors (with the inclusion of colorless) that any card can be, then there are types of each color, such as creature or spell or land. I need a way to create "n" decks from a database of several thousand cards. the structure needs to be easy to add cards to. Each player is going to need to be able to run my program and put together at least 60 cards, selected from the structure.

IIIIIIll at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 4

ahh, for that sort of detail I'd recommend having a Card class that holds all the desired info. Color, type, name, image, description, value, and whatever other attributes the cards have so that you can use it in all ways desired. For standard game cards the array or 4d array stuff is fine, but for cards in a game like that, you'll want a class for it as you'll have lots of information to hold for each card.

Malohkan at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 5

i already have a card class that has all the neccisarry information, I'm just saying that I dont want the player to have to input all the information in everytime they want to make a deck. I need some way to store each card somewhere so that the player can say, "yes, I will have 4 Black Lotus cards please"

IIIIIIll at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 6

The easiest, but not necessarily the best way, mathematically speaking, is to simply fetch 60 cards at random from the database, and either push the cards onto a stack or enqueue the cards. In this case, the stack or queue preserves the order that the cards are drawn, and the random selection of primary keys in the database does the same thing as shuffling the cards.

I suggest that if you go this route, you have a tweakable frequency table wherein you can suggest if certain cards are unique, very rare, rare, uncommon, whatever. Every time you randomly fetch a card, check to see if you've exceeded the limits allowed.

So the answer to your question would be to use a stack or a queue. When the player draws a card, he simply pops or dequeues the next available card.

However, you'll see this is not very mathematically sound - the odds of fetching that unique ancient multi-hued dragon are one in N where N is the total number of distinct cards, not the total number of cards period (which could theoretically be infinite).

To get realistic odds, you will have specify somewhere, just how many black swamp land cards are available in the universe. If you're interested in this approach, I'll see if I can whip up a decent algorithm for rebalancing the odds. You'd still probably want to either use an 60 element array (with a bottom of deck marker), a stack or a queue to store the draw pile.

TheDavid at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 7

Considering the information that just came to light as I typed my long-winded post...

You can also simply store the player-requested identifier in the same stack/queue. That is, if the player wants a black lotus and that card is internally recognized as card #83613, then add #83613 to the stack/queue.

TheDavid at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 8
So for the database of cards, should I make something like a text file containing the name of the card and any other information about it? Then i could read the cards into a tree or an array list and create my deck from that?
IIIIIIll at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 9

you could think of a concise way to store each unique bit of info in a string, and have a php database store the Strings for cards. You could write a toString() method in your Card class which would create it, then you could send that to store. Then have a constructor that takes the String for later creation. So if you had an Applet on your game's site, people could create cards using the fields in a normal constructor. Then when they want to save it, your Applet creates the Card based on the field info they submitted, then calls the toString method, and sends that string to your php database for storage and easy access later. Aside from being helpful to have other interested players to help you create a database, it'd also be handy as a quick and simple tool for you to create new Cards.

Malohkan at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 10
So, kind of like a hash-table, only with strings?
IIIIIIll at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 11
yeah sure :)
Malohkan at 2007-7-1 23:30:38 > top of Java-index,Other Topics,Java Game Development...
# 12

So lets pretend I decided to hold the important information about each card in a database, a Microsoft Works Database to be specific. Is there an easy way to access the information without going through the trouble of trying to learn SQL? Is there any kind of tutorial I should be looking at, or am I on my own on this one? once again, any and all help would be much appreciated. thanks

IIIIIIll at 2007-7-1 23:30:39 > top of Java-index,Other Topics,Java Game Development...
# 13

I advise you to bite the bullet anyway and learn SQL.

You can learn the basics of the language itself in about four hours or so and that will be enough to serve you through 85% of the tasks you'll ever do in your lifetime. (The remaining 15%, you can just look up as necessary.) If you ever do web applications, that SQL knowledge will come in handy.

That said, as far as I know, Microsoft Works has a practical limit to the number of cards (in this example), does not permit internet connections to your machine, and cannot be redistributed along with your game. So you will have to switch to a different database at some point in time anyway. You might as well consider doing it now.

I think the first thing you need to do is decide whether people are going to play this game via the net and consequently will get cards from you "live", or you will send them "expansion packs" that they load into the game, or you will send them all the cards at once as part of the game, or they have to create all of their own cards.

Once you decide which goal you want to hit, we can probably suggest appropriate database software (or Java libraries for loading in pre-formatted data).

TheDavid at 2007-7-1 23:30:39 > top of Java-index,Other Topics,Java Game Development...
# 14

I want to send the users all the cards I have available at once, with the game. Then, as more cards become available, send them "booster packs", containing all of then new cards. The players will not ever actually have to "make" a card, but will be able to choose from a searchable list of cards to create a deck.

IIIIIIll at 2007-7-1 23:30:39 > top of Java-index,Other Topics,Java Game Development...
# 15

You don't need a database if you're only going to have a few thousand cards. Those can just be loaded in memory from a large compressed file. Just make sure not to load the images unless they're needed - those take up a lot more space than simple text descriptions.

How are you planning to handle the various spell effects by the way? Do you trust the players to be honest, or will you have some sort of scripting language or Java code that deals with the many possible card abilities and effects?

Matei at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...
# 16
the card its self describes the spell it corrosponds with. there will also be an "official" class that contains all the rules of the game, and makes sure that every player takes their turn, and that cards are played at the correct times.
IIIIIIll at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...
# 17

I think the point of that statement is that you'll want to think of all the different spell types a card can have, and then code in a system that can handle them all, because you can't just type out a spell description and have your game translate from English and run the effect for you.

Malohkan at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...
# 18

yes, I understand that I can't have the spells translated from english. The only problem is there are an unlimited number of possible spells, so I have to decide on how to break down the spells into general types. To combat this problem, I was going to break them into types, creating subclasses of my Card class for each type, which will have methods like, dealDamage(int damage, Player player(or Creature)) and gainLife(int Life, Player player); the actual type of spell will be contained in the same place that the name of the card and its color are stored, in this case, a text file.

IIIIIIll at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...
# 19
That is why Matei suggested a scripting language.If you express the spell effects in a simple parsable format (something close to natural language) it shouldn't be too hard to implement any spell.
Abuse at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...
# 20

Starting to feel overwhelmed? :)

Relax.

One of the more useful tools we developers have is a concept known as Model - Controller - View or MVC. Essentially what that is, is a design template that splits a program up into three components, a data source, the core logic, and the presentation layer. The primary advantage of MVC is that you can completely replace one of the three components without having to recode the other two.

In this example, Microsoft Works would be the initial data source component, and a custom built scripting language would be the eventual data source. You would however, not have to change the GUI, or the various rules for playing the game (and otherwise "using" the program). Alternatively, if you decide to switch from a Swing GUI to an Applet, you should - in theory - only have to change the presentation layer code.

This may be a case where MVC is more appropriate than trying to decide specifically on what database to use for the main storage and what abstract data type to use for the draw pile, right from the start. The disadvantage is that you have to be pretty familiar with programming concepts, enough that you can "appropriately" break up various parts of your program into one of the three components.

I suggest that you try Googling for an introductory tutorial to the MVC design pattern, and see if it makes sense to you. If so, then use that and postpone the various data questions until you're ready for them.

TheDavid at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...
# 21

ok, somewhat off topic, but I want to network this game. I want to style it after the Yahoo games in that there will be an main "server" that you join inorder to meet up with other players. the players then choose a table to sit at. the problem is, I do not know of a way to connect to a computer when the ip address is not hard coded into the coding. any ideas or suggestions?

IIIIIIll at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...
# 22

If you're going for the Yahoo style server, you'll probably have to hard code the server's IP (or a location it can retrieve it from). For the actual tables, you could have the creating player host it and simply pass that player's IP to the other player who joins the table; all the server would have to do is keep a list of created tables and IP addresses, and maybe some describing text if you want to get fancy ^_^.

For simple networking (or at least my style of networking, which may or may not be correct), I suggest looking into the Socket, ServerSocket, ObjectInputStream, and ObjectOutputStream classes.

-Rejun

Rejun2000 at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...
# 23

Magic can be a very confusing thing to create scripting for. Since all cards can be destroyed, countered, dealt damage to, redirected damage to, given modifiers, given additional cards, given special abilities, etc. - thats just the tip of it... Hope you can pull it off.

What I would do in terms of making a deck would have a card search function. Type in the name and the code checks the database for cards with the same name. Search for a "swamp" and the database should return its Swamp cards, which you can add to your deck.

Daisuke_J at 2007-7-1 23:30:41 > top of Java-index,Other Topics,Java Game Development...