explain this code in extreme detail. what each function and line is doing. expla
ID: 3785555 • Letter: E
Question
explain this code in extreme detail. what each function and line is doing.
explain class and object
explain it all as if talking to someone who is fairly new to programing.
#ifndef NIM_GAME_H
#define NIM_GAME_H
#include <cstdlib>
class nim
{
public:
nim();
nim(int pile1, int pile2, int pile3);
void print();
int get_players_turn();
bool take_turn(int pile, int stones_to_remove);
bool is_game_over();
private:
int players_turn;
int piles[3];
};
#endif
#include "nim_game.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
nim::nim()
{
players_turn=1;
srand(time(NULL));
piles[0] = rand() % 6 + 5;
piles[1] = rand() % 6 + 5;
piles[2] = rand() % 6 + 5;
}
nim::nim(int pile1, int pile2, int pile3)
{
players_turn=1;
piles[0]= pile1;
piles[1] = pile2;
piles[2] = pile3;
}
void nim::print(){
cout << "Player " << players_turn << ": " << piles[0] << " " << piles[1] << " " << piles[2]<< endl;
}
int nim::get_players_turn(){
return players_turn;
}
bool nim::take_turn(int pile, int stones_to_remove){
switch (pile){
case 1:
if ((piles[0] - stones_to_remove) >= 0 && ((piles[0]+piles[1]+piles[2]) > stones_to_remove)){
piles[0] -= stones_to_remove;
(players_turn==1)?(players_turn=2):(players_turn=1);
return true;
}
else return false;
break;
case 2:
if ((piles[1] - stones_to_remove) >= 0 && ((piles[0]+piles[1]+piles[2]) > stones_to_remove)){
piles[1] -= stones_to_remove;
(players_turn==1)?(players_turn=2):(players_turn=1);
return true;
}
else return false;
break;
case 3:
if ((piles[2] - stones_to_remove) >= 0 && ((piles[0]+piles[1]+piles[2]) > stones_to_remove)){
piles[2] -= stones_to_remove;
(players_turn==1)?(players_turn=2):(players_turn=1);
return true;
}
else return false;
break;
default:
return false;
}
}
bool nim::is_game_over(){
return ((piles[0]+piles[1]+piles[2])==1);
}
#include <iostream>
#include "nim_game.h"
#include <cstdlib>
using namespace std;
void test(){
nim game(11,10,5);
cout << game.is_game_over() << endl ;
cout << game. get_players_turn() << endl ;
game.take_turn(1,10);
cout << game.get_players_turn() << endl ;
game.print();
game.take_turn(2,10);
game.print();
game.take_turn(3,5);
game.print();
cout << game.is_game_over() << endl ;
}
void play(){
nim game(11, 10, 5);
while (game.is_game_over()== false){
game.print();
cout << "Player " << game.get_players_turn() << ", Enter the pile then the number of stones: ";
int pile=0, stones=0;
cin >> pile >> stones;
cout << endl;
game.take_turn(pile,stones)? : cout << "error" << endl;
}
cout << "Player ";
game.get_players_turn() == 1? cout << 2 : cout << 1;
cout << " wins!" << endl;
cout << "Player ";
game.get_players_turn() == 1? cout << 1 : cout << 2;
cout << " loses!" << endl;
}
int main(){
//nim game(11,10,5);
//test();
play();
return 0;
}
Explanation / Answer
-->The #ifndef and # define are used to prevent the code redundancy means it checks the code between #ifndef and #else if no #else then #endif, in the file if it encounter the code anywhere then it doesn't get used otherwise it will get used.
Then in that one class names nim is declared and in the class it contains public and private member functions and variables. Then comes to end of this by #endif.
--> In the next part it includes nim_game.h code then there are couple of libraries included inorder to properly work the general functions like cin and cout etc..
Then the using namespace std is used for the instance of the variable which means it gives the flexibility that we can add cout and cin directly without need to use std::cout like this.
Then it will call the nim function i.e. num::num() using. The scope (::) resolution operator we are calling nim() function which is defined outside. Then in that we are are initialising the variables and member functions which are players turn and the array initialising.
Then we are here intialising another member function which we declared in the first one. Here in this part we are intialising all the member functions and the variables with values. Now here thr parameterized nim(pile1,pil2,pile3) will he intialized with the values players_turn and pile array.
Then it will come to void print() function in this we have written the print statement with the respective values.
The players_turn () function is initialised.
Then after that take_turn() function will be accessed in this there are 3 cases. In case 1 it checks the value in 0th location in pile array - stones to remove is greater than 0 and values in array location 0,1,2 is greater than stones_to_remove then because of the next statement value in the 0th location will be substred with stone_ no.
Then next (players_turn==1)?(players_turn=2):(players_turn=1); this statement checks that players_turn value if it is 1 then it returns true. Else condition fails then because of the break it comes out from that case.
Similarly case 2 and case 3 are also same like case 1 but with array elements of 1St position and 2nd position.
Then there is a function is_game_over it will return the value.
Then here comes the end of this part.
--> in this it has containing Library files for basic opera like printing and reading inputs.
Then test() is called and has given the arguments.Then cout statements prints the given data and similarly it will pass all the remaining arguments to the other member functions declared.
--> Next in the main code part we are calling the classes with the specified arguments.
2) Class :- A class is a blueprint of a datatype as the class contains only the information of what the variables and memeber functions and in the later on we intilise values.
Object:- An objects is a real-world entity which it is used to call the objects and classes etc..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.