/*****************************************************************
 Volleyball version 2

 This is the current "rally rules" scoring of volleyball that is
 used in the Olympics.

 A game is a series of rallies. Each rally has a winning team,
 and the winning team scores one point. The first team to 25
 points who ALSO has 2 points more than their opponent, wins the
 game.

 In other words, in order to win, you have to have at least 25
 points and you have to have at least 2 points more than the other
 team.
*****************************************************************/

#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 = 25; // how many points needed to win
  int winby = 2;

  // 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 << "Winner of the rally: ";
    cin >> winname;

    // update the appropriate score
    if (winname == name1) {
      score1 = score1 + 1;
      // check if team 1 has won
      if (score1 >= winat && score2 <= score1 - winby) {
        gameover = true;
      }
    }
    else if (winname == name2) {
      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;
    }

    // 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;
}