Skip to content

Commit

Permalink
real game assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-prodbase committed Sep 6, 2023
1 parent 9691b0b commit cef9dbf
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 47 deletions.
60 changes: 60 additions & 0 deletions src/ArgsMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import ArgsSerializer.*;
import Exceptions.*;
import GameExecutor.*;
import Logger.*;
import ReportMaker.ReportMaker;

/**
* Main entry point for the game simulation.
*/
public class ArgsMain {
private static final Logger logger = new Logger("Main");

/**
* Entry point for the application.
*
* @param args Command-line arguments.
* @throws InterruptedException If the game execution is interrupted.
*/
public static void main(String[] args) throws InterruptedException {
/* extract parameters */
logger.title("Parse arguments");
ArgsSerializer argsSerializer = new ArgsSerializer(args);
GameArguments gameArguments;
try {
gameArguments = argsSerializer.serialize();
} catch (InvalidGameException | NotEnoughArgumentsException e) {
logger.error(e.toString());
ArgsMain.printUsage();
return;
}
gameArguments.print();

/* run game */
logger.title("Execute Game");
GameExecutor gameExecutor = new GameExecutor(gameArguments);
GameExecutorResults results = gameExecutor.runGame();

/* create reports */
ReportMaker reportMaker = new ReportMaker();
reportMaker.generateReport(gameArguments.numberOfAgents(), gameArguments.gameType(), gameArguments.fraction(),
gameArguments.probability(), results.network(), results.audit());

/* conclude results */
logger.title("Game Results");
results.print();
}

/**
* Displays the correct usage of the application along with expected command-line arguments.
*/
public static void printUsage(){
logger.info("### For Prisoner’s Dilemma (PD-" + GameType.PD.getValue() +") ###");
logger.info("Usage: java Main <number_of_agents:int> <probability_of_connection:double> "
+ GameType.PD.getValue());

logger.info("### For Battle of the Sexes (BoS-"+GameType.BoS.getValue()+") ###");
logger.info("Usage: java Main <number_of_agents:int> <probability_of_connection:double> "
+ GameType.BoS.getValue() + " <friction:int>");
}
}
14 changes: 14 additions & 0 deletions src/ArgsSerializer/GameArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import Logger.Logger;

import java.util.Random;

/**
* Represents the parsed and validated game parameters.
*/
public record GameArguments(int numberOfAgents, double probability, GameType gameType, int fraction) {
private static final Random random = new Random();

// Format string for logging the parsed arguments
private final static String FORMATTED_ARGUMENTS_STRING = "Parsed arguments: numberOfAgents: %d, probability: %,.1f, gameType: %s, fraction: %s";
Expand All @@ -30,5 +33,16 @@ public GameArguments(int numberOfAgents, double probability, GameType gameType)
public void print(){
logger.debug(String.format(FORMATTED_ARGUMENTS_STRING, numberOfAgents, probability, gameType, fraction));
}

public static GameArguments getRandomArguments(int numberOfAgents, double probability){
int fraction = 0;
GameType gameType = random.nextBoolean() ? GameType.PD : GameType.BoS;

if(gameType == GameType.BoS){
fraction = random.nextInt(0, numberOfAgents);
}

return new GameArguments(numberOfAgents, probability, gameType, fraction);
}
}

64 changes: 17 additions & 47 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,30 @@
import ArgsSerializer.*;
import Exceptions.*;
import GameExecutor.*;
import Logger.*;
import ReportMaker.ReportMaker;

/**
* Main entry point for the game simulation.
*/
public class Main {
private static final int NUMBER_OF_GAMES = 10;

private static final Logger logger = new Logger("Main");

/**
* Entry point for the application.
*
* @param args Command-line arguments.
* @throws InterruptedException If the game execution is interrupted.
*/
public static void main(String[] args) throws InterruptedException {
/* extract parameters */
logger.debug("Parse arguments", LogType.Title);
ArgsSerializer argsSerializer = new ArgsSerializer(args);
GameArguments gameArguments;
try {
gameArguments = argsSerializer.serialize();
} catch (InvalidGameException | NotEnoughArgumentsException e) {
logger.error(e.toString());
Main.printUsage();
return;
}
gameArguments.print();

/* run game */
logger.debug("Execute Game", LogType.Title);
GameExecutor gameExecutor = new GameExecutor(gameArguments);
GameExecutorResults results = gameExecutor.runGame();
logger.title("Randomize arguments");
GameArguments gameArguments = GameArguments.getRandomArguments(10, 0.5);

/* create reports */
ReportMaker reportMaker = new ReportMaker();
reportMaker.generateReport(gameArguments.numberOfAgents(), gameArguments.gameType(), gameArguments.fraction(),
gameArguments.probability(), results.network(), results.audit());
logger.title("Executing " + NUMBER_OF_GAMES +" Random Games");
int totalRawRounds = 0;
int totalSW = 0;
for (int i = 0; i < NUMBER_OF_GAMES; i++) {
logger.info("Starting Game No." + (i + 1) + ": " + gameArguments.gameType());
GameExecutor gameExecutor = new GameExecutor(gameArguments);
GameExecutorResults results = gameExecutor.runGame();

/* conclude results */
logger.debug("Game Results", LogType.Title);
results.print();
}

/**
* Displays the correct usage of the application along with expected command-line arguments.
*/
public static void printUsage(){
logger.debug("### For Prisoner’s Dilemma (PD-" + GameType.PD.getValue() +") ###");
logger.debug("Usage: java Main <number_of_agents:int> <probability_of_connection:double> "
+ GameType.PD.getValue());
totalRawRounds += results.totalRounds();
totalSW += results.totalGain();
}

logger.debug("### For Battle of the Sexes (BoS-"+GameType.BoS.getValue()+") ###");
logger.debug("Usage: java Main <number_of_agents:int> <probability_of_connection:double> "
+ GameType.BoS.getValue() + " <friction:int>");
logger.title("Summarizing results");
logger.info("Average number of rounds: " + (totalRawRounds / NUMBER_OF_GAMES));
logger.info("Total SW: " + totalSW);
}
}

0 comments on commit cef9dbf

Please sign in to comment.