Soccer League Project

 

This project was written by Michel Mabene for UCSD Extension class, Advanced C++ IV.

 

Project Description

 

This project simulates a soccer league in which soccer teams play against one another during one season. During a league season each team plays against all other teams. Information about each game is displayed as the game unfolds and the team ranking is displayed at the end of the season.

 

Classes Involved

 

The Player class defines the basic functionality of a league player. It defines methods to make the player plays his/her role in the league as well as to maintain the state of the player.

 

A Striker is a Player who can shoot the ball left, center or right.

 

A Defender is a Player who can block the ball left, center or right

 

A Team has an even number of players. It has as many strikers as defenders. The Team class maintains containers for players and defines methods to manage players.

 

The class Score maintains the number of points for the home and the visitor teams during a game. It also provides methods for managing team points.

 

The player_mismatch class is an exception class representing a mismatch in a team’s player line-up. A player mismatch is when the number of strikers is not equal to the number of defenders.

 

The Game class plays two teams against one another. It encapsulates the rules of a soccer game. It runs one game between two teams and returns the game score at the end of the game.

 

The league class represents the soccer league. It manages all teams, run games and displays the team results at the end of the season.

 

Functor TeamCompare

 

The functor TeamCompare is used as sorting argument to the STL “sort” algorithm used to sort teams based on their earned points during the season.

 

Rules of the Game

 

a) A team must have an even number of players and as many strikers as defenders.

b) A game consists of each striker of the striking team playing once against one defender of the defending team. Each team strikes once and defends once during one game.

·         If a striker shoots the ball and the defender cannot block it, the striker gets 1 point added to his/her stats. If the defender blocks the shot, the defender gets 1 point added to his/her stats.

·         The winner of the game is the team whose players have gathered the higher number of points for this game.

·         The winner team gets 2 points added to its league record. The loser team gets 0 points.

·         In case of a tie, both teams get 1 point each.

 

c). Exception

There is one type of exception that can occur during a game. It is when a team does not have as many strikers as defenders. In that case the team at fault is penalized with 0 team points while the other team gets 2 team points for the victory.

 

 

The Main Function

 

The main program listed below creates a simulation of the league with 6 teams, each team consisting of 8 players (4 strikers and 4 defenders). To create a player mismatch exception, the test program adds the last team intentionally with more defenders (one more) than strikers.

 

After adding all teams to the league, the league is run and the results of the season are displayed at the end. The team ranking is displayed with the best team first.

 

Listing SoccerLeague.cpp

 

#include "League.h"

 

const int NUM_TEAMS_IN_LEAGUE = 6;

 

int main( /* int argc, char* argv[] */ ) {

 

      // Instantiate League

      League league;

 

      // create teams and add them to league

      // add players to team according to league rules: number of strikers == number of defenders == (NUM_PLAYERS_PER_TEAM/2)

      //

      for ( int numTeams =0; numTeams < NUM_TEAMS_IN_LEAGUE-1; numTeams++ ) {

 

            // create team

            Team *newTeam = new Team(League::NUM_PLAYERS_PER_TEAM/2, League::NUM_PLAYERS_PER_TEAM/2);

        // add team to league

            league.addTeam(newTeam);

      }

 

      // creating exception

      // intentional mismatch: add two defenders (instead of one striker and one defender) to last team

      //

      // create team with two players less than normal

      Team *newTeam = new Team(League::NUM_PLAYERS_PER_TEAM/2 -1, League::NUM_PLAYERS_PER_TEAM/2 - 1);

      // create two defenders and add them to team

      newTeam->addStriker(new Defender());

      newTeam->addDefender(new Defender());

      // add mismatched team to league

      league.addTeam(newTeam);

 

      // Now run league

      league.run();

 

      // Display league team results

      league.displayResults();

 

      return 0;

}

 

Test Program Output

 

The listing below is a sample output of the test program. It shows what happened during each game and the final results (team ranking) at the end of the season.

 

Listing: Test output

 

Game#1: Team#1 vs. Team#2

Shot left : Blocked center

