This repository has been archived by the owner on Jul 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d773575
commit 93ecd2c
Showing
20 changed files
with
636 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,41 @@ | ||
# defining variables: | ||
CC=g++ | ||
CFLAGS=-c | ||
HEADERS=match.h player.h round.h stadium.h team.h tournament.h | ||
OBJECTS=main.o match.o player.o round.o stadium.o team.o tournament.o | ||
HEADERS=match.h player.h round.h stadium.h team.h tournament.h cup.h leg.h | ||
OBJECTS=main.o match.o player.o round.o stadium.o team.o tournament.o cup.o leg.cpp | ||
|
||
#defining rules: | ||
all: out | ||
|
||
out: $(HEADERS) $(OBJECTS) | ||
$(CC) $(OBJECTS) -o out | ||
|
||
main.o: tournament.h | ||
main.o: main.cpp tournament.h | ||
$(CC) $(CFLAGS) main.cpp | ||
|
||
tournament.o: tournament.o tournament.h round.h team.h | ||
tournament.o: tournament.cpp tournament.h cup.h | ||
$(CC) $(CFLAGS) tournament.cpp | ||
|
||
team.o: team.o team.h stadium.h | ||
cup.o: cup.cpp cup.h round.h team.h | ||
$(CC) $(CFLAGS) cup.cpp | ||
|
||
team.o: team.cpp team.h stadium.h | ||
$(CC) $(CFLAGS) team.cpp | ||
|
||
round.o: round.o round.h team.h match.h | ||
round.o: round.cpp round.h team.h match.h | ||
$(CC) $(CFLAGS) round.cpp | ||
|
||
player.o: player.o player.h | ||
player.o: player.cpp player.h | ||
$(CC) $(CFLAGS) player.cpp | ||
|
||
match.o: match.o match.h team.h | ||
match.o: match.cpp match.h team.h leg.h | ||
$(CC) $(CFLAGS) match.cpp | ||
|
||
stadium.o: stadium.o stadium.h | ||
leg.o: leg.cpp leg.h team.h | ||
$(CC) $(CFLAGS) leg.cpp | ||
|
||
stadium.o: stadium.cpp stadium.h | ||
$(CC) $(CFLAGS) stadium.cpp | ||
|
||
clean: | ||
rm -rf *o *_out | ||
rm -rf *o out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "cup.h" | ||
|
||
using namespace std; | ||
|
||
void Cup::add_team(string team_name, string stadium_name, int capacity, float impact, map<string, int> fans_in_stadium) { | ||
teams.push_back(new Team(team_name, stadium_name, capacity, impact, fans_in_stadium)); | ||
} | ||
|
||
void Cup::add_player(string team_name, string player_name, int player_power, string player_post) { | ||
int index = get_team_index(team_name); | ||
if (index == NOT_FOUND) | ||
abort(); | ||
teams[index]->add_player(player_name, player_power, player_post); | ||
} | ||
|
||
int Cup::get_team_index(string team_name) { | ||
for (int i = 0; i < teams.size(); i++) | ||
if (teams[i]->get_name() == team_name) | ||
return i; | ||
return NOT_FOUND; | ||
} | ||
|
||
void Cup::simulate(){ | ||
check_teams(); | ||
vector<Team*> in_cup_teams = get_in_cup_teams(); | ||
if(validate_number_of_teams(in_cup_teams)){ | ||
int round_number = 1; | ||
while(in_cup_teams.size() != 1){ | ||
Round new_round(in_cup_teams, round_number); | ||
new_round.simulate(); | ||
|
||
in_cup_teams = get_in_cup_teams(); | ||
round_number++; | ||
} | ||
} | ||
} | ||
|
||
vector<Team*> Cup::get_in_cup_teams() { | ||
vector<Team*> in_cup_teams; | ||
for (int i = 0; i < teams.size(); i++) | ||
if (teams[i]->is_participating()) | ||
in_cup_teams.push_back(teams[i]); | ||
return in_cup_teams; | ||
} | ||
|
||
|
||
void Cup::check_teams() { | ||
for (int i = 0; i < teams.size() ; i++ ) { | ||
if (!teams[i]->is_valid()) { | ||
teams[i]->set_participation_status(false); | ||
cout << "Inadequate players in " << teams[i]->get_name() << endl; | ||
} | ||
else | ||
teams[i]->generate_squad(); | ||
} | ||
} | ||
|
||
bool Cup::validate_number_of_teams(vector<Team*> in_cup_teams) { | ||
int n = teams.size(); | ||
if ((n == 0) or ((n & (n-1)) != 0)) { | ||
cout << "Inadequate teams" << endl; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void Cup::print_tournament_results() { | ||
for (int i = 0; i < rounds.size(); i++) | ||
rounds[i].print_round(); | ||
} | ||
|
||
void Cup::print_round_results(int round_number) { | ||
rounds[round_number-1].print_round(); | ||
} | ||
|
||
void Cup::print_team_results(string team_name) { | ||
Team* team = teams[get_team_index(team_name)]; | ||
for (int i = 0; i < rounds.size(); i++) | ||
rounds[i].print_team_result(team); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef __CUP_H__ | ||
#define __CUP_H__ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <cstdlib> | ||
|
||
#include "team.h" | ||
#include "round.h" | ||
|
||
#define NOT_FOUND -1 | ||
|
||
class Cup { | ||
public: | ||
Cup(){}; | ||
void add_team(std::string team_name, std::string stadium_name, int capacity, float impact, | ||
std::map<std::string, int> fans_in_stadium); | ||
void add_player(std::string team_name, std::string player_name, int player_power, std::string player_post); | ||
void simulate(); | ||
void print_round_results(int round_number); | ||
void print_tournament_results(); | ||
void print_team_results(std::string team_name); | ||
|
||
int get_team_index(std::string team_name); | ||
bool validate_number_of_teams(std::vector<Team*> in_cup_teams); | ||
void check_teams(); | ||
std::vector<Team*> get_in_cup_teams(); | ||
void check_num_of_teams(); | ||
private: | ||
std::vector<Team*> teams; | ||
std::vector<Round> rounds; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "leg.h" | ||
|
||
using namespace std; | ||
|
||
Leg::Leg(Team* _home, Team* _away) | ||
: home(_home), away(_away){ | ||
home_goals = 0; | ||
away_goals = 0; | ||
} | ||
|
||
void Leg::simulate() { | ||
int home_fans_impact = home->get_fans_impact(); | ||
int away_fans_impact = home->get_fans_impact(away); | ||
|
||
int home_deffend = home->get_defend_power() + home_fans_impact; | ||
int home_midfield = home->get_midfield_power() + home_fans_impact; | ||
int home_attack = home->get_attack_power() + home_fans_impact; | ||
|
||
int away_deffend = away->get_defend_power() + away_fans_impact; | ||
int away_midfield = away->get_midfield_power() + away_fans_impact; | ||
int away_attack = away->get_attack_power() + away_fans_impact; | ||
|
||
home_goals = get_num_of_goals(home_attack, home_midfield, away_deffend, away_midfield); | ||
away_goals = get_num_of_goals(away_attack, away_midfield, home_deffend, home_midfield); | ||
} | ||
|
||
int Leg::get_num_of_goals(int home_attack, int home_midfield, int away_deffend, int away_midfield) { | ||
float result = (home_attack * home_midfield) / (away_midfield * away_deffend); | ||
return floor(3 * result * sqrt(result)); | ||
} | ||
|
||
void Leg::print_leg() { | ||
cout << '\t' << home->get_name() << ' ' << home_goals << " - " << away_goals << ' ' << away->get_name() << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef __GAME_H__ | ||
#define __GAME_H__ | ||
|
||
#include <iostream> | ||
#include <cmath> | ||
|
||
#include "team.h" | ||
|
||
class Leg { | ||
public: | ||
Leg(Team* _home, Team* _away); | ||
void simulate(); | ||
int get_num_of_goals(int home_attack, int home_midfield, int away_deffend, int away_midfield); | ||
int get_home_goals() {return home_goals;}; | ||
int get_away_goals() {return away_goals;}; | ||
void print_leg(); | ||
private: | ||
Team* home; | ||
Team* away; | ||
|
||
int home_goals; | ||
int away_goals; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,60 @@ | ||
#include "match.h" | ||
#include "match.h" | ||
|
||
using namespace std; | ||
|
||
Match::Match(Team* home, Team* away, int number) | ||
: first_leg(home, away), second_leg(away, home), match_number(number), first_team(home), second_team(away) {} | ||
|
||
void Match::simulate() { | ||
first_leg.simulate(); | ||
second_leg.simulate(); | ||
} | ||
|
||
void Match::determine_winner() { | ||
if (first_leg.get_home_goals() + second_leg.get_away_goals() > second_leg.get_home_goals() + first_leg.get_away_goals()) { | ||
second_team->set_participation_status(false); | ||
winner = first_team; | ||
} | ||
else if (first_leg.get_home_goals() + second_leg.get_away_goals() < second_leg.get_home_goals() + first_leg.get_away_goals()) { | ||
first_team->set_participation_status(false); | ||
winner = second_team; | ||
} | ||
else if (first_leg.get_home_goals() > second_leg.get_home_goals()) { | ||
second_team->set_participation_status(false); | ||
winner = first_team; | ||
} | ||
else if (first_leg.get_home_goals() < second_leg.get_home_goals()) { | ||
first_team->set_participation_status(false); | ||
winner = second_team; | ||
} | ||
else | ||
penalty_time(); | ||
} | ||
|
||
void Match::penalty_time() { | ||
int first_team_fans_impact = second_team->get_fans_impact(first_team); | ||
int second_team_fans_impact = second_team->get_fans_impact(); | ||
|
||
float prob1 = (first_team->get_attack_power() + first_team_fans_impact) / (second_team->get_goalkeeper_power()); | ||
float prob2 = (second_team->get_attack_power() + second_team_fans_impact) / (first_team->get_goalkeeper_power()); | ||
|
||
if (prob1 > prob2) { | ||
second_team->set_participation_status(false); | ||
winner = first_team; | ||
} | ||
else { | ||
first_team->set_participation_status(false); | ||
winner = second_team; | ||
} | ||
} | ||
|
||
void Match::print_match() { | ||
cout << match_number << ":" << endl; | ||
first_leg.print_leg(); | ||
second_leg.print_leg(); | ||
cout << '\t' << "=> winner: " << winner->get_name() << endl; | ||
} | ||
|
||
bool Match::contains(Team* team) { | ||
return (team == first_team) or (team == second_team); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,28 @@ | ||
#ifndef __MATCH_H__ | ||
#define __MATCH_H__ | ||
|
||
class Match | ||
{ | ||
#include <iostream> | ||
|
||
#include "leg.h" | ||
#include "team.h" | ||
|
||
class Match { | ||
public: | ||
Match(); | ||
Match(Team* home, Team* away, int match_number); | ||
void simulate(); | ||
void determine_winner(); | ||
void penalty_time(); | ||
bool contains(Team* team); | ||
|
||
Team* run_match(); | ||
void print_match(); | ||
private: | ||
Leg first_leg; | ||
Leg second_leg; | ||
Team* first_team; | ||
Team* second_team; | ||
Team* winner; | ||
int match_number; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
#include "player.h" | ||
#include "player.h" | ||
|
||
using namespace std; | ||
|
||
Player::Player(string _name, int _power , string _position) | ||
: name(_name), power(_power), position(_position){ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
#ifndef __PLAYER_H__ | ||
#define __PLAYER_H__ | ||
|
||
class Player | ||
{ | ||
#include <iostream> | ||
#include <string> | ||
#include <cstdlib> | ||
|
||
class Player { | ||
public: | ||
Player(); | ||
Player(std::string _name, int _power, std::string _position); | ||
std::string get_name() { return name;} | ||
int get_power() const { return power;} | ||
std::string get_position() { return position;} | ||
private: | ||
std::string name; | ||
int power; | ||
std::string position; | ||
}; | ||
|
||
#endif |
Oops, something went wrong.