/*****************************************************************
 Volleyball version 3

 This is the "old rules" of volleyball prior to the adoption of
 the current rally system.

 Every rally has a serving team (initially team 1), and the
 serving team is the only one that can score. So if the serving
 team wins the rally, they get one point, but if the other team
 wins the rally, the don't get any points. However, whoever
 wins each rally gets to serve the next rally.

 Because games are lower-scoring, you only play to 15 points.
 However, you still have to win by 2 points. So the winner is
 the first team to have at least 15 points and at least 2 points
 more than their opponent.
*****************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {
  // read in team names
  string name1, name2;
  cout << "Team 1 name: ";
  cin >> name1;
  cout << "Team 2 name: ";
  cin >> name2;
  cout << endl;

  // set up scores
  int score1 = 0; // current score of team 1
  int score2 = 0; // current score of team 2
  int winat = 15; // how many points needed to win
  int winby = 2;  // how many points you have to win over your opponent

  // which team is serving
  string serving = name1;

  // The winning condition is more complicated now, so we use a boolean.
  bool gameover = false;

  // Stores who wins each rally
  // It's at the outer scope so we can announce the winner at the end!
  string winname;

  // play the game until it's over
  while (! gameover) {
    // get winner of next rally
    cout << serving << " is serving. Winner of the rally: ";
    cin >> winname;

    if (winname == name1) {
      if (winname == serving) {
        // only score a point if you were serving
        score1 = score1 + 1;
        // check if team 1 has won
        if (score1 >= winat && score2 <= score1 - winby) {
          gameover = true;
        }
      }
    }
    else if (winname == name2) {
      if (winname == serving) {
        // only score a point if you were serving
        score2 = score2 + 1;
        // check if team 2 has won
        if (score2 >= winat && score1 <= score2 - winby) {
          gameover = true;
        }
      }
    }
    else {
      cout << "Invalid team name; please try again." << endl;
    }

    // whoever wins the rally serves the next one
    serving = winname;

    // print out current scores
    cout << name1 << ": " << score1 << ",  " << name2 << ": " << score2 << endl;
  }

  // print out who won
  // Notice: the winname is whoever won the last rally, which must be the
  // same as the overall game winner.
  cout << endl << winname << " is the winner!" << endl;

  return 0;
}