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;
}