Hilfe bei Highscore programmierung

enzo545

Mitglied
Hallo zusammen,
ich schlage mich seit ein paar Wochen mit dem Problem meiner Highscore. Ich möchte das die Punktzahl sich entsprechend nach der Größe sortiert und das funktioniert mit dem Befehl "Array.sort(Score);". Jetzt zu den Problem: Wie verschiebe ich mit der sortierten Punktzahl auch die Namen? Der Code ist etwas Umfangreich wegen der Oberfläche. Ich hoffe ihr könnt helfen. Hab diese Woche Präsentation und muss fertig werden ;(

Java:
import java.awt.*;
import java.awt.event.*;
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;
import java.io.Writer;

import javax.swing.*;
import javax.swing.event.*;
import java.util.Arrays;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 06.03.2012
  * @V. De Nicola
  */

public class Highscore_Frame extends JFrame {
  // Anfang Attribute
  Player player;
  Main main;
  static BufferedReader br;
  FileReader fr;
  Image hintergrund;
	
  private JLabel platz = new JLabel();
  private JLabel score = new JLabel();
  private JLabel name = new JLabel();
  private JLabel platz1 = new JLabel();
  private JLabel platz2 = new JLabel();
  private JLabel platz3 = new JLabel();
  private JLabel platz4 = new JLabel();
  private JLabel platz5 = new JLabel();
  private JTextField textbox = new JTextField();
  public static JButton speichern = new JButton();
  private static JLabel name1 = new JLabel();
  private static JLabel name2 = new JLabel();
  private static JLabel name4 = new JLabel();
  private static  JLabel name5 = new JLabel();
  private static  JLabel name3 = new JLabel();
  private static  JLabel jLabel11 = new JLabel();
  private static JLabel score1 = new JLabel();
  private static  JLabel score2 = new JLabel();
  private static  JLabel score3 = new JLabel();
  private static  JLabel score4 = new JLabel();
  private static  JLabel score5 = new JLabel();
  private JLabel fehler = new JLabel();
  public static JButton schließen = new JButton();
  
  static String Name[]= new String[5]; //Name und Score
  static int[] Score = new int[5];
  int flagge=1;
  static int punkte;
  int n = 0;
  int sc = 0;
  //name1 = Highscore[1][1];
  // Ende Attribute

  public Highscore_Frame(String title) 
{
    // Frame-Initialisierung
    super(title);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 532;
    int frameHeight = 489;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    Container cp = getContentPane();
    cp.setLayout(null);
    cp.setBackground(Color.GREEN);
    // Anfang Komponenten

    platz.setBounds(40, 24, 52, 29);
    platz.setText("Platz");
    platz.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    platz.setHorizontalAlignment(SwingConstants.CENTER);
    cp.add(platz);
    score.setBounds(416, 24, 64, 29);
    score.setText("Score");
    score.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    score.setHorizontalAlignment(SwingConstants.CENTER);
    cp.add(score);
    name.setBounds(224, 24, 65, 29);
    name.setText("Name");
    name.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    name.setHorizontalAlignment(SwingConstants.CENTER);
    cp.add(name);
    platz1.setBounds(50, 80, 19, 29);
    platz1.setText("1.");
    platz1.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(platz1);
    platz2.setBounds(50, 136, 19, 29);
    platz2.setText("2.");
    platz2.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(platz2);
    platz3.setBounds(50, 192, 19, 29);
    platz3.setText("3.");
    platz3.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(platz3);
    platz4.setBounds(50, 248, 19, 29);
    platz4.setText("4.");
    platz4.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(platz4);
    platz5.setBounds(50, 304, 19, 29);
    platz5.setText("5.");
    platz5.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(platz5);
    textbox.setBounds(200, 384, 113, 24);
    textbox.setText("Name");
    textbox.setFont(new Font("MS Sans Serif", Font.ITALIC, 13));
    cp.add(textbox);
    speichern.setBounds(200, 416, 113, 41);
    speichern.setText("Speichern");
    speichern.setMargin(new Insets(2, 2, 2, 2));
    speichern.addActionListener(new ActionListener() 
    {
      public void actionPerformed(ActionEvent evt) 
      {	
    	  speichern_ActionPerformed(evt);
		
		}
      });
    
    cp.add(speichern);
    schließen.setBounds(350, 416, 113, 41);
    schließen.setText("Schließen");
    schließen.setMargin(new Insets(2, 2, 2, 2));
    //cp.add(schließen);
    name1.setBounds(208, 80, 101, 32);
    name1.setText("");
    name1.setHorizontalAlignment(SwingConstants.CENTER);
    name1.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(name1);
    name2.setBounds(208, 136, 101, 32);
    name2.setText("");
    name2.setHorizontalAlignment(SwingConstants.CENTER);
    name2.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(name2);
    name4.setBounds(208, 248, 101, 32);
    name4.setText("");
    name4.setHorizontalAlignment(SwingConstants.CENTER);
    name4.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(name4);
    name5.setBounds(208, 304, 101, 32);
    name5.setText("");
    name5.setHorizontalAlignment(SwingConstants.CENTER);
    name5.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(name5);
    name3.setBounds(208, 192, 101, 32);
    name3.setText("");
    name3.setHorizontalAlignment(SwingConstants.CENTER);
    name3.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(name3);
    jLabel11.setBounds(82, 16, 3, 16);
    jLabel11.setText("");
    jLabel11.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel11.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(jLabel11);
    score1.setBounds(408, 80, 85, 32);
    score1.setHorizontalAlignment(SwingConstants.CENTER);
    score1.setText("");
    score1.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(score1);
    score2.setBounds(408, 136, 85, 32);
    score2.setHorizontalAlignment(SwingConstants.CENTER);
    score2.setText("");
    score2.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(score2);
    score3.setBounds(408, 192, 85, 32);
    score3.setText("");
    score3.setHorizontalAlignment(SwingConstants.CENTER);
    score3.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(score3);
    score4.setBounds(408, 248, 85, 32);
    score4.setText("");
    score4.setHorizontalAlignment(SwingConstants.CENTER);
    score4.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(score4);
    score5.setBounds(408, 304, 85, 32);
    score5.setText("");
    score5.setHorizontalAlignment(SwingConstants.CENTER);
    score5.setFont(new Font("MS Sans Serif", Font.PLAIN, 21));
    cp.add(score5);
    fehler.setBounds(320, 384, 179, 24);
    fehler.setBackground(Color.RED);
    fehler.setText("<~ Bitte Name eingeben!");
    fehler.setFont(new Font("MS Sans Serif", Font.PLAIN, 14));
    fehler.setVisible(false);
    cp.add(fehler);
    // Ende Komponenten

    setResizable(false);
    setVisible(true);
  }
  
  public void init()
  {
	  setBackground(Color.GREEN);
  }
  
  public static void übergabe(int finalscore)
	{
		punkte = finalscore;
	}

  // Anfang Methoden
  public void speichern_ActionPerformed(ActionEvent evt)
  {	  	  
	  try {
		schließen();
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	  
	  try {
		auslesen();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }	
  

  // Ende Methoden

  public void schließen() throws IOException
  {
	  sortiere(Score, Name);
	  
	  //Arrays.sort(Score);
	  
	  Writer output_name = null;
	  Writer output_score = null;
	  
  	  File highscore_name = new File("D:\\Eclipse\\RunAway!\\bin\\name.txt");
  	  File highscore_score = new File("D:\\Eclipse\\RunAway!\\bin\\score.txt");
  	   
  	  output_name = new BufferedWriter(new FileWriter(highscore_name));
  	  output_score = new BufferedWriter(new FileWriter(highscore_score));
  	  
	  
  	  for(int n=0; n<Score.length; n++)
  	  {
	  	  output_name.write(Name[n] + "\r\n"); //Ausgabe in der .txt datei
  	  }
  	  
  	for(int n=0; n<Name.length; n++)
	  { 
	  	  output_score.write(Score[n] + "\r\n");
	  }
  	  
  	  output_name.close();
  	  output_score.close();
  
	  System.out.println();
	  System.out.println("Datei wurde geschrieben.");//System.exit(Highscore_Frame);
	  System.out.println(Name[0]+" = "+Score[0]);
	  System.out.println(Name[1]+" = "+Score[1]);
	  System.out.println(Name[2]+" = "+Score[2]);
	  System.out.println(Name[3]+" = "+Score[3]);
	  System.out.println(Name[4]+" = "+Score[4]);
	  
  }
  
  public static void auslesen() throws IOException
  {
	  	FileReader fr_name = new FileReader("D:\\Eclipse\\RunAway!\\bin\\name.txt");
	  	FileReader fr_score = new FileReader("D:\\Eclipse\\RunAway!\\bin\\score.txt");
		BufferedReader na = new BufferedReader(fr_name);
		BufferedReader sc = new BufferedReader(fr_score);
		  
		//Score
		String seins = sc.readLine();
		String szwei = sc.readLine();
		String sdrei = sc.readLine();
		String svier = sc.readLine();
		String sfuenf = sc.readLine();
		//Name
		String neins = na.readLine();
		String nzwei = na.readLine();
		String ndrei = na.readLine();
		String nvier = na.readLine();
		String nfuenf = na.readLine();

	    Score[0] = Integer.parseInt(seins);
	    Score[1] = Integer.parseInt(szwei);
	    Score[2] = Integer.parseInt(sdrei);
	    Score[3] = Integer.parseInt(svier);
	    Score[4] = Integer.parseInt(sfuenf);
	    
	    Name[0] = neins;
	    Name[1] = nzwei;
	    Name[2] = ndrei;
	    Name[3] = nvier;
	    Name[4] = nfuenf;
	    
	      name1.setText(Name[0]);
		  score1.setText(""+Score[0]);
	 
		  name2.setText(Name[1]);
		  score2.setText(""+Score[1]);
	 
		  name3.setText(Name[2]);
		  score3.setText(""+Score[2]);
	  
		  name4.setText(Name[3]);
		  score4.setText(""+Score[3]);
	 
		  name5.setText(Name[4]);
		  score5.setText(""+Score[4]);
  }
  
  public static void sortiere(int[] x, String[] y) //Bubblesort
  {
	 /* boolean unsortiert=true;
	  int temp;
	  String name;
	
	  while (unsortiert)
	  {
		  unsortiert = false;
		  for (int i=0; i < x.length-1; i++)
			  if (x[i] < x[i+1])
			  {
				  temp = x[i];
				  x[i] = x[i+1];
				  x[i+1] = temp;
				  
				  name = y[i];
				  y[i] = y[i+1];
				  y[i+1] = name;
				  
				  unsortiert = true;
			  }
		  }*/
	  Arrays.sort(Score);
	  
	  
  }
 
 
  public static void main(String[] args)
  {
   	  

	  new Highscore_Frame("Highscore");
	  
	  sortiere(Score, Name);  
	  
	  try {
			auslesen();
		  } 
	  catch (IOException e) 
		  {
			e.printStackTrace();
		  }
	  
	  }
  	
  }

Im Anhang noch bilder...

Danke im voraus. Für weitere Fragen stehe ich gerne zur Verfügung
 

Anhänge

  • hs1.jpg
    hs1.jpg
    33 KB · Aufrufe: 115
  • hs2.jpg
    hs2.jpg
    31,6 KB · Aufrufe: 79
Mach eine eigene Klasse Score mit Instanzvariable String für den Namen und int für die Punkte, ungefähr so:
Java:
/**
  * Datenklasse für die Speicherung eines Spielergebnisses.
  */
public final class Score {

  /** Der name des Spielers */
  private final String spielerName;

  /** Die erzielten Punkte */
  private final int punkte;

  public Score(final String spielerName, final int punkte) {
    this.spielerName = spielerName;
    this.punkte = punkte;
  }
  public final String getSpielerName() {
     return this.spielerName;
   }

   public final int getPunkte() {
    return this.punkte;
   }
 }

Implementiere einen ScoreComparator, so:
Java:
public final ScoreComparator implements Comparator<Score> {

  public final int compare(final Score score1, final Score score2) {

    if (score1.getPunkte() < score2.getPunkte() {
      return -1;
    }

    if (score1.getPunkte() > score2.getPunkte() {
      return 1;
    }

   // Punktestände sind gleich, wir sortieren nun nach dem Namen.
   return score1.getSpielerName.compareTo(score2.getSpielerName());
  }
}
Speichere Deine Spieler und Punktestände nicht mehr in zwei primitiven Arrays, sondern in einem Array mit Score-Elementen. Rufe Dann die sort-Methode wie folgt auf.
Java:
Score[] scoreArray;
...
Arrays.sort(scoreArray, new ScoreComparator());

Damit ist die Anforderung abgedeckt. Wenn es besonders schön werden soll, baust Du noch Validierung bei Score ein (darf spielerName null oder leer sein?, gibt es negative Punktzahlen?), machst aus dem ScoreComparator eine enum mit genau einem Element, damit man nicht immer new ScoreComparator() macht etc.

P.S. Ach ja, und versuche mal, Deinen Code so zu strukturieren, dass in einer Klasse die ganzen Anweisungen zum Aufbau der GUI stehen, in einer zweiten die Daten und in einer dritten die Spiellogik. Das hilft bei der Fehlersuche.
 
Zuletzt bearbeitet von einem Moderator:
Danke für deine schnelle Antwort nillehammer...Tut mir leid dass ich nicht schnell zurückgeschrieben hab aber mein Internet hat für das WE nicht funktioniert...

Wie gesagt, DANKE aber könntest du mir vielleicht helfen zu verstehen was genau passiert (ich muss es schließlich auch erklären), wann soll ich genau was initialisieren? Die Klassen hab ich schon mal erstellt... Ich komme leider nicht ganz mit bin etwas durcheinander weil ich auch viel lernen muss 🙁 und ich bekomme bei Eclipse eine Fehlermeldung wenn ich versuche
Java:
public final ScoreComparator implements Comparator<Score>
zu starten...rot unterstrichen bei "final".

Wäre sehr nett wenn du mir weiter helfen könntest.

LG Enzo
 

Neue Themen


Zurück
Oben