ExampleGame

以下是ExampleGame的源代码。为简单起见,ExampleGame实际上并不包含用于玩游戏的代码。它只是检索或更新用户的高分。

要查看用户当前的高分值是多少,可以运行:

java ExampleGame get

要为用户设置新的高分值,可以运行:

java ExampleGame set score

要检索用户当前的高分,ExampleGame只需实例化HighScore对象并对其getHighScore方法进行调用。要为用户设置新的高分,ExampleGame实例化HighScore对象并调用setHighScore,将其传递给用户新的高分。

这是ExampleGameExampleGame.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);
    }
}