Shot center : Blocked left

Shot left : Blocked right

Shot left : Blocked right

Shot left : Blocked right

Shot right : Blocked center

Shot center : Blocked center

Shot center : Blocked center

Game#1: HomeTeam 6 : 2 visitorTeam

 

Game#2: Team#1 vs. Team#3

Shot left : Blocked left

Shot right : Blocked right

Shot left : Blocked left

Shot center : Blocked right

Shot center : Blocked right

Shot right : Blocked right

Shot right : Blocked right

Shot right : Blocked left

Game#2: HomeTeam 3 : 5 visitorTeam

 

Game#3: Team#1 vs. Team#4

Shot right : Blocked center

Shot right : Blocked left

Shot left : Blocked left

Shot center : Blocked right

Shot left : Blocked center

Shot right : Blocked center

Shot left : Blocked center

Shot left : Blocked center

Game#3: HomeTeam 3 : 5 visitorTeam

 

Game#4: Team#1 vs. Team#5

Shot center : Blocked right

Shot left : Blocked right

Shot center : Blocked center

Shot left : Blocked right

Shot left : Blocked center

Shot center : Blocked center

Shot center : Blocked right

Shot right : Blocked left

Game#4: HomeTeam 4 : 4 visitorTeam

 

Game#5: Team#1 vs. Team#6

Shot right : Blocked right

Shot right : Blocked center

Shot right : Blocked right

Shot center : Blocked left

Shot left : Blocked right

Shot center : Blocked right

Shot right : Blocked left

Blocked left : Blocked left

Game#5: Player mismatch in team #6. Team #1 is awarded victory.

 

Game#6: Team#2 vs. Team#3

Shot center : Blocked right

Shot right : Blocked center

Shot right : Blocked left

Shot left : Blocked center

Shot left : Blocked center

Shot right : Blocked right

Shot right : Blocked center

Shot left : Blocked right

Game#6: HomeTeam 5 : 3 visitorTeam

 

Game#7: Team#2 vs. Team#4

Shot left : Blocked center

Shot left : Blocked right

Shot right : Blocked center

Shot center : Blocked right

Shot left : Blocked center

Shot left : Blocked left

Shot left : Blocked center

Shot left : Blocked right

Game#7: HomeTeam 5 : 3 visitorTeam

 

Game#8: Team#2 vs. Team#5

Shot right : Blocked left

Shot left : Blocked right

Shot center : Blocked left

Shot center : Blocked center

Shot center : Blocked right

Shot right : Blocked right

Shot left : Blocked left

Shot left : Blocked center

Game#8: HomeTeam 5 : 3 visitorTeam

 

Game#9: Team#2 vs. Team#6

Shot right : Blocked left

Shot right : Blocked center

Shot center : Blocked right

Shot center : Blocked right

Shot left : Blocked center

Shot right : Blocked right

Shot right : Blocked right

Blocked left : Blocked right

Game#9: Player mismatch in team #6. Team #2 is awarded victory.

 

Game#10: Team#3 vs. Team#4

Shot right : Blocked right

Shot center : Blocked center

Shot center : Blocked right

Shot left : Blocked right

Shot right : Blocked center

Shot right : Blocked center

Shot right : Blocked right

Shot center : Blocked center

Game#10: HomeTeam 4 : 4 visitorTeam

 

Game#11: Team#3 vs. Team#5

Shot left : Blocked center

Shot left : Blocked left

Shot center : Blocked center

Shot right : Blocked left

Shot right : Blocked center

Shot left : Blocked right

Shot left : Blocked right

Shot right : Blocked center

Game#11: HomeTeam 2 : 6 visitorTeam

 

Game#12: Team#3 vs. Team#6

Shot left : Blocked left

Shot left : Blocked right

Shot right : Blocked right

Shot left : Blocked right

Shot left : Blocked left

Shot left : Blocked left

Shot left : Blocked left

Blocked right : Blocked left

Game#12: Player mismatch in team #6. Team #3 is awarded victory.

 

Game#13: Team#4 vs. Team#5

Shot left : Blocked center

Shot center : Blocked left

Shot left : Blocked right

Shot right : Blocked center

Shot center : Blocked left

Shot center : Blocked left

Shot center : Blocked center

Shot right : Blocked left

Game#13: HomeTeam 5 : 3 visitorTeam

 

Game#14: Team#4 vs. Team#6

Shot center : Blocked left

Shot left : Blocked left

Shot left : Blocked center

Shot center : Blocked center

Shot left : Blocked left

Shot center : Blocked left

Shot right : Blocked right

Blocked left : Blocked center

Game#14: Player mismatch in team #6. Team #4 is awarded victory.

 

Game#15: Team#5 vs. Team#6

Shot right : Blocked right

Shot right : Blocked left

Shot right : Blocked center

Shot right : Blocked left

Shot center : Blocked left

Shot right : Blocked left

Shot right : Blocked left

Blocked center : Blocked center

Game#15: Player mismatch in team #6. Team #5 is awarded victory.

 

 

League Results:

1. Team#2 - 12 points

2. Team#5 - 12 points

3. Team#1 - 11 points

4. Team#3 - 11 points

5. Team#4 - 11 points

6. Team#6 - 3 points

 

League Class

 

Listing League.h and League.cpp

 

#ifndef LEAGUE_H_

#define LEAGUE_H_

 

#include<vector>

#include "Team.h"

 

// League: The league class represents the soccer league. It manages all teams, run games and displays the results.

//

class League {

 

public:

      enum { NUM_PLAYERS_PER_TEAM = 8 };   // number of players per team: 4 strikers and 4 defenders

 

public:

      League() : playerRegistrationNo(1) {}

      virtual ~League();

 

      void addTeam(Team* team);     // add new team to league

      void run();                   // run league. Run all games

      void displayResults();        // display final league results

 

private:

    vector<Team*> teams;

      int playerRegistrationNo;

};

 

 

// Functor used to sort Teams based on their league points

//

class TeamCompare {

      public:

            TeamCompare() {}

           

            // override for () operator

            bool operator()(const Team* team1, const Team* team2) {

 

                  return team1->getTeamPoints() > team2->getTeamPoints();

            }

};

 

#endif  //ifndef LEAGUE_H_

 

// league.cpp : implementation for class League

//

#include "League.h"

#include "Team.h"

#include "Game.h"

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <algorithm>

 

 

using namespace std;

 

// league destructor

// frees memory allocated for teams

//

League::~League() {

 

    vector<Team*>::iterator iter;

 

      // delete all teams in the league

      for (iter = teams.begin(); iter != teams.end(); iter++) {

           

            delete *iter;

      }

}

 

 

 

// Adds new team to league

// Notes: the league object takes ownership of the added team object and will destroy it at the end of the league

//

void League::addTeam(Team* team) {

 

      // assign number to team

      team->setTeamNumber(teams.size() + 1);

 

    // assign registration numbers to players

      team->registerPlayers(playerRegistrationNo);

 

      // add team to league

      teams.push_back(team);

}

 

 

// run league. Run all games

//

void League::run() {

     

    using std::endl;

 

      Game game;

      int gameNumber = 0;

      int currentTeamIndex, opponentTeamIndex;

      Team *currentTeam, *opponentTeam;

 

      srand(time(0));

 

      int numTeams = teams.size();

      if ( numTeams < 2 )

            return;

 

      // Make each team play against all other teams

      for ( currentTeamIndex=0; currentTeamIndex < (numTeams-1); currentTeamIndex++ ) {

 

            for ( opponentTeamIndex = currentTeamIndex+1; opponentTeamIndex < numTeams; opponentTeamIndex++ ) {

 

                  // set game number

                  gameNumber++;

 

                  // get the two teams for this game

                  currentTeam = teams[currentTeamIndex];

                  opponentTeam = teams[opponentTeamIndex];

 

                  cout << "Game#" << gameNumber << ": " << "Team#" << currentTeam->getTeamNumber() << " vs. " << "Team#" << opponentTeam->getTeamNumber() << endl;

 

                  // run the game

                  try {

                      Score score = game.run(gameNumber, NUM_PLAYERS_PER_TEAM, *currentTeam, *opponentTeam);

 

                        cout << "Game#" << gameNumber << ": " << score << endl << endl;

                  }

                  catch(Player_mismatch & mismatch) {

 

                        cout << "Game#" << gameNumber << ": " << mismatch.what() << endl << endl;

 

                  }

            }

      }

}

 

 

// display final league results

// sort teams in descending number of points

//

void League::displayResults() {

 

      using std::endl;

     

    // sort teams in descending number of points

      std::sort(teams.begin(), teams.end(), TeamCompare());

 

      // display results

      vector<Team*>::iterator iter;

      int rank = 1;

 

      cout << endl << "League Results:" << endl;

     

      for (iter = teams.begin(); iter != teams.end(); iter++) {

           

            cout << rank++ << ". Team#" << (*iter)->getTeamNumber() <<" - " << (*iter)->getTeamPoints() << " points" << endl;

      }

}

 

Player Class

 

Listing Player.h

 

#ifndef PLAYER_H_

#define PLAYER_H_

 

#include <cstdlib>

#include <ctime>

#include <iostream>

 

using namespace std;  

 

 

// Player: The Player class defines the basic functionality of a player

//

class Player {

     

public:

      enum {SHOOT_LEFT=1, SHOOT_CENTER, SHOOT_RIGHT, BLOCK_LEFT, BLOCK_CENTER, BLOCK_RIGHT};

 

public:

      Player() : leagueRegistrationNo(0), playStats(0), gameNumber(0) {}

     

      virtual ~Player() {}

     

      virtual int play() { return 0;}

// check whether the move is a shot

      static bool isShot(int move) { return (move <= SHOOT_RIGHT);}

// check whether the move is a block

      static bool isBlock(int move) { return (move >= BLOCK_LEFT);}

// add to player's stats      void addToStats(int stats) { playStats += stats; } 

// get player's stats

      int getStats()             { return playStats;}    

// set the league registration number

void setRegistrationNo(int regNo) { leagueRegistrationNo = regNo;} // get the league registration number  

      int  getRegistrationNo()          { return leagueRegistrationNo;}    

// set current league game number

      void setGameNumber(int gameNo)  { gameNumber = gameNo; }

// get current league game number             

      int getGameNumber()              { return gameNumber; }              

     

 

protected:

// league registration number

      int leagueRegistrationNo;  

// player's stats

      int playStats;             

      int gameNumber;   

 

};

 

 

// Striker: A Striker is a Player who can shoot left, center or right

//

class Striker : public Player {

 

public:

      Striker() : Player() {}

      virtual ~Striker() {}

     

      virtual int play() {

            // shoot left, center or right

            int playResult = (rand() % SHOOT_RIGHT) + 1; 

 

            switch(playResult)

            {

                  case SHOOT_LEFT:

                        cout << "Shot left";

                        break;

                  case SHOOT_CENTER:

                        cout << "Shot center";

                        break;

                  case SHOOT_RIGHT:

                        cout << "Shot right";

                        break;

            }

 

            return playResult;

      }

};

 

 

// Defender: A Defender is a Player who can block left, center or right

class Defender : public Player {

 

public:

      Defender() : Player() {}

      virtual ~Defender() {}

     

      virtual int play() {

            // block left, center or right

            int playResult = (rand() % SHOOT_RIGHT) + 4; 

 

            switch(playResult)

            {

                  case BLOCK_LEFT:

                        cout << "Blocked left";

                        break;

                  case BLOCK_CENTER:

                        cout << "Blocked center";

                        break;

                  case BLOCK_RIGHT:

                        cout << "Blocked right";

                        break;

            }

 

            return playResult;

      }

};

 

#endif  //#ifndef PLAYER_H_

 

Team Class

 

Listing Team.h and Team.cpp

 

#ifndef TEAM_H_

#define TEAM_H_

 

#include <vector>

#include "player.h"

 

// Team: Class representing a team

//       A team has an even number of players.

//       It has as many strikers as defenders

class Team {

 

public:

      Team();

      Team(int numStrikers, int numDefenders);

 

      virtual ~Team();

      // set league team number

      void setTeamNumber(int number) { teamNumber = number;}   

// get league team number

      int  getTeamNumber() { return teamNumber;}               

// add striker to team

      void addStriker(Player * striker);

// add defender to team                      

      void addDefender(Player * defender);                     

// register all players with the league

      void registerPlayers(int & startRegNo); 

// return the next striker who has not played yet

      Player* getNextStriker(int gameNumber); 

// return the next defender who has not played yet

      Player* getNextDefender(int gameNumber);

// add points to team

      void addTeamPoints(int points) { teamPoints += points;}  

// return the number of points for this team

      int getTeamPoints()  const   { return teamPoints;}       

 

private:

    vector<Player*> strikers;

      vector<Player*> defenders;

      int teamNumber;

      int teamPoints;

 

      Player* getNextPlayer(std::string playerType, int gameNumber, vector<Player*>& players);

 

};

 

#endif  //#ifndef TEAM_H_

 

// team.cpp -- implementation file for class Team

//

#include "Team.h"

#include<sstream>

 

// constructor

Team::Team() {

 

      teamNumber = 0;

      teamPoints = 0;

}

 

// constructor

Team::Team(int numStrikers, int numDefenders) {

 

      teamNumber = 0;

      teamPoints = 0;

 

      int num;

      for (num=0; num < numStrikers; num++) {

            strikers.push_back(new Striker());

      }

 

      for (num=0; num < numDefenders; num++) {

            defenders.push_back(new Defender());

      }

}

 

// destructor

Team::~Team() {

 

      vector<Player*>::iterator iter;

 

      for (iter = strikers.begin(); iter != strikers.end(); iter++) {

 

            delete *iter;

      }

 

      for (iter = defenders.begin(); iter != defenders.end(); iter++) {

 

            delete *iter;

      }

}

 

 

// add striker to team

//

void Team::addStriker(Player * striker) {

 

      strikers.push_back(striker);

}

 

 

// add defender to team

//

void Team::addDefender(Player * defender) {

 

      defenders.push_back(defender);

}

 

 

// return the next player who has not played in this game yet

//

Player* Team::getNextPlayer(std::string playerType, int gameNumber, vector<Player*>& players) {

 

      vector<Player*>::iterator iter;

 

      for (iter = players.begin(); iter != players.end(); iter++) {

 

            if ( (*iter)->getGameNumber() < gameNumber) {

                 

                  (*iter)->setGameNumber(gameNumber);

                  return (*iter);  // return player found

            }

      }

 

      return 0;

}

 

 

// return the next striker who has not played in this game yet

//

Player* Team::getNextStriker(int gameNumber) {

 

      return getNextPlayer("striker", gameNumber, strikers);

}

 

 

// return the next defender who has not played in this game yet

//

Player* Team::getNextDefender(int gameNumber) {

 

      return getNextPlayer("defender", gameNumber, defenders);

}

 

// register all players with the league

//

void Team::registerPlayers(int & startRegNo) {

 

      vector<Player*>::iterator iter;

 

      // register strikers

      for (iter = strikers.begin(); iter != strikers.end(); iter++) {

 

            (*iter)->setRegistrationNo(startRegNo);

            startRegNo++;

      }

 

      // register defenders

      for (iter = defenders.begin(); iter != defenders.end(); iter++) {

 

            (*iter)->setRegistrationNo(startRegNo);

            startRegNo++;

      }

}

 

9          Score Class

 

Listing Score.h and Score.cpp

 

#ifndef SCORE_H_

#define SCORE_H_

 

#include <iostream>

 

// Score: The class Score maintains the number of points for the home and the visitor teams

//        It also provides methods for managing team points

class Score {

 

public:

      Score() : homeTeamPoints(0), visitorTeamPoints(0) {}

      virtual ~Score() {}

 

      int getHomeTeamPoints() { return homeTeamPoints;}

      void setHomeTeamPoints(int points) { homeTeamPoints = points;}

     

      int getVisitorTeamPoints() { return visitorTeamPoints;}

      void setVisitorTeamPoints(int points) { visitorTeamPoints = points;}

 

      void addToHomeTeam(int points) { homeTeamPoints += points;}

      void addToVisitorTeam(int points) { visitorTeamPoints += points;}

 

      friend std::ostream & operator<<(std::ostream & os, const Score & score);

 

private:

      int homeTeamPoints;

      int visitorTeamPoints;

};

 

#endif  //#ifndef SCORE_H_

 

// score.cpp : implementation file for the class Score

#include "Score.h"

 

std::ostream & operator<<(std::ostream & os, const Score & score) {

 

          os << "HomeTeam " << score.homeTeamPoints << " : " << score.visitorTeamPoints << " visitorTeam";

          return os;

      }

 

 

League Exceptions

 

Listing LeagueException.h

 

#ifndef LEAGUE_EXCEPTION_H_

#define LEAGUE_EXCEPTION_H_

 

#include <exception>

#include <iostream>

 

using namespace std;

 

// Player_mismatch : This exception is thrown when a team fields a striker instead of a defender, or vice versa.

//

class Player_mismatch : public std::exception {

 

public:

      explicit Player_mismatch(const string& what_arg) {

           

            whatHappened = what_arg;

      }

 

      const char * what() {

            return whatHappened.c_str();

      }

 

      virtual ~Player_mismatch() {};

 

private:

      string whatHappened;

};

 

#endif  //#ifndef LEAGUE_EXCEPTION_H_

 

Game Class

 

Listing Game.h and Game.cpp

 

#ifndef GAME_H_

#define GAME_H_

 

#include "Score.h"

#include "Team.h"

#include "LeagueException.h"

 

// Game: The Game class plays two teams against one another.

//       It encapsulates the rules of a soccer game.

//       It runs one game between two teams and returns

//          the game score at the end of the game.

//

class Game {

 

public:

      Game() {}

      virtual ~Game() {}

 

      Score run(int gameNumber, int numberOfPlayersPerTeam,

                  Team & homeTeam, Team & visitorTeam)

                  throw(Player_mismatch);

     

private:

      Score runStrike(int gameNumber, int numberOfPlayersPerTeam,

                        Team & strikingTeam, Team & defendingTeam)

                        throw(Player_mismatch);

};

 

#endif

 

// game.cpp : implementation file for Game class

//

#include "game.h"

#include <iostream>

#include <sstream>

 

using namespace std;

 

// run a game with two participating teams.

// See game rules below in method runStrike()

//

Score Game::run(int gameNumber, int numberOfPlayersPerTeam,

                        Team & homeTeam, Team & visitorTeam)

                throw(Player_mismatch) {

    Score score, score1, score2;

 

      // Home team strikes, visitor team defends

      score1 = runStrike(gameNumber, numberOfPlayersPerTeam, homeTeam, visitorTeam);

 

      // Visitor team strikes, home team defends

      score2 = runStrike(gameNumber, numberOfPlayersPerTeam, visitorTeam, homeTeam);

 

      // Total scores

      score.setHomeTeamPoints(score1.getHomeTeamPoints() + score2.getVisitorTeamPoints());

      score.setVisitorTeamPoints(score1.getVisitorTeamPoints() + score2.getHomeTeamPoints());

 

      return score;

}

 

 

// Game rules:

//    - each team must have: number of players =

//           (number of striker + number of defenders)

//    - number of defenders must be equal to the number of strikers

// A game consists of each striker of the striking team playing once

// against one defender of the defending team.

// If a striker shoots the ball and the defender cannot block it, the

// striker gets 1 point added to his/her stats.

// If the defender blocks the shot, the defender gets 1 point added to

// his/her stats

// The winner of the game is the team whose players have gathered the

// higher number of points for this game.

//  - The winner team gets 2 points added to its league record.

//    The loser team gets 0 points.

//  - In case of a tie, both teams get 1 point each.

//

// Exception:

// There is one type of exception that can occur during a game:

//     a team may not have as many strikers as defenders.

// In that case the team at fault is penalized with 0 game points

// while the other team gets 2 game points for the victory.

//

Score Game::runStrike(int gameNumber, int numberOfPlayersPerTeam,

                                Team & strikingTeam, Team & defendingTeam)

                                throw(Player_mismatch) {

 

      Score score;

 

      int numPlays = numberOfPlayersPerTeam / 2;

      int numPlayer;

      Player* strikingPlayer;

      Player* defendingPlayer;

      int strikingMove, defendingMove;

     

      // 1. Striking team strikes

      //

      for (numPlayer=0; numPlayer < numPlays; numPlayer++) {

 

            // get one player from each team

            strikingPlayer = strikingTeam.getNextStriker(gameNumber);

            defendingPlayer = defendingTeam.getNextDefender(gameNumber);

 

            if ( strikingPlayer && defendingPlayer ) {

 

                  // Make players play against one another

                  strikingMove = strikingPlayer->play();

                  cout << " : ";

                  defendingMove = defendingPlayer->play();

                  cout << endl;

 

                  // Check for player mismatch in striking team

                  //  and end the game if necessary

                  // striker should not block ==> player mismatch

                  if ( Player::isBlock(strikingMove) ) { 

                        `// defending team gets 2 points for victory

                        defendingTeam.addTeamPoints(2); 

 

                        //Player_mismatch

                        ostringstream outstr;

 

                        outstr << "Player mismatch in team #" <<

                          strikingTeam.getTeamNumber() << ". Team #"

                          << defendingTeam.getTeamNumber() <<

                          " is awarded victory.";

 

                        // stop game here with exception

                        throw Player_mismatch(outstr.str());

                  }

 

                  // Check for player mismatch in defending team

                  //   and end the game if necessary

                  // defender should not shoot ==> player mismatch

                  if ( Player::isShot(defendingMove) ) {

                        //striking team gets 2 points for victory 

                        strikingTeam.addTeamPoints(2); 

                        //Player_mismatch

                        ostringstream outstr;

 

                        outstr << "Player mismatch in team #" <<

                          defendingTeam.getTeamNumber() << ". Team #"

                          << strikingTeam.getTeamNumber() <<

                          " is awarded victory.";

 

                        // stop game here with exception

                        throw Player_mismatch(outstr.str());

                  }

 

                  // Now evaluate player's moves

                  if ( ((strikingMove == Player::SHOOT_LEFT)   &&

                        (defendingMove != Player::BLOCK_LEFT))   ||

                       ((strikingMove == Player::SHOOT_CENTER)  &&

                        (defendingMove != Player::BLOCK_CENTER)) ||

                       ((strikingMove == Player::SHOOT_RIGHT)  &&

                        (defendingMove != Player::BLOCK_RIGHT))     ) {

 

                        // defender did not block striker's shot ==>              

                        //     striker and his/her team win this move

                        strikingPlayer->addToStats(1);

                        score.addToHomeTeam(1);

                  // defender successfully blocked shot ==>

                  // defender and his/her team win this move

                  } else {

                        defendingPlayer->addToStats(1);

                        score.addToVisitorTeam(1);

                  }

            }

      }

 

      // 2. Determine which team won

      if ( score.getHomeTeamPoints() > score.getVisitorTeamPoints() ) {

 

            // striking team won game, add two points

            strikingTeam.addTeamPoints(2); 

      } else if ( score.getHomeTeamPoints() < score.getVisitorTeamPoints() ) {

 

            // defending team won game, add 2 points

            defendingTeam.addTeamPoints(2);

      }

      else {

            // the game is tied. Each team gets 1 point

            strikingTeam.addTeamPoints(1);

            defendingTeam.addTeamPoints(1);

      }

 

      return score;

}

Hosted by www.Geocities.ws

1