Matchstick game

This is a discussion on Matchstick game within the C Programming forums, part of the General Programming Boards category; Can anybody gimme an idea as to how this can be done..? Write a program for a matchstick game being ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    47

    Matchstick game

    Can anybody gimme an idea as to how this can be done..?

    Write a program for a matchstick game being played between the computer and a user.
    Your program should ensure that the computer always wins. Rules for the game are as follows:

    -There are 21 matchsticks.
    -The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
    -After the person picks, the computer does its picking.
    -Whoever is forced to pick up the last matchstick loses the game.


    I tried out some stuff..but as it is..my program was not quite organised..

    Code:
    #include<stdio.h>
    main()
    {
    	int matchsticks, total;
    
    	total = 21;
    
    	printf("Enter the number of matchsticks you wanna pick up at once:");
    	scanf("%d", &matchsticks);
    
    	matchsticks = total - matchsticks;
    
    	while (matchsticks<= 4)
    	{
    		printf("\n Computer Turn");
    		
    	/*	...the rest of the program is mystery! */
    Last edited by duffmckagan; 07-26-2006 at 09:59 AM.

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Code:
    		if (matchsticks == )
    			matchsticks=4;
    		else if (matchsticks;
    What is this supposed to be?
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    The program is incomplete man..

    I was just trying to figure it out..and then I posted it here...

    I will edit that part though..

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    Actually I was trying to figure out the logic...

    How do we make the computer win?
    How do we know what move to make..how many matchsticks the computer should pick up..in case the user enters n number of matchsticks..?

    My First game..hehe

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    The thing is, this actually requires you to use your brain. A lot of programming involves finding patterns and this is a good example of that. The trick is to keep the player from getting into a position where s/he can't beat you. The first thing you should realize is that if you can always get the player to end up at 5 matchsticks, there's no way s/he can win. If you make a little chart of how many matchsticks you want to pick up, it becomes clear:
    Code:
    1 1
    2 2
    3 3
    4 4
    5 bad
    6 1
    7 2
    8 3
    9 4
    10 bad
    11 1
    12 2
    13 3
    14 4
    15 bad
    16 1
    17 2
    18 3
    19 4
    20 bad
    21 1
    e.g. if there are 6 sticks left and you pick up 1 then you leave the player with 5. There's no choice s/he can make from there that will beat you. From the chart you can see a clear pattern emerge. Any multiple of 5 is bad so the key is to get the player to always end up on a multiple of 5. This is very easy to code.

    HINT: Use the modulus operator.

    EDIT: I had a whole working program in there, but decided to remove it for the OP's benefit.
    Last edited by itsme86; 07-26-2006 at 11:34 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > How do we make the computer win?
    Have you figured out how you would win if playing against another person?

    -There are 21 matchsticks.
    -Whoever is forced to pick up the last matchstick loses the game.
    So the last one is special, so it's all about how to get rid of 20 matches in pairs of turns.

    -The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
    So if we reduce the total by 5 each round, the sequence will go
    21 16 11 6 1
    In effect, whatever number the user picks (n), the computer picks 5-n
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Hmm...the logic I used breaks down if the user picks 1 every time. Oops.
    If you understand what you're doing, you're not learning anything.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    The bit about the brain was good though
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-07-2010, 11:08 PM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 02:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21