📄 aihard.java
字号:
Vector players = game.getPlayers();
for (int i=0; i<players.size(); i++) {
for (int j=0; j<continents.length; j++) {
if ( almostOwned((Player) players.elementAt(i), continents[j] ) == true
&& continents[j].isOwned( (Player)players.elementAt(i) ) == false
&& (Player) players.elementAt(i) != p ) {
Vector v = continents[j].getTerritoriesContained();
for (int k=0; k<v.size(); k++) {
if ( ((Country) v.elementAt(k)).getOwner() == null) {
return ((Country) v.elementAt(k)).getColor()+"";
}
}
}
}
}
return null;
}
/**
* Attempts to block an opposing player gaining a continent during the actual game
* @param p player object
* @return String name if a move to block the opponent is required/possible, else returns null
*/
public String keepBlocking(Player p) {
Continent[] continents = game.getContinents();
Vector players = game.getPlayers();
for (int i=0; i<players.size(); i++) {
for (int j=0; j<continents.length; j++) {
if ( almostOwned((Player) players.elementAt(i), continents[j]) == true
&& continents[j].isOwned((Player) players.elementAt(i)) == false
&& (Player) players.elementAt(i) != p ) {
Vector v = continents[j].getTerritoriesContained();
for (int k=0; k<v.size(); k++) {
if ( ((Country) v.elementAt(k)).getOwner() == p && ((Country) v.elementAt(k)).getArmies() < 5) {
return ((Country) v.elementAt(k)).getColor()+"";
}
}
}
}
}
return null;
}
/**
* Attempts to free a continent from an enemy player if it is possible to do so
* @param p player object
* @return Sring name is a move to free a continent is required/possible, else returns null
*/
public String freeContinent(Player p) {
Vector continentsToBreak = GetContinentsToBreak(p);
Vector t = p.getTerritoriesOwned();
for (int q=1; q<4; q++) {
for (int k=0; k<continentsToBreak.size(); k++) {
for (int i=0; i<t.size(); i++) {
Vector tNeighbors = ((Country)t.get(i)).getNeighbours();
for (int j=0; j<tNeighbors.size(); j++) {
if ( //((Country)t.get(i)).getArmies() + p.getExtraArmies() - 1 > ((Country)tNeighbors.get(j)).getArmies() &&
ShortPathToContinent((Continent)continentsToBreak.get(k), (Country)t.get(i), (Country)tNeighbors.get(j), q))
return ((Country)t.get(i)).getColor()+"";
}
}
}
}
// Continent[] continents = game.getContinents();
// Vector players = game.getPlayers();
//
// for (int i=0; i<players.size(); i++) {
// for (int j=0; j<continents.length; j++) {
// if ( continents[j].isOwned((Player) players.elementAt(i)) == true
// && (Player) players.elementAt(i) != p ) {
//
// Vector v = continents[j].getTerritoriesContained();
// for (int k=0; k<v.size(); k++) {
// Vector neighbours = ((Country) v.elementAt(k)).getNeighbours();
// for (int l=0; l<neighbours.size(); l++) { // .equals(p.getName())
// if ( (((Country) neighbours.elementAt(l)).getOwner()) == p && ((Country) neighbours.elementAt(l)).getArmies() < 5) {
// return ((Country) neighbours.elementAt(l)).getColor()+"";
// }
// }
// }
// }
// }
// }
return null;
}
/**
* Checks if the player owns almost all of the territories within a continent
* @param p player object
* @return booelan True if the player owns almost all of the territories within a continent,
* otherwise false if the player does not own most of the territories
*/
public boolean almostOwned(Player p, Continent co) {
int ownedByPlayer=0;
Vector territoriesContained = co.getTerritoriesContained();
for (int c=0; c< territoriesContained.size() ; c++) {
if ( ((Country)territoriesContained.elementAt(c)).getOwner() == p ) {
ownedByPlayer++;
}
}
if ( ownedByPlayer>=(territoriesContained.size()-2) ) {
return true;
}
else {
return false;
}
}
/**
* Checks whether a country owns its neighbours
* @param p player object, c Country object
* @return boolean True if the country owns its neighbours, else returns false
*/
public boolean ownsNeighbours(Country c) {
Vector neighbours = c.getNeighbours();
int count = 0;
for (int i=0; i<neighbours.size(); i++) {
if ( ((Country) neighbours.elementAt(i)).getOwner() == player)
count++;
}
if (count == neighbours.size())
return true;
return false;
}
/**
* Checks if a player is still playing the game
* @param p player object
* @return booelan True if player is present, else return false
*/
public boolean playerKilled(Player p) {
Vector play = game.getPlayers();
for (int i=0; i<play.size(); i++) {
if ( (Player) play.elementAt(i) == p)
return true;
}
return false;
}
/**
* Checks if a continent has a free territory
* @param ct Vector of countries in the continent
* @return boolean true if there is a free country, otherwise returns false
*/
public boolean hasFreeTerritories(Vector ct) {
for (int i=0; i<ct.size(); i++)
if (((Country) ct.elementAt(i)).getOwner() == null)
return true;
return false;
}
/**
* Checks for continents that are owned by a single player which is not the active player
* @param player Player
* @return Vector containing all of the continents with one owner, which is not the active player. null
* if none exist.
*/
public Vector GetContinentsToBreak(Player player) {
Continent[] continents = game.getContinents();
//sort the continents based on worth
for (int i=0; i<continents.length-1; i++) {
for (int j=0; j<continents.length-1; j++) {
if (continents[j].getArmyValue() < continents[j+1].getArmyValue()) {
Continent tmp = continents[j];
continents[j] = continents[j+1];
continents[j+1] = tmp;
}
}
}
Vector players = game.getPlayers();
Vector continentsToBreak = new Vector();
for (int i=0; i<continents.length; i++) {
for (int j=0; j<players.size(); j++) {
if (!((Player)players.get(j)).equals(player) && continents[i].isOwned((Player)players.get(j))) {
continentsToBreak.add(continents[i]);
// System.out.println("Continent to break: " + continents[i]);
}
}
}
return continentsToBreak;
}
/**
* Orders the players other than the active player in order from greatest to least
* @param player Player
* @return Player[] with the players ordered from least to greatest.
*/
public Player[] OrderPlayers(Player player) {
Vector players = game.getPlayers();
Player[] orderedPlayers = new Player[players.size()-1];
int num = 0;
for (int i=0; i<players.size(); i++) {
if ( !((Player)players.get(i)).equals(player) )
orderedPlayers[num++] = (Player)players.get(i);
}
//Simple Bubble Sort to sort the players in order.
for (int i=0; i<orderedPlayers.length-1; i++) {
for (int j=0; j<orderedPlayers.length-1; j++) {
if (orderedPlayers[j].getNoTerritoriesOwned() + orderedPlayers[j].getNoArmies() <
orderedPlayers[j+1].getNoTerritoriesOwned() + orderedPlayers[j+1].getNoArmies()) {
Player tmp = orderedPlayers[j];
orderedPlayers[j] = orderedPlayers[j+1];
orderedPlayers[j+1] = tmp;
}
}
}
return orderedPlayers;
}
/**
* Determines if a short path exists to the continent through that country
* @param Continent cont, Country attackFrom, Country attackThrough, int acceptableDistance
* @return boolean of true if there is a path existing, and false otherwise
*/
public boolean ShortPathToContinent(Continent cont, Country attackFrom, Country attackThrough, int acceptableDistance) {
//Countries are not valid for attacking
if (!attackFrom.isNeighbours(attackThrough) || attackThrough.getOwner().equals(attackFrom.getOwner()))
return false;
if (acceptableDistance <= 0 && !attackFrom.getContinent().equals(cont))
return false;
else if (attackFrom.getContinent().equals(cont))
return true;
else if (acceptableDistance > 0 && attackThrough.getContinent().equals(cont))
return true;
else {
Vector throughNeighbors = attackThrough.getNeighbours();
for (int i=0; i<throughNeighbors.size(); i++) {
if (ShortPathToContinent(cont, attackThrough, (Country)throughNeighbors.get(i), acceptableDistance-1 ) )
return true;
}
}
return false;
}
public String NextToEnemyToEliminate() {
Vector weakPlayers = new Vector();
for (int i=0; i<game.getPlayers().size(); i++) {
if (((Player)game.getPlayers().get(i)).getNoTerritoriesOwned() < 4)
weakPlayers.add(game.getPlayers().get(i));
}
if (weakPlayers.size() == 0)
return null;
Vector t = player.getTerritoriesOwned();
Vector targetCountries = new Vector();
for (int i=0; i<weakPlayers.size(); i++) {
for (int j=0; j<((Player)weakPlayers.get(i)).getNoTerritoriesOwned(); j++) {
targetCountries.add(((Player)weakPlayers.get(i)).getTerritoriesOwned().get(j));
}
}
for (int i=0; i<t.size(); i++) {
for (int j=0; j<targetCountries.size(); j++) {
if (((Country)t.get(i)).isNeighbours((Country)targetCountries.get(j)))
return ((Country)t.get(i)).getColor() + "";
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -