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;
}
}
}
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;
} elseif (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;
}
}
}
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);
}
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;
} elseif (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;
}
}
}
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.
#include <iostream>
#include <string>
#include <time.h>
usingnamespace 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;
}
elseif (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.
#include <iostream>
#include <string>
#include <time.h>
usingnamespace 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;
}
elseif (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.
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 ;
}
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');
}