import java.awt.Color; import java.awt.Image; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Queue; import java.util.StringTokenizer; import java.util.Vector; import javax.swing.JButton; import javax.swing.JEditorPane; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSeparator; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; /** * * * * @author Ji Eun Gu * * Description : This class works as a game engine which saves a game condition. Later, this class will * load a file to read the file condition so the game can be replayed. The file name will be the user's * name + .board. User can simply load a game he/she played on Game Load menu and save a game to simply click * Game Save Button on a menu bar. * * * */ public class RiskGameEngine { // GameInformation has the information about the selected plug-in // so the plug-in must be put into the GameInformation class private AllCard cardInfo; // image file he/she played with private String originalImageFile; // all user private ArrayList userList; // game condition private boolean placeArmy; private boolean attackTerritory; private boolean attackingTerritoryClick; private boolean firstTime; private Territory attackingTerritory; private Territory defensingTerritory; private User attackingUser; private User defensingUser; private int attackingUserIndex; private GameInformation gameInfo; private String allUserFile="allUsers.board"; private ArrayList allUserFileList = new ArrayList(); /** * this method is a constructor that reads userList as a parameter for * saving a game * @param userList */ public RiskGameEngine(ArrayList userList){ this.userList=userList; } /** * this method is a constructor for loading a game */ public RiskGameEngine(){ } /** * this method is an accessor to get userList * @return userList */ public ArrayList getUserList(){ return userList; } /** * this method is an accessor to get originalImageFile * @return originalImageFile */ public String getOriginalImageFile(){ return originalImageFile; } /** * this method is an accessor to get placeArmy(whether the game was placing or not) * @return placeArmy */ public boolean getPlaceArmy(){ return placeArmy; } /** * this method is an accessor to get attackingTerritory(whether the game * attacking a territory or not) * @return attackTerritory */ public boolean getAttackTerritory(){ return attackTerritory; } /** * this method is an accessor to get attackingTerritoryClick(whether the user selected a * attacking territory or not) * @return attackingTerritoryClick */ public boolean getAttackingTerritoryClick(){ return attackingTerritoryClick; } /** * this method is an accessor to get firstTime(whether or not the game first played) * @return firstTime */ public boolean getFirstTime(){ return firstTime; } /** * this method is an accessor to get attackingUSerIndex * @return attackingUserIndex */ public int getAttackingUserIndex(){ return attackingUserIndex; } /** * this method is an accessor to get attackingTerritory * @return attackingTerritory */ public Territory getAttackingTerritory(){ return attackingTerritory; } /** * this method is an accessor to get defensingTerritory * @return defensingTerritory */ public Territory getDefensingTerritory(){ return defensingTerritory; } /** * this method is an accessor to get attackingUser * @return attackingUser */ public User getAttackingUser(){ return attackingUser; } /** * this method is an accessor to get defensingUser * @return defensingUser */ public User getDefensingUser(){ return defensingUser; } /** * this method is to add a name to all user file * @param file */ public void addNameToAllUserFile(String file){ try{ FileWriter fw = new FileWriter(allUserFile); fw.write(file); fw.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } /** * this method will return a boolean value which tells whether or not * all user file is read successfully * @return success */ public boolean readAllUserFile(){ boolean success=true; BufferedReader d; try{ d = new BufferedReader(new InputStreamReader( new FileInputStream(allUserFile) ) ); }catch( Exception ex ){ success = false; } return success; } /** * this method is a accessor for allUserFileList * @return allUserFileList */ public ArrayList getAllUserFileList(){ return allUserFileList; } /** * this method is making a file which contains all conditions and information * about a game to save for further use */ public void makeFile(){ try { String userName=userList.get(0).getName(); userName.replaceAll(" ", ""); addNameToAllUserFile(userName+".board"); FileWriter fw = new FileWriter(userName+".board"); fw.write(originalImageFile); // ---.png fw.write("\n"); for(int i = 0 ; i < userList.size(); i++){ fw.write(userList.get(i).getName()); // user name in userList fw.write("\n"); fw.write(userList.get(i).getNumOfArmyToBeAdded()+""); fw.write("\n"); fw.write(userList.get(i).getTerritory().size()+""); // territory size fw.write("\n"); for(int j = 0 ;j continentList = gameInfo.getContinent(); for(int i = 0 ; i < continentList.size();i++){ Continent c = continentList.get(i); ArrayList territoryList = c.getTerritory(); for(int j=0; j