0 like 0 dislike

Past two weeks, we have learned how to create a simple game by using C language. We have also developed an automatic player (A.I.) with. According to the code given,

Question: Please set number(s) given below for numberOfSticks in which we take 3 sticks every turn, the computer will be forced to take 1 stick every turn as well. At the end, we will be the winner.

Choices: (Select all answers that are correct)

A: 10
B: 13
C: 12
D: 28
E: 25

CODE:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

	int numberOfSticks = ??; /* number of the sticks remaining in the game */
	int playerOnTurn = 0; /* player on turn, either 0 or 1 */

	/* game */
	/* Unless this game is over, let player on turn take some sticks... */
	while(numberOfSticks > 0){
		/* some game information and asking the user */
		printf("\n--------------------------------------------\n");
		printf("Player %d is on turn. %d stick(s) remaining\n", playerOnTurn + 1, numberOfSticks);
		/*Lets draw the sticks to the user */
		/*like this 5 sticks -> ||||| */
		int i;
		for(i = 0 ; i < numberOfSticks; i++ )
			printf("|");
		printf("\n");
        int sticksHeWantsToTake;
		
		if(playerOnTurn == 0){
            /* Player's turn starts here */
            printf("How many sticks would you like to take? Input 1 - 3.\n");
    		
    		/* read the number he wants to take */
    		scanf("%d", &sticksHeWantsToTake);
    		
    		while(sticksHeWantsToTake > 3 || sticksHeWantsToTake < 1){
    			/* The damn user gave us an invalid number....what to do now??????? */
    			printf("You are trying to make me angry. That number is not valid.\n");
    			scanf("%d", &sticksHeWantsToTake);
    		}
    		/*End of the players turn */
        }
        //if(playerOnTurn == 1){
        else {
            /* The computer is on turn */
            /*2 6 or 10 remaining sticks*/
            /*
            if(numberOfSticks == 2 || numberOfSticks == 6 || numberOfSticks == 10 || numberOfSticks == 14)
               sticksHeWantsToTake = 1;
            if(numberOfSticks == 3 || numberOfSticks == 7 || numberOfSticks == 11)
               sticksHeWantsToTake = 2;
            if(numberOfSticks == 4 || numberOfSticks == 8 || numberOfSticks == 12)   
               sticksHeWantsToTake = 3;
            if(numberOfSticks == 1 || numberOfSticks == 5 || numberOfSticks == 9 || numberOfSticks == 13)
               sticksHeWantsToTake = 1; //we are going to lose anyways, so just take 1
            */      
            
            if(numberOfSticks % 4 == 2)
               sticksHeWantsToTake = 1;
               
            if(numberOfSticks % 4 == 3)
               sticksHeWantsToTake = 2;
               
            if(numberOfSticks % 4 == 0)   
               sticksHeWantsToTake = 3;
               
            if(numberOfSticks % 4 == 1)
               sticksHeWantsToTake = 1; //we are going to lose anyways, so just take 1
            printf("The computer decided to take %d stick(s)\n", sticksHeWantsToTake);
        }
		
		/* Apply the turn to the game */
		//numberOfSticks = numberOfSticks - sticksHeWantsToTake; 
		numberOfSticks -= sticksHeWantsToTake; 
		
		/* Change the player on turn  for the next turn */
		playerOnTurn++;
		playerOnTurn %= 2; //playerOnTurn = playerOnTurn % 2;
		
	} 
	
	/* Let the players know that the game is over and someone won */
	printf("No more sticks remaining, the game is over. Player %d won! Congratulations!\n", playerOnTurn + 1);
    system("PAUSE");
	return 0;
}
[Exercise] Multichoice - asked in Chapter 3: Branching, Looping and Functions by (5.9k points)
Available when: Unlimited - Due to: Unlimited
| 89 views

18 Answers

0 like 0 dislike
Hidden content!

*
100/100 answered by (192 points)
0 like 0 dislike
Hidden content!

100/100 answered by (183 points)
0 like 0 dislike
Hidden content!
*
100/100 answered by (281 points)
0 like 0 dislike
Hidden content!
*
**
100/100 answered
0 like 0 dislike
Hidden content!
*
100/100 answered by (244 points)
0 like 0 dislike
Hidden content!
*
**
100/100 answered by (221 points)
0 like 0 dislike
Hidden content!
*
**
100/100 answered by (273 points)
0 like 0 dislike
Hidden content!

*
100/100 answered by (220 points)
0 like 0 dislike
Hidden content!

*
100/100 answered by (283 points)
0 like 0 dislike
Hidden content!

100/100 answered
edited by
Get it on Google Play Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.158.78.100
©2016-2019

Related questions

0 like 0 dislike
17 answers
[Exercise] Fill in the blank - asked Nov 26, 2017 in Chapter 3: Branching, Looping and Functions by nat236919 (5.9k points)
Available when: Unlimited - Due to: Unlimited
| 105 views
0 like 0 dislike
22 answers
[Exercise] Coding (C) - asked Dec 30, 2017 in Chapter 3: Branching, Looping and Functions by nat236919 (5.9k points)
Available when: Unlimited - Due to: Unlimited
| 90 views
0 like 0 dislike
18 answers
[Exercise] Fill in the blank - asked Dec 23, 2017 in Chapter 3: Branching, Looping and Functions by nat236919 (5.9k points)
Available when: Unlimited - Due to: Unlimited
| 49 views
0 like 0 dislike
16 answers
[Exercise] Coding (C) - asked Dec 23, 2017 in Chapter 3: Branching, Looping and Functions by nat236919 (5.9k points)
Available when: Unlimited - Due to: Unlimited
| 75 views
8,792 questions
72,435 answers
47,172 comments
2,047 users