?? score.java
字號:
/**
* @(#)Score.java
* @A high score ranking of normal mode and survival mode.
*
* @Link Scholes
* @version 1.00 2008/7/21
*/
package Data;
//Java core packages
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class Score implements DataInterface
{
private int temp;
private int tempRanking;
private int normalScores[];
private int survivalScores[];
private int survivalStages[];
private String score;
private String normalMaps[];
private String normalPlayers[];
private String survivalPlayers[];
private String scores[];
private BufferedReader input;
private BufferedWriter output;
private File file;
private Scanner scanner;
//construct a ranking based on the file "score.dat"
public Score()
{
normalScores = new int[10];
survivalScores = new int[10];
survivalStages = new int[10];
normalMaps = new String[10];
normalPlayers = new String[10];
survivalPlayers = new String[10];
file = new File("score.dat");
try
{
if (file.exists())
{
read();
}
else
{
create();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
//get the rankings of specified kind(normal/survival)
public String[] getRankings(boolean b)
{
scores = new String[10];
if (b)
{
for (int i = 0;i < 10;i ++)
{
score = "";
if (i < 9)
{
score += " ";
}
score += (i + 1) + " " + normalPlayers[i] + " ";
for (int j = 9;j > normalMaps[i].length();j --)
{
score += " ";
}
score += normalMaps[i] + " ";
for (int j = 9;j > Integer.toString(normalScores[i]).length();j --)
{
score += " ";
}
score += normalScores[i];
scores[i] = score;
}
}
else
{
for (int i = 0;i < 10;i ++)
{
score = "";
if (i < 9)
{
score += " ";
}
score += (i + 1) + " " + survivalPlayers[i] + " STAGE";
for (int j = 4;j > Integer.toString(survivalStages[i]).length();j --)
{
score += " ";
}
score += survivalStages[i] + " ";
for (int j = 9;j > Integer.toString(survivalScores[i]).length();j --)
{
score += " ";
}
score += survivalScores[i];
scores[i] = score;
}
}
return scores;
}
//get the temporary ranking of the specified score in the specified kind(normal/survival)
public int getTempRanking(boolean b,int n)
{
tempRanking = -1;
for (int i = 0;i < 10;i ++)
{
if (n > ((b)?normalScores[i]:survivalScores[i]))
{
tempRanking = i;
i = 10;
}
}
return tempRanking;
}
//update the ranking with specified kind(normal/survival),player name,map name/stage and score
public void update(boolean b,String s,String t,int n)
{
if (b)
{
for (int i = 9;i > tempRanking;i --)
{
normalPlayers[i] = normalPlayers[i - 1];
normalMaps[i] = normalMaps[i - 1];
normalScores[i] = normalScores[i - 1];
}
normalPlayers[tempRanking] = s;
normalMaps[tempRanking] = t;
normalScores[tempRanking] = n;
}
else
{
for (int i = 9;i > tempRanking;i --)
{
survivalPlayers[i] = survivalPlayers[i - 1];
survivalStages[i] = survivalStages[i - 1];
survivalScores[i] = survivalScores[i - 1];
}
survivalPlayers[tempRanking] = s;
survivalStages[tempRanking] = Integer.parseInt(t);
survivalScores[tempRanking] = n;
}
try
{
write();
}
catch(IOException e)
{
e.printStackTrace();
}
}
//reset the ranking with defaulted value
public void reset()
{
for (int i = 0;i < 10;i ++)
{
normalScores[i] = 1000 - i * 100;
survivalScores[i] = 10000 - i * 1000;
survivalStages[i] = 10 - i;
normalMaps[i] = "DEFAULT";
normalPlayers[i] = "MBP";
survivalPlayers[i] = "MBP";
}
try
{
write();
}
catch(IOException e)
{
e.printStackTrace();
}
}
//turn the ranking to String
public String toString()
{
score = normalPlayers[0] + " " + normalMaps[0] + " " + normalScores[0];
for (int i = 1;i < 10;i ++)
{
score += " " + normalPlayers[i] + " " + normalMaps[i] + " " + normalScores[i];
}
for (int i = 0;i < 10;i ++)
{
score += " " + survivalPlayers[i] + " " + survivalStages[i] + " " + survivalScores[i];
}
return score;
}
//create the file "score.dat" if not existed
public void create() throws IOException
{
output = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("score.dat")))));
output.close();
reset();
}
//read the contents of the file "score.dat"
public void read() throws IOException
{
score = "";
input = new BufferedReader(new InputStreamReader(new BufferedInputStream(new GZIPInputStream(new FileInputStream("score.dat")))));
temp = input.read();
while (temp != -1)
{
score += (char)temp;
temp = input.read();
}
input.close();
scanner = new Scanner(score);
for (int i = 0;i < 10;i ++)
{
normalPlayers[i] = scanner.next();
normalMaps[i] = scanner.next();
normalScores[i] = scanner.nextInt();
}
for (int i = 0;i < 10;i ++)
{
survivalPlayers[i] = scanner.next();
survivalStages[i] = scanner.nextInt();
survivalScores[i] = scanner.nextInt();
}
}
//write the contents of the ranking to the file "score.dat"
public void write() throws IOException
{
input = new BufferedReader(new StringReader(toString()));
output = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("score.dat")))));
temp = input.read();
while (temp != -1)
{
output.write(temp);
temp = input.read();
}
input.close();
output.close();
}
} //end class Score
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -