# Matchstick game programming using C



## chatterjee (Jul 12, 2005)

I don't know if it's a famous program(like "Hello World") or not.I've come across it yesterday in a book's exercise and solved the problem.

*The game:*

*There are 21 matchsticks.You can pick matchsticks ranging from 1 to 4.Whoever is forced to pick the last one will lose the game.*

Please download this 6 KB attached file,play the game(surely you'll lose) and give me your feedback.I'm ready to post the source code(small one) on request.

PS:I haven't been learning C for even a month.So,your comment will be valuable to me.


----------



## lotuseclat79 (Sep 12, 2003)

Hi chaterjee,

To make the game fair, introduce a new rule where the winner of the previous game goes first, otherwise, the strategy played by the computer will always win as its objective appears to be to arrive at a total of 6 matchsticks left after three pairs of moves - so, the player who moves first will never win. 

To make this happen, the game should be programmed in a loop (i.e. start loop) with a new who goes first condition. An alternative modification could be to increase/decrease the number of matchsticks within a certain range (perhaps chosen by a random number generator). Yet another modification could be to allow a new range of how many matchsticks may be chosen instead of 1-4.

-- Tom


----------



## chatterjee (Jul 12, 2005)

*Actually,the book mentioned that I should program to make computer win every time.

Computer's objective is not to arrive at a total of 6 matchsticks but to be always in the multiples of 5.(i.e. 5,10,15 etc.)That is to ensure that the user never wins.*



Tom said:


> An alternative modification could be to increase/decrease the number of matchsticks within a certain range (perhaps chosen by a random number generator).


*Can you assist me?How can I generate a random no?

I'm going to post the source code.Please make some alteration to make that happen.*


----------



## chatterjee (Jul 12, 2005)

The code goes here:


```
main()
{
      int mcount=21,u_choice,c_choice,i=5,sum;
      printf("\n Rules:\n \n There are 21 matchsticks.You're allowed to pick up matchsticks \n ranging from 1 to 4.Whoever(You and computer)is forced to pick\n the  last one will lose the game. IT'S A CHALLENGE");
                    
      while (mcount>0)
       {
            
            
            
               printf("\n \nHow many matchsticks do you want to pick?");
               scanf("%d",&u_choice);
               if (u_choice<=4&& u_choice>0&& u_choice<=mcount)
               {
               
               c_choice=i-u_choice;
               
               
               
               sum=u_choice+c_choice;
               
            
             
         mcount=mcount-sum;
          if (mcount<0)
            break;
         else 
         printf("\n \nComputer has picked up %d match stick(s)",c_choice);
         printf("\n \n %d matchsticks are left",mcount);
             }
             else
             printf("\n \nPlease enter a no within 1 to 4");
      }   
      
         printf("\n \n You have picked up the last matchstick.So,you have lost");
         getch();
      }
```


----------



## blaqDeaph (Nov 22, 2005)

To make a random number, with range x - y:

#include <math.h>
#include <time.h>

//code begin
srand(time(0))

int randNo = rand() % y - x;

//end code

randNo will now have a value between x and y


----------



## chatterjee (Jul 12, 2005)

Thank you so much!

Can you please explain the srand()?What does the value 0 mean here?


----------



## blaqDeaph (Nov 22, 2005)

Basically, to cut a long story short, computers are horrible at generating random numbers, since by definition, no predefined mathematical algorithm can produce a random result. So, computers use a combination of hashing a seed value to generate a pseudo random number stream.

srand(time(0)) basically tells the computer to seed the random number generator with the number of seconds elasped since Jan 1, 1990. That is what time(0) returns, by the way,


----------



## chatterjee (Jul 12, 2005)

But why Jan1,1990...so specified a date?How does time(0) is related to 1990?


----------



## blaqDeaph (Nov 22, 2005)

1 Jan 1990 was just a date picked by the people who created the time.h functions. They had to pick an arbitrary date, and it was 1/1/1990.

time(0) basically uses the system clock to calculate how many seconds have passed since then, and returns it as a double (?)


----------

