ExampleGame
以下是ExampleGame
的源代码。为简单起见,ExampleGame
实际上并不包含用于玩游戏的代码。它只是检索或更新用户的高分。
要查看用户当前的高分值是多少,可以运行:
java ExampleGame get
要为用户设置新的高分值,可以运行:
java ExampleGame set score
要检索用户当前的高分,ExampleGame
只需实例化HighScore
对象并对其getHighScore
方法进行调用。要为用户设置新的高分,ExampleGame
实例化HighScore
对象并调用setHighScore
,将其传递给用户新的高分。
这是ExampleGame
,ExampleGame.java的源代码:
package com.gamedev.games;
import java.io.*;
import java.security.*;
import java.util.Hashtable;
import com.scoredev.scores.*;
public class ExampleGame
{
public static void main(String args[])
throws Exception
{
HighScore hs = new HighScore("ExampleGame");
if (args.length == 0)
usage();
if (args[0].equals("set")) {
hs.setHighScore(Integer.parseInt(args[1]));
} else if (args[0].equals("get")) {
System.out.println("score = "+ hs.getHighScore());
} else {
usage();
}
}
public static void usage()
{
System.out.println("ExampleGame get");
System.out.println("ExampleGame set <score>");
System.exit(1);
}
}