top of page

The Matchstick Game!

  • Anonymous
  • Aug 5, 2015
  • 1 min read

Q. In a game, of two players(player1-user .. player 2 -computer), there are 21 matchsticks.... Each player is made to pick up any number of matchsticks between 1to 4 simultaneously starting from player 1...... and the player who picks up the last matchstick loses.

Write a code to design the aforementioned game such that the computer always wins!

Solution: The key concept used here is that each time the sum of the number of matchsticks picked up by the user and that by the computer should be equal to 5 so that after 4 turns..... a total of 20 matchsticks have been picked and only one is left that the user will have to pick and thus... the computer always wins the game!

CODE:

#include <iostream>

using namespace std;

int main()

{

int i,n,uc,cc;

n=0;

while(n<=20)

{

cout<< "\nEnter the number of matchsticks you want to pick: ";

cin>> uc;

if (uc>4)

{

cout<< "\n WRONG CHOICE....chose between 1-4 ";

}

else

{

cc=5-uc;

cout<<"\nComputer has picked up "<<cc<<" matchsticks\n";

n +=uc+cc;}

}

cout<< "\n\nITS YOUR TURN AND ONLY ONE MATCHSTICK IS LEFT.....\n\t\t YOU LOSE!";

return 0;

}

 
 
 

Comentários


Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square

Website created and maintained by ZHCET(Aligarh Muslim University) students

Copyright 2015 Code It !

Total visits:

bottom of page