Question

The game of Nim: This is a well-known game with a number of variants. The following...

The game of Nim: This is a well-known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses.

Write a C program in which the computer plays against a human opponent. Generate a random integer between 10 and 100 to denote the initial size of the pile. Generate a random integer between 0 and 1 to decide whether the computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide whether the computer plays smart or stupid. In stupid mode the computer simply takes a random legal value (between 1 and n / 2) from the pile whenever it has a turn. In smart mode the computer takes off enough marbles to make the size of the pile a power of two minus 1—that is, 3, 7, 15, 31, or 63. That is always a legal move, except when the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move.

You will note that the computer cannot be beaten in smart mode when it has the first move, unless the pile size happens to be 15, 31, or 63. Of course, a human player who has the first turn and knows the winning strategy can win against the computer.

Note: Loops can be written using while, for and do-while statements. For this question, you can’t use same loop statement twice (For example: if you have to use nested loop, you can’t use for loop inside a for loop. It has to be for loop inside a while or vice versa)!

This is what I have so far.....

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

int humanplays(int pile)
{
   printf("How many? ");
   int take;
   scanf("%d", &take);

   return pile - take;
}
//In stupid mode the computer simply takes a random legal value (between 1 and n / 2)
//from the pile whenever it has a turn.
int computerplays(int pile, int smart)
{
   if(smart)
   {

   }
   else
   {
       int take = rand() % (pile/2) + 1;
       printf("Computer stupidly takes %d\n", take);
       return pile - take;
   }
}

