Monday, April 14, 2014

Matchsticks Game – C Program

Problem Question


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.

Explanation of Problem


The user should make the first move. You have to ensure that you win in all cases. For the given problem with 21 matchsticks, the logic is quite simple. Whatever the user picks, the computer should pick somehow that the total of the two become 5. Like if user picked 1, computer picks 4; if user picked 3, computer picks 2; etc. This will ensure that the computer will always win.

Code


#include <stdio.h>
/*MATCHSTICKS.C*
*@Language: ANSI C*
*@Compiler: GNU GCC *
*@IDE: Code::Blocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 14-04-2014**/
int main()
{
  int availableMatchSticks = 21, pickedMatchSticks = 0, lastChance = 1;
  printf("\nAvailable MatchSticks: %d", availableMatchSticks);
  while (availableMatchSticks > 0)
  {
    pickedMatchSticks = 0;
    while (pickedMatchSticks < 1 || pickedMatchSticks > 4 || availableMatchSticks - pickedMatchSticks < 0)
    {
      printf("\nPick matchsticks: (1/2/3/4): ");
      scanf("%d", &pickedMatchSticks);
      if (pickedMatchSticks < 1 || pickedMatchSticks > 4 || availableMatchSticks - pickedMatchSticks < 0)
        printf("\aWrong move! Try again!");
    }
    availableMatchSticks -= pickedMatchSticks;
    lastChance = 1;
    printf("\nAvailable MatchSticks: %d", availableMatchSticks);
    if (availableMatchSticks == 0)
      break;
    printf("\nComputer's Turn..Please Wait...\n");
    pickedMatchSticks = 5 - pickedMatchSticks;
    printf("\nComputer Picked: %d", pickedMatchSticks);
    availableMatchSticks -= pickedMatchSticks;
    printf("\nAvailable MatchSticks: %d", availableMatchSticks);
    lastChance = 0;
  }
  if(lastChance == 1)
    printf("\nYou lose!\n");
  else
    printf("\nYou win!\n");
  system("pause");
  return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

while (availableMatchSticks > 0) -> Make sure the program ends as soon as the matchsticks count reaches 0.

pickedMatchSticks = 0; -> Before the start of next turn (of the payer-computer move pair), we set the pickedMatchsticks count to 0. I did this because in my next line I check if the user makes a wrong move, which includes picking less than 1 matchstick, more than 4 matchsticks, or picking more than available matchsticks. I do his checking in the nested while loop:
while (pickedMatchSticks < 1 || pickedMatchSticks > 4 || availableMatchSticks - pickedMatchSticks < 0)
{
printf("\nPick matchsticks: (1/2/3/4): ");
scanf("%d", &pickedMatchSticks);
if (pickedMatchSticks < 1 || pickedMatchSticks > 4 || availableMatchSticks - pickedMatchSticks < 0)
printf("\aWrong move! Try again!");
}

Asking for the user to input unless a legal move is made.

availableMatchSticks -= pickedMatchSticks; -> After each move I make sure I decrease the count of matchsticks available. I do this once after the player has made his move, and once again after the computer has made his move.

lastChance -> The variable ‘lastChance’ is used to check who is the person(user or computer) who moved the last time before the program came out of the loop. Thus deciding, who is the loser? I set this variable after the player’s move and also after the computer’s move. In my program,
if(lastChance == 1)
printf("\nYou lose!\n");
else
printf("\nYou win!\n");

I mean if the value of lastChance is 1 the user has lost the game, else he has won. Though the latter won’t happen! ;)

if (availableMatchSticks == 0)
break;
-> If the matchstick count reaches 0 after the user’s move, I cannot let the computer make the next move. Thus, I break out of the while loop as soon as this happens.

pickedMatchSticks = 5 - pickedMatchSticks; -> This is the main logic of the program causing the computer to always win! Since we have 21 matchsticks and we wish that the last matchstick remains for the user, I ask the computer to keep the sum of matchsticks picked by the user and computer to be 5, so that in the final move, only one matchstick is left which the user is forced to pick. As long as the total count of matchsticks is of the order (10n +1, where n is a Natural Number), and the user makes the first move, this logic will ensure the computer always wins.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)



No comments:

Post a Comment

Need help?