⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 risk.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "join server"); }

				}

				// NEW SERVER
				else if (input.equals("startserver")) {

					if (StringT.hasMoreTokens()==false) {

						if ( chatter == null ) {

							// CREATE A SERVER
							try {

								chatter = new ChatArea(controller,port);

								output=resb.getString( "core.startserver.started");
								controller.serverState(true);

							}
							catch(Exception e) {

								chatter = null;
								output=resb.getString( "core.startserver.error")+" "+e.getMessage();
								controller.showMessageDialog(output);

							}

						}
						else {
							output=resb.getString( "core.startserver.error");
						}
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "startserver"); }

				}
				// KILL SERVER
				else if (input.equals("killserver")) {

					if (StringT.hasMoreTokens()==false) {

						if ( chatter != null ) {

							try {

								// shut down the server
								//if (chatter.serverSocket != null) {
								//	chatter.serverSocket.close();
								//	chatter=null;
								//}

								if (chatter != null) {
									chatter.closeSocket();
									chatter=null;
								}

								output=resb.getString( "core.killserver.killed");
								controller.serverState(false);


							}
							catch (Exception e) {
								output=resb.getString( "core.killserver.error")+" "+e.getMessage();
							}


						}
						else {
							output=resb.getString( "core.killserver.noserver");
						}
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "killserver"); }

				}

				else { // if there is no game and the command was unknown
					output=resb.getString( "core.loadgame.nogame");
				}

				// if there was NO game

				controller.sendMessage(output, false, true );

				setHelp();

				getInput();

			}
			// IN GAME COMMANDS
			else {

				//replace all country names with there numbers

				// 5 commands have to be checked:

				// capital	x1
				// trade	x3
				// placearmies	x1
				// attack	x2
				// movearmies	x2

				// send the message to all the clients

				String mtemp = myAddress+" " + message;


				if ( chatSocket == null ) {

					//GameParser( "game " + message );
					// change for yura:lobby

					GameParser( mtemp );

				}
				else { // if this is a network game
					outChat.println( mtemp );
				}


			}

			//System.out.print("END PROCESSING\n");

		    }
		}
		catch(RuntimeException ex) {

			System.err.println("FAITAL ERROR IN RISK");

			ex.printStackTrace();
			run();
		}

	}

	private void saveGameToUndoObject() {


			if ( unlimitedLocalMode ) {

				// the game is saved
				try {
					//Undo = new SealedObject( game, nullCipher );

					
					Undo.reset();
					ObjectOutputStream out = new ObjectOutputStream(Undo);
					out.writeObject(game);
					out.flush();
					out.close();

				}
				catch (Exception e) {
					System.out.print(resb.getString( "core.loadgame.error.undo") + "\n");
					e.printStackTrace();
				}
			}
	}

	/**
	 * This parses the string, calls the relavant method and displays the correct error messages
	 * @param mem The string needed for parsing
	 */
	public void GameParser(String message) {

		controller.sendDebug(message);

		game.addCommand(message);

		int oldState=game.getState();

		boolean needInput=true;

		String input;
		String output=null;

		StringT = new StringTokenizer( message );

		String Addr = GetNext();

		if (Addr.equals("ERROR")) { // server has sent us a error

			String Pname = GetNext();

			while ( StringT.hasMoreElements() ) {
				Pname = Pname +" "+ GetNext();
			}	

			controller.showMessageDialog(Pname);

		}
		else if (Addr.equals("DICE")) { // a server command

			int attSize = RiskGame.getNumber(GetNext());
			int defSize = RiskGame.getNumber(GetNext());

			output=resb.getString( "core.dice.rolling") + System.getProperty("line.separator") + resb.getString( "core.dice.results");

			int att[] = new int[ attSize ];
			output = output + " " + resb.getString( "core.dice.attacker");
			for (int c=0; c< attSize ; c++) {
				att[c] = RiskGame.getNumber(GetNext());
				output = output + " " + (att[c]+1);
			}

			int def[] = new int[ defSize ];
			output = output + " " + resb.getString( "core.dice.defender");
			for (int c=0; c< defSize ; c++) {
				def[c] = RiskGame.getNumber(GetNext());
				output = output + " " + (def[c]+1);
			}

			output = output + System.getProperty("line.separator");

			int result[] = game.battle( att, def );

			if ( result[0]==1 ) {
				output = output + resb.getString( "core.dice.result").
									replaceAll( "\\{0\\}", "" + result[2]).//defeated
									replaceAll( "\\{1\\}", "" + result[1]);//lost


				if (result[3]==0) {
					int n=((Country)game.getAttacker()).getArmies()-1;

					output=output + System.getProperty("line.separator") + resb.getString( "core.dice.notdefeated") + " ";

					if (n > 0) {
						if (n > 3) { n=3; }
						output=output + resb.getString( "core.dice.attackagain").replaceAll( "\\{0\\}", "" + n);

						Player attackingPlayer = ((Country)game.getAttacker()).getOwner();

						if ( showHumanPlayerThereInfo( attackingPlayer ) ) {

							controller.showDice(n, true);

						}


					}
					else {
						output=output + resb.getString( "core.dice.noattackagain");
					}


				}
				else {

					Player attackingPlayer = ((Country)game.getAttacker()).getOwner();

					if ( showHumanPlayerThereInfo( attackingPlayer ) ) {

						//controller.setSlider(result[4], result[5]);
						controller.setSlider(result[4], ((Country)game.getAttacker()).getColor(), ((Country)game.getDefender()).getColor() );

					}

					output=output + System.getProperty("line.separator") + resb.getString( "core.dice.defeated") + " ";

					if ( result[3]==2 ) {
						output=output + resb.getString( "core.dice.eliminated") + " ";
					}

					// if there is only one amount of troops u can move
					if ( result[4] == result[5] ) {

						int noa = game.moveAll();

						int ma = game.moveArmies( noa );

						//Moved {0} armies to captured country.
						output=output + resb.getString( "core.dice.armiesmoved").replaceAll( "\\{0\\}", "" + noa);

						if (ma==2) {

							output=output + whoWon();

						}

					}
					else {
						//How many armies do you wish to move? ({0} to {1})
						output=output + resb.getString( "core.dice.howmanyarmies")
								.replaceAll( "\\{0\\}", "" + result[4])
								.replaceAll( "\\{1\\}", "" + result[5]);
					}


				}

				if ( battle ) {

					controller.showDiceResults( att, def );

					try{ Thread.sleep(1000); }
					catch(InterruptedException e){}

				}

			}
			else { output=resb.getString( "core.dice.error.unabletoroll"); }

			// ==1 this fixes the automove bug, when u need to trade after rolling and automove
			if ( game.getState()!=RiskGame.STATE_ROLLING && game.getState()!=RiskGame.STATE_DEFEND_YOURSELF) {

				closeBattle();

			}




		}
		else if (Addr.equals("PLAYER")) { // a server command

			String Pname = GetNext();

			while ( StringT.hasMoreElements() ) {
				Pname = Pname +" "+ GetNext();
			}

			Player p = game.setCurrentPlayer( Pname );

			controller.sendMessage("Game started", false, false);

			// moved to startgame for lobby
			//if ( chatSocket != null ) {
			//	controller.startGame(false);
			//}
			//else {
			//	controller.startGame(true);
			//}

			output=resb.getString( "core.player.randomselected").replaceAll( "\\{0\\}", p.getName());

			if ( game.getGameMode()==RiskGame.MODE_SECRET_MISSION || autoplaceall==true ) {
				needInput=false;
			}
			else {

				saveGameToUndoObject();
			}

		}
		else if (Addr.equals("CARD")) { // a server command

			// if the player deserves a card
			if ( StringT.hasMoreTokens() ) {

				// get the cards
				Vector cards = game.getCards();
				String name = GetNext();
				Card card = game.findCard( name );

				((Player)game.getCurrentPlayer()).giveCard( card );

				// find the card and remove it
				cards.remove( card );
				cards.trimToSize();

				if ( showHumanPlayerThereInfo( game.getCurrentPlayer() ) ) {

					String cardName;

					if (name.equals(Card.WILDCARD)) {
						cardName = name;
					}
					else {
						cardName = card.getName() + " " + game.getCountryInt( Integer.parseInt(name) ).getName();
					}

					controller.sendMessage("You got a new card: \"" + cardName +"\"", false , false);
				}
			}

			Player newplayer = game.endGo();

			output = resb.getString( "core.player.newselected").replaceAll( "\\{0\\}", newplayer.getName());

			// this is not a bug! (Easter egg)
			if ( unlimitedLocalMode && game.getSetup() && newplayer.getName().equals("Theo")) { newplayer.addArmies( newplayer.getExtraArmies() ); }

			saveGameToUndoObject();


		}
		else if (Addr.equals("PLACE")) { // a server command

			Country c = game.getCountryInt( Integer.parseInt( GetNext() ) );
			game.placeArmy( c ,1);
			controller.sendMessage( resb.getString( "core.place.oneplacedin").replaceAll( "\\{0\\}", c.getName()) , false, false); // Display
			output=resb.getString( "core.place.autoplaceok");

		}
		else if (Addr.equals("PLACEALL")) { // a server command

			for (int c=0; c< game.getNoCountries() ; c++) {

				Country t = game.getCountryInt( Integer.parseInt( GetNext() ) );
				game.placeArmy( t ,1);
				controller.sendMessage( resb.getString("core.place.getcountry")
						.replaceAll( "\\{0\\}", ((Player)game.getCurrentPlayer()).getName())
						.replaceAll( "\\{1\\}", t.getName()) // Display
						, false, false);
				game.endGo();
			}

			// the game is saved
			saveGameToUndoObject();

			controller.sendMessage("Auto place all successful.", false, false);
			//New player selected: {0}.
			output=resb.getString( "core.player.newselected").replaceAll( "\\{0\\}", ((Player)game.getCurrentPlayer()).getName());

		}
		else if (Addr.equals("MISSION")) { // a server command

			Vector m = game.getMissions();
			Vector p = game.getPlayers();

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

				int i = RiskGame.getNumber( GetNext() );
				((Player)p.elementAt(c)).setMission( (Mission)m.elementAt(i) );
				m.removeElementAt(i);

			}

			output=null;
			needInput=false;

		}
		else { // parse this normal cammand

			String echo = message.substring( Addr.length()+1 );

			if (game != null && game.getCurrentPlayer() != null && game.getState()!=RiskGame.STATE_GAME_OVER ) {

				if ( ((Player)game.getCurrentPlayer()).getType()==Player.PLAYER_HUMAN ) { // if the player is human
					controller.sendMessage( ((Player)game.getCurrentPlayer()).getName()+ "("+resb.getString("newgame.player.type.human")+")>"+echo, false, false );
				}
				else if ( ((Player)game.getCurrentPlayer()).getType()==Player.PLAYER_AI_CRAP ) { // if the player is AI
					controller.sendMessage( ((Player)game.getCurrentPlayer()).getName()+ "("+resb.getString("newgame.player.type.crapai")+")>"+echo, false, false );
				}
				else if ( ((Player)game.getCurrentPlayer()).getType()==Player.PLAYER_AI_EASY ) { // if the player is AI
					controller.sendMessage( ((Player)game.getCurrentPlayer()).getName()+ "("+resb.getString("newgame.player.type.easyai")+")>"+echo, false, false );
				}
				else if ( ((Player)game.getCurrentPlayer()).getType()==Player.PLAYER_AI_HARD ) { // if the player is AI
					controller.sendMessage( ((Player)game.getCurrentPlayer()).getName()+ "("+resb.getString("newgame.player.type.hardai")+")>"+echo, false, false );
				}

			}
			else {
				controller.sendMessage( "game>" + echo, false, false );
			}

			//if (StringT.hasMoreTokens()) {

			input=GetNext();
			output="";

			// CLOSE GAME
			if (input.equals("closegame")) {

				if (StringT.hasMoreTokens()==false) {

					boolean doclose=false;

					if ( chatSocket == null ) { // LOCAL GAME/ OR LOBBY CLIENT

						doclose = true;

					}
					else { // NETWORK GAME

						if ( myAddress.equals(Addr) ) { // only on the pc of who called it

							doclose = true;

							try {
								outChat.close();
								inChat.close();

								chatSocket.shutdownInput();
								chatSocket.shutdownOutput();

								chatSocket.close();
							}
							catch (IOException except) { }

							chatSocket = null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -