0 喜歡 0 不喜歡
1.2k 瀏覽

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;
}
[練習] 多選題 - 最新提問 分類:Chapter 3: Branching, Looping and Functions | 用戶: (5.9k 分)
ID: 35364 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 1.2k 瀏覽

18 個回答

0 喜歡 0 不喜歡
100/100 最新回答 用戶: (192 分)
0 喜歡 0 不喜歡
100/100 最新回答 用戶: (183 分)
0 喜歡 0 不喜歡
100/100 最新回答 用戶: (281 分)
0 喜歡 0 不喜歡
100/100 最新回答
0 喜歡 0 不喜歡
100/100 最新回答 用戶: (244 分)
0 喜歡 0 不喜歡
100/100 最新回答 用戶: (221 分)
0 喜歡 0 不喜歡
100/100 最新回答 用戶: (273 分)
0 喜歡 0 不喜歡
100/100 最新回答 用戶: (220 分)
0 喜歡 0 不喜歡
100/100 最新回答 用戶: (283 分)
0 喜歡 0 不喜歡
100/100 最新回答
修改於 用戶:
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:108.162.216.18
©2016-2024

相關問題

0 喜歡 0 不喜歡
17 回答
[練習] 克漏字填空 - 最新提問 11月 26, 2017 分類:Chapter 3: Branching, Looping and Functions | 用戶: nat236919 (5.9k 分)
ID: 34344 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 1k 瀏覽
0 喜歡 0 不喜歡
22 回答
[練習] Coding (C) - 最新提問 12月 30, 2017 分類:Chapter 3: Branching, Looping and Functions | 用戶: nat236919 (5.9k 分)
ID: 40236 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 2.5k 瀏覽
0 喜歡 0 不喜歡
18 回答
[練習] 克漏字填空 - 最新提問 12月 23, 2017 分類:Chapter 3: Branching, Looping and Functions | 用戶: nat236919 (5.9k 分)
ID: 39813 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 1.1k 瀏覽
0 喜歡 0 不喜歡
16 回答
[練習] Coding (C) - 最新提問 12月 23, 2017 分類:Chapter 3: Branching, Looping and Functions | 用戶: nat236919 (5.9k 分)
ID: 39810 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 2k 瀏覽
12,783 問題
183,443 回答
172,219 留言
4,824 用戶