Cards Monte-Carlo little problem but not in computer chess.

Discussion of chess software programming and technical issues.

Moderator: Ras

Solitaireman

Cards Monte-Carlo little problem but not in computer chess.

Post by Solitaireman »

Hi, i'm trying to find some probability for a solitaire game and i want to do it with the easiest way with a monte carlo simulation of the game. My knowledge of C is small but i hoped i could do it but i can't as it seems. :( I guess for all of you is very easy and quick to do it. I asked the same in another forum and from that forum i saw some references to this forum. I understand this is a board for chess but perhaps you can make an exception to help a noob like me. :lol:

Game is simple but for those who don't know nothing about cards let me explain: A card deck for solitaire consists of 52 cards.
Cards are "numbered" from 1 to 13(actually A,2,3,4,5,6,7,8,9,10,J,Q,K but the symbolism doesn't matter) and each card has 4 different kinds of either spades,hearts,clubs diamonds. So we have the following 52 cards:
1♠, 2♠, 3♠, 4♠, 5♠, 6♠, 7♠, 8♠, 9♠, 10♠, 11♠, 12♠, 13♠
1♥, 2♥, 3♥, 4♥, 5♥, 6♥, 7♥, 8♥, 9♥, 10♥, 11♥, 12♥, 13♥
1♦, 2♦, 3♦, 4♦, 5♦, 6♦, 7♦, 8♦, 9♦, 10♦, 11♦, 12♦, 13♦
1♣, 2♣, 3♣, 4♣, 5♣, 6♣, 7♣, 8♣, 9♣, 10♣, 11♣, 12♣, 13♣


My main problem (i have more) is that i can't see how to give in every (of the 52) card a double property. That of the number (1,2,3,...,13) and that of the kind(♥,♦,♣,♠). I'm trying with arrays but i can't.

For example i want to have an array e.g a[52] that will contain all the deck cards, e.g to have a[0]=1♦, a[2]=8♠, a[3]= 5♥, etc.
Of course the 4 symbols would be represented by numerical values so it would be handy each value of a[] to correspond to a 1x2 array with each of the 2 elements to contain the arithmetic value of the card and the second element to represent the kind, e.g like a[0] = [1,4](for A♠) , a[30]=[9,3], a[31]=[10,3], a[32]=[12,4], a[40]=[6,2], a[51]=[8,1], etc....

So i could compare them easily. For this solitaire game comparison has to be in order to know if the card dealt is the next or the previous one of a specific card, for example we want to compare 4♠ to 7♠ or 8♣ to 9♦ if they are near each other(obviously they aren't) by just comparing their arithmetic number (the difference should obviously be 1) and their kind where their difference should be 0, as they are of the same kind, e.g a[4]=[8,3] (for 8♣) and a[5]=[9,4] (for 9♦) give 9-8=1 correctly(meaning the one is the successor of the other like we want) and 4-3 = 1 != 0 so they are not of the same kind (♣ versus ♦).

But i can't do it. Can anyone tell me a way? Please no tricks but only a general way, for being able to handle for example the case where the property of a card is triple or generally N, example if we want to connect each a[] element to a 1xN array like a[1]=[12,3,5,8,...,N]. Because i found a trick for doing that for the cards(for a N=2, 1x2 array like in cards) by doing e.g a[1] = 1.1 or a[1]=4.2 or a[1]=12.2 or a[1]=8.3 and the integer number to represent the arithmetic of the card while the decimal to represent the card kind (spades, hearts etc). But as you see this method sucks.

Does it need structures? Or it can be with another way, only with array use for example?

In fact i would be very thankful if anyone could solve the whole (small and quick) problem for me. In C code only as it's the only one i know of. :cry:
Here is the description of the solitaire game i'm trying to find the probability to got solved:

•You start with all 52 cards in a pack upside down and start dealing them 3 by 3 in the main stack where we are dealing cards from our deck, and we are dealing them with the face looking above(of course).
•The upper card of the first 3-group, if it's an Ace or King(1 or 13 in the code) it is removed and placed in a new stack.
•If the revealed card of the now 2-group cards that remained is Ace or King it is also placed into a new stack. If it's a Queen of the same kind of the King that we have already placed, it is placed above the King.
•If no match is made then we continue dealing 3 by 3 the cards in the main stack.
•We progress like that until we are out of cards in our deck. A round is completed.
•Then our main stack will be full, so we take these cards and start dealing again.

We win if all cards will be dealt into the 8 stacks that will be created(4 starting with Aces and 4 starting with Kings obviously). While of course we lose if after we deal one round, no card gets out to be placed on an available stack(of the 8).

It's actually simpler than it looks. :D Look at the animation in the end of the post.

So what i'm trying to make is a C code that runs this game and put it on e.g a for loop with thousands or millions of repetitions in order to count the win-loss ratio.

An animated GIF about how this game is played in order to help for better understanding:
Image


Many thanks in advance.


I want to thank Mr Hyatt for answering in the other forum but his answer did not help me too much since what he proposes is not a general solution and i want something general to be able to apply it in other cases also where there are more than 2 "groups" of things i want to store.
gaard
Posts: 463
Joined: Mon Jun 07, 2010 3:13 am
Location: Holland, MI
Full name: Martin W

Re: Cards Monte-Carlo little problem but not in computer che

Post by gaard »

I see the heavy emphasis on "C code" but it really is trivial transforming the problem to C++. When you do that it's just a few lines of code to define the structure of the game. For example:

Code: Select all

struct Card {
    int number;
    int suit;
    int placement; // [0-51]
};
vector<struct Card> Deck(52);
or something like that


Using vectors, C++ would also allow you to shuffle or sort the deck with one line of code. After that, the elements of Deck could be accessed as if they were ordinary array elements (of structures), for example, Deck[3].number

King and Ace stacks would look something like: vector<Card> KingStack(4). For every third card, you would cycle through the top cards of all eight stacks to see if you can place them. You'd also need one more vector for holding the 3 cards that you flipped.

I hope that is helpful :)
rjgibert
Posts: 317
Joined: Mon Jun 26, 2006 9:44 am

Re: Cards Monte-Carlo little problem but not in computer che

Post by rjgibert »

If a card X is represented by a number from 0 to 51, then its suit is X&3 which returns a value from 0 to 3. For the rank, use X>>2 which returns a value from 0 to 12.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Cards Monte-Carlo little problem but not in computer che

Post by Don »

A simple representation for cards is to use 4 bits for the rank and 2 bits for the suit. If color is important, such as often is the case for solitaire, you can make one of the 2 bits a color bit so that's it's especially convenient to test for same or opposite color.

So the card would be: (suit << 4) | rank

where rank is a value from 0 - 12 (or 1-13 if you prefer) and the rank is 0-3


Solitaireman wrote:Hi, i'm trying to find some probability for a solitaire game and i want to do it with the easiest way with a monte carlo simulation of the game. My knowledge of C is small but i hoped i could do it but i can't as it seems. :( I guess for all of you is very easy and quick to do it. I asked the same in another forum and from that forum i saw some references to this forum. I understand this is a board for chess but perhaps you can make an exception to help a noob like me. :lol:

Game is simple but for those who don't know nothing about cards let me explain: A card deck for solitaire consists of 52 cards.
Cards are "numbered" from 1 to 13(actually A,2,3,4,5,6,7,8,9,10,J,Q,K but the symbolism doesn't matter) and each card has 4 different kinds of either spades,hearts,clubs diamonds. So we have the following 52 cards:
1♠, 2♠, 3♠, 4♠, 5♠, 6♠, 7♠, 8♠, 9♠, 10♠, 11♠, 12♠, 13♠
1♥, 2♥, 3♥, 4♥, 5♥, 6♥, 7♥, 8♥, 9♥, 10♥, 11♥, 12♥, 13♥
1♦, 2♦, 3♦, 4♦, 5♦, 6♦, 7♦, 8♦, 9♦, 10♦, 11♦, 12♦, 13♦
1♣, 2♣, 3♣, 4♣, 5♣, 6♣, 7♣, 8♣, 9♣, 10♣, 11♣, 12♣, 13♣


My main problem (i have more) is that i can't see how to give in every (of the 52) card a double property. That of the number (1,2,3,...,13) and that of the kind(♥,♦,♣,♠). I'm trying with arrays but i can't.

For example i want to have an array e.g a[52] that will contain all the deck cards, e.g to have a[0]=1♦, a[2]=8♠, a[3]= 5♥, etc.
Of course the 4 symbols would be represented by numerical values so it would be handy each value of a[] to correspond to a 1x2 array with each of the 2 elements to contain the arithmetic value of the card and the second element to represent the kind, e.g like a[0] = [1,4](for A♠) , a[30]=[9,3], a[31]=[10,3], a[32]=[12,4], a[40]=[6,2], a[51]=[8,1], etc....

So i could compare them easily. For this solitaire game comparison has to be in order to know if the card dealt is the next or the previous one of a specific card, for example we want to compare 4♠ to 7♠ or 8♣ to 9♦ if they are near each other(obviously they aren't) by just comparing their arithmetic number (the difference should obviously be 1) and their kind where their difference should be 0, as they are of the same kind, e.g a[4]=[8,3] (for 8♣) and a[5]=[9,4] (for 9♦) give 9-8=1 correctly(meaning the one is the successor of the other like we want) and 4-3 = 1 != 0 so they are not of the same kind (♣ versus ♦).

But i can't do it. Can anyone tell me a way? Please no tricks but only a general way, for being able to handle for example the case where the property of a card is triple or generally N, example if we want to connect each a[] element to a 1xN array like a[1]=[12,3,5,8,...,N]. Because i found a trick for doing that for the cards(for a N=2, 1x2 array like in cards) by doing e.g a[1] = 1.1 or a[1]=4.2 or a[1]=12.2 or a[1]=8.3 and the integer number to represent the arithmetic of the card while the decimal to represent the card kind (spades, hearts etc). But as you see this method sucks.

Does it need structures? Or it can be with another way, only with array use for example?

In fact i would be very thankful if anyone could solve the whole (small and quick) problem for me. In C code only as it's the only one i know of. :cry:
Here is the description of the solitaire game i'm trying to find the probability to got solved:

•You start with all 52 cards in a pack upside down and start dealing them 3 by 3 in the main stack where we are dealing cards from our deck, and we are dealing them with the face looking above(of course).
•The upper card of the first 3-group, if it's an Ace or King(1 or 13 in the code) it is removed and placed in a new stack.
•If the revealed card of the now 2-group cards that remained is Ace or King it is also placed into a new stack. If it's a Queen of the same kind of the King that we have already placed, it is placed above the King.
•If no match is made then we continue dealing 3 by 3 the cards in the main stack.
•We progress like that until we are out of cards in our deck. A round is completed.
•Then our main stack will be full, so we take these cards and start dealing again.

We win if all cards will be dealt into the 8 stacks that will be created(4 starting with Aces and 4 starting with Kings obviously). While of course we lose if after we deal one round, no card gets out to be placed on an available stack(of the 8).

It's actually simpler than it looks. :D Look at the animation in the end of the post.

So what i'm trying to make is a C code that runs this game and put it on e.g a for loop with thousands or millions of repetitions in order to count the win-loss ratio.

An animated GIF about how this game is played in order to help for better understanding:
Image


Many thanks in advance.


I want to thank Mr Hyatt for answering in the other forum but his answer did not help me too much since what he proposes is not a general solution and i want something general to be able to apply it in other cases also where there are more than 2 "groups" of things i want to store.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Cards Monte-Carlo little problem but not in computer che

Post by Don »

What happens when you run out of cards in the deck but have dealt just 1 or 2? Do you get to place them if possible?
Solitaireman wrote:Hi, i'm trying to find some probability for a solitaire game and i want to do it with the easiest way with a monte carlo simulation of the game. My knowledge of C is small but i hoped i could do it but i can't as it seems. :( I guess for all of you is very easy and quick to do it. I asked the same in another forum and from that forum i saw some references to this forum. I understand this is a board for chess but perhaps you can make an exception to help a noob like me. :lol:

Game is simple but for those who don't know nothing about cards let me explain: A card deck for solitaire consists of 52 cards.
Cards are "numbered" from 1 to 13(actually A,2,3,4,5,6,7,8,9,10,J,Q,K but the symbolism doesn't matter) and each card has 4 different kinds of either spades,hearts,clubs diamonds. So we have the following 52 cards:
1♠, 2♠, 3♠, 4♠, 5♠, 6♠, 7♠, 8♠, 9♠, 10♠, 11♠, 12♠, 13♠
1♥, 2♥, 3♥, 4♥, 5♥, 6♥, 7♥, 8♥, 9♥, 10♥, 11♥, 12♥, 13♥
1♦, 2♦, 3♦, 4♦, 5♦, 6♦, 7♦, 8♦, 9♦, 10♦, 11♦, 12♦, 13♦
1♣, 2♣, 3♣, 4♣, 5♣, 6♣, 7♣, 8♣, 9♣, 10♣, 11♣, 12♣, 13♣


My main problem (i have more) is that i can't see how to give in every (of the 52) card a double property. That of the number (1,2,3,...,13) and that of the kind(♥,♦,♣,♠). I'm trying with arrays but i can't.

For example i want to have an array e.g a[52] that will contain all the deck cards, e.g to have a[0]=1♦, a[2]=8♠, a[3]= 5♥, etc.
Of course the 4 symbols would be represented by numerical values so it would be handy each value of a[] to correspond to a 1x2 array with each of the 2 elements to contain the arithmetic value of the card and the second element to represent the kind, e.g like a[0] = [1,4](for A♠) , a[30]=[9,3], a[31]=[10,3], a[32]=[12,4], a[40]=[6,2], a[51]=[8,1], etc....

So i could compare them easily. For this solitaire game comparison has to be in order to know if the card dealt is the next or the previous one of a specific card, for example we want to compare 4♠ to 7♠ or 8♣ to 9♦ if they are near each other(obviously they aren't) by just comparing their arithmetic number (the difference should obviously be 1) and their kind where their difference should be 0, as they are of the same kind, e.g a[4]=[8,3] (for 8♣) and a[5]=[9,4] (for 9♦) give 9-8=1 correctly(meaning the one is the successor of the other like we want) and 4-3 = 1 != 0 so they are not of the same kind (♣ versus ♦).

But i can't do it. Can anyone tell me a way? Please no tricks but only a general way, for being able to handle for example the case where the property of a card is triple or generally N, example if we want to connect each a[] element to a 1xN array like a[1]=[12,3,5,8,...,N]. Because i found a trick for doing that for the cards(for a N=2, 1x2 array like in cards) by doing e.g a[1] = 1.1 or a[1]=4.2 or a[1]=12.2 or a[1]=8.3 and the integer number to represent the arithmetic of the card while the decimal to represent the card kind (spades, hearts etc). But as you see this method sucks.

Does it need structures? Or it can be with another way, only with array use for example?

In fact i would be very thankful if anyone could solve the whole (small and quick) problem for me. In C code only as it's the only one i know of. :cry:
Here is the description of the solitaire game i'm trying to find the probability to got solved:

•You start with all 52 cards in a pack upside down and start dealing them 3 by 3 in the main stack where we are dealing cards from our deck, and we are dealing them with the face looking above(of course).
•The upper card of the first 3-group, if it's an Ace or King(1 or 13 in the code) it is removed and placed in a new stack.
•If the revealed card of the now 2-group cards that remained is Ace or King it is also placed into a new stack. If it's a Queen of the same kind of the King that we have already placed, it is placed above the King.
•If no match is made then we continue dealing 3 by 3 the cards in the main stack.
•We progress like that until we are out of cards in our deck. A round is completed.
•Then our main stack will be full, so we take these cards and start dealing again.

We win if all cards will be dealt into the 8 stacks that will be created(4 starting with Aces and 4 starting with Kings obviously). While of course we lose if after we deal one round, no card gets out to be placed on an available stack(of the 8).

It's actually simpler than it looks. :D Look at the animation in the end of the post.

So what i'm trying to make is a C code that runs this game and put it on e.g a for loop with thousands or millions of repetitions in order to count the win-loss ratio.

An animated GIF about how this game is played in order to help for better understanding:
Image


Many thanks in advance.


I want to thank Mr Hyatt for answering in the other forum but his answer did not help me too much since what he proposes is not a general solution and i want something general to be able to apply it in other cases also where there are more than 2 "groups" of things i want to store.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Cards Monte-Carlo little problem but not in computer che

Post by Don »

I whipped together a program to count how many of these are solvable.

With 1 million trials I get 22.691 % of them are solvable.

It's likely there are bugs in the program - hopefully someone else will verify this.

Don


Solitaireman wrote:Hi, i'm trying to find some probability for a solitaire game and i want to do it with the easiest way with a monte carlo simulation of the game. My knowledge of C is small but i hoped i could do it but i can't as it seems. :( I guess for all of you is very easy and quick to do it. I asked the same in another forum and from that forum i saw some references to this forum. I understand this is a board for chess but perhaps you can make an exception to help a noob like me. :lol:

Game is simple but for those who don't know nothing about cards let me explain: A card deck for solitaire consists of 52 cards.
Cards are "numbered" from 1 to 13(actually A,2,3,4,5,6,7,8,9,10,J,Q,K but the symbolism doesn't matter) and each card has 4 different kinds of either spades,hearts,clubs diamonds. So we have the following 52 cards:
1♠, 2♠, 3♠, 4♠, 5♠, 6♠, 7♠, 8♠, 9♠, 10♠, 11♠, 12♠, 13♠
1♥, 2♥, 3♥, 4♥, 5♥, 6♥, 7♥, 8♥, 9♥, 10♥, 11♥, 12♥, 13♥
1♦, 2♦, 3♦, 4♦, 5♦, 6♦, 7♦, 8♦, 9♦, 10♦, 11♦, 12♦, 13♦
1♣, 2♣, 3♣, 4♣, 5♣, 6♣, 7♣, 8♣, 9♣, 10♣, 11♣, 12♣, 13♣


My main problem (i have more) is that i can't see how to give in every (of the 52) card a double property. That of the number (1,2,3,...,13) and that of the kind(♥,♦,♣,♠). I'm trying with arrays but i can't.

For example i want to have an array e.g a[52] that will contain all the deck cards, e.g to have a[0]=1♦, a[2]=8♠, a[3]= 5♥, etc.
Of course the 4 symbols would be represented by numerical values so it would be handy each value of a[] to correspond to a 1x2 array with each of the 2 elements to contain the arithmetic value of the card and the second element to represent the kind, e.g like a[0] = [1,4](for A♠) , a[30]=[9,3], a[31]=[10,3], a[32]=[12,4], a[40]=[6,2], a[51]=[8,1], etc....

So i could compare them easily. For this solitaire game comparison has to be in order to know if the card dealt is the next or the previous one of a specific card, for example we want to compare 4♠ to 7♠ or 8♣ to 9♦ if they are near each other(obviously they aren't) by just comparing their arithmetic number (the difference should obviously be 1) and their kind where their difference should be 0, as they are of the same kind, e.g a[4]=[8,3] (for 8♣) and a[5]=[9,4] (for 9♦) give 9-8=1 correctly(meaning the one is the successor of the other like we want) and 4-3 = 1 != 0 so they are not of the same kind (♣ versus ♦).

But i can't do it. Can anyone tell me a way? Please no tricks but only a general way, for being able to handle for example the case where the property of a card is triple or generally N, example if we want to connect each a[] element to a 1xN array like a[1]=[12,3,5,8,...,N]. Because i found a trick for doing that for the cards(for a N=2, 1x2 array like in cards) by doing e.g a[1] = 1.1 or a[1]=4.2 or a[1]=12.2 or a[1]=8.3 and the integer number to represent the arithmetic of the card while the decimal to represent the card kind (spades, hearts etc). But as you see this method sucks.

Does it need structures? Or it can be with another way, only with array use for example?

In fact i would be very thankful if anyone could solve the whole (small and quick) problem for me. In C code only as it's the only one i know of. :cry:
Here is the description of the solitaire game i'm trying to find the probability to got solved:

•You start with all 52 cards in a pack upside down and start dealing them 3 by 3 in the main stack where we are dealing cards from our deck, and we are dealing them with the face looking above(of course).
•The upper card of the first 3-group, if it's an Ace or King(1 or 13 in the code) it is removed and placed in a new stack.
•If the revealed card of the now 2-group cards that remained is Ace or King it is also placed into a new stack. If it's a Queen of the same kind of the King that we have already placed, it is placed above the King.
•If no match is made then we continue dealing 3 by 3 the cards in the main stack.
•We progress like that until we are out of cards in our deck. A round is completed.
•Then our main stack will be full, so we take these cards and start dealing again.

We win if all cards will be dealt into the 8 stacks that will be created(4 starting with Aces and 4 starting with Kings obviously). While of course we lose if after we deal one round, no card gets out to be placed on an available stack(of the 8).

It's actually simpler than it looks. :D Look at the animation in the end of the post.

So what i'm trying to make is a C code that runs this game and put it on e.g a for loop with thousands or millions of repetitions in order to count the win-loss ratio.

An animated GIF about how this game is played in order to help for better understanding:
Image


Many thanks in advance.


I want to thank Mr Hyatt for answering in the other forum but his answer did not help me too much since what he proposes is not a general solution and i want something general to be able to apply it in other cases also where there are more than 2 "groups" of things i want to store.
nevatre
Posts: 16
Joined: Fri Aug 01, 2008 6:09 pm
Location: UK

Re: Cards Monte-Carlo little problem but not in computer che

Post by nevatre »

I am struggling to understand what you mean. In general, if the 'pack' is composed of N groups with {n1, n2, ..., nN} elements in each group then you will have a pack whose size is S = n1 * ... * nN. (In the cards case N=2, n1=4, n2=13, and S=52). Is that what you mean?

How you define your 'pack of cards' in the general case will depend on how do you intend to define the playoff stacks in the general case. You haven't said how. (In the cards case you have n1 playoff stack pairs, that is hi and lo for each suit, so you can define the value of cards as 0,..,51 and find the pair of hi/lo playoff stacks belonging to a card as stack_pair = value / 13, all integers of course.)

One possibility in the general case is to define the cards with values in {0, ..., S-1}, then the groups for a single card with value V can be calculated by, say,
group 1 = V % n1, R1 = V / n1,
group 2 = R1 % n2, R2 = R1 / n2,
...
group N-1 = R(N-2) % nN,
group N = R(N-2) / nN

so you would use a loop, something like

R=value
for i=1,..,N-1 {
group = R % n
R = R / n
}
group[N] = R
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Cards Monte-Carlo little problem but not in computer che

Post by Don »

Has anyone tried to verify my result of 22.691% ?
Don wrote:I whipped together a program to count how many of these are solvable.

With 1 million trials I get 22.691 % of them are solvable.

It's likely there are bugs in the program - hopefully someone else will verify this.

Don


Solitaireman wrote:Hi, i'm trying to find some probability for a solitaire game and i want to do it with the easiest way with a monte carlo simulation of the game. My knowledge of C is small but i hoped i could do it but i can't as it seems. :( I guess for all of you is very easy and quick to do it. I asked the same in another forum and from that forum i saw some references to this forum. I understand this is a board for chess but perhaps you can make an exception to help a noob like me. :lol:

Game is simple but for those who don't know nothing about cards let me explain: A card deck for solitaire consists of 52 cards.
Cards are "numbered" from 1 to 13(actually A,2,3,4,5,6,7,8,9,10,J,Q,K but the symbolism doesn't matter) and each card has 4 different kinds of either spades,hearts,clubs diamonds. So we have the following 52 cards:
1♠, 2♠, 3♠, 4♠, 5♠, 6♠, 7♠, 8♠, 9♠, 10♠, 11♠, 12♠, 13♠
1♥, 2♥, 3♥, 4♥, 5♥, 6♥, 7♥, 8♥, 9♥, 10♥, 11♥, 12♥, 13♥
1♦, 2♦, 3♦, 4♦, 5♦, 6♦, 7♦, 8♦, 9♦, 10♦, 11♦, 12♦, 13♦
1♣, 2♣, 3♣, 4♣, 5♣, 6♣, 7♣, 8♣, 9♣, 10♣, 11♣, 12♣, 13♣


My main problem (i have more) is that i can't see how to give in every (of the 52) card a double property. That of the number (1,2,3,...,13) and that of the kind(♥,♦,♣,♠). I'm trying with arrays but i can't.

For example i want to have an array e.g a[52] that will contain all the deck cards, e.g to have a[0]=1♦, a[2]=8♠, a[3]= 5♥, etc.
Of course the 4 symbols would be represented by numerical values so it would be handy each value of a[] to correspond to a 1x2 array with each of the 2 elements to contain the arithmetic value of the card and the second element to represent the kind, e.g like a[0] = [1,4](for A♠) , a[30]=[9,3], a[31]=[10,3], a[32]=[12,4], a[40]=[6,2], a[51]=[8,1], etc....

So i could compare them easily. For this solitaire game comparison has to be in order to know if the card dealt is the next or the previous one of a specific card, for example we want to compare 4♠ to 7♠ or 8♣ to 9♦ if they are near each other(obviously they aren't) by just comparing their arithmetic number (the difference should obviously be 1) and their kind where their difference should be 0, as they are of the same kind, e.g a[4]=[8,3] (for 8♣) and a[5]=[9,4] (for 9♦) give 9-8=1 correctly(meaning the one is the successor of the other like we want) and 4-3 = 1 != 0 so they are not of the same kind (♣ versus ♦).

But i can't do it. Can anyone tell me a way? Please no tricks but only a general way, for being able to handle for example the case where the property of a card is triple or generally N, example if we want to connect each a[] element to a 1xN array like a[1]=[12,3,5,8,...,N]. Because i found a trick for doing that for the cards(for a N=2, 1x2 array like in cards) by doing e.g a[1] = 1.1 or a[1]=4.2 or a[1]=12.2 or a[1]=8.3 and the integer number to represent the arithmetic of the card while the decimal to represent the card kind (spades, hearts etc). But as you see this method sucks.

Does it need structures? Or it can be with another way, only with array use for example?

In fact i would be very thankful if anyone could solve the whole (small and quick) problem for me. In C code only as it's the only one i know of. :cry:
Here is the description of the solitaire game i'm trying to find the probability to got solved:

•You start with all 52 cards in a pack upside down and start dealing them 3 by 3 in the main stack where we are dealing cards from our deck, and we are dealing them with the face looking above(of course).
•The upper card of the first 3-group, if it's an Ace or King(1 or 13 in the code) it is removed and placed in a new stack.
•If the revealed card of the now 2-group cards that remained is Ace or King it is also placed into a new stack. If it's a Queen of the same kind of the King that we have already placed, it is placed above the King.
•If no match is made then we continue dealing 3 by 3 the cards in the main stack.
•We progress like that until we are out of cards in our deck. A round is completed.
•Then our main stack will be full, so we take these cards and start dealing again.

We win if all cards will be dealt into the 8 stacks that will be created(4 starting with Aces and 4 starting with Kings obviously). While of course we lose if after we deal one round, no card gets out to be placed on an available stack(of the 8).

It's actually simpler than it looks. :D Look at the animation in the end of the post.

So what i'm trying to make is a C code that runs this game and put it on e.g a for loop with thousands or millions of repetitions in order to count the win-loss ratio.

An animated GIF about how this game is played in order to help for better understanding:
Image


Many thanks in advance.


I want to thank Mr Hyatt for answering in the other forum but his answer did not help me too much since what he proposes is not a general solution and i want something general to be able to apply it in other cases also where there are more than 2 "groups" of things i want to store.
Solitaireman

Re: Cards Monte-Carlo little problem but not in computer che

Post by Solitaireman »

Don wrote:Has anyone tried to verify my result of 22.691% ?
From my experience by playing this game it seems too low. I would say something like 35-40% is the correct one, but of course i very easily may be wrong.
Last edited by Solitaireman on Fri Dec 24, 2010 11:31 am, edited 1 time in total.
Solitaireman

Re: Cards Monte-Carlo little problem but not in computer che

Post by Solitaireman »

Don wrote:I whipped together a program to count how many of these are solvable.

With 1 million trials I get 22.691 % of them are solvable.

It's likely there are bugs in the program - hopefully someone else will verify this.
Can you share the program or you want to keep it private? It would be a good start for me to understand how it is done and apply it to other Solitaire games also.
Thanks.