explain the nim game

alright so I am still pretty new at c++. I came across this and really didnt understand it fully. I was wondering if someone could comment it out or explain what is happening through out. Any help would be appreciated even if you explain a small chunk. I have a decent idea about functions but still would like to see how someone would explain it and give me insight on what I dont understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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;

     (pile1>=5);
     (pile2>=5);
     (pile3>=5);

     (pile1<=11);
     (pile2<=11);
     (pile3<=11);

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


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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); // remove 10 stones from pile 1
cout << game.get_players_turn() << endl ;
game.print(); // Player 2: 1 10 5
game.take_turn(2,10); 
game.print(); // Player 1: 1 0 5
game.take_turn(3,5); 
game.print(); // Player 2 : 1 0 0
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 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;
}
It would be better if you stated which lines you don't understand. The code is relatively straight forward. I'm not going to sit here and explain every line.

BTW, nim.cpp lines 22-28 don't do anything.


Last edited on
Lines 22-28 do not do anything.

pile1 is an integer
And
(pile1>=5)
Gives a value of a bool however it is not saved anywhere or put in a condition statement.

Which part would you like explained? Would you like the entire code explained or just part of it?
Mainly the class and what's being called where from it
Topic archived. No new replies allowed.