This compiles fine, but gives the wrong output.
Basically its a bowling program.
It reads in an appropriate score (in the proper range), determines if its a normal, spare, strike or whatever and adds it to either playerA or player B's score (Player A always goes first)
it uses a sliding weight scale to calculate the score
To illustrate this (if you already understand what Im trying to do, you can skip this part).
A strike adds the next two bowls to this score (standard 10 pin rules), and a spare adds the next bowl to this score.
An example score for a one person game might be
In standard terms it might look like:
10---10 + 10 + 10
10---10 + 10 + 7
10---10 + 7 + 2
7 --- 7
2 --- 2
3 --- 3
4 --- 4
.
.
.
Total score 92. This can also be expressed as:
10 Weight1 = 1, weight2 = 1, score = 10 * 1 = 10
10 Weight1 = 2, weight2 = 2, score = 10 * 2 = 20
10 Weight1 = 3, weight2 = 2, score = 10 * 3 = 30
7 Weight1 = 3, weight2 = 2, score = 7 * 3 = 21
2 Weight1 = 2, weight2 = 1, score = 2 * 2 = 4
3 Weight1 = 1, weight2 = 1, score = 3 * 1 = 3
4 Weight1 = 1, weight2 = 1, score = 4 * 1 = 4
which of course also give 92.
This algorithm is basically what I'm trying to put in my program and adapt to two person play.
I have a bowl function that returns a status number: 1 for normal, 2 for spare, 3 for strike, 4 for in progress (frame isnt over for that player yet) which I use a lot.
Any help, questions or guidance?
Is my code poorly organized?
where is the mistake?
I am doing this for an assignment, so please dont write code for me (since I would rather not get kicked out of college) but any good faith things like, 'you have these two things in the wrong order' should be fine.
thanks to anyone at all who can help, even if its just a little bit.
If the lack of whitespace makes it hard to read, you can always quote me and look at it there.
#include <iostream>
using namespace std;
bool readScore(int &thisScore, int &errorCount);
int bowl(int &pins, int &thisScore, int &frames, int weight1);
void calcScore(int &thisScore, int weight1);
void incrementWeight(int &weight1, int &weight2);
void updateWeight(int status, int &weight1, int &weight2);
void addScore(char player, int thisScore, int &scoreA, int &scoreB);
void playerSwitch(int status, char &player, int &pins, int &frames);
void weightSwitch(int status, int &weight1, int &weight2, int &weight1PH, int &weight2PH);
int main() {
int thisScore = 0, errorCount = 0, pins = 10, frames = 0;
int weight1 = 1, weight2 = 1, weight1PH = 1, weight2PH = 1;
int status = 5;
int scoreA = 0, scoreB = 0;
char player = 'A';
while(frames < 10) {
readScore(thisScore, errorCount);
status = bowl(pins, thisScore, frames, weight1);
cout << "------------------------------------" << endl;
cout << "pins1: " << pins << endl;
cout << "ThisScore: " << thisScore << endl;
cout << "Error Count: " << errorCount << endl;
cout << "Pins: " << pins << endl;
cout << "Frames: " << frames << endl;
cout << "Status: " << status << endl;
cout << "Weight1: " << weight1 << endl;
cout << "Weight2: " << weight2 << endl;
cout << "ScoreA: " << scoreA << endl;
cout << "ScoreB: " << scoreB << endl;
cout << "Player: " << player << endl;
cout << "------------------------------------" << endl;
incrementWeight(weight1, weight2);
updateWeight(status, weight1, weight2);
addScore(player, thisScore, scoreA, scoreB);
playerSwitch(status, player, pins, frames);
weightSwitch(status, weight1, weight2, weight1PH, weight2PH);
cout << "ThisScore: " << thisScore << endl;
cout << "Error Count: " << errorCount << endl;
cout << "Pins: " << pins << endl;
cout << "Frames: " << frames << endl;
cout << "Status: " << status << endl;
cout << "Weight1: " << weight1 << endl;
cout << "Weight2: " << weight2 << endl;
cout << "ScoreA: " << scoreA << endl;
cout << "ScoreB: " << scoreB << endl;
cout << "Player: " << player << endl;
}
return 0;
}
bool readScore(int &thisScore, int &errorCount) {
cin >> thisScore;
return 1;
}
int bowl(int &pins, int &thisScore, int &frames, int weight1) {
//Determines if score in range and updates pins accordingly
//0 = error
//1 = normal
//2 = spare
//3 = strike
//4 = in progress
if (thisScore < 0 || thisScore > 10 || pins < 0){
//ERROR
return 0;
} else {
if (pins == 10) { //First Half
if (thisScore == 10) { //Strike
pins -= thisScore;
calcScore(thisScore, weight1);
return 3;
} else { //continue frame
pins -= thisScore;
calcScore(thisScore, weight1);
return 4;
}
} else { //Second Half
if ((pins + thisScore) == 10) { //Spare
pins -= thisScore;
calcScore(thisScore, weight1);
return 2;
} else { //Normal
pins -= thisScore;
calcScore(thisScore, weight1);
return 1;
}
}
}
}
void calcScore(int &thisScore, int weight1) {
thisScore *= weight1;
}
void incrementWeight(int &weight1, int &weight2) {
weight1 = weight2;
weight2 = 1;
}
void updateWeight(int status, int &weight1, int &weight2) {
if (status == 3) { //Strike
weight1 += 1;
weight2 += 1;
} else if (status == 2) { //Spare
weight1 += 1;
} else {
//do nothing
}
}
void addScore(char player, int thisScore, int &scoreA, int &scoreB) {
if (player == 'A') {
scoreA += thisScore;
} else {
scoreB += thisScore;
}
}
void playerSwitch(int status, char &player, int &pins, int &frames) {
if (status == 1 || status == 2 || status == 3) {
if (player == 'A') {
pins = 10;
player = 'B';
} else {
pins = 10;
player = 'A';
frames++;
}
} else {
//do nothing
}
}
void weightSwitch(int status, int &weight1, int &weight2, int &weight1PH, int &weight2PH) {
int tempWeight1, tempWeight2;
if (status == 1 || status == 2 || status == 3) {
tempWeight1 = weight1;
tempWeight2 = weight2;
weight1 = weight1PH;
weight2 = weight2PH;
weight1PH = tempWeight1;
weight2PH = tempWeight2;
}
}