2nd c++ Problem

Write a Program for matchstick game between the computer and a user ..Your program should ensure that the computer always wins ..Rules for game are
1:-There are 21 matchsticks
2:-The computer asks the player to pick 1,2,3,4 matchsticks
3:-After the person picks,the computer does its picking.
whoever is forced to pick up the last match-stick loses the Game

Replies

  • pradeep_agrawal
    pradeep_agrawal
    Find below my piece of code for same.

    #include "stdio.h"
    
    int main() {
      int choice = 0;
      int sum = 0;
    
      while(sum != 20) {
        while((choice < 1) || (choice > 4)) {
          printf("Enter number of matchstick you want to pick (between 1 to 4): ");
          scanf("%d", &choice);
          if((choice < 1) || (choice > 4)) {
            printf("Invalid input\n");
          }
        }
        sum += choice;
        printf("Matchstick left: %d\n", 21 - sum);
        printf("Matchsticks picked by computer: %d\n", 5 - choice);
        sum += 5 - choice;
        printf("Matchstick left: %d\n", 21 - sum);
        scanf("%c", &choice);
        choice = 0;
      }
    
      printf("Only one stick left and your turn to pick. You lost.\n");
    
      return 0;
    }
    
    -Pradeep
  • gaurav.bhorkar
    gaurav.bhorkar
    Good, Pradeep.
  • Manish Goyal
    Manish Goyal
    pradeep_agrawal
    Find below my piece of code for same.

    #include "stdio.h"
    
    int main() {
      int choice = 0;
      int sum = 0;
    
      while(sum != 20) {
        while((choice < 1) || (choice > 4)) {
          printf("Enter number of matchstick you want to pick (between 1 to 4): ");
          scanf("%d", &choice);
          if((choice < 1) || (choice > 4)) {
            printf("Invalid input\n");
          }
        }
        sum += choice;
        printf("Matchstick left: %d\n", 21 - sum);
        printf("Matchsticks picked by computer: %d\n", 5 - choice);
        sum += 5 - choice;
        printf("Matchstick left: %d\n", 21 - sum);
        scanf("%c", &choice);
        choice = 0;
      }
    
      printf("Only one stick left and your turn to pick. You lost.\n");
    
      return 0;
    }
    
    -Pradeep
    Good ...you have correct output
  • Sahithi Pallavi
    Sahithi Pallavi
    You have done a good job pradeep...!

    keep it up..!

You are reading an archived discussion.

Related Posts

hi sir i want to organise some intersting and new events in my college technical event which was going to held on feb so please give me some ideas about...
Can Anybody tell me what is the meaning of 1. BE/B.Tech OR B.Arch/B.Planning 2 BE/B.Tech AND B.Arch/B.Planning
What is actual use of excel macros in offices ?
hello im pavan doin final year be from bangalore. i have taken up project on automatic electric meter reading using gsm network. Plz help me reagarding this
Hi, Starting a little project and I needed some help. Searched around google but its all rubbish now but this forum popped up a few times so I thought I...