Java桌面游戏—争霸的设计与实现毕业论文(7)

2019-05-18 13:03

第26页 共41页

图4-6 AIEasy类的类图

图4-7 AIHard类的类图

其中复杂ai的几个重要的判断方法的主要实现代码如下:

1. 判断某一个版块是否大部分被某个玩家占领了的方法mostOwned(Continent w){ }: 方法中定义了一个整型的变量ownedByPlayer来记录被该版快中的一个国家一个玩家所占领,在用for循环方法一个一个检验该版快内的所有国家是否都被某个player给占领了,如果被某个玩家占领的国家数超过了该版快上国家数的一半则算是该玩家大部分占领了该版快,则方法返回true,否则返回false。

第27页 共41页

public boolean mostlyOwned(Continent w) {

int ownedByPlayer=0;

Vector territoriesContained = w.getTerritoriesContained();

for (int c=0; c< territoriesContained.size() ; c++) {

if ( ((Country)territoriesContained.elementAt(c)).getOwner() == player ) { ownedByPlayer++; } }

if ( ownedByPlayer>=(territoriesContained.size()/2) ) { return true; }

else {

return false; } }

2. 用来判断将军队放在哪个大陆上主要方法getPlaceArmies():

public String getPlaceArmies() { String output;

if ( game.NoEmptyCountries()==false ) {

??

for (int i=0; i

??

}

??

ratio = extraArmiesForContinent - numberOfBorders - (neighborTerritories - numberOfBorders)+(territorynum * 0.9) - (numberofEnemyUnits * 1.2);

if (check <= ratio && hasFreeTerritories(ct) == true) { check = ratio; val = i; } ??

for (int j=0; j

if ( ((Country)ct.elementAt(j)).getOwner() == null ) { name=((Country)ct.elementAt(j)).getColor()+\ picked = true; break; } } }

if (picked == false) { ?? }

?? } else { ??

第28页 共41页

}

String s = keepBlocking(player); if( !s.equals(\ name = s;

String f = freeContinent(player); if( !f.equals(\ name = f; if ( name.equals(\ ??

else if (game.getSetup() )

output = \ else

output = \ }

return output;

}

4.5 其他工具类的设计

为了使游戏的开发能变得方便,我们必须完全依靠Java API中提供的类或接口,我们必须要定义一些自己的工具类和控制类来方便我们的设计,Java语言不像C++或其他一些具有多重继承特性的面向对象编程语言,它是单继承的,所以为了实现与类似与多重继承的功能,必须用到接口,这是接口被开发最主要的目的:

游戏中我们定义一个监听器接口RiskListener和适配器RiskAdapter,其中定义一些常用的方法,具体代码如下: RiskListener:

public interface RiskListener {

public void sendMessage(String output, boolean a, boolean b); public void needInput(int s); public void noInput();

public void setGameStatus(String state); public void newGame(boolean t); public void startGame(boolean s); public void closeGame();

public void setSlider(int min, int c1num, int c2num); public void armiesLeft(int l, boolean s); public void showDice(int n, boolean w); public void showMapPic(java.awt.Image p);

public void showCardsFile(String c, boolean m); public void serverState(boolean s);

public void openBattle(int c1num, int c2num); public void closeBattle();

public void addPlayer(int type, String name, java.awt.Color color, String ip); public void delPlayer(String name);

public void showDiceResults(int[] att, int[] def); public void setNODAttacker(int n); public void setNODDefender(int n); }

第29页 共41页

RiskAdapter类:该适配器类主要是用空实现接口类的,将RiskListener中的方法全实现了,如public void sendMessage(String output, boolean a, boolean b)方法被实现为: public void sendMessage(String output, boolean a, boolean b){ },其他所有方法类似,虽然它实现了接口中的所有抽象方法,但是没有具体实现方法的内容,这有什么用呢?因为如果你要是其他的程序中实现该接口,你必须要实现其中的多有方法,无论你用不用的上,所以就会导致一些无用功,然而我们定义了这样一个适配器类,你就不用实现接口,可以继承该方法,而重写其中的一些你需要用到的方法,这样就省去了很多的无用功,能节省很多的宝贵时间!

在游戏中还需要通过java输入输出包来读取数据或将数据写入文件保存,这时候需要对文件或数据进行筛选,这时候我们就需要有自己的文件过滤器,所以我们定义一个RiskFileFilter类:

public class RiskFileFilter extends FileFilter {

??

public RiskFileFilter(String ext) { extension = ext; }

public boolean accept(File f) { if (f.isDirectory()) { return true; }

String ext = getExtension(f); if (ext != null) {

if (ext.equals( extension )) { return true; }else {

return false; } }

return false; }

public String getDescription(){ ?? }

public static String getExtension(File f) {?? }

}

最后我们需要主要的类Risk:

public class Risk extends Thread {

??

public Risk(String a,String b) {} public Risk() {}

public void addRiskListener(RiskListener o) {} public void deleteRiskListener(RiskListener o) {} public synchronized void parser(String m) {} public void run() {}

public void GameParser(String mem) {} public void DoEndGo() {}

第30页 共41页

}

public void getInput() {}

public String getAutoDefendString() {} public String whoWon() {}

public static Frame findParentFrame(Container c) {} public static String getNewFile(Frame f,String a) {} public synchronized void kickedOff() { } public Vector getCurrentCards() {}

public boolean isOwnedCurrentPlayerInt(int name) {} public String getCurrentMission() {} public Color[] getPlayerColors() {} public Color getCurrentPlayerColor() {}

public boolean canTrade(String c1, String c2, String c3) {} public RiskGame getGame() {}

public String getCountryName(int c) {} public boolean getAutoEndGo() {} public boolean getAutoDefend() {}

public static Color getTextColorFor(Color c) {}

4.6 游戏界面的设计

一个游戏只有好的界面的才能吸引玩家,要有很好的交互性,所以本游戏的开发过程中就是以操作简便、界面美观、灵活实用的用户要求为出发点的,当玩家进入游戏时,游戏的主界面的设计如下图所示:


Java桌面游戏—争霸的设计与实现毕业论文(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017年上半年青海省抹灰工技师模拟试题

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: