Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 3, 2022 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/dd5d7be80a1609a235b7e574cd3277ff to your computer and use it in GitHub Desktop.
Save parzibyte/dd5d7be80a1609a235b7e574cd3277ff to your computer and use it in GitHub Desktop.
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