Okay, I've got a project due tomorrow and I have all the main stuff done besides the failsafes. The program is supposed to play a game of 23 with toothpicks against the computer. So you start with 23 toothpicks and the game is turn based. Each player removes 1-3 toothpicks per turn and whoever removes the last toothpick loses.
The program compiles fine, but somehow I have a logic error in how the number you enter is subtracted from the total. The player always goes first so I have the user enter the first value outside of the loop. Every other turn occurs inside a while loop and the computer's input gets subtracted correctly, but mine doesn't.
As you can see by the code below, I use the same calculation for the first turn outside the loop as I do for the following turns inside the loop. After the first turn if I enter a 1 or a 2, nothing gets subtracted from the total. If I enter a 3, 1 gets subtracted from the total. If I enter a 4, 3 will get subtracted from the total. If I enter anything higher than 4, the actual input value gets subtracted from the total. I have never encountered a problem like this before and office hours for my TA's and my teacher ended hours ago.
#include <iostream>
using namespace std;
int main()
{
//Declare Variables
int h_turn, t_picks = 23;
//Input Data
cout << "Welcome! This program will allow you to play the game of 23 against the computer.";
cout << "In this game each player removes 1, 2, or 3 toothpicks per turn. Whoever takes away the last toothpick loses.\n\n";
cout << "The human player always goes first, so enter the number of toothpicks you want to remove: ";
cin >> h_turn;
t_picks -= h_turn;
cout << endl << "There are "<< t_picks << " toothpicks left.\n";
//Process Data
int i = 1;
while (1)
{
if (i % 2 == 0)
{
if (t_picks > 1)
{
cout << "Your turn: ";
cin >> h_turn;
t_picks -= h_turn;
cout << endl<< "There are "<< t_picks << " toothpicks left.\n";
i++;
}
else
{
cout << "There is one toothpick left, you lose.";
break;
}
}
else
{
if (t_picks > 4)
{
cout << endl << "The computer removed "<< 4-h_turn<< " toothpicks.\n";
cout << endl<< "There are "<< t_picks - (4-h_turn) << " toothpicks left.\n";
i++;
}
else if ((t_picks > 2) && (t_picks < 4))
{
cout << "The computer removed 1 toothpick.\n";
cout << "There are "<< --t_picks << " toothpicks left.\n";
i++;
}
else
{
cout << "You have left the computer with one toothpick, you win!";
break;
}
}
}
return 0;
}
If there are more than 4 toothpicks left, the computer is supposed to remove 4-(user input) from the total. When there are 2-4 toothpicks left the computer removes only one toothpick.
If somebody can tell me what I'm doing wrong that would be great, because I have no clue.
Okay, I've got a project due tomorrow and I have all the main stuff done besides the failsafes. The program is supposed to play a game of 23 with toothpicks against the computer. So you start with 23 toothpicks and the game is turn based. Each player removes 1-3 toothpicks per turn and whoever removes the last toothpick loses.
The program compiles fine, but somehow I have a logic error in how the number you enter is subtracted from the total. The player always goes first so I have the user enter the first value outside of the loop. Every other turn occurs inside a while loop and the computer's input gets subtracted correctly, but mine doesn't.
As you can see by the code below, I use the same calculation for the first turn outside the loop as I do for the following turns inside the loop. After the first turn if I enter a 1 or a 2, nothing gets subtracted from the total. If I enter a 3, 1 gets subtracted from the total. If I enter a 4, 3 will get subtracted from the total. If I enter anything higher than 4, the actual input value gets subtracted from the total. I have never encountered a problem like this before and office hours for my TA's and my teacher ended hours ago.
#include <iostream>
using namespace std;
int main()
{
//Declare Variables
int h_turn, t_picks = 23;
//Input Data
cout << "Welcome! This program will allow you to play the game of 23 against the computer.";
cout << "In this game each player removes 1, 2, or 3 toothpicks per turn. Whoever takes away the last toothpick loses.\n\n";
cout << "The human player always goes first, so enter the number of toothpicks you want to remove: ";
cin >> h_turn;
t_picks -= h_turn;
cout << endl << "There are "<< t_picks << " toothpicks left.\n";
//Process Data
int i = 1;
while (1)
{
if (i % 2 == 0)
{
if (t_picks > 1)
{
cout << "Your turn: ";
cin >> h_turn;
t_picks -= h_turn;
cout << endl<< "There are "<< t_picks << " toothpicks left.\n";
i++;
}
else
{
cout << "There is one toothpick left, you lose.";
break;
}
}
else
{
if (t_picks > 4)
{
cout << endl << "The computer removed "<< 4-h_turn<< " toothpicks.\n";
cout << endl<< "There are "<< t_picks - (4-h_turn) << " toothpicks left.\n";
i++;
}
else if ((t_picks > 2) && (t_picks < 4))
{
cout << "The computer removed 1 toothpick.\n";
cout << "There are "<< --t_picks << " toothpicks left.\n";
i++;
}
else
{
cout << "You have left the computer with one toothpick, you win!";
break;
}
}
}
return 0;
}
If there are more than 4 toothpicks left, the computer is supposed to remove 4-(user input) from the total. When there are 2-4 toothpicks left the computer removes only one toothpick.
If somebody can tell me what I'm doing wrong that would be great, because I have no clue.
Probably help if you actually removed the toothpicks.
Learn to step through programs, it helps a lot. All I had to do was step through it to figure out that when you're having it subtract the computer's move from the toothpick total you're only doing it for that statement, you're never actually -setting- the toothpick count.
So if I remove two toothpicks on my first turn it goes to 21, then it goes to the computers turn and says I have 19 toothpicks left, but the variable is still set to 21. Then I remove whatever, and the value will confuse the hell out of you.
Although to be fair you really should recode the whole thing to: a. use more descriptive variable names, b. use useful comments, c. have better flow. That might be a bit hard when you're new though.
Also cin.ignore() will help if the program starts skipping cin calls.
Probably help if you actually removed the toothpicks.
Learn to step through programs, it helps a lot. All I had to do was step through it to figure out that when you're having it subtract the computer's move from the toothpick total you're only doing it for that statement, you're never actually -setting- the toothpick count.
So if I remove two toothpicks on my first turn it goes to 21, then it goes to the computers turn and says I have 19 toothpicks left, but the variable is still set to 21. Then I remove whatever, and the value will confuse the hell out of you.
Although to be fair you really should recode the whole thing to: a. use more descriptive variable names, b. use useful comments, c. have better flow. That might be a bit hard when you're new though.
Also cin.ignore() will help if the program starts skipping cin calls.
Thank you so much! I didn't really think to look into the code for the computers moves because I thought it was miscalculating how many toothpicks were removed by the user.
I usually put more comments in, I have a habit of putting them in after I actually get the program working though. I'll try to break that. I'll try to work on use better variable names as well.
Can you elaborate on what you mean by flow? Are you talking more about the layout of the program or the actual methods used to acquire the solution?
Rollback Post to RevisionRollBack
Quote from TheFieldZy »
Nobody's perfect, so neither is Hannah Montana Linux, but it's pretty great.
Quote from BC_Programming on Operating Systems »
They all suck. They just suck differently. Sort of like prostitutes.
Can you elaborate on what you mean by flow? Are you talking more about the layout of the program or the actual methods used to acquire the solution?
More just the whole design of the thing, it's kind of confusing to keep track of, but that's probably to be expected since you're practicing still.
It's not really designed for error checking either, like it explodes if you put anything in besides a number, and I don't think it loops if you put in one that's too high or low does it?
It's not really designed for error checking either, like it explodes if you put anything in besides a number, and I don't think it loops if you put in one that's too high or low does it?
I just briefly mentioned in my first post that I haven't implemented failsafes yet. Just now I added in a loop to check for non-integer numbers and numbers less than 1 or greater than 3.
Just wondering, but is there an easier way to check for non-digit values that what I've been doing in the following block of code?
for (int i = 0; i < len; i++)
{
if(!isdigit(variable[i]))
{
num_check = 0;
break;
}
else
{num_check = 1;}
}
The num_check is so I can check the result of the test in an if statement.
Rollback Post to RevisionRollBack
Quote from TheFieldZy »
Nobody's perfect, so neither is Hannah Montana Linux, but it's pretty great.
Quote from BC_Programming on Operating Systems »
They all suck. They just suck differently. Sort of like prostitutes.
The program compiles fine, but somehow I have a logic error in how the number you enter is subtracted from the total. The player always goes first so I have the user enter the first value outside of the loop. Every other turn occurs inside a while loop and the computer's input gets subtracted correctly, but mine doesn't.
As you can see by the code below, I use the same calculation for the first turn outside the loop as I do for the following turns inside the loop. After the first turn if I enter a 1 or a 2, nothing gets subtracted from the total. If I enter a 3, 1 gets subtracted from the total. If I enter a 4, 3 will get subtracted from the total. If I enter anything higher than 4, the actual input value gets subtracted from the total. I have never encountered a problem like this before and office hours for my TA's and my teacher ended hours ago.
If there are more than 4 toothpicks left, the computer is supposed to remove 4-(user input) from the total. When there are 2-4 toothpicks left the computer removes only one toothpick.
If somebody can tell me what I'm doing wrong that would be great, because I have no clue.
Probably help if you actually removed the toothpicks.
Learn to step through programs, it helps a lot. All I had to do was step through it to figure out that when you're having it subtract the computer's move from the toothpick total you're only doing it for that statement, you're never actually -setting- the toothpick count.
So if I remove two toothpicks on my first turn it goes to 21, then it goes to the computers turn and says I have 19 toothpicks left, but the variable is still set to 21. Then I remove whatever, and the value will confuse the hell out of you.
Although to be fair you really should recode the whole thing to: a. use more descriptive variable names, b. use useful comments, c. have better flow. That might be a bit hard when you're new though.
Also cin.ignore() will help if the program starts skipping cin calls.
Thank you so much! I didn't really think to look into the code for the computers moves because I thought it was miscalculating how many toothpicks were removed by the user.
I usually put more comments in, I have a habit of putting them in after I actually get the program working though. I'll try to break that. I'll try to work on use better variable names as well.
Can you elaborate on what you mean by flow? Are you talking more about the layout of the program or the actual methods used to acquire the solution?
More just the whole design of the thing, it's kind of confusing to keep track of, but that's probably to be expected since you're practicing still.
It's not really designed for error checking either, like it explodes if you put anything in besides a number, and I don't think it loops if you put in one that's too high or low does it?
I just briefly mentioned in my first post that I haven't implemented failsafes yet. Just now I added in a loop to check for non-integer numbers and numbers less than 1 or greater than 3.
Just wondering, but is there an easier way to check for non-digit values that what I've been doing in the following block of code?
The num_check is so I can check the result of the test in an if statement.