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

📄 risk.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
								outChat.println( "PLACE " + game.getEmptyCountry() ); // recursive call
							}


						    }

						    needInput=false;
						    output = null;

						}
						else {
							output = resb.getString( "core.autoplace.error.unable");
						}

					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "autoplace"); }
				}
				else { output=resb.getString( "core.error.incorrect").replaceAll( "\\{0\\}", "showarmies, placearmies, autoplace"); }

			}
			else if (game.getState()==RiskGame.STATE_ATTACKING) {

				if (input.equals("attack")) {
					if (StringT.countTokens()==2) {

						String arg1=GetNext();
						String arg2=GetNext();
						int a1=RiskGame.getNumber(arg1);
						int a2=RiskGame.getNumber(arg2);

						Country country1;
						Country country2;

						if (a1 != -1) {
							country1=game.getCountryInt(a1);
						}
						else {
							//YURA:LANG country1=game.getCountryByName(arg1);
							country1=null;
						}

						if (a2 != -1) {
							country2=game.getCountryInt(a2);
						}
						else {
							//YURA:LANG country2=game.getCountryByName(arg2);
							country2=null;
						}

						int a[]=game.attack(country1, country2);

						if ( a[0]==1 ) {
							//Attack {0} ({1}) with {2} ({3}). (You can use up to {4} dice to attack)
							output = resb.getString( "core.attack.attacking")
										.replaceAll( "\\{0\\}", country2.getName()) // Display
										.replaceAll( "\\{1\\}", "" + country2.getArmies())
										.replaceAll( "\\{2\\}", country1.getName()) // Display
										.replaceAll( "\\{3\\}", "" + country1.getArmies())
										.replaceAll( "\\{4\\}", "" + a[1]);

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

							if ( showHumanPlayerThereInfo( attackingPlayer ) ) {

								controller.showDice(a[1], true);
							}

						}
						else { output=resb.getString( "core.attack.error.unable"); }
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "attack country country"); }
				}
				else if (input.equals("endattack")) {
					if (StringT.hasMoreTokens()==false) {
						if ( game.endAttack() ) {
							output=resb.getString( "core.attack.end.ended");
						}
						else { output=resb.getString( "core.attack.end.error.unable"); }
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "endattack"); }
				}
				else { output=resb.getString( "core.error.incorrect").replaceAll( "\\{0\\}", "attack, endattack"); }

			}
			else if (game.getState()==RiskGame.STATE_ROLLING) {

				if (input.equals("roll")) {

					if (StringT.countTokens()==1) {

						int dice=RiskGame.getNumber( GetNext() );

						if ( dice != -1 && game.rollA(dice) ) {

							if ( battle ) {

								controller.setNODAttacker(dice);

							}

							int n=((Country)game.getDefender()).getArmies();

							if (game.getSimone()) {

								if (n>3) { n=3; }
							}
							else {

								if (n>2) { n=2; }
							}

							//Rolled attacking dice, {0} defend yourself! (you can use up to {1} dice to defend)
							output = resb.getString( "core.roll.rolled")
										.replaceAll( "\\{0\\}", ((Player)game.getCurrentPlayer()).getName())
										.replaceAll( "\\{1\\}", "" + n);

							Player defendingPlayer = ((Country)game.getDefender()).getOwner();

							if ( showHumanPlayerThereInfo(defendingPlayer) ) {
								controller.showDice(n, false);
							}

						}
						else { output=resb.getString( "core.roll.error.unable"); }
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "roll number"); }
				}
				else if (input.equals("retreat")) {
					if (StringT.hasMoreTokens()==false) {

						if ( game.retreat() ) {
							output=resb.getString( "core.retreat.retreated");
						}
						else { output=resb.getString( "core.retreat.error.unable"); }
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "retreat"); }
				}
				else { output=resb.getString( "core.error.incorrect").replaceAll( "\\{0\\}", "roll, retreat"); }

			}
			else if (game.getState()==RiskGame.STATE_BATTLE_WON) {

				if (input.equals("move")) {
					if (StringT.countTokens()==1) {

						String num = GetNext();
						int noa;

						if (num.equals("all")) {
							noa=game.moveAll();
						}
						else {
							noa=RiskGame.getNumber( num );
						}

						int mov=game.moveArmies(noa);

						if ( mov != 0 ) {
							//Moved {0} armies to captured country.
							output = resb.getString( "core.move.moved").replaceAll( "\\{0\\}", "" + noa);

							if (mov == 2) {

								output=output + whoWon();

							}

						}
						else { output=resb.getString( "core.move.error.unable"); }
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "move number"); }
				}
				else { output=resb.getString( "core.error.incorrect").replaceAll( "\\{0\\}", "move"); }

			}
			else if (game.getState()==RiskGame.STATE_FORTIFYING) {

				if (input.equals("movearmies")) {
					if (StringT.countTokens()==3) {

						String arg1=GetNext();
						String arg2=GetNext();
						int a1=RiskGame.getNumber(arg1);
						int a2=RiskGame.getNumber(arg2);

						Country country1;
						Country country2;

						if (a1 != -1) {
							country1=game.getCountryInt(a1);
						}
						else {
							//YURA:LANG country1=game.getCountryByName(arg1);
							country1=null;
						}

						if (a2 != -1) {
							country2=game.getCountryInt(a2);
						}
						else {
							//YURA:LANG country2=game.getCountryByName(arg2);
							country2=null;
						}

						int noa=RiskGame.getNumber( GetNext() );

						if ( game.moveArmy(country1, country2, noa) ) {
							//Moved {0} armies from {1} to {2}.
							output = resb.getString( "core.tacmove.movedfromto")
									.replaceAll("\\{0\\}", "" + noa)
									.replaceAll("\\{1\\}", country1.getName()) // Display
									.replaceAll("\\{2\\}", country2.getName()); // Display
						}
						else { output=resb.getString( "core.tacmove.error.unable"); }
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "movearmies country country number"); }
				}
				else if (input.equals("nomove")) {
					if (StringT.hasMoreTokens()==false) {
						if ( game.noMove() ) {
							output=resb.getString( "core.tacmove.no.nomoves");
						}
						else { output=resb.getString( "core.tacmove.no.error.unable"); }
					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "nomove"); }
				}
				else { output=resb.getString( "core.error.incorrect").replaceAll( "\\{0\\}", "movearmies, nomove"); }

			}
			else if (game.getState()==RiskGame.STATE_END_TURN) {

				if (input.equals("endgo")) {
					if (StringT.hasMoreTokens()==false) {

						needInput=false;
						output=null;

						controller.sendMessage(resb.getString( "core.endgo.ended"), false , false);
						DoEndGo();

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

			}
			else if (game.getState()==RiskGame.STATE_GAME_OVER) {

				if (input.equals("continue")) {
					if (StringT.hasMoreTokens()==false) {

						if ( game.continuePlay() ) {
							output=resb.getString( "core.continue.successful");
						}
						else {
							output=resb.getString( "core.continue.error.unable");
						}

					}
					else { output=resb.getString( "core.error.syntax").replaceAll( "\\{0\\}", "continue"); }
				}
				else {
					//The game is over. {0} won! (current possible commands are: continue)
					output = resb.getString( "core.gameover.won").replaceAll( "\\{0\\}", ((Player)game.getCurrentPlayer()).getName());
				}

			}
			else if (game.getState()==RiskGame.STATE_SELECT_CAPITAL) {

				if (input.equals("capital")) {
					if (StringT.countTokens()==1) {

						String strCountry = GetNext();
						int nCountryId = RiskGame.getNumber(strCountry);
						Country t;

						if (nCountryId != -1) {
							t = game.getCountryInt( nCountryId);
						} else {
							//YURA:LANG t = game.getCountryByName( strCountry);
							t=null;
						}

						if ( t != null && game.setCapital(t) ) {

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

								output=resb.getString( "core.capital.selected").replaceAll( "\\{0\\}", t.getName()); // Display
							}
							else {
								output=resb.getString( "core.capital.hasbeenselected");
							}

						}
						else { output=resb.getString( "core.capital.error.unable"); }

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


			}
			else if (game.getState()==RiskGame.STATE_DEFEND_YOURSELF) {

				if (input.equals("roll")) {

					if (StringT.countTokens()==1) {

						int dice=RiskGame.getNumber( GetNext() );
						if ( dice != -1 && game.rollD(dice) ) {

							if ( battle ) {

								controller.setNODDefender(dice);

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

							}
							// client does a roll, and this is not called
							if ( !replay && (chatSocket == null || myAddress.equals(Addr)) ) { // recursive call

								int[] attackerResults = game.rollDice( game.getAttackerDice() );
								int[] defenderResults = game.rollDice( game.getDefenderDice() );

								String serverRoll = "DICE ";

								serverRoll = serverRoll + attackerResults.length + " ";
								serverRoll = serverRoll + defenderResults.length + " ";

								for (int c=0; c< attackerResults.length ; c++) {
									serverRoll = serverRoll + attackerResults[c] + " ";
								}
								for (int c=0; c< defenderResults.length ; c++) {
									serverRoll = serverRoll + defenderResults[c] + " ";
								}

								if (chatSocket == null ) {
									GameParser( serverRoll );
								}
								else if ( myAddress.equals(Addr) ) {
									outChat.println( serverRoll );
								}



							}

							output=null;
							needInput=false;

						}
						else { output=resb.getString( "core.roll.error.unable"); }

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

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

			//} // this was the end of "if there is somthing to pass" but not needed any more



			if (game != null ) {

				//System.out.print("oldstate = " +oldState+" newstate = " +game.getState()+"\n");

				if (game.getState()==RiskGame.STATE_ROLLING && battle==false) {

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

					if ( showHumanPlayerThereInfo(attackingPlayer) || showHumanPlayerThereInfo(defendingPlayer) ) {

						controller.openBattle( ((Country)game.getAttacker()).getColor() , ((Country)game.getDefender()).getColor() );
						battle=true;
					}

				}

				// if someone retreats
				else if ( game.getState()==RiskGame.STATE_ATTACKING ) {

					closeBattle();

				}

			}


		}// end of parse of normal command

		// give a output if there is one
		if (output!=null) {

			// give a output
			if (game==null) {
				controller.sendMessage(output, false, true );
				setHelp();
			}
			else if ( game.getState()==RiskGame.STATE_NEW_GAME ) {
				controller.sendMessage(output, false, true );
				setHelp();
			}
			else if ( game.getState()==RiskGame.STATE_GAME_OVER ) {
				controller.sendMessage(output, true, true );
				setHelp();
			}
			else if (game.getState()==RiskGame.STATE_END_TURN) {

				// if ( game.getCurrentPlayer().getAutoEndGo() && chatSocket == null ) {
				// @todo:?????
				if ( game.getCurrentPlayer().getAutoEndGo() ) {
					controller.sendMessage(output, false, false );
				}
				else {
					controller.sendMessage(output, true, true );
					setHelp();
				}
			}
			else {// if player type is human or neutral or ai
				controller.sendMessage(output, true, true );
				if ( inbox.isEmpty() ) setHelp();
			}

		}

		// check to see if the players go should be ended
		if ( game != null && game.getState()==RiskGame.STATE_END_TURN && game.getCurrentPlayer().getAutoEndGo() ) {

			needInput=false;
			DoEndGo();

		}

		// ask for input if u need one
		if (needInput && inbox.isEmpty() ) {
			getInput();
		}

	}

	private void setupPreview() {

		if (controller.countListeners()>0) try {

			controller.showMapPic( PicturePanel.getImage( game ) );

		}
		catch(Throwable e) { controller.showMapPic(null); }

	}

	public static int getType(String type) {


		if (type.equals("human")) {

			return Player.PLAYER_HUMAN;
		}

		if (type.equals("ai easy")) {

			return Player.PLAYER_AI_EASY;
		}

		if (type.equals("ai hard")) {

			return Player.PLAYER_AI_HARD;
		}

		if (type.equals("ai crap")) {

			return Player.PLAYER_AI_CRAP;
		}

		return -1;
	}

	/**
	 * return true ONLY if info of this Player p should be disclosed to this computer
	 */
	private boolean showHumanPlayerThereInfo(Player p) {

		return (p != null) && ( p.getType()==Player.PLAYER_HUMAN ) && ( unlimitedLocalMode || myAddress.equals( p.getAddress() ) );

⌨️ 快捷键说明

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