I am implementing a card trick on a website. What I want to do is the user picks 4 cards then based on those cards the user is forced to pick 4 more that are of opposite color. So right now I have the cards displayed on the page but I want to keep track of each card. For example set reds to zero and blacks to ones. So lets say the user picks 4 black cards. Then I want it to display all cards that have the value zero (red cards). Then the user picks. Then it refreshes the cards to display the cards with the value zero and the user picks another card and it does that 2 more times. Is there a way I can assign a value to each card (1 or 0) and then display cards by there value?

Any help would be greatly appreciated.

This is what I have so far for displaying and flipping the cards
thumb1= new Image();
thumb1.src = "75/back-blue-75-3.png";
hover1 = new Image();
hover1.src = "75/clubs-2-75.png";

function imageflip(thumbnailID,imageName)
document.images[thumbnailID].src = eval(imageName + ".src");

<a href="#" onClick="imageflip('icon1','hover1')">
<img src="75/back-blue-75-3.png" border="0" name="icon1"/></a>
7 Years
Discussion Span
Last Post by Airshow

When I wrote a JavaScript card game several years ago, I came to the rapid conclusion that the only realistic way to manage it was to have constructor functions for Game, Pack, Suit, Stack, Card, with (multiple) instances of each.

Internally, each instance:
a) cross-referred to other instances by means of Arrays. For example, an instance of Pack would contain a Suits array containing four instances of Suit.
b) had display method(s), eg. show card at coordinates (x,y)
c) had manipulation method(s), eg. turn card over, move card from one stack to another etc.

A single instance of the Game constructor contained all the "glue" that made the whole thing behave as the intended real life game.

The whole thing was quite extensive but manageable because all the code was contained within well-defined constructors.

Unfortunately, the code now looks rather poor with regard to it's event handling and use of closures. I write my Javascript somewhat differently these days and would not want to encourage anyone to follow my bad habits of yesteryear. Consequently it is best that I don't post any actual code but hopefully my outline above gives you plenty of clues on how to proceed.


Edited by Airshow: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.