diff --git a/src/com/concordia/riskgame/model/Modules/Player.java b/src/com/concordia/riskgame/model/Modules/Player.java index 39d913c..413aa84 100644 --- a/src/com/concordia/riskgame/model/Modules/Player.java +++ b/src/com/concordia/riskgame/model/Modules/Player.java @@ -65,35 +65,6 @@ public Player(int playerIndex, String playerName) { this.strategy = new Human(); } - public int getNumOfInfCard() { - int n = 0; - for (Card card : cardsOwned) { - if (card == Card.INFANTRY) { - n++; - } - } - return n; - } - - public int getNumOfCavCard() { - int n = 0; - for (Card card : cardsOwned) { - if (card == Card.CAVALRY) { - n++; - } - } - return n; - } - public int getNumOfArtCard() { - int n = 0; - for (Card card : cardsOwned) { - if (card == Card.ARTILLERY) { - n++; - } - } - return n; - } - public void setPlayerIndex(int playerIndex) { this.playerIndex = playerIndex; } @@ -250,6 +221,53 @@ public void attack(String command) { } + /** + * To get player's number of infantry card + * @return + */ + public int getNumOfInfCard() { + int n = 0; + for (Card card : cardsOwned) { + if (card == Card.INFANTRY) { + n++; + } + } + return n; + } + + /** + * To get player's number of cavalery card + * @return + */ + public int getNumOfCavCard() { + int n = 0; + for (Card card : cardsOwned) { + if (card == Card.CAVALRY) { + n++; + } + } + return n; + } + + /** + * To get number of art cards + * @return + */ + public int getNumOfArtCard() { + int n = 0; + for (Card card : cardsOwned) { + if (card == Card.ARTILLERY) { + n++; + } + } + return n; + } + + /** + * check defending command + * @param command + */ + public void defendCommand(String command) { if (!defendCommandInput) { System.out.println("Not defending time"); @@ -554,6 +572,12 @@ private void autoAttack() { } + /** + * Player do fortification + * @param command + * @return + */ + public boolean fortifyArmy(String command) { String[] commands = command.split(" "); fromCountry = gameMap.searchCountry(commands[1]); diff --git a/src/com/concordia/riskgame/model/Modules/Stratigies/Aggressive.java b/src/com/concordia/riskgame/model/Modules/Stratigies/Aggressive.java index 9311a51..3e98e5c 100644 --- a/src/com/concordia/riskgame/model/Modules/Stratigies/Aggressive.java +++ b/src/com/concordia/riskgame/model/Modules/Stratigies/Aggressive.java @@ -9,15 +9,16 @@ import java.io.Serializable; import java.util.*; +/** + * This class is one of the player's strategy, aggressive player . + * + */ + public class Aggressive implements Strategy,Serializable { - /** - * - */ private static final long serialVersionUID = 1L; private String strategyName = "Aggressive"; public String ANSI_BLUE = "\u001B[34m"; - //private Gameplay gameplay =Gameplay.getInstance(); private Country strongestFrontCountry; public String getColor() { @@ -29,8 +30,11 @@ public String getStrategyName() { return strategyName; } + /** + * Aggressive player do card exchange once cards are available. + */ + public void doCardExchange(){ - // gameplay =Gameplay.getInstance(); try { System.out.println("Bot Executing Command : " + "exchangecards 3 0 0"); @@ -50,6 +54,10 @@ public void doCardExchange(){ } + /** + * Aggressive player do reinforcement in reinforce phase. + */ + public void doReinforcement(){ try { strongestFrontCountry = getStrongestFrontCountry(); @@ -63,6 +71,10 @@ public void doReinforcement(){ } + /** + * Aggressive player do attack in attack phase. + */ + public void doAttack(){ try { for (String neighbor : strongestFrontCountry.getListOfNeighbours()) { @@ -94,6 +106,11 @@ public void doAttack(){ } } + /** + * Aggressive player do fortification in fortify phase. First find the player's front battle line country, check the + * most reinforce they can get, and where they can get, then find the best country, get the best neighbor. + */ + public void doFortification() { Map frontBattleCountry = new HashMap<>(); HashMap frontBattleCountryNeighbor = new HashMap<>(); @@ -113,7 +130,7 @@ public void doFortification() { frontBattleCountry.put(country, most + country.getNoOfArmiesPresent()); frontBattleCountryNeighbor.put(country, bestNeighbor); } - + //find the best country for (Country country : frontBattleCountry.keySet()) { int m = frontBattleCountry.get(country); int n = frontBattleCountry.get(bestCountry); @@ -142,6 +159,10 @@ public void doFortification() { } } + /** + * This function is to find the strongest country to do attack + * @return + */ public Country getStrongestFrontCountry(){ Country strongest = frontBattleLine().get(0); for(Country c : frontBattleLine()){ @@ -152,6 +173,10 @@ public Country getStrongestFrontCountry(){ return strongest; } + /** + * this country find all the available attack country + * @return front line country arraylist + */ public ArrayList frontBattleLine() { ArrayList frontBattleCountry = new ArrayList<>(); for (String country : Gameplay.getInstance().getCurrentPlayer().getCountriesOwned()) { diff --git a/src/com/concordia/riskgame/model/Modules/Stratigies/Benevolent.java b/src/com/concordia/riskgame/model/Modules/Stratigies/Benevolent.java index de6868c..f796a18 100644 --- a/src/com/concordia/riskgame/model/Modules/Stratigies/Benevolent.java +++ b/src/com/concordia/riskgame/model/Modules/Stratigies/Benevolent.java @@ -7,16 +7,15 @@ import java.io.Serializable; import java.util.ArrayList; - +/** + * This class is one of the player's strategy, Benevolent player . + * + */ public class Benevolent implements Strategy,Serializable { - /** - * - */ private static final long serialVersionUID = 1L; private String strategyName = "Benevolent"; public String ANSI_YELLOW = "\u001B[33m"; - // Gameplay gameplay = Gameplay.getInstance(); public String getColor() { return ANSI_YELLOW; @@ -27,8 +26,10 @@ public String getStrategyName() { return strategyName; } + /** + * Benevolent player do card exchange. + */ public void doCardExchange(){ - // gameplay =Gameplay.getInstance(); try { System.out.println("Bot Executing Command : " + "exchangecards 3 0 0"); @@ -47,8 +48,11 @@ public void doCardExchange(){ } } + /** + * Benevolent player do reinforce + */ + public void doReinforcement(){ - // gameplay =Gameplay.getInstance(); int armies_available = Gameplay.getInstance().getCurrentPlayer().getArmyCount(); String countryName = getWeakestCountry(); String reinforceCommand; @@ -69,6 +73,10 @@ public void doReinforcement(){ } } + /** + * Benevolent player do attack, just skip attack phase + */ + public void doAttack(){ System.out.println("Benevolent Bot Does not attack."); try{ @@ -81,6 +89,9 @@ public void doAttack(){ } + /** + * Benevolent player do fortification + */ public void doFortification() { int armies_available = Gameplay.getInstance().getCurrentPlayer().getArmyCount(); String countryName = getWeakestCountry(); @@ -104,6 +115,11 @@ public void doFortification() { } } + /** + * Benevolent player get the weakest country to fortify + * @return weakest country's name + */ + public String getWeakestCountry(){ Country weakest = Gameplay.getInstance().getSelectedMap().getOwnedCountries(Gameplay.getInstance().getCurrentPlayer().getPlayerName()).get(0); ArrayList countryOwnedList = Gameplay.getInstance().getSelectedMap().getOwnedCountries(Gameplay.getInstance().getCurrentPlayer().getPlayerName()); diff --git a/src/com/concordia/riskgame/model/Modules/Stratigies/Cheater.java b/src/com/concordia/riskgame/model/Modules/Stratigies/Cheater.java index 34fa70f..94abcc5 100644 --- a/src/com/concordia/riskgame/model/Modules/Stratigies/Cheater.java +++ b/src/com/concordia/riskgame/model/Modules/Stratigies/Cheater.java @@ -9,6 +9,9 @@ import java.util.ArrayList; import java.util.List; +/** + * Cheater player's turn + */ public class Cheater implements Strategy,Serializable { /** @@ -17,7 +20,6 @@ public class Cheater implements Strategy,Serializable { private static final long serialVersionUID = 1L; private String strategyName = "Cheater"; public String ANSI_PURPLE = "\u001B[35m"; - // Gameplay gameplay = Gameplay.getInstance(); public String getColor() { return ANSI_PURPLE; @@ -29,9 +31,10 @@ public String getStrategyName() { return strategyName; } + /** + * Cheater do card exchange + */ public void doCardExchange(){ - // this.gameplay = Gameplay.getInstance(); //For updating the class gameplay object when we load a saved game. - try { System.out.println("Bot Executing Command : " + "exchangecards 3 0 0"); CommandController.parseCommand("exchangecards 3 0 0"); @@ -49,6 +52,10 @@ public void doCardExchange(){ } } + /** + * cheater do reinforcement + */ + public void doReinforcement(){ Country tempCountry = null; @@ -86,6 +93,9 @@ public void doReinforcement(){ } } + /** + * cheater do attack + */ public void doAttack(){ System.out.println(getStrategyName() + " bot playing reinforcement phase."); ArrayList ownedCountries = Gameplay.getInstance().getSelectedMap().getOwnedCountries(Gameplay.getInstance().getCurrentPlayer().getPlayerName()); @@ -138,6 +148,10 @@ public void doAttack(){ } } + /** + * cheater do fortification + */ + public void doFortification() { if(Gameplay.getInstance().getCurrentPlayer().isWinner()){ diff --git a/src/com/concordia/riskgame/model/Modules/Stratigies/Random.java b/src/com/concordia/riskgame/model/Modules/Stratigies/Random.java index 63d9951..3a73ef1 100644 --- a/src/com/concordia/riskgame/model/Modules/Stratigies/Random.java +++ b/src/com/concordia/riskgame/model/Modules/Stratigies/Random.java @@ -6,6 +6,10 @@ import java.io.Serializable; import java.util.*; +/** + * Random player's turn + */ + public class Random implements Strategy,Serializable { /** @@ -14,7 +18,6 @@ public class Random implements Strategy,Serializable { private static final long serialVersionUID = 1L; private String strategyName = "Random"; public String ANSI_CYAN = "\u001B[36m"; - //Gameplay gameplay = Gameplay.getInstance(); public String getColor() { @@ -27,9 +30,11 @@ public String getStrategyName() { return strategyName; } + /** + * Random player do card exchange + */ public void doCardExchange(){ - // gameplay =Gameplay.getInstance(); try { System.out.println("Bot Executing Command : " + "exchangecards 3 0 0"); CommandController.parseCommand("exchangecards 3 0 0"); @@ -47,6 +52,10 @@ public void doCardExchange(){ } } + /** + * Random player do reinforcement + */ + public void doReinforcement(){ int armies_available = Gameplay.getInstance().getCurrentPlayer().getArmyCount(); int randomIndex = generateRandomNumber(0, Gameplay.getInstance().getCurrentPlayer().getCountriesOwned().size()); @@ -69,6 +78,10 @@ public void doReinforcement(){ } } + /** + * Random player do attack + */ + public void doAttack(){ int numberOfTimesToAttack = generateRandomNumber(1, 10); System.out.println("\nBot will perform attack " + numberOfTimesToAttack + " times\n"); @@ -98,6 +111,10 @@ public void doAttack(){ } } + /** + * Random player do random attack + */ + public void performRandomAttack(){ Country attackCountry= null; Country defendCountry= null; @@ -150,7 +167,9 @@ public void performRandomAttack(){ } - + /** + * Random player do fortification + */ public void doFortification() { int armies_available = Gameplay.getInstance().getCurrentPlayer().getArmyCount(); @@ -176,6 +195,12 @@ public void doFortification() { } } + /** + * Generate a random number for reinforcement, attack, fortification + * @param min + * @param max + * @return a random integer + */ public int generateRandomNumber(int min, int max){ java.util.Random rand = new java.util.Random(); //using fully qualified name as my class name is also Random return rand.nextInt((max - min) ) + min ; diff --git a/src/com/concordia/riskgame/model/Modules/TournamentGame.java b/src/com/concordia/riskgame/model/Modules/TournamentGame.java index ea85153..38c7ba4 100644 --- a/src/com/concordia/riskgame/model/Modules/TournamentGame.java +++ b/src/com/concordia/riskgame/model/Modules/TournamentGame.java @@ -5,6 +5,10 @@ import com.concordia.riskgame.controller.CommandController; import com.concordia.riskgame.view.TournamentView; +/** + * This class is the tournament mode + */ + public class TournamentGame extends Thread{ private ArrayList mapFiles; private ArrayList playerStrategies; @@ -12,7 +16,13 @@ public class TournamentGame extends Thread{ private int numTurns; private ArrayList tournamentResult; - + /** + * Constructor, get command from command controller, parse everything from the command + * @param mapFiles map list + * @param playerStrategies player's strategy and also are player's name + * @param gameId number of game + * @param numTurns number of turn + */ public TournamentGame(ArrayList mapFiles, ArrayList playerStrategies, int gameId, int numTurns){ this.mapFiles = mapFiles; this.playerStrategies = playerStrategies; @@ -60,13 +70,16 @@ public void run(){ runTournament(); } + /** + * Run tournament mode in triple-nesting loop, outer loop control the number of game, second loop control the map, + * third loop control the number of turn. + */ public void runTournament(){ System.out.println("Starting tournament"); int n = gameId; while (n != 0) { for (String mapPath : mapFiles){ - // Gameplay gameplay = Gameplay.getInstance(); Gameplay.getInstance().setGameMode("Tournament"); CommandController.parseCommand("loadmap " + mapPath); Gameplay.getInstance().getPlayerQueue().clear(); @@ -97,6 +110,11 @@ public void runTournament(){ new TournamentView(tournamentResult, gameId, mapFiles.size()); } + /** + * Tournament mode initialize player. + * @param gameplay + */ + private void addPlayer(Gameplay gameplay) { for (String strategy : playerStrategies){ diff --git a/src/com/concordia/riskgame/view/TournamentView.java b/src/com/concordia/riskgame/view/TournamentView.java index d7aa2f4..7d45efe 100644 --- a/src/com/concordia/riskgame/view/TournamentView.java +++ b/src/com/concordia/riskgame/view/TournamentView.java @@ -18,6 +18,12 @@ public class TournamentView extends JFrame { private int gameCount; private int mapCount; + /** + * Tournament view constructor + * @param result + * @param gameCount + * @param mapCount + */ public TournamentView(ArrayList result, int gameCount, int mapCount){ this.resultList = result; @@ -45,6 +51,11 @@ public void initUI() } + /** + * Create tournament table + * @return + */ + public JScrollPane createTable() { String[] columnNames = new String[gameCount+1];