the game of nim

Hello all,
I need help with the game of nim. This is an assignment so I do not want it done for my how else would I learn. I just need a little help figuring out where to start. the game has to be two players and no one can take the last stone. this is what the game should do play:create a nim game with random piles while the game isn’t over display the game board ask the current player for their move tell the game to make the move and if the move fails, display an error display the winning and losing player.

any help is appreciated
Try writing out the game is pseudo-code.

- Create two players
- Create some number of piles, each containing a random number of tiles.
- Decide whether the two players are human, or the computer plays both sides, or if it's a human verses the computer.
- Create a program loop that alternates turns between players.
- Create a function that determines if a player has won.




this is what I got but its not what I need per say. I cant figure out how to make the marbles random at the begging.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
int main()
{
int numOfMarbles, turn, marblesToPick;
cout<<"Enter the initial number of marbles in the pile: ";
cin>>numOfMarbles;
cout<<"Enter the number of player with first turn 1. Player-1. 2. Player-2: ";
cin>>turn;
while(turn < 1 || turn > 2)
{
cout<<"This game has only 2 players.\n";
cout<<"Enter the number of player with first turn 1. Player-1. 2. Player-2: ";
cin>>turn;
}
while(numOfMarbles != 1)
{
cout<<"Player-"<<turn<<": Enter the number of marbles to be picked in the range: 1 - "<<numOfMarbles/2<<": ";
cin>>marblesToPick;
while(marblesToPick < 1 || marblesToPick > numOfMarbles/2)
{
cout<<"The marbles you're trying to pick is out of range.\n";
cout<<"Player-"<<turn<<": Enter the number of marbles to be picked in the range: 1 - "<<numOfMarbles/2<<": ";
cin>>marblesToPick;
}
numOfMarbles -= marblesToPick;
turn = turn % 2 + 1;
}
cout<<"Only one marble left. And its Player-"<<turn<<"'s turn.\n";
cout<<"Player-"<<turn<<" lost the game.\n";
return 0;
}
I haven't done any C++ in a while but I think you might be looking for std::rand

1
2
std::srand(0); // Edt would help if i had explained this, this seeds the number generator, look into using ctime so it has a new seed at every time you run
int random = (std::rand()%100)+1 // random number between 1-100 (I think) 
Last edited on
use header file
1
2
3
4
5
6
7
8
9
10
11
#include<stdlib.h>
#include<time.h>
// then in int main use

srand(time_t(1)); // if you want fixed random value use whatever number form 1 to infinity
// otherwise use NULL for every time you compile value will be different

int a;
a=rand(); // random value is generated   
//if you want the number between 1 to 10 then use mod e.g a=rand()%10;
//if you want the number between 1 to 1000 then use mod e.g a=rand()%1000; 
Last edited on
Topic archived. No new replies allowed.