No one actually answered this question with an answer. Therefore, I will give it a try, although nneonneo actually put his or her finger on the probable source of your problem.
The first problem, which is probably not really the problem in this case, but sticks out like a sore finger ... you use double to store the value that you basically consider as a whole. In this case, on most systems this is probably OK. But, as a general practice, this is very bad. In particular, because you check whether the double value corresponds to 0. Probably, it will, like most systems, most compilers, double can store integer values to fairly large sizes with full accuracy, if you restrict yourself to add, subtract and multiply other integers or duplicate disguise as integers to get a new value.
But, most likely, this is not the source of the error you see, it simply disables all good programmer sound alarms for the smelly code. It must be fixed. The only time you really need them to double is when you calculate the relative probability of red or black.
And that brings me to what is probably your problem. You have two statements in your code:
number_of_total_cards = number_of_red_cards + number_of_black_cards; prob_red_card = number_of_red_cards/number_of_total_cards; prob_black_card = number_of_black_cards/number_of_total_cards;
which, of course, should read:
number_of_total_cards = number_of_red_cards + number_of_black_cards; prob_red_card = number_of_red_cards/double(number_of_total_cards); prob_black_card = number_of_black_cards/double(number_of_total_cards);
because you were a good programmer and declared these variables as integers.
prob_red_card and prob_black_card are double variables. But they are not declared anywhere in the code that you show us. This means that no matter where they were declared or what their types were, they should be effectively used by all subzones in the recursive call tree for Game::Value_of_game .
This is almost certainly not what you want. It is very difficult to understand what values these variables have and what these values represent during any given call in the recursive call tree for your function. They really need to be local variables so that the algorithm can be convenient for analysis. Fortunately, they seem to be used only in the else clause of a specific if . Thus, they can be declared when they are initially assigned values. This code should probably be read:
unsigned const int number_of_total_cards = number_of_red_cards + number_of_black_cards; const double prob_red_card = number_of_red_cards/double(number_of_total_cards); const double prob_black_card = number_of_black_cards/double(number_of_total_cards);
Note that I also declare them const . It is good practice to declare any variable whose value you do not expect to change as a const throughout the lifetime of the variable. This helps you write code that is more correct by asking the compiler to tell you when you accidentally wrote the wrong code. It can also help the compiler generate better code, although in this case even a trivial analysis of the code shows that they do not change during their lifetime and can be considered as const, so most worthy optimizers essentially put const in for you in order to optimize the code, although it still won’t give you an advantage if the compiler tells you if you accidentally use them in a non-constant way.