📄 aihard.java
字号:
// Group D
package risk.engine.ai;
import risk.engine.*;
import risk.engine.core.*;
import java.util.*;
/**
* <p> Class for AIHardPlayer </p>
* @author SE Group D
*/
public class AIHard extends AIEasy {
public String getPlaceArmies() {
String output;
if ( game.NoEmptyCountries()==false ) {
Continent[] cont = game.getContinents();
/* ai looks at all the continents and tries to see which one it should place on
The formula for placement is :
extraArmiesForContinent - numberOfBorders - (neighborTerritories - numberOfBorders)+(territorynum * 0.9)-(IIF(numberofEnemyUnits=0,-3,numberofEnemyUnits) * 1.2)
*/
int extraArmiesForContinent = 0;
int numberOfBorders = 0;
int neighborTerritories = 0;
int numberofEnemyUnits = -3;
int territorynum = 0;
boolean isBorder = false;
double check = -20;
double ratio = -20;
String name=null;
int val = -1;
for (int i=0; i<cont.length; i++) {
Vector ct = new Vector();
Continent co = cont[i];
extraArmiesForContinent = co.getArmyValue();
ct = co.getTerritoriesContained();
for (int j=0; j<ct.size(); j++) {
if ( ((Country)ct.elementAt(j)).getOwner() == player ) {
/* This territory belongs to the player */
territorynum++;
}
else {
if (((Country)ct.elementAt(j)).getOwner() != null) {
/* This territory belongs to an enemy */
if (numberofEnemyUnits == -3) {
numberofEnemyUnits = 1;
}
else {
numberofEnemyUnits++;
}
}
}
Vector w = ((Country)ct.elementAt(j)).getNeighbours();
for (int k=0; k<w.size(); k++) {
if (((Country)w.elementAt(k)).getContinent() != co) {
/* This is a territory to protect from */
neighborTerritories++;
isBorder = true;
}
}
if (isBorder) {
numberOfBorders++;
}
}
/* Calculate the value of that continent */
ratio = extraArmiesForContinent - numberOfBorders - (neighborTerritories - numberOfBorders)+(territorynum * 0.9) - (numberofEnemyUnits * 1.2);
if (check <= ratio && hasFreeTerritories(ct) == true) {
check = ratio;
val = i;
}
territorynum = 0;
numberofEnemyUnits = -3;
neighborTerritories = 0;
numberOfBorders = 0;
}
if (val==-1) { val=0; } // YURA: val is not being set
/* ..pick country from that continent */
boolean picked = false;
if (check > 0) {
Continent co = cont[val];
Vector ct = co.getTerritoriesContained();
for (int j=0; j<ct.size(); j++) {
if ( ((Country)ct.elementAt(j)).getOwner() == null ) {
name=((Country)ct.elementAt(j)).getColor()+"";
picked = true;
break;
}
}
}
if (picked == false) {
Continent co = cont[val];
Vector ct = co.getTerritoriesContained();
for (int j=0; j<ct.size(); j++) {
if ( ((Country)ct.elementAt(j)).getOwner() == null ) {
name=((Country)ct.elementAt(j)).getColor()+"";
Vector v = ((Country)ct.elementAt(j)).getNeighbours();
for (int k=0; k<v.size(); k++) { // YURA: was ct.size()
if (((Country)v.elementAt(k)).getOwner() != player && ((Country)ct.elementAt(j)).isNeighbours((Country)v.elementAt(k))) {
name=((Country)ct.elementAt(j)).getColor()+"";
break;
}
}
}
}
}
String s = blockOpponent(player);
if( s != null )
name = s;
if (name == null)
output = "autoplace";
else
output = "placearmies " + name +" 1";
}
else {
Vector t = player.getTerritoriesOwned();
Vector n;
String name = null;
for (int a=0; a< t.size() ; a++) {
if ( ownsNeighbours( (Country)t.elementAt(a)) == false && ((Country)t.elementAt(a)).getArmies() <= 11 ) {
name=((Country)t.elementAt(a)).getColor()+"";
break;
}
if ( name != null ) { break; }
}
String s = keepBlocking(player);
if( s != null )
name = s;
String f = freeContinent(player);
if( f != null )
name = f;
String k = NextToEnemyToEliminate();
if (k != null)
name = k;
if (name == null)
name = findAttackableTerritory(player);
if ( name == null )
output = "placearmies " + ((Country)t.elementAt(0)).getColor() +" "+player.getExtraArmies();
else if (game.getSetup() )
output = "placearmies " + name +" "+player.getExtraArmies();
else
output = "placearmies " + name +" 1";
}
return output;
}
public String getBattleWon() {
String output;
/* Attempt to safeguard critical areas when moving armies to won country */
Country attacker = game.getAttacker();
Continent continent = attacker.getContinent();
if (continent.isOwned(player) == true || mostlyOwned(continent) == true) {
/* Set 50% security limit */
if (attacker.getArmies() > 6)
output = "move " + (attacker.getArmies()-1);
else if (attacker.getArmies() > 3 && attacker.getArmies() <= 6)
output = "move " + (attacker.getArmies()-1);
else
output = "move all";
}
else
output="move all";
return output;
}
public String getTacMove() {
String output=null;
/* reinforces armies from less critical to more critical areas */
Vector t = player.getTerritoriesOwned();
Vector n;
boolean possible = false;
int difference = 0;
Country sender = null;
Country receiver = null;
Country temp = null;
int highest = 1;
for (int a=0; a<t.size(); a++) {
Country country = ((Country)t.elementAt(a));
difference = country.getArmies();
if (difference > highest) {
highest = difference;
sender = country;
}
}
if (sender != null) {
receiver = check1(sender);
if (receiver == null) {
n = sender.getNeighbours();
for (int i=0; i<n.size(); i++) {
temp = (Country) n.elementAt(i);
if (temp.getOwner() == player) {
possible = check2(temp);
}
if (possible == true) { receiver = temp; break; }
}
}
}
if (receiver != null && receiver != sender) {
output= "movearmies " + ((Country)sender).getColor() + " " + ((Country)receiver).getColor() + " " + (((Country)sender).getArmies()-1);
//System.out.println(((Country)sender).getName()); TESTING
//System.out.println(((Country)receiver).getName()); TESTING
}
if ( output == null ) {
output = "nomove";
}
return output;
}
public String getAttack() {
String output=null;
Vector t = player.getTerritoriesOwned();
Vector n;
boolean chosen = false;
Continent[] cont = game.getContinents();
Vector options = new Vector();
for (int a=0; a< t.size() ; a++) {
if ( ((Country)t.elementAt(a)).getArmies() > 1 ) {
n = ((Country)t.elementAt(a)).getNeighbours();
/* attack ratio set to 50% of attack force */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -