📄 aihardcapital.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 AIHardCapital extends AIHard {
public String getPlaceArmies() {
String output;
if ( game.NoEmptyCountries()==false ) {
Continent[] cont = game.getContinents();
/* ai attempts to gain control of a continent during initial placement */
int territorynum = 0;
int check = -1;
String name=null;
int val = -1;
for (int i=0; i<cont.length; i++) {
Vector ct = new Vector();
Continent co = cont[i];
ct = co.getTerritoriesContained();
for (int j=0; j<ct.size(); j++) {
if ( ((Country)ct.elementAt(j)).getOwner() == player ) { territorynum++; }
}
if (check <= territorynum) {
check = territorynum;
val = i;
}
territorynum = 0;
}
/* ..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<ct.size(); k++) {
if (((Country)v.elementAt(k)).getOwner() != player) {
name=((Country)ct.elementAt(j)).getColor()+"";
break;
}
}
}
}
}
String str = expandBase(player.getTerritoriesOwned());
if( str != null )
name = str;
String s = blockOpponent(player);
if( s != null )
name = s;
if (name == null)
output = "autoplace";
else
output = "placearmies " + name +" 1";
}
else {
Vector pt = player.getTerritoriesOwned();
String name=null;
Country capital = player.getCapital();
if (capital == null)
capital = findCapital();
for (int a=0; a< pt.size() ; a++) {
if ( ownsNeighbours( (Country)pt.elementAt(a)) == false && ((Country)pt.elementAt(a)).getArmies() <= 11 ) {
name=((Country)pt.elementAt(a)).getColor()+"";
break;
}
if ( name != null ) { break; }
}
String st = attackCapital();
if( st != null )
name = st;
String s = keepBlocking(player);
if( s != null )
name = s;
String f = freeContinent(player);
if( f != null )
name = f;
String str = reinforceBase(capital);
if (str != null)
name = str;
if (name == null)
name = findAttackableTerritory(player);
if ( name == null )
output = "placearmies " + ((Country)pt.elementAt(0)).getColor() +" "+player.getExtraArmies();
//Removed so that it will add armies one at a time, thus reinforcing the base until it has enough defense
//at which time it will place in other territories as well. Re-added so that it would not take forever
//for the computer to place armies. Re-removed after seeing that the time is neglibile.
else if (game.getSetup() )
output = "placearmies " + name +" "+player.getExtraArmies();
else
output = "placearmies " + name +" 1";
}
return output;
}
public String getAttack() {
String output=null;
boolean attackFromCap = player.getCapital().getArmies() > addEnemies(player.getCapital())*2+5;
boolean chosen = false;
Continent[] cont = game.getContinents();
Vector t = player.getTerritoriesOwned();
Vector n;
Vector options = new Vector();
for (int a=0; a< t.size(); a++) {
if ( ((Country)t.elementAt(a)).getArmies() > 1 ) {
if ( (Country)t.elementAt(a) != player.getCapital() ||
attackFromCap ) {
n = ((Country)t.elementAt(a)).getNeighbours();
/* attack ratio set to 50% of attack force */
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) {
//output= "attack " + ((Country)t.elementAt(a)).getColor() + " " + ((Country)n.elementAt(b)).getColor();
Vector attack = new Vector();
attack.add(((Country)t.elementAt(a)).getColor()+"");
attack.add((Country)n.elementAt(b));
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
&& ((Country)n.elementAt(j)) != player.getCapital()) {
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 && (((Country)n.elementAt(j)) != player.getCapital() || attackFromCap))
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
&& (((Country)n.elementAt(j)) != player.getCapital() || attackFromCap)) {
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;
}
}
}
}
Vector continentsToBreak = GetContinentsToBreak(player);
String tmp = null;
//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++) {
if (((Country)t.get(j)).getArmies()-1 > ((Country)tNeighbors.get(k)).getArmies() &&
ShortPathToContinent((Continent)continentsToBreak.get(i), (Country)t.get(j), (Country)tNeighbors.get(k), q) &&
(((Country)t.get(j)).getColor() != player.getCapital().getColor() || attackFromCap)) {
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() < 3 && ( (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++) {
if (((Country) neighbours.elementAt(k)).getOwner() == player
&& (((Country)territories.elementAt(j)).getArmies() + 1 < ((Country) neighbours.elementAt(k)).getArmies())
&& ((Country) neighbours.elementAt(k) != player.getCapital() || attackFromCap)
&& ((Country) neighbours.elementAt(k)).isNeighbours((Country)territories.elementAt(j))) {
output= "attack " + ((Country) neighbours.elementAt(k)).getColor() + " " + ((Country)territories.elementAt(j)).getColor();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -