📄 riskgame.java
字号:
int country=Integer.parseInt( st.nextToken() ); //System.out.print(country+"\n"); // testing
while (st.hasMoreElements()) {
((Country)Countries.elementAt( country - 1 )).addNeighbour( ((Country)Countries.elementAt( Integer.parseInt(st.nextToken()) - 1 )) );
}
}
else if (mode.equals("newsection")) {
mode = input.substring(1, input.length()-1); // set mode to the name of the section
if (mode.equals("files") ) {
//System.out.print("Section: files found\n"); // testing
ImagePic=null;
ImageMap=null;
}
else if (mode.equals("continents") ) {
//System.out.print("Section: continents found\n"); // testing
}
else if (mode.equals("countries") ) {
//System.out.print("Section: countries found\n"); // testing
}
else if (mode.equals("borders") ) {
//System.out.print("Section: borders found\n"); // testing
}
else {
throw new Exception("unknown section found in map file: "+mode);
}
}
else {
if (input.equals("test")) {
runmaptest = true;
}
else if (input.startsWith("name")) {
}
else {
throw new Exception("unknown item found in map file: "+input);
}
}
}
input = bufferin.readLine(); // get next line
}
bufferin.close();
this.Countries = (Country[])Countries.toArray( new Country[Countries.size()] );
this.Continents = (Continent[])Continents.toArray( new Continent[Continents.size()] );
//System.out.print("Map Loaded\n");
}
/**
* Sets the filename of the map file
* @param f The name of the new file
* @return boolean Return trues if missions are supported
* @throws Exception The file cannot be found
*/
public boolean setMapfile(String f) throws Exception {
StringTokenizer st=null;
if (f.equals("default")) {
f = defaultMap;
}
BufferedReader bufferin=new BufferedReader(new InputStreamReader(risk.engine.RiskUtil.openMapStream(f)));
/*
File file;
if (f.equals("default")) {
file = new File("maps/" + defaultMap);
}
else {
file = new File("maps/" + f);
}
FileReader filein = new FileReader(file);
BufferedReader bufferin = new BufferedReader(filein);
*/
previewPic = null;
mapName = null;
String input = bufferin.readLine();
String mode = null;
boolean yesmap = false;
boolean returnvalue = false;
boolean yescards = false;
while(input != null) {
if (input.equals("") || input.charAt(0)==';') {
}
else {
if (input.charAt(0)=='[' && input.charAt( input.length()-1 )==']') {
mode="newsection";
}
else { st = new StringTokenizer(input); }
if ("files".equals(mode)) {
String fm = st.nextToken();
if ( fm.equals("pic") ) { ImagePic = st.nextToken(); }
else if ( fm.equals("prv") ) { previewPic = st.nextToken(); }
else if ( fm.equals("crd") ) { yescards=true; returnvalue = setCardsfile( st.nextToken() ); }
}
else if ("borders".equals(mode)) {
yesmap=true;
}
else if ("newsection".equals(mode)) {
mode = input.substring(1, input.length()-1); // set mode to the name of the section
}
else if (mode == null) {
if (input.startsWith("name ")) {
mapName = input.substring(5,input.length());
}
}
}
input = bufferin.readLine(); // get next line
}
if ( yesmap==false ) { throw new Exception("error with map file"); }
if ( yescards==false ) { throw new Exception("cards file not specified in map file"); }
mapfile = f;
bufferin.close();
risk.engine.translation.MapTranslator.setMap( f );
return returnvalue;
}
public void setMemoryLoad() {
mapfile = null;
cardsfile = null;
ImagePic = null;
ImageMap = null;
}
public void setupNewMap() {
Countries = new Country[0];
Continents = new Continent[0];
Cards = new Vector();
Missions = new Vector();
setMemoryLoad();
}
public void setCountries(Country[] a) {
Countries = a;
}
public void setContinents(Continent[] a) {
Continents = a;
}
/**
* Loads the cards
* @param filename The cards filename
* @throws Exception There was a error
*/
public void loadCards(String filename) throws Exception {
StringTokenizer st=null;
Cards = new Vector();
Missions = new Vector();
//System.out.print("Starting load cards and missions...\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("cards")) {
//System.out.print("Adding cards\n"); // testing
String name=st.nextToken(); //System.out.print(name+"\n"); // testing
if (name.equals(Card.WILDCARD)) {
Card card = new Card(name, null);
Cards.add(card);
}
else if ( name.equals(Card.CAVALRY) || name.equals(Card.INFANTRY) || name.equals(Card.CANNON) ) {
int country=Integer.parseInt( st.nextToken() );
//System.out.print( Countries[ country - 1 ].getName() +"\n"); // testing
Card card = new Card(name, Countries[ country - 1 ]);
Cards.add(card);
}
else {
throw new Exception("unknown item found in cards file: "+name);
}
if ( st.hasMoreTokens() ) { throw new Exception("unknown item found in cards file: "+ st.nextToken() ); }
}
else if (mode.equals("missions")) {
//System.out.print("Adding Mission\n"); // testing
//boolean add=true;
int s1 = Integer.parseInt(st.nextToken());
Player p;
if (s1==0 || s1>Players.size() ) {
p = null;
}
else {
p = (Player)Players.elementAt( s1-1 );
}
int noc = Integer.parseInt(st.nextToken());
int noa = Integer.parseInt(st.nextToken());
String s4 = st.nextToken();
String s5 = st.nextToken();
String s6 = st.nextToken();
Continent c1 = getMissionContinentfromString( s4 );
Continent c2 = getMissionContinentfromString( s5 );
Continent c3 = getMissionContinentfromString( s6 );
String missioncode=s1+"-"+noc+"-"+noa+"-"+s4+"-"+s5+"-"+s6;
String description=risk.engine.translation.MapTranslator.getTranslatedMissionName(missioncode);
if (description==null) {
description=st.nextToken();
while (st.hasMoreElements()) {
description = description +" "+ (String)st.nextToken();
}
}
if (p !=null) description = description.replaceAll( "PLAYER"+s1, p.getName() );
if ( s1 <= Players.size() ) { // || Players.size()==0 null but there for the map editor
//System.out.print(description+"\n"); // testing
Mission mission = new Mission(p, noc, noa, c1, c2, c3, description);
Missions.add(mission);
}
else {
//System.out.print("NOT adding this mission as it refures to an unused player\n"); // testing
}
}
else if (mode.equals("newsection")) {
mode = input.substring(1, input.length()-1); // set mode to the name of the section
if (mode.equals("cards") ) {
//System.out.print("Section: cards found\n"); // testing
}
else if (mode.equals("missions") ) {
//System.out.print("Section: missions found\n"); // testing
}
else {
throw new Exception("unknown section found in cards file: "+mode);
}
}
else {
throw new Exception("unknown item found in cards file: "+input);
}
}
input = bufferin.readLine(); // get next line
}
bufferin.close();
//System.out.print("Cards and missions loaded.\n");
}
private Continent getMissionContinentfromString(String a) {
if (a.equals("*")) {
return ANY_CONTINENT;
}
else {
int s = Integer.parseInt(a);
if (s==0) {
return null;
}
else {
return Continents[ s-1 ];
}
}
}
/**
* Sets the filename of the cards file
* @param f The name of the new file
* @return boolean Return trues if missions are supported
* @throws Exception The file cannot be found
*/
public boolean setCardsfile(String f) throws Exception {
StringTokenizer st=null;
if (f.equals("default")) {
f = defaultCards;
}
BufferedReader bufferin=new BufferedReader(new InputStreamReader(risk.engine.RiskUtil.openMapStream(f)));
/*
File file;
if (f.equals("default")) {
file = new File("maps/" + defaultCards);
}
else {
file = new File("maps/" + f);
}
FileReader filein = new FileReader(file);
BufferedReader bufferin = new BufferedReader(filein);
*/
String input = bufferin.readLine();
String mode = "none";
boolean yesmissions=false;
boolean yescards=false;
while(input != null) {
if (input.equals("") || input.charAt(0)==';') {
}
else {
if (input.charAt(0)=='[' && input.charAt( input.length()-1 )==']') {
mode="newsection";
}
else { st = new StringTokenizer(input); }
if (mode.equals("newsection")) {
mode = input.substring(1, input.length()-1); // set mode to the name of the section
if (mode.equals("cards")) {
yescards=true;
}
else if (mode.equals("missions")) {
yesmissions=true;
}
}
}
input = bufferin.readLine(); // get next line
}
if ( yescards==false ) { throw new Exception("error with cards file"); }
cardsfile = f;
bufferin.close();
risk.engine.translation.MapTranslator.setCards( f );
return yesmissions;
}
/**
* Shuffles the countries
*/
public Vector shuffleCountries() {
Vector oldCountries = new Vector(Arrays.asList( Countries ));
//Vector newCountries = new Vector();
//while(oldCountries.size() > 0) {
// int a = r.nextInt(oldCountries.size()) ;
// newCountries.add ( oldCountries.remove(a) );
//}
//return newCountries;
Collections.shuffle(oldCountries);
return oldCountries;
}
/**
* Creates a new game
* @return RiskGame Returns the new game created
public static RiskGame newGame() {
RiskGame game = new RiskGame();
//System.out.print("Game State: "+game.getState()+"\n"); // testing
return game;
}
*/
/**
* Loads a saved game
* @param file The saved game's filename
* @return Riskgame Return the saved game object if it loads, returns null if it doe not load
*/
public static RiskGame loadGame(String file) throws Exception {
RiskGame game = null;
//try {
InputStream filein = risk.engine.RiskUtil.getLoadFileInputStream(file);
ObjectInputStream objectin = new ObjectInputStream(filein);
game = (RiskGame) objectin.readObject();
objectin.close();
//XMLDecoder d = new XMLDecoder( new BufferedInputStream( new FileInputStream(file)));
//game = (RiskGame)d.readObject();
//d.close();
//}
//catch (Exception e) {
//System.out.println(e.getMessage());
//}
return game;
}
/**
* Closes the current game
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -