Nim 2 player?

This is the code that I have created for the game Nim. So far my code works perfectly fine (or at least exactly the way I want it to). Currently, the user can only play against the computer, but how would I go about making this a two player game?

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 /*      The game of NIM         */
#include<iostream>      // include two libraries
#include<cstdlib>
#include<ctime>
using namespace std;
int main()              // main starts the actual program
{
        //--------------------variable declarations-------------------------
        int num_objects = 23;
        int current_player = 1;
        int move;
        srand(time(NULL));
        char c;
        int Player1_score = 0;
        int Computer_score = 0;
        int minimum_move = 1;
        int maximum_move = 4;
        //-------------------- Beginning of the game loop -------------------
        cout << "How to play NIM: " << endl;
        cout << "The traditional game of Nim is played with a number of coins arranged in heaps: the number of coins and heaps is up to you. " << endl;
        cout << " There are two players. When it's a player's move he or she can take any number of coins from a single heap. They have to take at least" << endl;
        cout << " one coin, though, and they can't take coins from more than one heap. The winner is the player who makes the last move, so there are no" << endl;
        cout << "coins left after that move. (Some people play the game the other way around, with the last person to make a move losing the game, but we'll" << endl;
        cout << " ignore that version for the moment." << endl;
        cout << " It's clear that there is no luck involved here. You can work out the best move to make by cleverly predicting the sequence of moves that would follow it." << endl;

    do{
        cout << "Enter the number of objects you would like to begin with: ";
        cin >> num_objects;
        while(num_objects <= 0)
            {
            cout << "Invalid input, you cannot start with zero or negative objects. Enter another number: ";
            cin >> num_objects;
            }

        do {
           if (current_player == 1) {                                           // conditional: if
                cout << "Player 1, enter your move (1-3): ";            // output
                cin >> move;
                if(move >= minimum_move || move <= maximum_move || move <= num_objects)
                {
                    cout << "Player 1 removed " << move << endl;
                }                                                    // input
                while (move < minimum_move || move > maximum_move || move > num_objects) {
                cout << "Illegal move. \nEnter a new move: ";
                cin >> move;
                if(move >= minimum_move || move <= maximum_move || move <= num_objects)
                {
                    cout << "Player 1 removed " << move << endl;
                }
              }
           } else {                                                     // else part of conditional
                do {                                                    // make sure move is legal
                        move = 1 + rand() % 3;                          // random computer move
                        if(num_objects == 8 || num_objects == 7 || num_objects ==6)
                        {
                            move = num_objects % 5;
                        }
                            if(num_objects == 4)
                            {
                                move = 3;
                            }
                            if(num_objects == 3)
                            {
                                move = 2;
                            }
                            if(num_objects == 2)
                            {
                                move = 1;
                            }
                } while (move < minimum_move || move > maximum_move || move > num_objects);
                cout << "Computer removed " << move << endl;
           }

           num_objects = num_objects - move;                                    // implement the move
           cout << num_objects << " objects remaining. \n";
           current_player = (current_player + 1) % 2;                           // switch player
        } while (num_objects > 0);
        //--------------end of the game loop-----------------
        if(current_player == 0)
        {
            Player1_score = Player1_score + 1;
            cout << "Player 1 wins!" << endl;
        }
        if(current_player == 1)
        {
            Computer_score = Computer_score + 1;
            cout << "Computer wins!" << endl;
        }

        cout << "Score:" << endl;
        cout << "Computer: " << Computer_score << endl;
        cout << "Player 1: " << Player1_score << endl;

     // cout << "Nice going player " << current_player << ", you win!\n";
        cout << "Would you like to play again? [Y/N]: ";
        cin >> c;
    }
        while(c == 'Y' || c == 'y');

        if(c == 'N' || c == 'n')
        {
            return 0;
        }
}
It's basically simple.
You don't need if/else (line 37) anymore. Show which player like so:

cout << "Player " << current_player <<", enter your move (1-3): ";
Sorry, I didn't ask exactly what I meant to ask. I want to give the player the choice to play 2 player or against the computer. For example, in the beginning I will do this: cout << "1 player or 2 player?: "; cin << num_players;
This will be after declaring the variable num_players of course, but I'm not sure what I should add in my code to give the player the choice.
Still: change "Player 1" -> "Player " << current_player

To detect whether the computer or another player you may introduce a variable like
bool is_computer = false;

According to this (on line 37) you decide about the else branch, like so
if ( (current_player == 1) or (not is_computer) )

wherever something like cout << "computer" appears you then need an if clause like if(is_computer)...
Topic archived. No new replies allowed.