-
December 4th, 2014, 07:09 PM
#1
VolleyBall Game Tracker
So we are working on a big code right now. It is a Volley Ball game tracker.
Here is what the program is going to need to do.
- Ask the user to entering Team A and Team B
- Display the scores for the team, right justified. Within 20 columns.
- User is asked what team scored. A or B and it will add a point to the total for that time and update the score board. is a or b is not entered it will repeat until it is done.
- After a team wins a set the teams will flip and the set counter at the top of the board will update.
- If they win 1 each it goes to a tie breaker. Set is changed to tie breaker and it goes on until winner has 15 and is 2 over the other.
- At the end it announces the winner and asks if you would like to play again.
Here is a sample board.
Code:
Beach VolleyBall Game Tracker
Enter the Name of Team A: China
Enter the Name of Team B: USA
Let the Games begin ! Best 2 of 3 sets win
Current Status Set 1
Team A China | Team B USA
0 | 0
After a team scores 7 points they will switch sides and the scored board will be flipped.
Here is the code I have now.
Code:
#include <iostream>
using namespace std;
int main()
{
char a[21], b[21];
char yn='n';
int ap = 0;
int bp = 0;
int set = 1;
char point;
do
{
cout << "\t\t Beach VolleyBall Game Tracker" << endl << endl;
cout << "Enter the name of Team A: ";
cin >> a;
cout << "Enter the name of Team B: ";
cin >> b;
cout << "Let the Games begin! Best 2 of 3 sets wins!" << endl << endl;
//Above this point is looking good. Wont need to be repeated at all anymore.
}while(yn !='n' || yn !='N');
return 0;
}
I have just started. Any tips on what I have now would be very helpful.
But my main question is, I want to loop the score board every point, and the new point is changed. But I am not sure what loop to use, I would need it check both the scores and if one is at 7 or 14 or 21 and then finish that loop and move on.
-
December 4th, 2014, 07:14 PM
#2
Re: VolleyBall Game Tracker
Also wouldn't I need to have a loop within this loop that would constantly check if one of the scores 25 and if it is it would end that set?
-
December 5th, 2014, 04:36 AM
#3
Re: VolleyBall Game Tracker
Just a point re posted code. What happens if the names of the teams exceed 20 characters? Wouldn't it be better to have a and b as type string rather than char array?
I would strongly suggest that before you dive in any further re coding, you take a step backwards and design the program on paper. Think about the required input and output, what data structures are required to store necessary data, and what processing is required. Once you have the program designed, then code the program from the design and test and debug it.
Code:
determine input requirements
determine output requirements
determine data structures required
determine processing/algorithms needed
do {
design program
code program
test program
debug program
} while (program_not_working)
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 18th, 2014, 09:50 AM
#4
Re: VolleyBall Game Tracker
Sorry I didn't get back, after I posted this the next day I sat down with my book and a paper and started over. Here was the final code.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
char set12(char teamA[21], char teamB[21], int set);
char tie(char teamA[21], char teamB[21], int set);
int main()
{
char teamA[21], teamB[21];
char yn='n';
int bScore;
int aScore;
int matchA,matchB;
do
{ matchA=matchB=0;
//cin.ignore();
cout << "\t\t Beach VolleyBall Game Tracker" << endl << endl;
cout << "Enter the name of Team A: ";
cin.getline (teamA, 20);
cout << "Enter the name of Team B: ";
cin.getline (teamB, 20);
cout << "Let the Games begin! Best 2 of 3 sets wins!" << endl << endl;
int set = 1;
char set1 = set12(teamA, teamB, set);
if (set1=='A')
{
cout << teamA << " Won Set 1!" << endl << endl;
aScore++;
}
else
{
cout << teamB << " Won Set 1!" << endl << endl;
bScore++;
}
set++;
char set2 = set12(teamA, teamB, set);
if (set2 =='A')
{
cout << teamA << " Won Set 2!" << endl<< endl;
aScore++;
}
else
{
cout << teamB << " Won Set 2!" << endl<<endl;
bScore++;
}
if(aScore==1 && bScore==1)
{
char tie(char teamA[21], char teamB[21], int set);
if (set1=='A')
{
cout << "Tie breaker and Matches to Team A " << teamA << endl << endl;
aScore++;
}
else
{
cout << "Tie breaker and Matches to Team B " << teamB << endl << endl;
bScore++;
}
}
if(aScore==2)
{
cout << "Team A " << teamA << " to set count 2, Team B " << teamB << " to set count 0" << endl;
}
else if (bScore==2)
{
cout << "Team B " << teamB << " to set count 2, Team A " << teamA << " to set count 0" << endl;
}
cout << "Would you like to play again? (Y/N)? ";
cin >> yn;
}while(yn !='n' && yn !='N');
cin.get();
return 0;
}
char set12(char teamA[21], char teamB[21], int set)
{
int aPoints = 0;
int bPoints = 0;
int totalPoints = 0;
int win = 21;
bool winner=false;
bool side=true;
char point;
do{
if(side)
{
cout << " Current Status Set " << set << endl;
cout << setw(20) << right << " Team A " << teamA << " | " << "Team B " << teamB << endl;
cout << setw(20) << right << " " << aPoints << " | " << " " << bPoints << endl;
}
else
{
cout << " Current Status Set " << set << endl;
cout << setw(20) << right << " Team B " << teamB << " | " << "Team A " << teamA << endl;
cout << setw(20) << right << " " << bPoints << " | " << " " << aPoints << endl;
}
do
{
cout << "What team scored a point (A or B)? ";
cin >> point;
} while(point != 'a' && point != 'A' && point != 'b' && point != 'B');
if(point == 'a' || point == 'A')
{
aPoints++;
totalPoints++;
}
else
{
bPoints++;
totalPoints++;
}
if (totalPoints%7==0)
{
cout << endl << endl << "Switch Sides!!!" << endl;
side=!side;
}
if(aPoints >= 21 && aPoints-bPoints >=2)
{
winner=true;
return 'A';
}
else if(bPoints >=21 && bPoints-aPoints >1)
{
winner=true;
return 'B';
}
}while (!winner);
}
char tie(char teamA[21], char teamB[21], int set)
{
int aPoints = 0;
int bPoints = 0;
int totalPoints = 0;
int win = 21;
bool winner=false;
bool side=true;
char point;
do{
if(side)
{
cout << " Current Status Set Tie Breaker" << endl;
cout << setw(20) << right << " Team A " << teamA << " | " << "Team B " << teamB << endl;
cout << setw(20) << right << " " << aPoints << " | " << " " << bPoints << endl;
}
else
{
cout << " Current Status Set Tie Breaker" << endl;
cout << setw(20) << right << " Team B " << teamB << " | " << "Team A " << teamA << endl;
cout << setw(20) << right << " " << bPoints << " | " << " " << aPoints << endl;
}
do
{
cout << "What team scored a point (A or B)? ";
cin >> point;
}while(point != 'a' && point != 'A' && point != 'b' && point != 'B');
if(point == 'a' || point == 'A')
{
aPoints++;
totalPoints++;
}
else
{
bPoints++;
totalPoints++;
}
if (totalPoints%5==0)
{
cout << endl << endl << "Switch Sides!!!" << endl;
side=!side;
}
if(aPoints >= 15 && aPoints-bPoints >=2)
{
winner=true;
return 'A';
}
else if(bPoints >=15 && bPoints-aPoints >1)
{
winner=true;
return 'B';
}
}while (!winner);
}
-
December 18th, 2014, 10:06 AM
#5
Re: VolleyBall Game Tracker
Now all you have to do is to debug it to get it to work properly.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 19th, 2014, 09:02 AM
#6
Re: VolleyBall Game Tracker
There are also some potential issues with the design as the number of sets is hard-coded as 3 and it would be difficult to change this as there is specific code for each set (which of itself is not a good design). A better design would cater for having a higher number of sets. A possible way using classes could be
Code:
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <cctype>
using namespace std;
const int MaxSets = 3;
const int swSides = 7;
const int swTie = 5;
const int wnPoints = 21;
const int tiePoints = 15;
class Volley
{
private:
struct Team
{
string name;
int points;
int sets;
};
int curteam;
int curSet;
int totalPoints;
int mxsets;
bool istie;
Team teams[2];
void tie();
void showScore() const;
public:
Volley(const string& n1, const string& n2, int msets = MaxSets);
void addPoint();
bool checkWin();
bool checkSets();
int maxSets() const;
};
Volley::Volley(const string& n1, const string& n2, int msets) : mxsets(msets)
{
teams[0].name = n1;
teams[0].points = 0;
teams[0].sets = 0;
teams[1].name = n2;
teams[1].points = 0;
teams[1].sets = 0;
curteam = 0;
curSet = 1;
totalPoints = 0;
istie = false;
}
int Volley::maxSets() const
{
return mxsets;
}
void Volley::tie()
{
cout << "Both teams have won " << teams[0].sets << " set(s). Tiebreaker" << endl;
istie = true;
do {
addPoint();
} while (checkWin() == false);
istie = false;
}
bool Volley::checkSets()
{
if ((teams[0].sets == (mxsets / 2)) && (teams[1].sets == (mxsets / 2)))
tie();
bool win = false;
int t = 0;
for (t = 0; t < 2 && !win; t++)
win = (teams[t].sets == (mxsets / 2 + 1));
if (win)
cout << "Team " << teams[t - 1].name << " won!" << endl;
return win;
}
bool Volley::checkWin()
{
int win = -1;
const int chkPoints = (istie) ? tiePoints : wnPoints;
if (teams[0].points >= chkPoints && teams[0].points - teams[1].points >= 2)
win = 0;
else
if (teams[1].points >= chkPoints && teams[1].points - teams[0].points >= 2)
win = 1;
if (win >= 0) {
cout << "\nTeam " << teams[win].name << " won set " << curSet << endl;
teams[win].sets++;
teams[0].points = 0;
teams[1].points = 0;
curSet++;
totalPoints = 0;
}
return (win >= 0);
}
void Volley::addPoint()
{
char tm;
const int sides = (istie) ? swTie : swSides;
showScore();
while ((cout << "What team scored a point (A or B)? ") && (cin >> tm) && (((tm = (char)tolower(tm)) != 'a') && (tm != 'b')))
cout << "Please enter either 'A' or 'B'" << endl;
teams[tm - 'a'].points++;
totalPoints++;
if ((totalPoints % sides) == 0) {
cout << "\nSwitch Sides!!!" << endl;
curteam = (curteam + 1) % 2;
}
}
void Volley::showScore() const
{
const int t1 = curteam,
t2 = (curteam + 1) % 2;
cout << "\nCurrent Status - Set: " << curSet;
if (istie)
cout << " Tiebreaker";
cout << endl;
ostringstream osa,
osb;
osa << "Team " << char('A' + t1) << " " << teams[t1].name;
osb << "Team " << char('A' + t2) << " " << teams[t2].name;
cout << setw(20) << right << osa.str() << " | " << osb.str() << endl;
osa.str("");
osa << "Points " << teams[t1].points;
cout << setw(20) << osa.str() << " | " << teams[t2].points << endl;
osa.str("");
osa << "Sets " << teams[t1].sets;
cout << setw(20) << right << osa.str() << " | " << teams[t2].sets << endl << endl;
}
int main()
{
char ans = 'n';
do {
cout << "\n\t\t Beach VolleyBall Game Tracker" << endl << endl;
string na, nb;
int sets;
cout << "Enter the name of Team A: ";
getline(cin, na);
cout << "Enter the name of Team B: ";
getline(cin, nb);
while ((cout << "How many sets: ") && (cin >> sets) && ((sets < 3) || ((sets > 3) && (sets % 2) == 0)))
cout << "Must be at least 3 sets and an odd number of sets" << endl;
Volley volley(na, nb, sets);
cout << "\nLet the Games begin! Best " << volley.maxSets() / 2 + 1 << " of " << volley.maxSets() << " sets wins!" << endl;
do {
do {
volley.addPoint();
} while (volley.checkWin() == false);
} while (volley.checkSets() == false);
while ((cout << "\nWould you like to play again? (Y/N)? ") && (cin >> ans) && (((ans = (char)tolower(ans)) != 'n') && (ans != 'y')))
cout << "Please enter 'y or 'n'" << endl;
cin.ignore(1000, '\n');
} while (ans == 'y');
return 0;
}
Last edited by 2kaud; December 19th, 2014 at 09:06 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|