10 Coins Puzzle
Hello Friends,
I m facing problems in coding of 10 coins Puzzle. I have made it, but the process was not good enough. I think there is a recursive process to sole this problem, but i couldn't get it. Plz help me out with efficient algorithm/code.

## All 3 Replies

This is the c program made by me. It also works...

``````/* Manjeet Singh Harsh
10 Coins puzzle */

#include<stdio.h>
#include<conio.h>
void win_player1(int);
void win_player2(int);
void play1(int,int,int);
void play2(int,int,int);

int counterme=0, counterthem =0;
char player1[10] = "USER";
char player2[10] = "Computer";

void win_player1(int n)
{
if(n ==1 || n==2 || n==4)
printf("%s wins!\n",player1);
}

void win_player2(int n)
{
if(n ==1 || n==2 || n==4)
printf("%s wins!\n",player2);
}

void play1(int n,int me,int them)
{
int p,q;
win_player1(n);
if(n < 0)
{
printf("Illegal Entry\n");
}
else if(n>1 && n != 4 && n != 2)
{
printf("Pick the coins: ");
scanf("%d",&p);
q=n-p;

printf("\nLeft %d coins for %s :: %d coins for %s\n\n",q,player2,p,player1);
play2(q, them, me);

}
}
//Turn for Player2

void play2(int q,int me,int them)
{
if(q>2 && q != 4 && q != 2)
{
if(q%2 == 0)
{
printf("Computer Pick 1 coin\n");
q=q-1;
printf("Left %d coins for %s\n",q,player1);
play1(q, them, me);
}
else
{

printf("Computer Picks 2 coins\n");
q=q-2;
printf("Left %d coins for %s\n",q,player1);
play1(q, them, me);
}
}
else if(q ==1 || q==2 || q==4)
{
printf("Computer picks %d coins\n",q);
win_player2(q);
}
}

int main()
{
int n;
printf("*****WELCOME TO 10 COINS PUZZLE*****\n\n");
printf("RULES:\n1. You have a bunch of 10 coins\n");
printf("2. Each time a player is allowed to pick 1, 2 or 4 coins\n");
printf("3. The player that gets the last coin is the winner\n");
printf("4. Here you are playing with your computer\n");

printf("\nYou start first\n");
printf("Enter the number of coins(should be less than or eqal to 10): ");
scanf("%d",&n);
play1(n,0,1);
getch();
return 0;

}
``````

What is this 10 coin puzzle? Please explain in English language, you just post your code no one have that much time to read your code and understand.......

Your program plays the puzzle, but it doesn't play it as smartly as it could. The puzzle is all about the first player taking the initiative, and keeping it. If player1 does that, he can always win the game.

If not, he can be made to lose, every time.

It's all about counting what's left for the opponent to use, after you make your play. Here's a winning strategy for player1:

``````Player 1  Coins Left   Player2  Coins Left
==========================================
1. take 2    8         take 1      7
2  take 1    6         take 1      5
3. take 2    3         take Any #  *  (he can take 1 or 2, but not 3 coins)
4. take the last coin(s)``````

To win, player1 must always leave an even number of coins greater than 4, after his move, or three coins. Anything else, and player2 can win. That means to force a win, player1 must take 2 coins on his first move.

Player2 must try and play so player1 is left facing an even number of coins, greater than 4, or three coins. If player1 makes one error, he can force a win.

Your program plays the game, but it doesn't use strategy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.