All pastes #967604 Raw Edit

HighScore

public text v1 · immutable
·published 2008-04-02 15:25 UTC
rendered paste body
package Wumpus;
import java.util.Collection;
import java.util.TreeSet;
import java.util.Iterator;
import java.io.*;

/**
 * Summary description for Score.
 */
public class Score
{
	private Collection scores = new TreeSet();
	private static final int MAX_SCORES = 10;
	private boolean victory;
	private int start, end;
	private String name;
	private int score;
	private String cave;
	private ScoreEntry player;
	private String towrite;
	private Iterator iter;

	public Score()
	{
		load("highscores.dat");
		victory = false;
	}

	public void add(ScoreEntry score)
	{
		scores.add(score);
		save("highscores.dat");
	}

	public void show()
	{
		// create form

		int n = 0;
		for (iter = scores.iterator(); iter.hasNext(); )
		{
			if (++n > MAX_SCORES)
			{
				break;
			}

			ScoreEntry entry = (ScoreEntry)iter.next();
			// add entry to form
		}
		// show form
	}

	private void load(String fileName)
	{
		try
		{
			FileReader rfile = new FileReader("HighScores.dat");
			BufferedReader bfile = new BufferedReader(rfile);
			String readln = bfile.readLine();
			while (readln != null)
			{
				// parse the lines
				// format of Name;Score;Map

				// reads the name
				start = 0;
				end = readln.indexOf(";", start);
				name = readln.substring(start, end);

				// reads the score
				start = end + 1;
				end = readln.indexOf(";", start);
				score = (Integer.valueOf(readln.substring(start, end))).intValue();

				// reads the map number
				start = end + 1;
				end = readln.indexOf(";", start);
				cave = readln.substring(start, end);

				// makes a score entry with previously read info
				player = new ScoreEntry(name, score, cave);
				scores.add(player);
			}
			bfile.close();

		}
		catch (java.io.FileNotFoundException e1)
		{
			System.out.println("File Not Found Exception Caught - " + e1.toString());
		}
		catch (java.io.IOException e2)
		{
			System.out.println("I/O Exception Caught - " + e2.toString());
		}
	}

	private void save(String fileName)
	{
		try
		{
			FileWriter wfile = new FileWriter("HighScores.dat");
			if(!scores.isEmpty())
			{
				for(int i = 0; i < scores.size(); i++)
				{
					//Get the next item
					iter = scores.iterator();
					player = (ScoreEntry)(iter.next());

					//Build up the string
					towrite = player.getName();
					towrite += ";";
					towrite += player.getScore();
					towrite += ";";
					towrite += player.getMap();
					towrite += "\n";

					//write it to disk
					wfile.write(towrite, 0, towrite.length());
				}	
			}
			wfile.close();
		}
		catch (java.io.IOException e3)
		{
			System.out.println("I/O Exception Caught - " + e3.toString());
		}
	}
}