-
-
Save parzibyte/dd5d7be80a1609a235b7e574cd3277ff to your computer and use it in GitHub Desktop.
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
void actualizarJugadorEnRanking(string nick) | |
{ | |
JugadorParaRanking jugador = calcular_puntaje(nick); | |
vector<JugadorParaRanking> jugadores = obtenerJugadoresRanking(); | |
int posibleIndice = indiceDeJugador(jugadores, jugador.nombre); | |
if (posibleIndice == -1) | |
{ | |
jugadores.push_back(jugador); | |
} | |
else | |
{ | |
jugadores[posibleIndice] = jugador; | |
} | |
guardarJugadoresRanking(ordenar(jugadores)); | |
} | |
void guardarJugadoresRanking(vector<JugadorParaRanking> jugadores) | |
{ | |
ofstream archivo; | |
archivo.open(ARCHIVO_RANKING.c_str(), fstream::out); | |
// Solo escribir [MAXIMO_JUGADORES_RANKING] jugadores | |
int verdaderoFin = MAXIMO_JUGADORES_RANKING; | |
if (jugadores.size() < verdaderoFin) | |
{ | |
verdaderoFin = jugadores.size(); | |
} | |
int i; | |
for (i = 0; i < verdaderoFin; i++) | |
{ | |
JugadorParaRanking jugador = jugadores[i]; | |
archivo << jugador.nombre << DELIMITADOR_RANKING << jugador.puntuacion << DELIMITADOR_RANKING << jugador.movimientos << "\n"; | |
} | |
archivo.close(); | |
} | |
vector<JugadorParaRanking> obtenerJugadoresRanking() | |
{ | |
vector<JugadorParaRanking> jugadores; | |
JugadorParaRanking jugadorParaRanking; | |
ifstream archivo(ARCHIVO_RANKING.c_str()); | |
string linea, nombre, puntuacion, movimientos; | |
while (getline(archivo, linea)) | |
{ | |
stringstream input_stringstream(linea); | |
getline(input_stringstream, nombre, DELIMITADOR_RANKING); | |
getline(input_stringstream, puntuacion, DELIMITADOR_RANKING); | |
getline(input_stringstream, movimientos, DELIMITADOR_RANKING); | |
jugadorParaRanking.nombre = nombre; | |
jugadorParaRanking.puntuacion = atof(puntuacion.c_str()); | |
jugadorParaRanking.movimientos = atof(movimientos.c_str()); | |
jugadores.push_back(jugadorParaRanking); | |
} | |
return jugadores; | |
} | |
vector<JugadorParaRanking> ordenar(vector<JugadorParaRanking> jugadores) | |
{ | |
int i, j; | |
for (i = 0; i < jugadores.size() - 1; i++) | |
{ | |
for (j = i + 1; j < jugadores.size(); j++) | |
{ | |
JugadorParaRanking jugadorActual = jugadores[i]; | |
JugadorParaRanking jugadorDerecha = jugadores[j]; | |
if (jugadorActual.puntuacion < jugadorDerecha.puntuacion) | |
{ | |
jugadores[i] = jugadorDerecha; | |
jugadores[j] = jugadorActual; | |
} | |
else if (jugadorActual.puntuacion == jugadorDerecha.puntuacion) | |
{ | |
if (jugadorActual.movimientos > jugadorDerecha.movimientos) | |
{ | |
jugadores[i] = jugadorDerecha; | |
jugadores[j] = jugadorActual; | |
} | |
} | |
} | |
} | |
return jugadores; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment