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

📄 aihard.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                             int ratio = ((Country)t.elementAt(a)).getArmies();
			     for (int b=0; b< n.size() ; b++) {
			         if ( ((Country)n.elementAt(b)).getOwner() != player && ((Country)n.elementAt(b)).getArmies() + (ratio / 2) < ratio  ) {
			        	 Vector attack = new Vector();
			        	 attack.add(((Country)t.elementAt(a)).getColor()+"");
			        	 attack.add((Country)n.elementAt(b));
			        	 //output= "attack " + ((Country)t.elementAt(a)).getColor() + " " + ((Country)n.elementAt(b)).getColor();
			        	 options.add(attack);
			         }
			     }
		         }
		    }
		    Player[] playersGreatestToLeast = OrderPlayers(player);
	    	outer: for (int j=0; j<playersGreatestToLeast.length; j++) {
	    		for (int i=0; i<options.size(); i++) {
		    		if ( ((Country) ((Vector)options.get(i)).get(1)).getOwner().equals(playersGreatestToLeast[j])  ) {
		    			output = "attack " + ((String) ((Vector)options.get(i)).get(0)) + " " + ((Country) ((Vector)options.get(i)).get(1)).getColor();
		    			break outer;
		    		}
		    	}
		    }

		   /* attempt to attack continent which is almost all owned, if scenario is there */

                   int count = 0;
                   Country attackfrom = null;
                   boolean complex = false;

                   for (int i=0; i<cont.length; i++) {

                       if ( mostlyOwned( cont[i] ) == true) {

			   n = cont[i].getTerritoriesContained();

			   for (int j=0; j<n.size(); j++) {

                               if ( ((Country)n.elementAt(j)).getArmies() > count && ((Country)n.elementAt(j)).getOwner() == player ) {
                                     count = ((Country)n.elementAt(j)).getArmies();
                                     attackfrom = (Country)n.elementAt(j);
                               }

			   }

			   for (int b=0; b< n.size() ; b++) {
			        if ( ((Country)n.elementAt(b)).getOwner() != player && (((Country)n.elementAt(b)).getArmies()+1) < count && attackfrom.isNeighbours(((Country)n.elementAt(b))) == true ) {
                                      output= "attack " + attackfrom.getColor() + " " + ((Country)n.elementAt(b)).getColor();
                                      complex = true;
                                      break;
                                }
			   }
                       }
                   }

                   // else attempt to attack continent with the greatest territories owned, that has yet to be conquered.

                   if (complex == false) {
                      int value = 0;
                      Continent choice = null;

                      for (int i=0; i<cont.length; i++) {

                         if ( cont[i].isOwned(player) == false) {

                             n = cont[i].getTerritoriesContained();

                             int check = 0;
                             for (int j=0; j<n.size(); j++) {
                                if ( ((Country)n.elementAt(j)).getOwner() == player )
                                   check++;
                             }

                             if (check > value)
                                choice = cont[i];
                         }
                      }

                      if (choice !=null) {

                         //System.out.println(choice.getName()); TESTING
                         n = ((Continent)choice).getTerritoriesContained();

                         for (int j=0; j<n.size(); j++) {

                             if ( ((Country)n.elementAt(j)).getArmies() > count && ((Country)n.elementAt(j)).getOwner() == player ) {
                                   count = ((Country)n.elementAt(j)).getArmies();
                                   attackfrom = (Country)n.elementAt(j);
                             }
                         }

                         for (int b=0; b< n.size() ; b++) {
			     if ( ((Country)n.elementAt(b)).getOwner() != player && (((Country)n.elementAt(b)).getArmies()+1) < count && attackfrom.isNeighbours(((Country)n.elementAt(b))) == true ) {
                                   output= "attack " + attackfrom.getColor() + " " + ((Country)n.elementAt(b)).getColor();
                                   break;
                             }
                         }
                      }
                   }

                   // check to see if there are any continents that can be broken
                   
                   Vector continentsToBreak = GetContinentsToBreak(player);
                   String tmp = null;
//                   for (int i=0; i<continentsToBreak.size() && tmp == null; i++) {
//                	   Vector countriesInContinent = ((Continent)continentsToBreak.get(i)).getTerritoriesContained();
//                	   for (int j=0; j<t.size() && tmp == null; j++) {
//                		   for (int k=0; k<countriesInContinent.size() && tmp == null; k++)
//                			   if ( ((Country)t.get(j)).isNeighbours( ((Country)countriesInContinent.get(k)) ) &&
//                					   ((Country)t.get(j)).getArmies() - 1 > ((Country)countriesInContinent.get(k)).getArmies()   )
//                				   tmp = "attack " + ((Country)t.get(j)).getColor() + " " + ((Country)countriesInContinent.get(k)).getColor();
//                	   }
//                   }
                   
                   //Attempt to find a path to the continent - distance of 1, then 2, then 3 away.
                   if (continentsToBreak != null) {
                	   outer: for (int q=1; q<4; q++) { 
                		   for (int i=0; i<continentsToBreak.size(); i++) {
	                		   for (int j=0; j<t.size(); j++) {
	                			   Vector tNeighbors = ((Country)t.get(j)).getNeighbours();
	                			   for (int k=0; k<tNeighbors.size(); k++) {
	                				   //Fight to the death on the last step of breaking a continent bonus
	                				   if (  (((Country)t.get(j)).getArmies()-1 > ((Country)tNeighbors.get(k)).getArmies() || (q==1 && ((Country)t.get(j)).getArmies() > 1)) &&
	                						   ShortPathToContinent((Continent)continentsToBreak.get(i), (Country)t.get(j), (Country)tNeighbors.get(k), q)  ) {
	                					   tmp = "attack " + ((Country)t.get(j)).getColor() + " " + ((Country)tNeighbors.get(k)).getColor();
	                					   break outer;
	                				   }
	                						   
	                			   }
	                		   }
	                	   }
                	   }
                   }
                   
                   if (tmp != null)
                	   output = tmp;
                   
                   // check to see if there are any players to eliminate

                   Vector players = game.getPlayers();
                   Vector cankill = new Vector();

                   for (int i=0; i<players.size(); i++) {
                      if (( (Player) players.elementAt(i)).getNoTerritoriesOwned() < 4 && ( (Player) players.elementAt(i)) != player)
                         cankill.addElement((Player) players.elementAt(i));
                   }

                   if (cankill.size() > 0) {
                      for (int i=0; i<cankill.size(); i++) {
                         Vector territories = ((Player) cankill.elementAt(i)).getTerritoriesOwned();
                         for (int j=0; j<territories.size(); j++) {

                        	Vector neighbours = ((Country)territories.elementAt(j)).getNeighbours();
                            for (int k=0; k<neighbours.size(); k++) {
                            	//Fight when outnumbered to eliminate a player.
                            	if (((Country) neighbours.elementAt(k)).getOwner() == player && 
                            			((Country)territories.elementAt(j)).getArmies() - 2 < ((Country) neighbours.elementAt(k)).getArmies() &&  
                            			((Country) neighbours.elementAt(k)).getArmies() > 1 && 
                            			((Country) neighbours.elementAt(k)).isNeighbours((Country)territories.elementAt(j))) {
                                      output= "attack " + ((Country) neighbours.elementAt(k)).getColor() + " " + ((Country)territories.elementAt(j)).getColor();
                                      //System.out.println("eliminate: " + output); TESTING
                                      break;
                                  }
                            }
                         }
                      }
                   }

	      	   if ( output == null ) {
		  	output="endattack";
		   }

		return output;

    }

    public String getRoll() {

		String output;

		int n=((Country)game.getAttacker()).getArmies() - 1;


		/* Only roll for as long as while attacking player has more armies than defending player */
		int m=((Country)game.getDefender()).getArmies();

		// If we are trying to eliminate a player, fight longer.
		if (game.getDefender().getOwner().getNoTerritoriesOwned() < 4)
			m -= 3;
		
		//If we are trying to break a continent bonus, fight to the death.
		if (game.getDefender().getContinent().isOwned(game.getDefender().getOwner()))
			m = 0;
		
		if (n > 3 && n > m) {
		    output= "roll "+3;
		}
		else if (n > 0 && n <= 3 && n > m) {
		    output= "roll "+n;
		}
		else {
		    output= "retreat";
		}

		return output;

    }








    /**
     * Checks if the player owns most of the territories within a continent
     * @param p player object
     * @return boolean True if the player owns most of the territories within a continent,
     * otherwise false if the player does not own most of the territories
     */
    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;
	}

    }

    /**
     * Checks if the player can make a valid tactical move
     * @param p player object, b Country object
     * @return Country if the tactical move is valid, else returns null.
     */
    public Country check1(Country b) {

      Vector neighbours = b.getNeighbours();
      Country c = null;

      for (int i=0; i<neighbours.size(); i++) {
         if ( ownsNeighbours( (Country)neighbours.elementAt(i)) == false && ((Country)neighbours.elementAt(i)).getOwner() == player)
              return (Country)neighbours.elementAt(i);
      }
     return c;
    }

    /**
     * Checks if the player can make a valid tactical move
     * @param p player object, b Country object
     * @return booelean True if the tactical move is valid, else returns false.
     */
    public boolean check2(Country b) {

      Vector neighbours = b.getNeighbours();
      Country c = null;

      for (int i=0; i<neighbours.size(); i++) {
         if ( ownsNeighbours( (Country)neighbours.elementAt(i)) == false && ((Country)neighbours.elementAt(i)).getOwner() == player)
              return true;
      }
     return false;
    }


    /**
     * Attempts to block an opposing player gaining a continent during the initial placement
     * @param p player object
     * @return String name if a move to block the opponent is required/possible, else returns null
     */
    public String blockOpponent(Player p) {
       Continent[] continents = game.getContinents();

⌨️ 快捷键说明

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