Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class RankingJere {
- // Le pasamos el archivo como parametro para que pueda escribir en el y el
- // puntaje de la partida actual;
- public static void setPuntaje(File scoreFile, String highScore) {
- FileWriter writeFile = null;
- BufferedWriter writer = null;
- try {
- writeFile = new FileWriter(scoreFile);
- writer = new BufferedWriter(writeFile);
- // writer.write(this.highScore);
- writer.write(highScore);
- // writer.write(puntaje);
- } catch (Exception e) {
- // errores
- } finally {
- try {
- if (writer != null) {
- writer.close();
- }
- } catch (Exception e) {
- // errosr
- }
- }
- }
- //
- // EL ENCARGADO DEL LEER EL ARCHIVO CREADO POR EL JUEGO
- public static String getPuntaje() throws FileNotFoundException {
- String ret = "";
- FileReader readFile = null;
- BufferedReader reader = null;
- // formato = Jorge: 666
- try {
- readFile = new FileReader("highscore.txt");
- reader = new BufferedReader(readFile);
- return reader.readLine();
- }
- catch (Exception e) {
- ret = " nadie : 0";
- } finally {
- try {
- if (reader != null) {
- reader.close();
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return ret;
- }
- //
- // EL TEST CONSISTE EN CAMBIAR LAS VARIABLES Y VER QUE EL ARCHIVO QUE SE
- // GENERA SE VA SOBRE ESCRIBIENDO
- // PISANDO ASI EL PUNTAJE ANTERIOR
- // FALTA COMPARAR EL puntajeMain CON EL VALOR ENTERO DEL ARCHIVO
- // ("highScore.txt")
- // SI putajeMain > VALOR_ENTERO_DENTRO_DEL_STRING
- public static void main(String[] args) throws FileNotFoundException {
- // variables
- String nombreJugador = "jorge";
- int puntaje = 1220;
- String highScoreMain = "";
- highScoreMain = nombreJugador + ":" + puntaje;
- // crea archivo
- File scoreFile = new File("highscore.txt");
- if (!scoreFile.exists()) {
- try {
- scoreFile.createNewFile();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // fin de la creacion del archivo
- // variable que supone ya saque el valor dentro del String del archivo
- String puntajeMasAltoEver = getPuntaje();
- String puntajeS = "";
- boolean asd = false;
- for (int i = 0; i < puntajeMasAltoEver.length(); i++) {
- if(asd == true){
- puntajeS+= ""+puntajeMasAltoEver.charAt(i);
- }
- if(puntajeMasAltoEver.charAt(i) == ':'){
- asd = true;
- }
- }
- Integer valor = new Integer(puntajeS);
- System.out.println(puntajeS);
- if (puntaje > valor) {
- setPuntaje(scoreFile, highScoreMain);
- System.out.println();
- // Integer parseado = new Integer (highScoreMain":"[1]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement