From 781b3cf3b08b674923bb8565d5ec53f05ef571f5 Mon Sep 17 00:00:00 2001 From: arnxxau Date: Tue, 3 May 2022 02:05:13 +0200 Subject: [PATCH] changed structure of almost everything + eff fix --- BinTree.hh | 2 - Makefile | 19 ++++---- categories.cc | 6 +-- categories.hh | 6 +-- circuit.cc | 36 ++------------ circuit.hh | 47 ++----------------- player.cc | 20 +++----- player.hh | 3 +- program.cc | 127 ++++++++++++++++++++++++++------------------------ ranking.cc | 41 ++++++++++------ ranking.hh | 19 +++++--- tournament.cc | 42 ++++++++--------- tournament.hh | 17 ++++--- 13 files changed, 165 insertions(+), 220 deletions(-) diff --git a/BinTree.hh b/BinTree.hh index 4bd9b4d..579d0d2 100644 --- a/BinTree.hh +++ b/BinTree.hh @@ -8,8 +8,6 @@ using namespace std; - - // A BinTree implements binary trees with values of type T. template class BinTree { diff --git a/Makefile b/Makefile index b6aa185..34e8f78 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,27 @@ -OPCIONS = -D_JUDGE_ -D_GLIBCXX_DEBUG -O2 -Wall -Wextra -Werror -Wno-sign-compare -std=c++11 +flags = -D_JUDGE_ -D_GLIBCXX_DEBUG -O2 -Wall -Wextra -Werror -Wno-sign-compare -std=c++11 # -G++ = g++ # program.exe: program.o player.o tournament.o ranking.o categories.o circuit.o - $(G++) -o program.exe program.o player.o tournament.o ranking.o categories.o circuit.o + g++ -o program.x program.o player.o tournament.o ranking.o categories.o circuit.o # program.o: program.cc - $(G++) -c program.cc $(OPCIONS) + g++ -c program.cc $(flags) # player.o: player.cc player.hh - $(G++) -c player.cc $(OPCIONS) + g++ -c player.cc $(flags) # tournament.o: tournament.cc tournament.hh - $(G++) -c tournament.cc $(OPCIONS) + g++ -c tournament.cc $(flags) # ranking.o: ranking.cc ranking.hh - $(G++) -c ranking.cc $(OPCIONS) + g++ -c ranking.cc $(flags) # categories.o: categories.cc categories.hh - $(G++) -c categories.cc $(OPCIONS) + g++ -c categories.cc $(flags) # circuit.o: circuit.cc circuit.hh - $(G++) -c circuit.cc $(OPCIONS) + g++ -c circuit.cc $(flags) # clean: rm -f *.o - rm -f *.exe + rm -f *.x diff --git a/categories.cc b/categories.cc index 950690d..b65401d 100644 --- a/categories.cc +++ b/categories.cc @@ -16,11 +16,11 @@ void categories::read_categories() { this -> points_per_level = points_per_level; } -int categories::get_points(int category, int level) const { +int categories::get_points(const int& category, const int& level) const { return points_per_level[category - 1][level - 1]; } -std::string categories::get_name(int category) const { +std::string categories::get_name(const int& category) const { return names[category - 1]; } @@ -40,7 +40,7 @@ int categories::get_max_lvl() const { } -void categories::print_categories(){ +void categories::print_categories() const{ for (int i = 0; i < max_categories; ++i) { std::cout << names[i]; for (int j = 0; j < max_lvl; ++j) diff --git a/categories.hh b/categories.hh index 2aec58c..2a1c12e 100644 --- a/categories.hh +++ b/categories.hh @@ -45,14 +45,14 @@ public: \pre cierto \post Retorna los puntos que hay que sumar al jugador en función de la categoría y el nivel introducidos. */ - int get_points(int category, int level) const; + int get_points(const int& category, const int& level) const; /** @brief Consultora para el nombre de la categoría. \pre El entero debe existir entre 1 y max_categories. \post Retorna el nombre de la categoría introducida. */ - std::string get_name(int category) const; + std::string get_name(const int& category) const; /** @brief Consultora para el nivel de un nombre de una categoría. @@ -80,7 +80,7 @@ public: \pre cierto \post Se habrán imprimido los datos por pantalla. */ - void print_categories(); + void print_categories() const; }; #endif \ No newline at end of file diff --git a/circuit.cc b/circuit.cc index 47bf3e8..7188c68 100644 --- a/circuit.cc +++ b/circuit.cc @@ -1,8 +1,6 @@ #include "circuit.hh" -circuit::circuit(categories cat) { - this -> cat = cat; -} +circuit::circuit() {} void circuit::read_tournaments() { cin >> n_tournaments; @@ -10,7 +8,7 @@ void circuit::read_tournaments() { int level; for (int i = 0; i < n_tournaments; ++i) { cin >> name >> level; - tournament tour(name, level, cat); + tournament tour(name, level); tournaments.insert(make_pair(name, tour)); } } @@ -33,11 +31,11 @@ int circuit::get_n_tournaments() const { return n_tournaments; } -void circuit::print_tournaments() { +void circuit::print_tournaments(const categories& c) const { std::cout << n_tournaments << std::endl; map::const_iterator it = tournaments.begin(); while (it != tournaments.end()) { - it -> second.print_tournament(); + it -> second.print_tournament(c); ++it; } @@ -46,29 +44,3 @@ void circuit::print_tournaments() { bool circuit::exists_tournament(const std::string& name) const { return tournaments.end() != tournaments.find(name); } - -void circuit::read_players() { - cin >> n_players; - string name; - for (int i = 0; i < n_players; ++i) { - cin >> name; - global_rank.add_player(player(name)); - } -} - -void circuit::add_player(const player& p) { - global_rank.add_player(p); - ++n_players; -} - -void circuit::remove_player(const std::string& name) { - global_rank.remove_player(name); -} - -ranking circuit::get_global_ranking() { - return global_rank; -} - -categories circuit::get_categories() { - return cat; -} \ No newline at end of file diff --git a/circuit.hh b/circuit.hh index cd818c7..56538be 100644 --- a/circuit.hh +++ b/circuit.hh @@ -20,10 +20,8 @@ class circuit { private: - int n_players = 0, n_tournaments = 0; // n_players -> P, n_tournaments -> T - map tournaments; - ranking global_rank; - categories cat; + int n_tournaments = 0; // n_players -> P + std::map tournaments; public: @@ -33,7 +31,7 @@ public: \pre cierto \post El resultado es un circuito vacio con las categorías introducidas. */ - circuit(categories cat); + circuit(); // tournaments @@ -77,7 +75,7 @@ public: \pre cierto \post Se habrán imprimido todos los torneos del circuito. */ - void print_tournaments(); + void print_tournaments(const categories& c) const; /** @brief Operación de consulta para la disponibilidad de torneos. @@ -85,43 +83,6 @@ public: \post Devuelve verdadero si ya existe previamente el torneo en el circuito, y en el caso contrario, falso. */ bool exists_tournament(const std::string& name) const; - - // players - - /** @brief Operación de lectura para jugadores. - - \pre P es mayor o igual a 0. - \post El resultado es un ranking con los jugadores leídos. - */ - void read_players(); - - /** @brief Operación de añadido para jugadores. Usa esencialmente las operaciones de ranking. - - \pre El jugador no existe en la lista. - \post El resultado es el mismo ranking pero con el jugador nuevo añadido en la última posición y n_players + 1 - */ - void add_player(const player& p); - - /** @brief Operación de borrado para jugadores. Usa esencialmente las operaciones de ranking. - - \pre El jugador existe en la lista. - \post El resultado es el mismo ranking pero sin el jugador implícito y n_players - 1. Consultar la clase ranking para un mayor detalle. - */ - void remove_player(const std::string& name); - - /** @brief Operación de consulta. - - \pre cierto - \post Retorna el ranking global del circuito. - */ - ranking get_global_ranking(); - - /** @brief Operación de consulta. - - \pre cierto - \post Retorna las categorías del circuito. - */ - categories get_categories(); }; #endif \ No newline at end of file diff --git a/player.cc b/player.cc index 8db1d6c..0a32b98 100644 --- a/player.cc +++ b/player.cc @@ -1,7 +1,8 @@ #include "player.hh" -player::player(std::string name) { +player::player(const std::string name, const int& rank_pos) { this -> name = name; + this -> rank_pos = rank_pos; } std::string player::get_name() const { @@ -44,16 +45,9 @@ void player::modify_total_points(int points) { } void player::print_player() const { - std::cout << name << " Rk:" << rank_pos << " Ps:" << total_points << " Ts:" << player_stats.played_tours << " WM:" << player_stats.matches_won_lost.first - << " LM:" << player_stats.matches_won_lost.second << " WS:" << player_stats.sets_won_lost.first << " LS:" << - player_stats.sets_won_lost.second << " WG:" << player_stats.games_won_lost.first << " LG:" << - player_stats.games_won_lost.second << std::endl; -/* - std::cout << "games won and lost -> " << player_stats.games_won_lost.first - << player_stats.games_won_lost.second << std::endl; - std::cout << "sets won and lost -> " << player_stats.sets_won_lost.first - << player_stats.sets_won_lost.second << std::endl; - std::cout << "matches won and lost -> " << player_stats.matches_won_lost.first - << player_stats.matches_won_lost.second << std::endl; -*/ + std::cout << name << " Rk:" << rank_pos << " Ps:" << total_points + << " Ts:" << player_stats.played_tours << " WM:" << player_stats.matches_won_lost.first + << " LM:" << player_stats.matches_won_lost.second << " WS:" << player_stats.sets_won_lost.first + << " LS:" << player_stats.sets_won_lost.second << " WG:" << player_stats.games_won_lost.first + << " LG:" << player_stats.games_won_lost.second << std::endl; } \ No newline at end of file diff --git a/player.hh b/player.hh index d84d494..8e965f6 100644 --- a/player.hh +++ b/player.hh @@ -6,7 +6,6 @@ #define AKIRA_PLAYER_HH #ifndef NO_DIAGRAM -#include #include #endif @@ -37,7 +36,7 @@ public: \pre cierto \post El resultado es un jugador con 0 puntos iniciales, todas las estadísticas a 0 junto con su correspondiente nombre. */ - player(std::string name); + player(const std::string name, const int& rank_pos); /** @brief Modificadora de posición en el rango global. diff --git a/program.cc b/program.cc index 284fb24..5ba6618 100644 --- a/program.cc +++ b/program.cc @@ -11,114 +11,121 @@ /** @brief Programa principal */ int main() { - string s; + std::string s; + categories cat; cat.read_categories(); - circuit circuit(cat); + // cout << "cat read" << endl; + + circuit circuit; circuit.read_tournaments(); - circuit.read_players(); - // cout << "hola" << endl; - cin >> s; + + // cout << "circuit read" << endl; + + ranking global_ranking; + global_ranking.read_players(); + // cout << "ranking read" << endl; + + + std::cin >> s; while (s != "fin") { - // cout << "s ->>>> " << s << endl; if (s == "nuevo_jugador" or s == "nj") { - string id; - cin >> id; - cout << "#" << s << ' ' << id << endl; - if (not circuit.get_global_ranking().is_player_there(id)) { - circuit.add_player(player(id)); - cout << circuit.get_global_ranking().get_number_of_players() << endl; + std::string id; + std::cin >> id; + std::cout << "#" << s << ' ' << id << std::endl; + if (not global_ranking.is_player_there(id)) { + global_ranking.add_player(player(id, 0)); + std::cout << global_ranking.get_number_of_players() << std::endl; } else - cout << "error: ya existe un jugador con ese nombre" << endl; + std::cout << "error: ya existe un jugador con ese nombre" << std::endl; } else if (s == "nuevo_torneo" or s == "nt"){ - string t; - cin >> t; + std::string t; + std::cin >> t; int c; - cin >> c; - cout << "#" << s << ' ' << t << ' ' << c << endl; + std::cin >> c; + std::cout << "#" << s << ' ' << t << ' ' << c << std::endl; if (circuit.exists_tournament(t)) - cout << "error: ya existe un torneo con ese nombre" << endl; - else if (1 > c or c > circuit.get_categories().get_max_categories()) - cout << "error: la categoria no existe" << endl; + std::cout << "error: ya existe un torneo con ese nombre" << std::endl; + else if (1 > c or c > cat.get_max_categories()) + std::cout << "error: la categoria no existe" << std::endl; else { - circuit.add_tournament(tournament(t, c, cat)); - cout << circuit.get_n_tournaments() << endl; + circuit.add_tournament(tournament(t, c)); + std::cout << circuit.get_n_tournaments() << std::endl; } } else if (s == "baja_jugador" or s == "bj") { - string id; - cin >> id; - cout << "#" << s << ' ' << id << endl; - if (circuit.get_global_ranking().is_player_there(id)) { - circuit.remove_player(id); - cout << circuit.get_global_ranking().get_number_of_players() << endl; + std::string id; + std::cin >> id; + std::cout << "#" << s << ' ' << id << std::endl; + if (global_ranking.is_player_there(id)) { + global_ranking.remove_player(id); + std::cout << global_ranking.get_number_of_players() << std::endl; } else - cout << "error: el jugador no existe" << endl; + std::cout << "error: el jugador no existe" << std::endl; } else if (s == "baja_torneo" or s == "bt") { - string t; - cin >> t; - cout << "#" << s << ' ' << t << endl; + std::string t; + std::cin >> t; + std::cout << "#" << s << ' ' << t << std::endl; if (circuit.exists_tournament(t)) { circuit.remove_tournament(t); - cout << circuit.get_n_tournaments() << endl; + std::cout << circuit.get_n_tournaments() << std::endl; } else - cout << "error: el torneo no existe" << endl; + std::cout << "error: el torneo no existe" << std::endl; } else if (s == "iniciar_torneo" or s == "it") { - string t; - cin >> t; - cout << "#" << s << ' ' << t << endl; - circuit.get_tournament(t).start_tour(circuit.get_global_ranking()); + std::string t; + std::cin >> t; + std::cout << "#" << s << ' ' << t << std::endl; + circuit.get_tournament(t).start_tour(global_ranking); } else if (s == "finalizar_torneo" or s == "ft") { - cout << "#" << s << endl; - string t; - cin >> t; - circuit.get_tournament(t).end_tour(); // pendiente pautas de impresión !!!!!!! + std::cout << "#" << s << std::endl; + std::string t; + std::cin >> t; + circuit.get_tournament(t).end_tour(); } else if (s == "listar_ranking" or s == "lr") { - cout << "#" << s << endl; - // cout << "listando" << endl; - circuit.get_global_ranking().print_ranking(); + std::cout << "#" << s << std::endl; + global_ranking.print_ranking(); } else if (s == "listar_jugadores" or s == "lj") { - cout << "#" << s << endl; - cout << circuit.get_global_ranking().get_number_of_players() << endl; - circuit.get_global_ranking().print_players(); + std::cout << "#" << s << std::endl; + std::cout << global_ranking.get_number_of_players() << std::endl; + global_ranking.print_players(); } else if (s == "consultar_jugador" or s == "cj") { - string id; - cin >> id; - cout << "#" << s << ' ' << id << endl; - if (circuit.get_global_ranking().is_player_there(id)) - circuit.get_global_ranking().get_player_by_name(id).print_player(); + std::string id; + std::cin >> id; + std::cout << "#" << s << ' ' << id << std::endl; + if (global_ranking.is_player_there(id)) + global_ranking.get_player_by_name(id).print_player(); else - cout << "error: el jugador no existe" << endl; + std::cout << "error: el jugador no existe" << std::endl; } else if (s == "listar_torneos" or s == "lt") { - cout << "#" << s << endl; - circuit.print_tournaments(); + std::cout << "#" << s << std::endl; + circuit.print_tournaments(cat); } else if (s == "listar_categorias" or s == "lc") { - cout << "#" << s << endl; - cout << circuit.get_categories().get_max_categories() << ' ' << circuit.get_categories().get_max_lvl() << endl; - circuit.get_categories().print_categories(); + std::cout << "#" << s << std::endl; + std::cout << cat.get_max_categories() << ' ' << cat.get_max_lvl() << std::endl; + cat.print_categories(); } - cin >> s; + std::cin >> s; } } \ No newline at end of file diff --git a/ranking.cc b/ranking.cc index 4752d4a..5fefef5 100644 --- a/ranking.cc +++ b/ranking.cc @@ -8,40 +8,47 @@ ranking::ranking(std::vector players) { ranking::ranking(){} -int ranking::eff_search(const std::string& name) const { +int ranking::linear_search(const std::string& name) const { for (int i = 0; i < number_of_players; ++i) { - if (rank[i].first.get_name() == name) + if (rank[i].first == name) return i; } return -1; } void ranking::add_player(player p) { + std::string s = p.get_name(); p.modify_rank_position(number_of_players + 1); - rank.push_back(std::make_pair(p, 0)); + rank.push_back(std::make_pair(s, p.get_rank_position())); ++number_of_players; - std::string s = p.get_name(); players_map.insert(std::make_pair(p.get_name(), p)); } +void ranking::read_players() { + std::cin >> number_of_players; + std::string name; + for (int i = 0; i < number_of_players; ++i) { + std::cin >> name; + rank.push_back(std::make_pair(name, i + 1)); + players_map.insert(std::make_pair(name, player(name, i + 1))); + } +} + void ranking::sort_rank() { sort(rank.begin(), rank.end(), order); } -bool ranking::order(const std::pair& p1, const std::pair& p2) { - return p1.first.get_rank_position() < p2.first.get_rank_position(); +bool ranking::order(const std::pair& p1, const std::pair& p2) { + return p1.second < p2.second; } - - void ranking::remove_player(const std::string& name) { players_map.erase(name); - int pos = eff_search(name); + int pos = linear_search(name); int v_size = rank.size(); for (int i = pos + 1; i < v_size; ++i) { rank[i - 1] = rank[i]; - players_map.find(rank[i].first.get_name()) -> second.modify_rank_position(i); - rank[i - 1].first.modify_rank_position(i); + players_map.find(rank[i].first) -> second.modify_rank_position(i); } rank.pop_back(); --number_of_players; @@ -56,19 +63,25 @@ player ranking::get_player_by_name(const std::string& name) { } player ranking::get_player_by_pos(int position) { + return players_map.find(rank[position - 1].first) -> second; +} + + +std::string ranking::get_player_name_by_pos(int position) const{ return rank[position - 1].first; } + bool ranking::is_player_there(const std::string& name) const { return players_map.end() != players_map.find(name); } -void ranking::print_ranking() { +void ranking::print_ranking() const { for (int i = 0; i < number_of_players; ++i) - std::cout << i + 1 << ' ' << rank[i].first.get_name() << ' ' << rank[i].first.get_total_points() << std::endl; + std::cout << i + 1 << ' ' << rank[i].first<< ' ' << players_map.find(rank[i].first) -> second.get_total_points() << std::endl; } -void ranking::print_players() { +void ranking::print_players() const { std::map::const_iterator it = players_map.begin(); while (it != players_map.end()) { it -> second.print_player(); diff --git a/ranking.hh b/ranking.hh index ee4cf4d..c719ee3 100644 --- a/ranking.hh +++ b/ranking.hh @@ -22,12 +22,14 @@ class ranking { private: - std::vector > rank; + std::vector > rank; + std::map players_map; + int number_of_players = 0; - std::map players_map; // lista ordenada con los jugadores ordenados por órden lexográfico para buscar de forma más eficiente - int eff_search(const std::string& name) const; - static bool order(const std::pair& p1, const std::pair& p2); + int linear_search(const std::string& name) const; + static bool order(const std::pair& p1, const std::pair& p2); + public: @@ -68,6 +70,9 @@ public: */ void add_player(player p); + + void read_players(); + /** @brief Elimina un jugador. \pre El jugador tiene que estar dentro del ranking. @@ -89,6 +94,8 @@ public: */ player get_player_by_pos(int position); + std::string get_player_name_by_pos(int position) const; + /** @brief Consulta si un jugador está en el ranking. \pre El string name tiene que ser un identificador de un jugador válido. @@ -101,14 +108,14 @@ public: \pre cierto \post Los datos se habrán imprimido por pantalla. */ - void print_ranking(); + void print_ranking() const; /** @brief Imprime cada jugador en orden lexográfico creciente. \pre cierto \post Los datos se habrán imprimido por pantalla. */ - void print_players(); + void print_players() const; /** @brief Consulta el número de jugadores. diff --git a/tournament.cc b/tournament.cc index 262a05e..45c4385 100644 --- a/tournament.cc +++ b/tournament.cc @@ -1,47 +1,43 @@ #include "tournament.hh" -#include -tournament::tournament(std::string name, int difficulty, categories cat) { + +tournament::tournament(std::string name, int difficulty) { this -> name = name; this -> difficulty = difficulty; - this -> cat = cat; } -void tournament::init_tour(ranking global_rank) { +void tournament::init_tour(const ranking& global_rank) { int n_players; - cin >> n_players; + std::cin >> n_players; int position; for (int i = 0; i < n_players; ++i) { - cin >> position; - local_rank.add_player(global_rank.get_player_by_pos(position)); + std::cin >> position; + std::string s = global_rank.get_player_name_by_pos(position); + names_with_points.push_back(std::make_pair(s, 0)); } } -BinTree tournament::arborescence(int n_players, int m, int root) { +BinTree tournament::arborescence(int n_players, int m, int root) const { if (n_players < m + 1 - root) return BinTree(root); else return BinTree(root, arborescence(n_players, m * 2, root), arborescence(n_players, m * 2, m + 1 - root)); } -void tournament::preorder_print(BinTree tree) { - if (tree.left().empty() and tree.right().empty()) cout << tree.value() << '.' << local_rank.get_player_by_pos(tree.value()).get_name(); +void tournament::preorder_print(const BinTree& tree) { + if (tree.left().empty() and tree.right().empty()) std::cout << tree.value() << '.' << names_with_points[tree.value() - 1].first; else { - cout << '('; + std::cout << '('; preorder_print(tree.left()); - cout << ' '; + std::cout << ' '; preorder_print(tree.right()); - cout << ')'; + std::cout << ')'; } } -void tournament::start_tour(ranking global_ranking) { +void tournament::start_tour(const ranking& global_ranking) { init_tour(global_ranking); - // cout << "inicializado" << endl; - local_rank.sort_rank(); - // cout << "sorteado" << endl; - pairing_chart = arborescence(local_rank.get_number_of_players(), 2, 1); - + pairing_chart = arborescence(names_with_points.size(), 2, 1); preorder_print(pairing_chart); - cout << endl; + std::cout << std::endl; } bool tournament::already_played() const { @@ -49,13 +45,13 @@ bool tournament::already_played() const { } void tournament::end_tour() { - std::cout << "test" << std::endl; + std::cout << "to be done" << std::endl; } std::string tournament::get_name() const { return name; } -void tournament::print_tournament() const { - std::cout << name << ' ' << cat.get_name(difficulty) << endl; +void tournament::print_tournament(const categories& c) const { + std::cout << name << ' ' << c.get_name(difficulty) << std::endl; } \ No newline at end of file diff --git a/tournament.hh b/tournament.hh index a7c282c..8f0d0e2 100644 --- a/tournament.hh +++ b/tournament.hh @@ -23,20 +23,19 @@ class tournament private: std::string name; int difficulty; // tipo de categoría - ranking local_rank; - // int n_levels; // número de niveles del torneo - categories cat; bool played = false; + std::vector > names_with_points; + BinTree pairing_chart; BinTree results; - BinTree arborescence(int n_players, int level, int root); + BinTree arborescence(int n_players, int level, int root) const; - void preorder_print(BinTree tree); + void preorder_print(const BinTree& tree); - void init_tour(ranking global_rank); + void init_tour(const ranking& global_rank); public: /** @brief Creadora por defecto. @@ -45,7 +44,7 @@ public: \pre Debe ser una categoría válida entre 1 y C. \post El resultado es un torneo con el nombre y la dificultad asignadas. */ - tournament(std::string name, int difficulty, categories cat); + tournament(std::string name, int difficulty); /** @brief Empieza un torneo, confecciona la tabla de emparejamientos e imprime por pantalla la tabla. @@ -53,7 +52,7 @@ public: \post El árbol binario "pairing_chart" está completado con la tabla de emparejamientos y el árbol estará impreso por pantalla. */ - void start_tour(ranking global_ranking); + void start_tour(const ranking& global_ranking); /** @brief Acaba con el torneo, da los resultatos finales y actualiza la ficha de los jugadores. @@ -84,7 +83,7 @@ public: \pre cierto \post Se ha imprimido el nombre y la categoría por pantalla. */ - void print_tournament() const; + void print_tournament(const categories& c) const; }; #endif \ No newline at end of file