Stumped with the flip of a card

Bluevoss
Okay, imagine there is a card game where you only have one suit of cards (so there are 13 of them). We will represent them with a simple number, so ace is 1, and so on, up to the king, 13.

I need to be able to have a function that will be able to pull a card and return it as the integer of what you pulled. Yet I can't pull the same card twice.

The game mechanics won't let this run more than 13 times (it is only for tension in the game, I don't want it to always be the same, etc. I just need some way of not getting the same card.

I tried to do it recursively using the code below but couldn't get it to work. All sorts of problems getting the output to print right.

Couple of points - this test was only with four cards (I wanted to get the basics working before replication) and two, I'm using integers as booleans because I don't know how to test a "not" value - anyone know that one? Here, card1, card2 etc refer to flagging a drawn card, 0=not pulled, 1=pulled.

@set card1=0
@set card2=0
@set card3=0
@set card4=0
@set print

[[four card montey]]

[[four card montey]]:

var rnd = Math.ceil(Math.random() * 4);
squiffy.set("rnd",rnd)

@set print=1

[[Draw a card]]

[[Draw a card]]:

if (squiffy.get("rnd")==1){
if (squiffy.get("card1")==0){
squiffy.set("card1",1);
squiffy.set("print",1);
}
else{
squiffy.set("rnd",2);
squiffy.set("print",0);
squiffy.story.go("Draw a card");
}
}

if (squiffy.get("rnd")==2){
if (squiffy.get("card2")==0){
squiffy.set("card2",1);
squiffy.set("print",1);
}
else{
squiffy.set("rnd",3);
squiffy.set("print",0);
squiffy.story.go("Draw a card");
}
}

if (squiffy.get("rnd")==3){
if (squiffy.get("card3")==0){
squiffy.set("card3",1);
squiffy.set("print",1);
}
else{
squiffy.set("rnd",4);
squiffy.set("print",0);
squiffy.story.go("Draw a card");
}
}

if (squiffy.get("rnd")==4){
if (squiffy.get("card4")==0){
squiffy.set("card4",1);
squiffy.set("print",1);
}
else{
squiffy.set("rnd",1);
squiffy.set("print",0);
squiffy.story.go("Draw a card");
}
}

{if print=1:
Your card is {rnd}
[[Draw a card]]
}

Bluevoss
Okay, after a couple of days of pondering this, I got it to work. This is the abbreviated version - four cards. Once it runs, each of the variables (card1, card2, card3...) will have the associated card number in it.

[[setup]]:

squiffy.story.go("deal");

@set card1=0
@set card2=0
@set card3=0
@set card4=0

@set cardNum = 1

[[deal]]:

var rnd = Math.ceil(Math.random() * 4);
squiffy.set("rnd",rnd);

if (squiffy.get("rnd")==1){
if (squiffy.get("card1")==0){
squiffy.set("card1",(squiffy.get("cardNum")));
squiffy.set("cardNum",(squiffy.get("cardNum")+1));
}
}

if (squiffy.get("rnd")==2){
if (squiffy.get("card2")==0){
squiffy.set("card2",(squiffy.get("cardNum")));
squiffy.set("cardNum",(squiffy.get("cardNum")+1));
}
}

if (squiffy.get("rnd")==3){
if (squiffy.get("card3")==0){
squiffy.set("card3",(squiffy.get("cardNum")));
squiffy.set("cardNum",(squiffy.get("cardNum")+1));
}
}

if (squiffy.get("rnd")==4){
if (squiffy.get("card4")==0){
squiffy.set("card4",(squiffy.get("cardNum")));
squiffy.set("cardNum",(squiffy.get("cardNum")+1));
}
}

if (squiffy.get("cardNum")>4){
squiffy.story.go("exit");
}
else{
squiffy.story.go("deal");
}

[[exit]]:

In exit

{card1}
{card2}
{card3}
{card4}

Bluevoss
The odd thing I'll add was the used of squiffy.story.go, which doesn't work as I would have expected. I had the devil of a time getting the recursion to work. When I put the loop-back to "deal" in various ways, it would lock the code up. I think the go command is interpreted in a non-linear fashion - things that should have worked did not.

Once I centralized it in one command at the end of the code, it worked fine.

Bluevoss
So, having done this, I'll now have a card number in a squiffy variable (say I move it into {card}).

Is there some way I can look this up so every time I reference the card, I don't get a number but rather a name (i.e. "Ace of spades", "two of spades", "three of spades"...)

I'm thinking this could have applications in other games (i.e. using this random generator to work through a list of characters). However, I don't want to do a big...

{if card=1:
Ace of spades}
{if card=2:
two of spades}...

Some sort of function or lookup array would work nice but I'm not sure how to do it. Any suggestions?

Bluevoss
Got it. Remember this being a misconception I had some time ago. Looks like that came in handy after all...


[[first section]]:

@set name=3

{varName}

[[second section]]:

This is the second section

[[varName]]:

{if name=1:Ace of spades}
{if name=2:Two of spades}
{if name=3:Three of spades}

I feel compelled to post this here, maybe for future people showing up here via Google? Not sure.

[[setup]]:
    var cards = [1, 2, 3, 4].sort(function() { return 0.5 - Math.random(); });
    for(var i = 0; i < cards.length; i++) {
        squiffy.set("card" + (i + 1), cards[i]);
    }
    
{exit}

[[exit]]:

In exit

{card1}
{card2}
{card3}
{card4}

Please do note though that the random is not unbaised. If you want it to be completely unbiased use a shuffle like this.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums