0 thích 0 k thích
568 đã xem

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 - đã hỏi trong Chapter 3: Branching, Looping and Functions bởi (5.9k điểm)
ID: 35364 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 568 đã xem

18 Trả lời

0 thích 0 k thích
100/100 trả lời bởi (192 điểm)
0 thích 0 k thích
100/100 trả lời bởi (183 điểm)
0 thích 0 k thích
100/100 trả lời bởi (281 điểm)
0 thích 0 k thích
100/100 trả lời
0 thích 0 k thích
100/100 trả lời bởi (244 điểm)
0 thích 0 k thích
100/100 trả lời bởi (221 điểm)
0 thích 0 k thích
100/100 trả lời bởi (273 điểm)
0 thích 0 k thích
100/100 trả lời bởi (220 điểm)
0 thích 0 k thích
100/100 trả lời bởi (283 điểm)
0 thích 0 k thích
100/100 trả lời
đã sửa bởi
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.7.105
©2016-2024

Những câu hỏi liên quan

0 thích 0 k thích
17 trả lời
[Exercise] Fill in the blank - đã hỏi ngày 26 tháng 11 năm 2017 trong Chapter 3: Branching, Looping and Functions bởi nat236919 (5.9k điểm)
ID: 34344 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 485 đã xem
0 thích 0 k thích
22 trả lời
[Exercise] Coding (C) - đã hỏi ngày 30 tháng 12 năm 2017 trong Chapter 3: Branching, Looping and Functions bởi nat236919 (5.9k điểm)
ID: 40236 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 1k đã xem
0 thích 0 k thích
18 trả lời
[Exercise] Fill in the blank - đã hỏi ngày 23 tháng 12 năm 2017 trong Chapter 3: Branching, Looping and Functions bởi nat236919 (5.9k điểm)
ID: 39813 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 605 đã xem
0 thích 0 k thích
16 trả lời
[Exercise] Coding (C) - đã hỏi ngày 23 tháng 12 năm 2017 trong Chapter 3: Branching, Looping and Functions bởi nat236919 (5.9k điểm)
ID: 39810 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 775 đã xem
12,783 câu hỏi
183,443 trả lời
172,219 bình luận
4,824 thành viên