#include <iostream>
#include <ctime> //for time method
#include <cstdlib> //for rand & srand
using namespace std;
// #1 The program starts by displaying an introduction of the game, including its rules
void HowToPlay() {
cout << "How to play the Game of Nim" << endl;
cout << "Initially there is a pile of n stones, where n is a random integer between 10 & 21 (inclusive)" << endl;
cout << "Each player (the computer vs. the human player) alternates turns removing 1, 2, or 3 stones." << endl;
cout << "The player who makes the last move loses.\n" << endl;
return;
}
int main() {
HowToPlay();
// #2 It then asks the human player to choose between an easy mode and a hard mode.
char mode;
cout << "Would you like to play Easy mode or Hard mode? (e for easy h for hard): \n";
cin >> mode;
if (mode == 'e') {
cout << "You have chosen the easy mode." << endl;
} else {
cout << "You have chosen the hard mode." << endl;
}
// #3 The size of the pile of stones should be a random number between 10 and 21, inclusive
srand (time(0));
int randNumStones = 10 + rand() % 11;
cout << "\nThe initial number of stones is " << randNumStones << endl;
/* #4 The program chooses a player randomly to start the game (i.e. the computer and the
human player should each have an equal chance of taking the first move).*/
srand (time(0));
cout << "Now a roll of the die to determine who plays first...\n" << endl;
int turn = 1 + rand() % 2;
if (turn == 1) {
}
else {
cout << "The computer plays first." << endl;
}
/* #5 When it is the human player’s turn, the human player should be allowed to remove 1, 2, or 3 stones
but never more than the number of stones left.*/
// cout << "It is your turn. How many stones would you like to remove? (1-3): ";
int lessStones, currentStones = randNumStones;
while (currentStones >= 1) {
if(turn == 1){
while(true) {
cout << "Your turn. How many stones would you like to remove? \n";
cin >> lessStones;
if(lessStones >= 1 && lessStones <= 3 && lessStones <= currentStones) {
cout << "You removed " << lessStones << " from the pile" << endl;
currentStones = currentStones - lessStones;
cout << "The pile now has " << currentStones << " stones left.\n\n";
break;
} else {
cout << "choose only 1-3 stones" << endl;
}
}
turn = 2;
}
else {
// #6 computer behavior
if(mode == 'e') {
int randNum = 1 + rand() % 3;
if (randNum > currentStones) {
randNum = 1 + rand() % currentStones;
}
currentStones = currentStones - randNum;
cout << "Computer removed " << randNum << " from the pile" << endl;
cout << "The pile now has " << currentStones << " stones left.\n\n";
} else {
if(currentStones %4 == 1) {
lessStones = 1;
} else if(currentStones %4 == 3) {
lessStones = 2;
} else if (currentStones %4 == 2) {
lessStones = 1;
} else {
lessStones = 3;
}
currentStones = currentStones - lessStones;
cout << "Computer removed " << lessStones << " from the pile" << endl;
cout << "The pile now has " << currentStones << " stones left.\n\n";
}
turn = 1;
}}
if (turn == 1) {
cout << "You won!" << endl;
} else {
cout << "You lose" << endl;
}
}
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}