0 like 0 dislike
1.2k views

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)
ID: 35364 - Available when: Unlimited - Due to: Unlimited
| 1.2k views

18 Answers

0 like 0 dislike
100/100 answered by (269 points)
0 like 0 dislike
100/100 answered by (243 points)
0 like 0 dislike
100/100 answered by (269 points)
0 like 0 dislike
100/100 answered by (153 points)
edited by
0 like 0 dislike
100/100 answered by (252 points)
0 like 0 dislike
100/100 answered by (290 points)
0 like 0 dislike
100/100 answered by (271 points)
0 like 0 dislike
100/100 answered by (268 points)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.6.148
©2016-2024

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)
ID: 34344 - Available when: Unlimited - Due to: Unlimited
| 1k 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)
ID: 40236 - Available when: Unlimited - Due to: Unlimited
| 2.5k 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)
ID: 39813 - Available when: Unlimited - Due to: Unlimited
| 1.1k 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)
ID: 39810 - Available when: Unlimited - Due to: Unlimited
| 2k views
12,783 questions
183,443 answers
172,219 comments
4,824 users