package game; import java.util.Properties; import java.io.InputStream; import java.io.FileInputStream; import data.Money; /** * class to handle initial game configuration and setup * * @author Rukmal Fernando * @author Hardik Kotecha * @author Radhika Srinivasan */ public class Config { private static int timeLimit; //amount of time allowed per player time : 20 private static int maxVars; //the maximum number of variables allowed : 10 private static int maxClauses; //the maximum number of clauses allowed : 1110 private static int maxWeight; //the maximum weight that can be assigned : 2^20 private static int maxTurns; //the number of turns in the game : 20 private static String blackBoardPath; //location of blackboard private static int maxClauseLength; //maximum length of a clause : 3 private static int maxTypeLength; //maximum length of a raw material type : 2 private static Float initialMoney; //initial player money : 5.0 private static Config reference; //reference to Config singleton /** * private default constructor */ private Config() { reference = null; } /** * read file at given filepath and parse properties * * @param pFilePath */ private Config( String pFilePath ) { Properties p = new Properties(); try { if( pFilePath != null ) { InputStream is = new FileInputStream( pFilePath ); p.load( is ); } } catch( Exception e ) { System.out.println( "Could not find config file but will use wiki defaults" ); e.printStackTrace(); } new Config( Integer.parseInt( p.getProperty( "timeLimit" , "20" ) ), Integer.parseInt( p.getProperty( "maxVars", "10" ) ), Integer.parseInt( p.getProperty( "maxClauses", "1110" ) ), Integer.parseInt( p.getProperty( "maxWeight", "1048576" ) ), Integer.parseInt( p.getProperty( "maxTurns" , "20" ) ), p.getProperty( "blackBoardPath", "c:/blackboard" ), Integer.parseInt( p.getProperty( "maxClauseLength", "3" ) ), Integer.parseInt( p.getProperty( "maxTypeLength", "2" ) ), Float.parseFloat( p.getProperty( "initialMoney", "5.0" ) ) ); } /** * constructor for object of type Config * * @param int pTimeLimit * @param int pMaxVars, * @param int pMaxClauses, * @param int pMaxWeight, * @param int pMaxTurns, * @param String pBlackboardPath, * @param int pMaxClauseLength, * @param int pMaxTypeLength, * @param Float pInitialMoney */ private Config( int pTimeLimit, int pMaxVars, int pMaxClauses, int pMaxWeight, int pMaxTurns, String pBlackboardPath, int pMaxClauseLength, int pMaxTypeLength, Float pInitialMoney) { setTimeLimit( pTimeLimit ); setMaxVars( pMaxVars ); setMaxClauses( pMaxClauses ); setMaxWeight( pMaxWeight ); setMaxTurns( pMaxTurns ); setBlackBoardPath( pBlackboardPath ); setMaxClauseLength( pMaxClauseLength ); setMaxTypeLength( pMaxTypeLength ); setInitialMoney( pInitialMoney ); } /** * get or create the config object * * @return Config */ public static Config initialize() { if( reference == null ) //if no config exists return new Config(); else //if a config exists return reference; } /** * get or create the config object * * @param pFilePath * @return Config */ public static Config initialize( String pFilePath ) { if( reference == null ) //if no config exists return new Config( pFilePath ); else //if a config exists return reference; } /*======================================================================== | getters and setters +-----------------------------------------------------------------------*/ /** * get the time limit for a player's turn * * @return int : time limit in seconds */ static protected int getTimeLimit() { return timeLimit; } /** * set the time limit for a player's turn * * @param pTimeLimit : time in seconds */ private void setTimeLimit( int pTimeLimit ) { timeLimit = pTimeLimit; } /** * get the maximum number of variables allowed * * @return int : number of variables allowed */ public static int getMaxVars() { return maxVars; } /** * set the maximum number of variables allowed * * @param maxVars : number of variables allowed */ private void setMaxVars( int pMaxVars ) { maxVars = pMaxVars; } /** * get the maximum number of clauses allowed * * @return int */ protected static int getMaxClauses() { return maxClauses; } /** * set the maximum number of clauses allowed * * @param maxClauses : number of clauses */ private void setMaxClauses( int pMaxClauses ) { maxClauses = pMaxClauses; } /** * get the maximum number of turns * * @return int */ protected static int getMaxTurns() { return maxTurns; } /** * set the maximum number of turns * * @param maxTurns : int */ private void setMaxTurns( int pMaxTurns ) { maxTurns = pMaxTurns; } /** * get the maximum weight * * @return int */ public static int getMaxWeight() { return maxWeight; } /** * set the maximum weight * * @param maxTurns : int */ private void setMaxWeight( int pMaxWeight ) { maxWeight = pMaxWeight; } /** * get the path of the blackboard * * @return String */ public static String getBlackBoardPath() { return blackBoardPath; } /** * set the path of the blackboard * * @param pBlackBoardPath */ private void setBlackBoardPath( String pBlackBoardPath ) { blackBoardPath = pBlackBoardPath; } /** * get the maximum length for a clause * * @return int */ public static int getMaxClauseLength() { return maxClauseLength; } /** * set the maximum length for a clause * * @param pMaxClauseLength */ private void setMaxClauseLength( int pMaxClauseLength ) { maxClauseLength = pMaxClauseLength; } /** * get the max type length * * @return int */ public static int getMaxTypeLength() { return maxTypeLength; } /** * set the max type length * * @param pMaxTypeLength : int */ private void setMaxTypeLength( int pMaxTypeLength ) { maxTypeLength = pMaxTypeLength; } /** * get the initial money * * @return Money */ public static Money getInitialMoney() { return new Money( initialMoney ); } /** * set the initial money * * @param pInitialMoney : Float */ private void setInitialMoney( Float pInitialMoney ) { initialMoney = pInitialMoney; } /*----------------------------------------------------------------------- | end getters and setters +======================================================================*/ }