📄 riskgame.java
字号:
attackerDice = 0;
result[0]=1;
}
return result;
}
/**
* Moves a number of armies from the attacking country to defending country
* @param noa Number of armies to be moved
* @return boolean Return trues if you can move the number of armies across, returns false if you cannot
*/
public int moveArmies(int noa) {
int result=0;
if (gameState==STATE_BATTLE_WON && mustmove>0 && noa>= mustmove && noa<attacker.getArmies() ) {
attacker.removeArmies(noa);
defender.addArmies(noa);
attacker=null;
defender=null;
mustmove=0;
if (tradeCap==true) {
gameState=STATE_TRADE_CARDS;
}
else {
gameState=STATE_ATTACKING;
}
result=1;
if ( checkPlayerWon() ) {
result=2;
}
return result;
}
return result;
}
/**
* Moves all of armies from the attacking country to defending country
* @return int Return trues if you can move the number of armies across, returns false if you cannot
*/
public int moveAll() {
if (gameState==STATE_BATTLE_WON && mustmove>0) {
return attacker.getArmies() - 1;
}
return -1;
}
public int getMustMove() {
return mustmove;
}
/**
* Retreats from attacking a country
* @return boolean Returns true if you can retreat, returns false if you cannot
*/
public boolean retreat() {
if (gameState==STATE_ROLLING) { // if we were in the attacking phase
currentPlayer.currentStatistic.addRetreat();
attacker=null;
defender=null;
gameState=STATE_ATTACKING; // go to attack phase
//System.out.print("Retreating\n");
return true;
}
return false;
}
/**
* Moves armies from one country to an adjacent country and goes to the end phase
* @param t1 Country where the armies are moving from
* @param t2 Country where the armies are moving to
* @param noa Number of Armies to move
* @return boolean Returns true if the tactical move is allowed, returns false if the tactical move is not allowed
*/
public boolean moveArmy(Country t1, Country t2, int noa) {
if (gameState==STATE_FORTIFYING) {
// do they exist //check if they belong to the player //check if they are neighbours //check if there are enough troops in country1
if (
t1!=null &&
t2!=null &&
t1.getOwner()==currentPlayer &&
t2.getOwner()==currentPlayer &&
t1.isNeighbours(t2) &&
// t2.isNeighbours(t1) && // not needed as there is code to check this
t1.getArmies() > noa &&
noa > 0
) {
t1.removeArmies(noa);
t2.addArmies(noa);
gameState=STATE_END_TURN;
checkPlayerWon();
//System.out.println("Armies Moved. "+gameState); // testing
return true;
}
}
return false;
}
/**
* Choosing not to use the tactical move and moves to the end phase
* @return boolean Returns true if you are in the right phase to use the tactical move and returns false otherwise
*/
public boolean noMove() {
if (gameState==STATE_FORTIFYING) { // if we were in the move phase
gameState=STATE_END_TURN; // go to end phase
//System.out.print("No Move.\n"); // testing
return true;
}
else return false;
}
public void workOutEndGoStats(Player p) {
int countries = p.getNoTerritoriesOwned();
int armies = p.getNoArmies();
int continents = getNoContinentsOwned(p);
int conectedEmpire = ((Vector)getConnectedEmpire(p)).size() ;
p.currentStatistic.endGoStatistics(countries, armies, continents, conectedEmpire);
}
public Vector getConnectedEmpire(Player p) {
Vector t = (Vector)p.getTerritoriesOwned().clone();
Vector a = new Vector();
Vector b = new Vector();
while ( t.isEmpty() == false ) {
Country country = ((Country)t.remove(0));
a.add( country );
getConnectedEmpire( t, a, country.getNeighbours() , p );
if (a.size() > b.size() ) {
b = a;
}
a = new Vector();
}
return b;
}
/**
* Finds the largest number of connected territories owned by a single player
* @param t Vector of territories owned by a single player (volatile)
* @param a Vector of adjacent territories
* @param n Vector of territories owned by a single player (non-volatile)
* @param p The current player
*/
public void getConnectedEmpire(Vector t, Vector a, Vector n, Player p) {
for (int i = 0; i < n.size() ; i++) {
if ( ((Country)n.elementAt(i)).getOwner() == p && t.contains( n.elementAt(i) ) ) {
Country country = (Country)n.elementAt(i);
t.remove( country );
a.add( country );
getConnectedEmpire( t, a, country.getNeighbours(), p);
}
}
}
/**
* Sets the capital for a player - ONLY FOR CAPITAL RISK
* @param c The capital country
* @return boolean Returns true if the country is set as the capital, returns false otherwise
*/
public boolean setCapital(Country c) {
if (gameState== STATE_SELECT_CAPITAL && gameMode == 2 && c.getOwner()==currentPlayer && currentPlayer.getCapital()==null) {
currentPlayer.setCapital(c);
for (int b=0; b< Cards.size() ; b++) {
if ( c== ((Card)Cards.elementAt(b)).getCountry() ) {
Cards.removeElementAt(b);
//System.out.print("card removed because it is a capital\n");
}
}
gameState=STATE_END_TURN;
return true;
}
return false;
}
/**
* Check if a player has won the game
* @return boolean Returns true if the player has won the game, returns false otherwise
*/
public boolean checkPlayerWon() {
boolean result=false;
// check if the player has won
int won=0;
for (int c=0; c< Continents.length ; c++) {
if ( Continents[c].isOwned(currentPlayer) ) {
won++;
}
}
if (won == Continents.length ) {
result=true;
//System.out.print("The Game Is Over, "+currentPlayer.getName()+" has won!\n");
}
// check if the player has won 2 player risk
/* @todo: maybe add this back, as crap player can never win
else if (setup == Players.size() && gameMode==1) {
Player target=null;
for (int c=0; c< Players.size() ; c++) {
// ((Player)Players.elementAt(c)).getType() !=3 &&
if ( (Player)Players.elementAt(c) != currentPlayer ) {
target = (Player)Players.elementAt(c);
}
}
if ( target.getNoTerritoriesOwned()==0 ) {
result=true;
}
}
*/
// check if the player has won capital risk!
else if (setup == Players.size() && gameMode==MODE_CAPITAL && currentPlayer.getCapital() !=null ) {
int capitalcount=0;
if ( currentPlayer==((Country)currentPlayer.getCapital()).getOwner() ) {
for (int c=0; c< Players.size() ; c++) {
if ( ((Vector)currentPlayer.getTerritoriesOwned()).contains((Country)((Player)Players.elementAt(c)).getCapital()) ) {
capitalcount++;
}
}
}
if ( capitalcount==Players.size() ) {
result=true;
}
}
// check if the player has won mission risk!
else if (setup == Players.size() && gameMode==MODE_SECRET_MISSION ) {
Mission m = currentPlayer.getMission();
if (
m.getPlayer() !=null && // check is this is indeed a Elim Player card
m.getPlayer() != currentPlayer && // check if its not the current player u need to eliminate
((Player)m.getPlayer()).getNoTerritoriesOwned()==0 && // chack if that player has been eliminated
((Vector)currentPlayer.getPlayersEliminated()).contains( m.getPlayer() ) //check if it was you who eliminated them
) {
// yay you have won
result=true;
}
else if (
m.getNoofcountries() != 0 && m.getNoofarmies() != 0 && // check if this card has a value for capture teretories
( m.getPlayer() == null || ((Player)m.getPlayer()).getNoTerritoriesOwned()==0 || (Player)m.getPlayer() == currentPlayer ) &&
m.getNoofcountries() <= currentPlayer.getNoTerritoriesOwned() // do you have that number of countries captured
) {
int n=0;
for (int c=0; c< currentPlayer.getNoTerritoriesOwned() ; c++) {
if ( ((Country)((Vector)currentPlayer.getTerritoriesOwned()).elementAt(c)).getArmies() >= m.getNoofarmies() ) n++;
}
if (n >= m.getNoofcountries() ) {
// yay you have won
result=true;
}
}
else if (
(m.getContinent1() !=null) && // this means its a continent mission
checkPlayerOwnesContinentForMission(m.getContinent1(),1) &&
checkPlayerOwnesContinentForMission(m.getContinent2(),2) &&
checkPlayerOwnesContinentForMission(m.getContinent3(),3)
) {
// yay you have won
result=true;
}
}
if (result==true) {
gameState=STATE_GAME_OVER;
}
return result;
}
private boolean checkPlayerOwnesContinentForMission(Continent c,int n) {
if ( ANY_CONTINENT.equals(c) ) {
return (getNoContinentsOwned(currentPlayer) >=n);
}
else if (c!=null) {
return c.isOwned(currentPlayer);
}
else {
return true;
}
}
public boolean continuePlay() {
if (gameState==STATE_GAME_OVER && gameMode != 0 && gameMode != 1) {
int oldGameMode=gameMode;
gameMode=0;
if ( checkPlayerWon() ) {
gameMode=oldGameMode;
return false;
}
if (tradeCap==true) { gameState=STATE_TRADE_CARDS; }
else if ( currentPlayer.getExtraArmies()==0 ) { gameState=STATE_ATTACKING; }
else { gameState=STATE_PLACE_ARMIES; }
return true;
}
return false;
}
//private URL getURL(String a) throws Exception {
// return new URL(risk.engine.Risk.mapsdir,a);
//}
/**
* Loads the map
* @param filename The map filename
* @throws Exception There was a error
*/
public void loadMap(String filename) throws Exception {
StringTokenizer st=null;
runmaptest = false;
Vector Countries = new Vector();
Vector Continents = new Vector();
//System.out.print("Starting Load Map...\n");
BufferedReader bufferin=new BufferedReader(new InputStreamReader( risk.engine.RiskUtil.openMapStream(filename)));
String input = bufferin.readLine();
String mode = "none";
while(input != null) {
if (input.equals("") || input.charAt(0)==';') {
// do nothing
//System.out.print("Nothing\n"); // testing
}
else {
//System.out.print("Something found\n"); // testing
if (input.charAt(0)=='[' && input.charAt( input.length()-1 )==']') {
//System.out.print("Something beggining with [ and ending with ] found\n"); // testing
mode="newsection";
}
else { st = new StringTokenizer(input); }
if (mode.equals("files")) {
//System.out.print("Adding files\n"); // testing
String fm = st.nextToken();
if ( fm.equals("pic") ) { ImagePic = st.nextToken(); } //System.out.print("file: ImagePic added!\n"); // testing
else if ( fm.equals("map") ) { ImageMap = st.nextToken(); } //System.out.print("file: ImageMap added!\n"); // testing
else if ( fm.equals("crd") ) { st.nextToken(); }
else if ( fm.equals("prv") ) { st.nextToken(); }
else { throw new Exception("error with files section in map file: "+fm); }
if ( st.hasMoreTokens() ) { throw new Exception("unknown item found in map file: "+ st.nextToken() ); }
}
else if (mode.equals("continents")) {
//System.out.print("Adding continents\n"); // testing
String id=st.nextToken(); //System.out.print(name+"\n"); // testing
// get translation
String name = risk.engine.translation.MapTranslator.getTranslatedMapName(id).replaceAll( "_", " ");
int noa=Integer.parseInt( st.nextToken() ); //System.out.print(noa+"\n"); // testing
Color color=getColor( st.nextToken() ); //System.out.print(color.toString()+"\n"); // testing
if (color==null) {
// there was no check for null b4 here, but now we need this for the map editor
color = getRandomColor();
}
if ( st.hasMoreTokens() ) { throw new Exception("unknown item found in map file: "+ st.nextToken() ); }
Continent continent = new Continent(id, name, noa, color);
Continents.add(continent);
}
else if (mode.equals("countries")) {
//System.out.print("Adding countries\n"); // testing
int color = Integer.parseInt(st.nextToken());
String id=st.nextToken(); //System.out.print(name+"\n"); // testing
// get translation
String name = risk.engine.translation.MapTranslator.getTranslatedMapName(id).replaceAll( "_", " ");
int continent = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
if ( st.hasMoreTokens() ) { throw new Exception("unknown item found in map file: "+ st.nextToken() ); }
if ( Countries.size() != (color-1) ) { throw new Exception("unexpected number found in map file: "+color ); }
Country country = new Country(color, id, name, (Continent)Continents.elementAt( continent - 1 ), x, y);
Countries.add(country);
((Continent)Continents.elementAt( continent - 1 )).addTerritoriesContained(country);
}
else if (mode.equals("borders")) {
//System.out.print("Adding borders\n"); // testing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -