📄 risk.java
字号:
// do that autoplace thing
if ( game.getGameMode()==RiskGame.MODE_SECRET_MISSION || autoplaceall==true ) {
if ( chatSocket == null || myAddress.equals(Addr) ) {
Vector a = game.shuffleCountries();
String outputb="PLACEALL";
for (int c=0; c< a.size() ; c++) {
outputb=outputb + " " + ((Country)a.elementAt(c)).getColor();
//outputb=outputb + " " + ((Country)a.elementAt(c)).getName() ;
}
if (chatSocket == null) {
GameParser( outputb );
}
else if ( myAddress.equals(Addr) ) {
outChat.println( outputb );
}
}
}
}
output=null;
needInput=false;
}
else { output=resb.getString( "core.start.error.players"); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "startgame gametype cardtype (autoplaceall recycle)"); }
}
// REPLAY A GAME
else if (input.equals("play")) {
if (StringT.countTokens() >= 1) {
String filename = GetNext();
while ( StringT.hasMoreElements() ) {
filename = filename + " " + GetNext();
}
try {
URL url;
// TODO dont think this can ever work as an applet anyway
//if (Risk.applet==null) {
url = (new File(filename)).toURI().toURL();
//}
//else {
// url = new URL( risk.engine.Risk.applet.getCodeBase() , filename );
//}
BufferedReader bufferin=new BufferedReader(new InputStreamReader(url.openStream()));
//create thread with bufferin
class Replay extends Thread {
private Risk risk;
private BufferedReader bufferin;
public Replay(Risk r, BufferedReader in) {
risk=r;
bufferin=in;
}
public void run() {
try {
String input = bufferin.readLine();
while(input != null) {
//System.out.print(input+"\n");
risk.GameParser(input);
input = bufferin.readLine();
}
bufferin.close();
}
catch(Exception error) {
error.printStackTrace();
}
//set replay off
replay = false;
getInput();
}
};
Thread replaythread = new Replay(this, bufferin);
//set boolean that replay is on
replay = true;
replaythread.start();
output="playing \""+filename+"\"";
}
catch(Exception error) {
output="unable to play \""+filename+"\"";
}
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "play filename"); }
}
else { output=resb.getString( "core.error.incorrect").replaceAll( "\\{0\\}", "newplayer, delplayer, startgame, choosemap, choosecards, info, autosetup"); }
}
// UNDO
else if (input.equals("undo")) {
if ( StringT.hasMoreTokens()==false ) {
if ( unlimitedLocalMode ) {
try {
if (Undo!=null && Undo.size()!=0) {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(Undo.toByteArray()));
game = (RiskGame)in.readObject();
//game = (RiskGame)Undo.getObject( nullCipher );
output =resb.getString( "core.undo.undone");
}
else {
output =resb.getString( "core.undo.error.unable");
}
}
catch (Exception e) {
System.out.print(resb.getString( "core.loadgame.error.undo") + "\n");
e.printStackTrace();
}
}
else {
output = resb.getString( "core.undo.error.network");
}
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "undo"); }
}
// REPLAY A GAME FROM THE GAME FILE
else if (input.equals("replay")) {
if ( StringT.hasMoreTokens()==false ) {
if ( unlimitedLocalMode ) {
try {
Vector replayCommands = game.getCommands();
replayCommands.remove( replayCommands.size()-1 );
saveGameToUndoObject();
game = new RiskGame();
replay = true;
for (Enumeration e = replayCommands.elements() ; e.hasMoreElements() ;) {
GameParser( (String)e.nextElement() );
//try{ Thread.sleep(1000); }
//catch(InterruptedException e){}
}
replay = false;
output="replay of game finished";
}
catch (Exception e) {
System.out.print(resb.getString( "core.loadgame.error.undo") + "\n");
e.printStackTrace();
}
}
else {
output = resb.getString( "core.undo.error.network");
}
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "replay"); }
}
// SAVE GAME
else if (input.equals("savegame")) {
if (StringT.countTokens() >= 1) {
if ( unlimitedLocalMode ) {
Vector replayCommands = game.getCommands();
replayCommands.remove( replayCommands.size()-1 );
String filename = GetNext();
while ( StringT.hasMoreElements() ) {
filename = filename + " " + GetNext();
}
if ( battle==false && game.saveGame(filename) ) {
output=resb.getString( "core.save.saved");
}
else {
output=resb.getString( "core.save.error.unable");
}
}
else {
output = resb.getString( "core.save.error.unable" );
}
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "savegame filename"); }
}
else if (input.equals("showmission")) {
if (StringT.hasMoreTokens()==false) {
// only show to the right player!
if ( showHumanPlayerThereInfo( game.getCurrentPlayer() ) ) {
output = resb.getString( "core.showmission.mission") + " " + getCurrentMission();
}
else { output=resb.getString( "core.showmission.error"); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "showmission"); }
}
else if (input.equals("showarmies")) {
if (StringT.hasMoreTokens()==false) {
if ( game.getState() != RiskGame.STATE_NEW_GAME) {
Country[] v = game.getCountries();
output=resb.getString( "core.showarmies.countries") + System.getProperty("line.separator");
for (int c=0; c< v.length ; c++) {
output = output + v[c].getColor() + " " + v[c].getName()+" - "; // Display
if ( v[c].getOwner() != null ) {
output = output + ((Player)v[c].getOwner()).getName() +" ("+v[c].getArmies() +")";
if (game.getGameMode() == 2 && game.getSetup() && game.getState() !=RiskGame.STATE_SELECT_CAPITAL) {
Vector players = game.getPlayers();
for (int a=0; a< players.size() ; a++) {
if ( ((Player)players.elementAt(a)).getCapital() != null && ((Player)players.elementAt(a)).getCapital() == v[c] ) {
output = output + " " + resb.getString( "core.showarmies.captial")
.replaceAll( "\\{0\\}", ((Player)players.elementAt(a)).getName());
}
}
}
output = output + System.getProperty("line.separator");
}
else {
output = output + resb.getString( "core.showarmies.noowner") + System.getProperty("line.separator");
}
}
}
else { output=resb.getString( "core.showarmies.error.unable"); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "showarmies"); }
}
else if (input.equals("showcards")) {
if (StringT.hasMoreTokens()==false) {
if ( showHumanPlayerThereInfo( game.getCurrentPlayer() ) ) {
Vector c = ((Player)game.getCurrentPlayer()).getCards();
if (c.size() == 0) {
output=resb.getString( "core.showcards.nocards");
}
else {
output=resb.getString( "core.showcards.youhave");
for (int a=0; a< c.size() ; a++) {
if ( ((Card)c.elementAt(a)).getName().equals(Card.WILDCARD) ) {
output = output + " " + Card.WILDCARD; // resb.getString( "core.showcards.wildcard"); // dont use this as user needs to type it in
}
else {
output = output + " \"" + ((Card)c.elementAt(a)).getName() +" "+ ((Country)((Card)c.elementAt(a)).getCountry()).getName() +" ("+((Country)((Card)c.elementAt(a)).getCountry()).getColor()+")\""; // Display
}
}
}
if(game.getCardMode()==RiskGame.CARD_FIXED_SET) {
output = output+"\n"+ resb.getString("cards.fixed");
}
else {
output = output+"\n"+ resb.getString("cards.nexttrade").replaceAll( "\\{0\\}", "" + getNewCardState());
}
}
else { output=resb.getString( "core.showcards.error.unable"); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "showcards"); }
}
else if (input.equals("autoendgo")) {
if (StringT.hasMoreTokens()==false) {
String strSelected;
if ( game.getCurrentPlayer().getAutoEndGo() ) {
strSelected = "core.autoendgo.on";
}
else {
strSelected = "core.autoendgo.off";
}
output = resb.getString( "core.autoendgo.setto").replaceAll( "\\{0\\}", resb.getString( strSelected));
}
else if (StringT.countTokens() == 1) {
String option = GetNext();
if (option.equals("on") ) {
game.getCurrentPlayer().setAutoEndGo(true);
output = resb.getString( "core.autoendgo.setto").replaceAll( "\\{0\\}", resb.getString( "core.autoendgo.on"));
}
else if (option.equals("off") ) {
game.getCurrentPlayer().setAutoEndGo(false);
output = resb.getString( "core.autoendgo.setto").replaceAll( "\\{0\\}", resb.getString( "core.autoendgo.off"));
}
else { output=resb.getString( "core.autoendgo.error.unknown").replaceAll( "\\{0\\}", option); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "autoendgo on/off"); }
}
else if (input.equals("autodefend")) {
if (StringT.hasMoreTokens()==false) {
String strSelected;
if ( game.getCurrentPlayer().getAutoDefend() ) {
strSelected = "core.autodefend.on";
}
else {
strSelected = "core.autodefend.on";
}
output = resb.getString( "core.autodefend.setto").replaceAll( "\\{0\\}", resb.getString( strSelected));
}
else if (StringT.countTokens() == 1) {
String option = GetNext();
if (option.equals("on") ) {
game.getCurrentPlayer().setAutoDefend(true);
output = resb.getString( "core.autodefend.setto").replaceAll( "\\{0\\}", resb.getString( "core.autodefend.on"));
}
else if (option.equals("off") ) {
game.getCurrentPlayer().setAutoDefend(false);
output = resb.getString( "core.autodefend.setto").replaceAll( "\\{0\\}", resb.getString( "core.autodefend.off"));
}
else { output=resb.getString( "core.autodefend.error.unknown").replaceAll( "\\{0\\}", option); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "autodefend on/off"); }
}
else if (game.getState()==RiskGame.STATE_TRADE_CARDS) {
if (input.equals("trade")) {
if (StringT.countTokens()==3) {
// trade Japan wildcard Egypt
int noa=0;
Card cards[] = game.getCards(GetNext(),GetNext(),GetNext());
if (cards[0] != null && cards[1] != null && cards[2] != null) { // if the player DOES HAVE all the cards he chose
noa = game.trade(cards[0], cards[1], cards[2]);
}
if ( noa != 0 ) { // if the trade WAS SUCCESSFUL
output=resb.getString( "core.trade.traded").replaceAll( "\\{0\\}", "" + noa);
}
else { output=resb.getString( "core.trade.error.unable"); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "trade card card card"); }
}
else if (input.equals("endtrade")) {
if (StringT.hasMoreTokens()==false) {
if ( game.endTrade() ) {
output=resb.getString( "core.trade.endtrade");
}
else { output=resb.getString( "core.trade.end.error.unable"); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "endtrade"); }
}
else { output=resb.getString( "core.error.incorrect").replaceAll( "\\{0\\}", "showcards, trade, endtrade"); }
}
else if (game.getState()==RiskGame.STATE_PLACE_ARMIES) {
if (input.equals("placearmies")) {
if (StringT.countTokens()==2) {
String country=GetNext();
int c=RiskGame.getNumber( country );
int num=RiskGame.getNumber( GetNext() );
Country t;
if (c != -1) {
t=game.getCountryInt(c);
}
else {
//YURA:LANG t=game.getCountryByName(country);
t=null;
}
if ( t != null && num!=-1 && !( game.getGameMode() == 1 && t.getOwner() == null) && !( game.getGameMode() == 3 && t.getOwner() == null) ) {
int result = game.placeArmy(t, num);
if (result!=0) {
//{0} new army placed in: {1}
output= resb.getString( "core.place.placed")
.replaceAll( "\\{0\\}", "" + num)
.replaceAll( "\\{1\\}", t.getName()); // Display
if (result == 2) {
output=output + whoWon();
}
}
else { output=resb.getString( "core.place.error.unable"); }
}
else { output=resb.getString( "core.place.error.invalid"); }
}
else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "placearmies country number"); }
}
else if (input.equals("autoplace")) {
if (StringT.hasMoreTokens()==false) {
if ( game.NoEmptyCountries() == false ) {
if (!replay) {
if ( chatSocket == null) {
GameParser( "PLACE " + game.getEmptyCountry() );
}
else if ( myAddress.equals(Addr) ) { // if this is a network game
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -