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

📄 angry.java

📁 good project for programmer,,
💻 JAVA
字号:
package com.sillysoft.lux.agent;import com.sillysoft.lux.*;import com.sillysoft.lux.util.*;////  Angry.java//  Lux////  Copyright (c) 2002-2007 Sillysoft Games. //	http://sillysoft.net//	lux@sillysoft.net////	This source code is licensed free for non-profit purposes. //	For other uses please contact lux@sillysoft.net//////	Angry is a rather simplistic agent.//	He tries to attack lots.//import java.util.Random;import java.util.List;import java.util.*;public class Angry implements LuxAgent{// This agent's ownerCode:protected int ID;// We store some refs the board and to the country arrayprotected Board board;protected Country[] countries;// It is useful to have a random number generator for a couple of thingsprotected Random rand;public Angry()	{	rand = new Random();	}// Save references to public void setPrefs( int newID, Board theboard )	{	ID = newID;		// this is how we distinguish what countries we own	board = theboard;	countries = board.getCountries();	}public String name()	{	return "Angry";	}public float version()	{	return 1.0f;	}public String description()	{	return "Angry is an AI that likes to attack.";	}// Because Angry doesn't ever consern himself with continents,// we will try to get some continents at the beginning,// by choosing countries in the smallest continents.public int pickCountry()	{	int goalCont = BoardHelper.getSmallestPositiveEmptyCont(countries, board);	if (goalCont == -1) // oops, there are no unowned conts		goalCont = BoardHelper.getSmallestPositiveOpenCont(countries, board);	// So now pick a country in the desired continent	return pickCountryInContinent(goalCont);	}// Picks a country in <cont>, starting with	countries that have neighbors that we own.// If there are none of those then pick the country with the fewest neighbors total.protected int pickCountryInContinent(int cont)	{	// Cycle through the continent looking for unowned countries that have neighbors we own	CountryIterator continent = new ContinentIterator(cont, countries);	while (continent.hasNext())		{		Country c = continent.next();		if (c.getOwner() == -1 && c.getNumberPlayerNeighbors(ID) > 0)			{			// We found one, so pick it			return c.getCode();			}		}	// we neighbor none of them, so pick the open country with the fewest neighbors	continent = new ContinentIterator(cont, countries);	int bestCode = -1;	int fewestNeib = 1000000;	while (continent.hasNext())		{		Country c = continent.next();		if (c.getOwner() == -1 && c.getNumberNeighbors() < fewestNeib)			{			bestCode = c.getCode();			fewestNeib = c.getNumberNeighbors();			}		}	if (bestCode == -1)		{		// We should never get here, so print an alert if we do		System.out.println("ERROR in Angry.pickCountryInContinent() -> there are no open countries");		}	return bestCode;	}// Treat initial armies the same as normal armiespublic void placeInitialArmies( int numberOfArmies )	{	placeArmies( numberOfArmies );	}// The game will automatically cash a random set of ours if we// return from this method and still have 5 or more cards.// For now just let that always happenpublic void cardsPhase( Card[] cards )	{	// xxagentxx Angry cards phase	}// Angry's thought process when placing his armies is simple. // He puts all of his armies where they can attack the most countries.// Thus we will cycle through all the countries that we own remembering // the one with the most enemy countries beside it.// Cycle through all our countries placing <num> armies on each country that// has at least <numberOfArmies> armiespublic void placeArmies( int numberOfArmies )	{	int mostEnemies = -1;	Country placeOn = null;	int subTotalEnemies = 0;	CountryIterator neighbors = null;	// Use a PlayerIterator to cycle through all the countries that we own.	CountryIterator own = new PlayerIterator( ID, countries );	while (own.hasNext()) 		{		Country us = own.next();		subTotalEnemies = us.getNumberEnemyNeighbors();		// If it's the best so far store it		if ( subTotalEnemies > mostEnemies )			{			mostEnemies = subTotalEnemies;			placeOn = us;			}		}	// So now placeOn is the country that we own with the most enemies.	// Tell the board to place all of our armies there	board.placeArmies( numberOfArmies, placeOn);	}// During the attack phase, Angry has a clear goal:// 		Take over as much land as possible. // Therefore he performs every available attack that he thinks he can win,// starting with the best matchupspublic void attackPhase()	{	// Keep cycling until we make no attacks	boolean madeAttack = true;	while ( madeAttack )		{		madeAttack = false;	// reset it. if we win an attack somewhere we set it to true.		// cycle through all of the countries that we have 2 or more armies on		CountryIterator armies = new ArmiesIterator( ID, 2, countries );		while (armies.hasNext()) 			{			Country us = armies.next();			// Find its weakest neighbor			Country weakestNeighbor = us.getWeakestEnemyNeighbor();			// So we have found the best matchup for Country <us>. (if there are any enemies)			// Even though this agent is a little angry, he is still consious of the odds.			// He will only attack if it is a good-chance of winning.			if (weakestNeighbor != null && us.getArmies() > weakestNeighbor.getArmies())				{				// Angry is a proud dude, and doesn't like to retreat.				// So he will always attack till death.				board.attack(us, weakestNeighbor, true);				// Set madeAttack to true, so that we loop through all our armies again				madeAttack = true;				}			}		}	} // End of attackPhase// When deciding how many armies to move in after a successful attack, // Angry will just put them all into the country with more enemiespublic int moveArmiesIn( int cca, int ccd)	{	int Aenemies = countries[cca].getNumberEnemyNeighbors();	int Denemies = countries[ccd].getNumberEnemyNeighbors();	// If the attacking country had more enemies, then we leave all possible 	// armies in the country they attacked from (thus we move in 0):	if ( Aenemies > Denemies )		return 0;	// Otherwise the defending country has more neighboring enemies, move in everyone:	return countries[cca].getArmies()-1;	}public void fortifyPhase()	{	// Cycle through all the countries and find countries that we could move from:	for (int i = 0; i < board.getNumberOfCountries(); i++)		{		if (countries[i].getOwner() == ID && countries[i].getMoveableArmies() > 0)			{			// This means we've found a country of ours that we can move from if we want to.			// We determine the best country by counting the enemy neighbors it has.			// The most enemy neighbors is where we move. Also, if there are 0 enemy 			// neighbors where the armies are on now, we move to a random neighbor (in 			// the hopes we'll find an enemy eventually).			// To cycle through the neighbors we could use a NeighborIterator, 			// but we can also directly use the country's AdjoingingList array.			// Let's use the array...			Country[] neighbors = countries[i].getAdjoiningList();			int countryCodeBestProspect = -1;			int bestEnemyNeighbors = 0;			int enemyNeighbors = 0;			for (int j = 0; j < neighbors.length; j++)				{				if (neighbors[j].getOwner() == ID)					{					enemyNeighbors = neighbors[j].getNumberEnemyNeighbors();					if ( enemyNeighbors > bestEnemyNeighbors )						{						// Then so far this is the best country to move to:						countryCodeBestProspect = neighbors[j].getCode();						bestEnemyNeighbors = enemyNeighbors;						}					}				}			// Now let's calculate the number of enemies of the country where the armies 			// already are, to see if they should stay here:			enemyNeighbors = countries[i].getNumberEnemyNeighbors();			// If there's a better country to move to, move:			if ( bestEnemyNeighbors > enemyNeighbors )				{				// Then the armies should move:				// So now the country that had the best ratio should be moved to:				board.fortifyArmies( countries[i].getMoveableArmies(), i, countryCodeBestProspect );				}			// If there are no good places to move to, move to a random place:			else if ( enemyNeighbors == 0 )				{				// We choose an int from [0, neighbors.length]:				int randCC = rand.nextInt(neighbors.length);				board.fortifyArmies( countries[i].getMoveableArmies(), i, neighbors[randCC].getCode() );				}			}		}	}	// End of fortifyPhase() method// Oh boy. If this method ever gets called it is because we have won the game.// Send back something witty to tell the user.public String youWon()	{ 	// For variety we store a bunch of answers and pick one at random to return.	String[] answers = new String[] {"Muh-Ha-Ha-Ha\nAngry now very happy!",	"Your skull is squishy and mellon-like",	"ME STILL ANGRY!!!" };	return answers[ rand.nextInt(answers.length) ];	}/** We get notified through this methos when certain things happen. Angry parses out all the different messages and does nothing with them. */public String message( String message, Object data )	{	if ("youLose".equals(message))		{		int conqueringPlayerID = ((Integer)data).intValue();		// now you could log that you have lost this game...//		board.playAudioAtURL("http://sillysoft.net/sounds/boo.wav");		}	else if ("attackNotice".equals(message))		{		List dataList = (List) data;		int attackingCountryCode = ((Integer)dataList.get(0)).intValue();		int defendingCountryCode = ((Integer)dataList.get(1)).intValue();		// now you could log that an attack has been performed against you...		}	else if ("chat".equals(message))		{		List dataList = (List) data;		String from = (String) dataList.get(0);		String chatText = (String) dataList.get(1);		// now you could log the chat data//		System.out.println("Player '"+from+"' said: "+chatText);		}	else if ("emote".equals(message))		{		List dataList = (List) data;		String from = (String) dataList.get(0);		String emoteText = (String) dataList.get(1);		// now you could log the emote data//		System.out.println("Player '"+from+"' emoted: "+emoteText);		}	return null;	}}	// End of Angry class

⌨️ 快捷键说明

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