Hi everybody,

I'm trying to write a program to simulate a solitaire game that my math teacher showed me so that I can eventually get an approximate probability of winning. Although I eventually plan to simulate the game many times, I'm only going through it once at this point with a predefined deck to make sure it works before I randomize things later. I ran through it with a debugger, and it is reaching the return statement that it needs to, after following all the right steps. However, when it reaches the return, it returns control back over to itself. Eventually, it runs over the stack, and I end up with a segfault. If someone could help me make sure the test function returns control to the game function, I would greatly appreciate the help.

The basic function of the game is:

1) Begin by flipping four cards face-up from the deck, one on top of the other.

2) Look at the top card and the card three below it. If they are of the same suit, remove the two cards in between the cards you were comparing. If they are the same value (number), remove all four of the top four cards. for example, if the top four cards are 2♥, 7♦, J♣, A♥, the seven and the jack would be removed. If the top four cards are 2♥, 7♦, J♣, 2♦, then all four cards would be removed.

3) If no move is possible, or there are fewer than four cards face-up, flip another card from the deck, and do the tests in step two again.

4) The game ends when no further moves are possible. If there are no cards left, the player wins.

While I believe that there is no error in the logic of the program, I am certainly not perfect. There may also be more efficient ways to achieve the same goals.

Here is my attempt so far. Once again, my problem seems to be in the recursive calling of the test function, and the inability to transfer control to the game function.

```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct card{
int value;
int index;
};
int test (struct card *x, struct card *y, struct card const *first, struct card const *last);
int game( void);
int main()
{
int i; //counter
int win = 0; // number of wins
int lose = 0; //number of losses
srand( 1); //this is only to test things until later use srand(time(0))
// for (i = 0; i < 1000000; i++;) //runs a bunch of iterations
win += game();
printf("number of wins: %d\n", win);
getchar();
return 0;
}
int test (struct card *x, struct card *y, struct card const *first, struct card const *last)
{
int j = 0; //counter
int k = 0; //another counter
struct card const *temp;
if (((x -> value) - 1) % 13 == ((y -> value) - 1) % 13) //same number
{
temp = y;
for ( y = x; y <= temp; y++)
y -> value = 0;
x = y;
while (j < 4)
{
if (x == first)
{
if ((x -> value) == 0)
{
while ((x -> value) == 0)
{
if (x == last)
return 1; // ************ WIN ************ RETURN 1
x++;
}
break;
}
}
else
{
x--;
if ((x -> value) != 0)
j++;
}
}
y = x;
while (k < 3)
{
if (y == last)
return 0; // Should exit on this return statement
y++;
if ((y -> value) != 0)
k++;
}
test (x, y, first, last); //Recursive function call
}
if (((x -> value) - 1) / 13 == ((y -> value) - 1) / 13) //same suit
{
j = 0;
temp = y - 1;
for (y = x + 1; y <= temp; y++)
y -> value = 0;
x = y;
while (j < 3)
{
if (x == first)
{
if ((x -> value) == 0)
{
while ((x -> value) == 0)
{
if (x == last)
return 1; // ************ WIN ************ RETURN 1
x++;
}
}
y = x;
do
{
if (y == last)
return 0; // ************ LOSE ************ RETURN 0
y++;
if ((y -> value) != 0)
k++;
}while (k < 3);
test (x, y, first, last); //Recursive function call
}
else
{
x--;
if ((x -> value) != 0)
j++;
}
}
test (x, y, first, last); //Recursive function call
}
do
{
x++;
}while ((x -> value) == 0);
do
{
y++;
if (y == last)
return 0;
}while ((y -> value) == 0);
test (x, y, first, last);
}
int game (void)
{
struct card deck[52];
int i = 0;
int win = 0; // Local copy, separate from copy in main function
struct card *x;
struct card *y;
for (i = 0; i < 52; i++)
{
deck[i].value = i + 1;
deck[i].index = rand();
}
x = &deck[0];
y = &deck[3];
win += test (x, y, &deck[0], &deck[51]); //1 for win, 0 for lose
return win;
}
```