第16页 共41页
}
4.1.3 Mission类的设计与实现:
Mission类主要用来代表在不同模式的游戏当中你的任务,其类图如下所示:
图4-4 Mission类的类图
4.1.4 Country类的设计与实现:
在本游戏中就是以国家为基本单位的,所以它为最核心的类,大部分其他类都需要访问它,其类图如下:
图4-3 Country类的类图
第17页 共41页
其中该类通过其构造函数构造一个任务对象,传递的参数有:任务要消灭的敌人,需要占领的的国家数,在一个版块上需要的最少军队数,第一个需要占领的国家,第二个需要占领的国家,第三个需要占领的国家以及任务的描述,其构造函数代码如下:
public Mission(Player p, int noc, int noa, Continent c1, Continent c2, Continent c3, String d) { player = p; noofcountries = noc; noofarmies = noa; con1 = c1; con2 = c2; con3 = c3; discription = d; }
4.1.5 Player类的设计与实现:
玩家用来代表一个玩家对象,该类也是游戏程序中所要用到的最基本的类,其中玩家的属性有:名字,颜色,玩家任务,玩家类型等,为了后面的统计功能的实现,还在其中增加了已经消灭的敌人向量,所拥有的卡片,所拥有的版块数等等,为了实现网络联机的功能,还增加了玩家的网络地址等等。
而玩家类中有一些重要的方法,像在后面要实现卡片交易,必要能获得玩家所拥有的卡片,还要判断一下玩家多拥有的卡片是否可以进行交易,以及为了实现网络联机要统计功能,必须要获得一些统计数据和玩家的网络地址,具体代码如下:
public Card takeCard() {
Card c=(Card)cardsOwned.firstElement(); cardsOwned.removeElementAt(0); return c; }
public void tradeInCards(Card card1, Card card2, Card card3) {
int noaFORcard = 2;
// 看看你是否可以从拥有的国家中获得额外的军队
if (territoriesOwned.contains( card1.getCountry() ) ) { ((Country)card1.getCountry()).addArmies(noaFORcard); currentStatistic.addReinforcements(noaFORcard); }
else if (territoriesOwned.contains( card2.getCountry() ) ) { ((Country)card2.getCountry()).addArmies(noaFORcard); currentStatistic.addReinforcements(noaFORcard); }
else if (territoriesOwned.contains( card3.getCountry() ) ) { ((Country)card3.getCountry()).addArmies(noaFORcard); currentStatistic.addReinforcements(noaFORcard); }
cardsOwned.remove(card1); cardsOwned.remove(card2); cardsOwned.remove(card3); cardsOwned.trimToSize(); }
public void addPlayersEliminated(Player p) {
第18页 共41页
playersEliminated.add(p); }
public Vector getPlayersEliminated() { return playersEliminated; }
4.1.6 Statistic类的设计与实现:
统计类主要是用来实现游戏的统计功能而设计的类,每个玩家拥有一个统计向量用来记录一些数据,给类里面只要一个包含记录各种数据的整形数组statistic属性,统计数据有国家数,军队数,毁灭数,伤亡数,增强过程,大陆数,国家数,进攻次数,撤退次数,胜利次数等等,该类的类图如下所示:
图4-5 Statistic类的类图
其中的重要方法有结束一轮进攻进行统计endGoStatistics(int a, int b, int c, int d),向量statistics向量中0位置记录国家数,1记录军队数,2记录毁掉数,3记录伤亡数,4
中其他一些数据的计算方法,具体代码如下:
记录增强数,5记录大陆数,6记录帝国数,7记录进攻次数,8记录撤退次数,9记录胜利的次数,10记录战胜次数,11记录受到进攻的次数:
public void endGoStatistics(int a, int b, int c, int d) {
statistics[0] = a; statistics[1] = b; statistics[5] = c; statistics[6] = d; }
public void addReinforcements(int a) { statistics[4] = statistics[4] + a; }
public void addKill() { statistics[2]++;
第19页 共41页
}
public void addCasualty() { statistics[3]++; }
public void addAttack() { statistics[7]++; }
public void addAttacked() { statistics[11]++; }
public void addRetreat() { statistics[8]++; }
public void addCountriesWon() { statistics[9]++; }
public void addCountriesLost() { statistics[10]++; }
4.2 本地翻译包的设计
本包的设计是为了本游戏的国际化,在游戏过程中可以通过本包中的类实现语言的本地化,本包中包括两个类:TranslationBundle和MapTranslator。
4.2.1 TranslationBoudle类的设计与实现
该类主要是用来获得编写的资源包(这指语言包),然后该类可以通过Java API中的一些方法获得该游戏所处系统的语言环境,然后将游戏中的语言变换成本地语言;
该类中主要用到了java.util.ResourceBundle类,资源包包含特定于语言环境的对象。当程序需要一个特定于语言环境的资源时(如 String),程序可以从适合当前用户语言环境的资源包中装入它。以这种方式可以编写很大程度上独立于用户语言环境的程序代码,它将资源包中大部分(如果不是全部)特定于语言环境的信息隔离开来。
这就使您所编写的程序可以: 轻松地本地化或翻译成不同的语言 一次处理多个语言环境
以后可以轻松地进行修改,支持更多的语言环境
资源包属于这样的系列,其成员共享一个公共的基本名称,但是名称中还有标识其语言环境的其他组件。例如,某个资源包系列的基本名称可能是 \。该系列应该有一个默认资源包,其名称与其系列名相同( \),并且如果不支持指定的语言环境,则此资源包应该用作最后的手段。然后,此系列可根据需要提供特定于语言环境的成员,例如一个名为 \的德语资源包。
某个系列中的每个资源包都包含相同的项,但是已经针对该资源包所代表的语言环境翻译了各项。例如,\和 \可能有用在取消操作按钮上的 String。在 \中,String 可能含有 \,而在 \中
第20页 共41页
则可能含有 \。
如果不同的国家/地区有不同的资源,则可以进行限定:例如,\是瑞士 (CH) 中包含德语 (de) 的对象。
Translation类中主要的方法有获得资源包和载入资源包,具体实现代码如下:
public class TranslationBundle {
??
static public ResourceBundle getBundle() {
if (TranslationBundle.resBundle == null) { TranslationBundle.loadBundle(); }
return TranslationBundle.resBundle; }
static private void loadBundle() {
if (TranslationBundle.strLanguage == null) { loc = Locale.getDefault(); } else {
loc = new Locale(TranslationBundle.strLanguage); }
try {
TranslationBundle.resBundle =
ResourceBundle.getBundle(\
} catch( java.util.MissingResourceException e) {
System.out.println( \ System.exit( 2); } }
static private void setLanguage(String strLanguage) {
TranslationBundle.strLanguage = strLanguage; } }
4.2.2 MapTranslator类的设计与实现
该类主要是用来将游戏中图片显示中的语言变换成本地语言; 具体实现代码如下:
public class MapTranslator
{
private static ResourceBundle MapResb = null; private static ResourceBundle CardsResb = null; public static void setMap(String strFile) {
String strName = strFile.substring( 0, strFile.lastIndexOf( '.'));
strFile = strName + \+ TranslationBundle.getBundle().getLocale().getLanguage() + \
try {
MapResb = new PropertyResourceBundle( (new URL(king.engine.Risk.mapsdir,strFile)).openStream() );