int main()
{
   srand(time(NULL));
   int pile = rand() % 91 + 10;
//   printf("got %d\n", pile);

//zero is computer 1 is human
   int next = rand() % 2;
//decides whether computer is smart(1) or stupid(0)
   int smart =rand() % 2;
   smart = 0;

   printf("%d in pile, next = %d smart = %d\n", pile, next, smart);

   while(pile > 0)
   {
       pile = humanplays(pile);
       printf("%d remain\n", pile);
       if(pile == 1)
       {
           printf("Human wins!");
           break;
       }
       pile = computerplays(pile, smart);
       printf("%d remain\n", pile);
       if(pile == 1)
       {
           printf("Computer wins!");
           break;
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
// Generating the random size, smartness and turn
int pile = rand()%91 + 10;
int next = rand()%2;
int smart = rand()%2;
//print the smartness
if(smart==1)
printf("Computer is smart");
else
printf("Computer is stupid");
while(pile > 1)
{
// Print the size
printf(" The size of the pile is %d ", pile);
if(next == 0)
{
int value = 0;
// Take input till a valid value is entered
do
{
printf(" Its your turn. Enter a legal value between 1 and %d ", pile/2);
scanf("%d", &value);
} while (value <= 0 || value > pile/2);
pile = pile - value;
next = 1;
}
else
{
// The computer's turn
printf(" Its computer's turn! ");
if(smart == 0)
{
// Select random number of withdrawl
int value = rand()%(pile/2) + 1;
printf(" The computer has taken out %d marbles. ", value);
pile = pile - value;
}
else
{
int power = 1;
// In order to find out which is the largest power of 2 that can be achieved
for(power = 2; power < pile + 1; power = power*2)
{
if(pile <= power - 1)
{
break;
}
}
// If already at a stage of power of 2 - 1
if(pile == power - 1)
{
int value = rand()%(pile/2) + 1;
printf(" The computer has taken out %d marbles. ", value);
pile = pile - value;
}
// Achieve the required value
else
{
power = power/2;
int value = pile - (power - 1);
printf(" The computer has taken out %d marbles. ", value);
pile = pile - value;
}
}
next = 0;
}
}
if(next == 0)
printf(" Computer wins ");
else
printf(" You win ");
return 0;
}

Add a comment
Know the answer?
Add Answer to:
The game of Nim: This is a well-known game with a number of variants. The following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need Help with homework problem writing the game of nim in Python IDLE. Its a well...

    Need Help with homework problem writing the game of nim in Python IDLE. Its a well known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses. Instructions...

  • In python this is what I have for task 3, but I don't know how to...

    In python this is what I have for task 3, but I don't know how to fix it so that the computer stops picking from the pile once it reaches zero Task 3: Extend the game to have two piles of coins, allow the players to specify which pile they take the coins from, as well as how many coins they take Finish Task 3 and try three test cases import random remaining_coins1 = 22 remaining_coins2 = 22 while 1:...

  • 24. The game called “Nim” goes like this: there are a pile of five stones on...

    24. The game called “Nim” goes like this: there are a pile of five stones on the ground. Player 1 can take either 1 or 2 stones. Then player 2 can take either 1 or 2 stones. They continue taking either 1 or 2 stones in turn until all the stones are gone. The player who takes the last stone (or stones) wins. a. Model this as an extensive form game and find the subgame perfect Nash equilibria. b. What...

  • A subtraction game Subtraction games are two-player games in which there is a pile of objects,...

    A subtraction game Subtraction games are two-player games in which there is a pile of objects, say coins. There are two players, Alice and Bob, who alternate turns subtracting 4.9. A SUBTRACTION GAME 19 from the pile some number of coins belonging to a set S (the subtraction set). Alice goes first. The first player who is unable to make a legal move loses. For example, suppose the initial pile contains 5 coins, and each player can, on his turn,...

  • 1. NIM game. This is a different version or easier version of NIM game Consider a pile of 5 matchsticks. Two people take turns removing 1 or 2 sticks each time from this pile. Suppose both players pl...

    1. NIM game. This is a different version or easier version of NIM game Consider a pile of 5 matchsticks. Two people take turns removing 1 or 2 sticks each time from this pile. Suppose both players play smartly (nobody plays a fool move trying to let the opponent wins. But there is only one winner anyway) a)If the person getting the last stick wins, will the first player win? Why? Show the steps the first and second player will...

  • can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() {...

    can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() { return (rand() % 6+1); } void askYoNs(){ cout<<"Do you want to roll a dice (Y/N)?:"<<endl; } void printScores(int turnTotal,int humanTotal,int compTotal){ int player; int human; if(player==human){ cout<<"Your turn total is "<<turnTotal<<endl; } else{ cout<<"computer turn total is "<<turnTotal<<endl; } cout<<"computer: "<<compTotal<<endl; cout<<"human: "<<humanTotal<<endl; cout<<endl; } int human; int changePlayer(int player){ if(player==human) return 1; return human; } int process(int& turnTotal,int roll,int curr_player,int& humanTotal,int& computerTotal){ if(roll==2...

  • please help me fix this. I'm getting it all mixed up in user defined functions. here's...

    please help me fix this. I'm getting it all mixed up in user defined functions. here's the question. This week we'll write a C program that lets the user play the game of Rock, Paper, Scissors against the computer. Your program will call a user-defined function named display_rules that displays the rules of the game. It then proceeds to play the game. To determine the winner, your program will call another user- defined function called determine_winner that takes two integer...

  • can someone help me fix my jeopardy dice game I am having a hard time figuring...

    can someone help me fix my jeopardy dice game I am having a hard time figuring out what i am doing wrong #include #include using namespace std; //this function makes a number between 1 and 6 come out just like a dice int rollDie() { return (rand() % 6+1); } //this function asks the user with a printed statement if they want to roll the dice and gives instructions for yes and no (y/n) void askYoNs(){ cout<<"Do you want to...

  • Answer the following Nim game style questions. (Robert's Game) In this game, two players take turns removing stones from a pile that begins with n stones. The player who takes the last stone w...

    Answer the following Nim game style questions. (Robert's Game) In this game, two players take turns removing stones from a pile that begins with n stones. The player who takes the last stone wins. A player removes either one stone or p stones, where p is a prime dividing the number of stones in the pile at the start of the turn For which n does the First Player have a winning strategy? A winning strategy for the First Player...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT