Writing a game

int compturn(int sticks)
{
int choice; //Represents computer's choice
do {
srand(time(0));//seed the random number generator
int choice = rand() % 5;
cout<<"The computer takes" <<choice<<" "<<endl;

} while (sticks - choice < 1);
return (choice);
}

This is just a snippet of my code for writing a game, but can anyone tell me why it is not working. I am trying to set up the game so when it is the computers turn it takes a random number between 1 & 4 and then returns that value subtracted from a user determined starting value back to the main program.The error occurs on the 2nd line in the above code. Error message is to few functions, which means nothing to me.


My function for the users choice runs just fine.

int userturn(int sticks)
{
//Interacts with the user, allowing a choice for the game of Nim
int choice;
cin >> choice;
//Loop until a proper choice is made
while (choice <1 || choice > 4 || sticks - choice <= 0)
{
if (choice < 1 || choice > 4) //Can only choose between 1 and 4
{
cout << "Your choice must be between 1 and 4" << endl;
cout << "Enter another choice: " << endl;
cin >> choice;
}
else if (sticks - choice < 0) //User has taken too many markers
{
cout << "You cannot take more sticks than the pile contains" << endl;
cout << "Enter another choice: " << endl;
cin >> choice;
}
}
}
Use code tags and clean up your code like this:

1
2
3
4
5
6
7
8
9
int compturn( int sticks ){
     int choice; //Represents computer's choice
     do {
          srand(time(0));//seed the random number generator
          int choice = rand() % 5;
          cout<<"The computer takes" <<choice<<" "<<endl;
     } while (sticks - choice < 1);
     return (choice);
} 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int userturn( int sticks ){
     //Interacts with the user, allowing a choice for the game of Nim
     int choice;

     cin >> choice;

     //Loop until a proper choice is made
     while (choice <1 || choice > 4 || sticks - choice <= 0) {
          if (choice < 1 || choice > 4) { //Can only choose between 1 and 4
               cout << "Your choice must be between 1 and 4" << endl;
               cout << "Enter another choice: " << endl;
               cin >> choice;
          } else if (sticks - choice < 0) { //User has taken too many markers
               cout << "You cannot take more sticks than the pile contains" << endl;
               cout << "Enter another choice: " << endl;
               cin >> choice;
          }
     }
}


What does you compiler say is wrong?
So this is the part of the code for a game I am creating. I am getting errors for the functions userturn and compturn. Error message says that there are too many arguments for this function at this point in file. I have a feeling it is a simple error that I am just not catching. Any help would be appreciated.


1
2
3
4
5
6
7
8
9
10
int compturn(){
     int sticks;
     int choice; //Represents computer's choice
     do {
          srand(time(0));//seed the random number generator
          int choice = rand() % 5;
          cout<<"The computer takes" <<choice<<" "<<endl;
     } while (sticks - choice < 1);
     return (choice);
} 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int userturn()
{
     //Interacts with the user, allowing a choice for the game of Nim
     int choice;
     int sticks;
     cout<<"How mant sticks do you take?"<<endl;
     cin >> choice;

     //Loop until a proper choice is made
     while (choice <1 || choice > 4 || sticks - choice <= 0) {
          if (choice < 1 || choice > 4) { //Can only choose between 1 and 4
               cout << "Your choice must be between 1 and 4" << endl;
               cout << "Enter another choice: " << endl;
               cin >> choice;
          } else if (sticks - choice < 0) { //User has taken too many markers
               cout << "You cannot take more sticks than the pile contains" << endl;
               cout << "Enter another choice: " << endl;
               cin >> choice;
               return choice;
          }
     }
}


(oh and thanks helios)
Last edited on
please use code tags [co de] [/co de] and oranize more
don't know what you are talking about!! Very new at this!!
[code]
std::cout <<"Your code";
std::cout <<" here.";
[/code]

becomes

1
2
std::cout <<"Your code";
std::cout <<" here.";
Last edited on
We need to see you function declaration statements too.
Look at your first and second post - in your first post the function is taking one arguement per function, and in your second post each function is taking no arguments. I imagine when you are calling your function from main() you are calling them with arguments but the function is not expecting any.
Okay so I have gotten the game to run, but every time the computer takes one when it is supposed to pick a random number between 1 & 4. Any advice?

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
#include <iostream>
#include <string>
#include <time.h>

using namespace std;

int comp(int stones);
int user(int stones);
int random()
{
    int random;
    random=(rand()%5);
    return random;
}
void welcome()
{
                cout << "Welcome to Nim"<<endl;
                cout << "The user starts first by entering the number of stones"<<endl;
                cout << "they wish to takes away (Between 1 and 4)"<<endl;   
                cout << "Then the computer will take away another amount between 1 and 4"<<endl;
                cout << "Who ever is the player to take the last stone loses"<<endl;

}
int comp(int stones)
  //Returns a random number between 1 and 4 representing the computer's choice
{
  int choice; //Represents computer's choice
  do {
    choice = (random() % 5) ;
  } while (stones - choice < 0);
  return choice;
}  
  
int main ()
{        
     welcome();

     bool winner = false;  //Variable indicates when we have a winner
     int computer;
   //Variable represents the computer's choice
     int stones;
     cout<<"How many stones shall we start with?"<<endl;
     cin>>stones; 
     cout << "Welcome to the game of Nim.  There are " << stones <<" stones."<< endl;

  while (!winner)
  {

    cout <<"Enter how many you would like to take away: "<< endl;
    //User makes a choice
    stones -= user(stones);
    cout <<"The number of stones left is " << stones << "." << endl;
    if (stones == 1) 
    {
      winner = true;
      cout << "You win!" << endl;
      cin.get();
    }
    else
    {
      //Computer's turn
      computer = comp(stones);
      stones -= computer;
      cout << "Computer chooses " << comp << endl;
      cout << "The number of stones left is " << stones << "." << endl;
      if (stones == 1)
      {
    winner = true;
        cout << "Computer wins" << endl;
        cin.get();
      }
    }
  }
}
int user(int stones)
{
  //Interacts with the user, allowing a choice for the game of Nim
  int choice;
  cin >> choice;
  cin.get();
  //Loop until a proper choice is made
  while (choice <1 || choice > 4 || stones - choice <= 0)
  {
    if (choice < 1 || choice > 4) //Can only choose between 1 and 3
    {
      cout << "Your choice must be between 1 and 4" << endl;
      cout << "Enter another choice: " << endl;
      cin >> choice;
    }
    else if (stones - choice <= 0) //User has taken too many markers
      {
    cout << "You must leave at least one stone" << endl;
        cout << "Enter another choice: " << endl;
        cin >>  choice;
        cin.get();
      }
    return choice;
  }
    return choice;
}

Also I would appreciate any advice on making the main program shorter by incorporating more functions.
% gives you the remainder of division.

IE: 5 % 3 = 2
9 % 3 = 0

Therefore, rand() % 5 gives you a number [0-4].

If you want the number to be 1-4, you need to can do the following:

(rand() % 4) + 1;

1
2
rand() % 4  // <- [0-3]
+ 1         // <- [1-4] 
Okay I did that but the computer is still picking 1 every time!! Am I missing something?
Are you still using srand(). If your program looks anything like the last one above then no you're not...
Okay so I have now inserted the srand() but the computer is still selecting one stone everytime. I am lost!!

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
#include <iostream>
#include <string>
#include <time.h>

using namespace std;

int comp(int stones);
int user(int stones);
int random()
{
    int random;
    srand(time(0));     
    random=(rand()%4);
    +1;
    return random;
}
void welcome()
{
                cout << "Welcome to Nim"<<endl;
                cout << "The user starts first by entering the number of stones"<<endl;
                cout << "they wish to takes away (Between 1 and 4)"<<endl;   
                cout << "Then the computer will take away another amount between 1 and 4"<<endl;
                cout << "Who ever is the player to take the last stone loses"<<endl;

}
int comp(int stones)
  //Returns a random number between 1 and 4 representing the computer's choice
{
  int choice; //Represents computer's choice

    srand(time(0));     
    choice = (rand() % 4);
    +1 ;
  return choice;
}  
  
int main ()
{        
     welcome();
     random();

     bool winner = false;  //Variable indicates when we have a winner
     int computer;
   //Variable represents the computer's choice
     int stones;
     cout<<"How many stones shall we start with?"<<endl;
     cin>>stones; 
     cout << "Welcome to the game of Nim.  There are " << stones <<" stones."<< endl;

  while (!winner)
  {

    cout <<"Enter how many you would like to take away: "<< endl;
    //User makes a choice
    stones -= user(stones);
    cout <<"The number of stones left is " << stones << "." << endl;
    if (stones == 1) 
    {
      winner = true;
      cout << "You win!" << endl;
      cin.get();
    }
    else
    {
      //Computer's turn
      computer = comp(stones);
      stones -= computer;
      cout << "Computer chooses " << comp << endl;
      cout << "The number of stones left is " << stones << "." << endl;
      if (stones == 1)
      {
    winner = true;
        cout << "Computer wins" << endl;
        cin.get();
      }
    }
  }
}
int user(int stones)
{
  //Interacts with the user, allowing a choice for the game of Nim
  int choice;
  cin >> choice;
  cin.get();
  //Loop until a proper choice is made
  while (choice <1 || choice > 4 || stones - choice <= 0)
  {
    if (choice < 1 || choice > 4) //Can only choose between 1 and 3
    {
      cout << "Your choice must be between 1 and 4" << endl;
      cout << "Enter another choice: " << endl;
      cin >> choice;
    }
    else if (stones - choice <= 0) //User has taken too many markers
      {
    cout << "You must leave at least one stone" << endl;
        cout << "Enter another choice: " << endl;
        cin >>  choice;
        cin.get();
      }
    return choice;
  }
    return choice;
}
1) only srand() once at the start of your program. Do not srand every time you generate a random number.

2) You misunderstood my previous post:
1
2
3
4
5
6
// do this:
random = (rand() % 4) + 1;  // generates [1-4]

// not this:
random = rand() % 4;  // generates [0-3]
+1;                   // does nothing -- empty statement 


3) You don't need to have rand() in two spots like this. You already wrote a random() function. Use it. You can get rid of lines 31-33 and replace them with a call to your random() function. Duplicate code is bad

4) Look carefully at line 68. Note that you're not outputting what you think you are.

5) Putting the return statement on line 101 defeats the entire point of that loop.
Last edited on
Thanks so much for all the advice!!
Can anyone hint at what I am missing. This function is supposed to return a random number between 1 &4 unless there are 4 or less stones left in the game then it is supposed to take all the pieces. However it is taking 0 pieces when there are 4 or less stones remaining, also it sometimes takes 0 pieces and I thought the way I had it set up that, that wasn't possible.

1
2
3
4
5
6
7
8
9
10
int comp(int stones)//Gives a random value between 1 & 4 when it is the computers turn
{
    srand(time(0)); 
    int choice;
    if (stones<=4)
       choice==stones;
    else
      choice=(rand()%4)+1; 
    return choice ;
}  
Last edited on
1) Don't call srand here. See my previous post. srand() should be called once by your program and only once.

2) Look close at line 6. == != =

3) If the goal is for the loser to pick the last stone, it makes much more sense to pull "stones-1" if there are 5 or less stones remaining. That way the computer at least has a chance to win instead of jumping on the first opportunity to lose.
Okay so on to a new problem now. The program runs great and does exactly what it is supposed too except when I try to put a do while function in so that the user can play again if they like. When the loop is executed it just repeats a cout line over and over again until I exit out.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main ()//Plays the game of Nim
{        
    welcome();
    int computer,stones,again;
    gameplay();   
    do{
                  int again,computer,stones;
                  cout<<"Do you want to play again?"<<endl; 
                  cin>>again;
                  gameplay();
    }while (again='y');
                  
}
1) note that you check their answer AFTER you call gameplay(), which means even if they say no, it'll always play a second game

2) look closely at line 11

EDIT:

3) why are you declaring all of the variables twice? Get rid of line 7. And actually the only var you should have on line 4 is 'again'.
Last edited on
Topic archived. No new replies allowed.