From 413997df49bbdfc30664953914d30fa95cf02ac1 Mon Sep 17 00:00:00 2001 From: AishwaryaShinde07 Date: Tue, 3 Oct 2023 23:38:51 -0400 Subject: [PATCH 01/18] Edit Continent and edit country cmds --- .../warzone/Commands/MapEditorCommands.java | 130 ++++++++--- .../app/warzone/Features/MapFeatures.java | 215 +++++++++++++++++- .../app/warzone/Utilities/Commands.java | 10 +- 3 files changed, 324 insertions(+), 31 deletions(-) diff --git a/src/main/java/project/app/warzone/Commands/MapEditorCommands.java b/src/main/java/project/app/warzone/Commands/MapEditorCommands.java index 4197147..4b4c571 100644 --- a/src/main/java/project/app/warzone/Commands/MapEditorCommands.java +++ b/src/main/java/project/app/warzone/Commands/MapEditorCommands.java @@ -28,6 +28,11 @@ public class MapEditorCommands { public PlayerCommands playerCommands; public PlayerFeatures playerFeatures; private final MapResouces mapResources; + + + + + public MapEditorCommands(MapFeatures mapFeatures, GameEngine gameEngine, PlayerFeatures playerFeatures, MapResouces mapResources){ this.mapFeatures = mapFeatures; @@ -50,6 +55,7 @@ public String loadMap(@ShellOption String p_filename){ } } + @ShellMethod(key= "showmap", value="Used to display map continents with terriotories and boundaries") public void showmap(){ String p_mapLocation=gameEngine.gameMap.getMapDirectory()+"/"+gameEngine.gameMap.get_USER_SELECTED_FILE()+".map"; //mac @@ -66,15 +72,16 @@ public void showmap(){ */ @ShellMethod(key= "editcontinent", prefix = "-", value="This is used to add or update continents") public String editcontinent(@ShellOption(value="a",defaultValue=ShellOption.NULL)String p_editcmd, @ShellOption(value="r",defaultValue=ShellOption.NULL) String p_editremovecmd){ - if(gameEngine.prevUserCommand==Commands.EDITMAP){ + Map listofContinents= new HashMap(); + +if(gameEngine.prevUserCommand == Commands.EDITMAP || gameEngine.prevUserCommand == Commands.ADDCONTINENT || gameEngine.prevUserCommand == Commands.REMOVECONTINENT){ //String l_addCmd = "-add"; Dictionary continentDict = new Hashtable(); - // if(p_editcmd != null && p_editcmd != ""){ + if(p_editcmd != null && p_editcmd != ""){ String[] editCmd= p_editcmd.split(","); System.out.println("length of string:"+editCmd.length); System.out.println("Inside edit continent"); - Map listofContinents= new HashMap(); int l_i=0; String commandToCheck= "-add"; @@ -93,8 +100,19 @@ public String editcontinent(@ShellOption(value="a",defaultValue=ShellOption.NULL System.out.println("listofContinents" +listofContinents); } + } + + try{ + mapFeatures.writeContinentsToFile(listofContinents,gameEngine); + + } + catch(IOException e){ + e.printStackTrace(); + + } + // if(gameEngine.gameMap.getListOfContinents() != null){ // List continentList =gameEngine.gameMap.getListOfContinents(); @@ -123,29 +141,32 @@ public String editcontinent(@ShellOption(value="a",defaultValue=ShellOption.NULL // return ""; // } - return " "; - + } else{ return("Invalid command. Please enter editmap command to edit the continent"); } - - - - // else{ - // return "You cannot edit map now. Please enter editmap cmd"; - // } + } + return " "; + } + @ShellMethod(key= "editcountry", prefix = "-", value="This is used to add continents") - public String editcountry(@ShellOption(value="a",defaultValue=ShellOption.NULL)String p_editcmd, @ShellOption(value="r",defaultValue=ShellOption.NULL) String p_editremovecmd){ - if(gameEngine.prevUserCommand==Commands.EDITMAP){ + public String editcountry(@ShellOption(value="a",defaultValue=ShellOption.NULL)String p_editcmd, @ShellOption(value="r",defaultValue=ShellOption.NULL) String p_editremovecmd) throws IOException{ + + //Dictionary countryDict = new Hashtable(); + + if(gameEngine.prevUserCommand==Commands.ADDCONTINENT|| gameEngine.prevUserCommand==Commands.REMOVECONTINENT || gameEngine.prevUserCommand==Commands.ADDCOUNTRY || gameEngine.prevUserCommand==Commands.REMOVECOUNTRY){ - Dictionary countryDict = new Hashtable(); + + if(p_editcmd != null && p_editcmd !=""){ + + Map listofCountries= new HashMap(); + String[] editCmd= p_editcmd.split(","); System.out.println("length of string:"+editCmd.length); System.out.println("Inside edit Country"); - Map listofCountries= new HashMap(); int l_i=0; String commandToCheck= "-add"; @@ -160,26 +181,82 @@ public String editcountry(@ShellOption(value="a",defaultValue=ShellOption.NULL)S else{ listofCountries.put(editCmd[l_i], editCmd[l_i+1]); l_i+=2; - //System.out.println("variable"+l_i); + System.out.println("listofCountries" +listofCountries); } + + } + try{ + mapFeatures.writeCountriesToFile(listofCountries,gameEngine); - + } + catch(IOException e){ + e.printStackTrace(); + + } + return "Countries addded succesfully"; + } + else{ + + List listofCountries = new ArrayList<>(); + String[] l_editremovecmd= p_editremovecmd.split(","); + System.out.println("length of string:"+l_editremovecmd.length); + System.out.println("Inside edit Country"); + int l_i=0; + + String commandToCheck= "-remove"; + + while(l_i< l_editremovecmd.length){ + + System.out.println(l_editremovecmd[l_i]+":"+commandToCheck); + if(l_editremovecmd[l_i].toString().equals(commandToCheck)){ + + continue; + } + else{ + listofCountries.add(l_editremovecmd[l_i]); - - - } - else{ - return("Invalid command. Please enter editmap command to edit the continent"); + + System.out.println("listofCountries" +listofCountries); + } + l_i++; + + } + try{ + mapFeatures.removeCountriesFromFile(listofCountries,gameEngine); + + } + catch(IOException e){ + e.printStackTrace(); + + } + return "Countries removed succesfully"; } + } + else{ + return "You cannnot add players at this stage.Please enter loadmap command first"; + } - return "You can edit countries here"; } + @ShellMethod(key= "editneighbor", value="This is used to add or update neighbor") - public String editneighbor(){ + public String editNeighbor(@ShellOption(value="a",defaultValue=ShellOption.NULL)String p_editcmd, @ShellOption(value="r",defaultValue=ShellOption.NULL) String p_editremovecmd) throws IOException{ + if(gameEngine.prevUserCommand==Commands.ADDCOUNTRY || gameEngine.prevUserCommand==Commands.REMOVECOUNTRY || gameEngine.prevUserCommand==Commands.ADDNEIGHBOUR || gameEngine.prevUserCommand==Commands.REMOVENEIGHBOUR){ + if(p_editcmd != null && p_editcmd !=""){ + + + } + + + + + + + } + return "You can edit neighbor here"; } @@ -190,13 +267,14 @@ public String editmap(@ShellOption String p_filename){ if(gameEngine.gameMap.fileExists(p_filename)){ System.out.println("One file found."); gameEngine.gameMap.set_USER_SELECTED_FILE(p_filename); - showmap(); + showmap(); // add check to see if file is proper return("Please use gameplayer -add command to add players in the game"); } else{ gameEngine.prevUserCommand=Commands.EDITMAP; //System.out.println("File not found."); - + gameEngine.gameMap.set_USER_SELECTED_FILE(p_filename); + gameEngine.gameMap.createNewMapFile(p_filename); // System.out.println("New File created successfully.."); System.out.println("\n"); diff --git a/src/main/java/project/app/warzone/Features/MapFeatures.java b/src/main/java/project/app/warzone/Features/MapFeatures.java index 53b46fa..cbade7e 100644 --- a/src/main/java/project/app/warzone/Features/MapFeatures.java +++ b/src/main/java/project/app/warzone/Features/MapFeatures.java @@ -1,22 +1,31 @@ package project.app.warzone.Features; import java.io.BufferedReader; -import java.io.File; +import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Component; import project.app.warzone.Model.Continent; +import project.app.warzone.Model.GameEngine; import project.app.warzone.Model.Map; -import project.app.warzone.Model.Node;; +import project.app.warzone.Model.Node; +import project.app.warzone.Utilities.MapResouces;; @Component public class MapFeatures { + public MapResouces mapResouces; + + public MapFeatures(MapResouces mapResouces){ + this.mapResouces = mapResouces; + + } + public Map readMap(String filename){ String l_line=""; @@ -256,4 +265,202 @@ public Map writeToMap( Map p_gameMap, String p_filename){ // } // } // } -} \ No newline at end of file + + /** + * @param listofContinents + * @param gameEngine + * @throws IOException + */ + public void writeContinentsToFile(java.util.Map listofContinents,GameEngine gameEngine) throws IOException{ + + for(String s : listofContinents.keySet()){ + System.out.println(s); + } + System.out.println(); + String l_mapLocation=gameEngine.gameMap.getMapDirectory()+"/"+gameEngine.gameMap.get_USER_SELECTED_FILE()+".map"; //mac + java.util.Map listOfContinentsResource = mapResouces.getAllContinents(); + + try(BufferedWriter writer = new BufferedWriter(new FileWriter(l_mapLocation, true))){ + + BufferedReader l_reader = new BufferedReader(new FileReader(l_mapLocation)); + //System.out.println(l_reader.readLine()); + //System.out.println(l_reader.readLine()); + List l_lineToWrite =new ArrayList(); + String l_line = l_reader.readLine(); + if(l_line == null){ + + System.out.println("l_mapLocation" +l_mapLocation); + l_lineToWrite.add("[continents]"); + } + else if(l_line == "[continents]"){ + l_line=l_reader.readLine(); + while(l_line != " "){ + l_line=l_reader.readLine(); + + } + + } + l_reader.close(); + for(String continentId : listofContinents.keySet()){ + + l_lineToWrite.add(listOfContinentsResource.get(Integer.parseInt(continentId))+" "+listofContinents.get(continentId)); + //System.out.println(listOfContinentsResource.get(Integer.parseInt(continentId))+" "+listofContinents.get(continentId)); + //l_lineToWrite.add(continentId; + } + + for(String line : l_lineToWrite){ + writer.append(line); + writer.newLine(); + writer.flush(); + } + writer.close(); + + + + }catch(IOException e){ + e.printStackTrace(); + } + } + + public void writeCountriesToFile(java.util.Map listofCountries,GameEngine gameEngine)throws IOException{ + String l_mapLocation=gameEngine.gameMap.getMapDirectory()+"/"+gameEngine.gameMap.get_USER_SELECTED_FILE()+".map"; //mac + + //java.util.Map listOfContinentsResource = mapResouces.getAllContinents(); + java.util.Map listOfCountriesResource = mapResouces.getAllCountries(); + List l_lineToWrite =new ArrayList(); + + try(BufferedReader l_reader = new BufferedReader(new FileReader(l_mapLocation));){ + + + String l_line = l_reader.readLine(); + // if(l_line == "[continents]"){ + // l_line=l_reader.readLine(); + // while(l_line != " "){ + // l_line=l_reader.readLine(); + // } + // } + boolean l_foundCountries =false; + while(l_line!=null && !l_line.toString().equals("[countries]") ){ + System.out.println(l_line); + System.out.println(l_line!="[countries]"); + l_line=l_reader.readLine(); + + } + if(l_line==null){ + l_lineToWrite.add(" "); + l_lineToWrite.add("[countries]"); + + + } + + else{ + l_line=l_reader.readLine(); + while(l_line != "" && l_line != null){ + l_line=l_reader.readLine(); + } + } + l_reader.close(); + + } + + + + catch(IOException e){ + e.printStackTrace(); + + + } + + try(BufferedWriter writer = new BufferedWriter(new FileWriter(l_mapLocation, true))){ + for(String countryId : listofCountries.keySet()){ + + l_lineToWrite.add(countryId+" "+listOfCountriesResource.get(Integer.parseInt(countryId))+" "+listofCountries.get(countryId)); + //+" "+listOfContinentsResource.get(continentId)); + //System.out.println(listOfContinentsResource.get(Integer.parseInt(continentId))+" "+listofContinents.get(continentId)); + System.out.println((listofCountries.get(countryId)+" "+listOfCountriesResource.get(Integer.parseInt(countryId))+" "+listofCountries.get(countryId))); + + } + + for(String line : l_lineToWrite){ + writer.append(line); + writer.newLine(); + writer.flush(); + } + writer.close(); + + } + catch(IOException e){ + e.printStackTrace(); + + } + + } + +public void removeCountriesFromFile(List listofCountries,GameEngine gameEngine)throws IOException{ + + String l_mapLocation=gameEngine.gameMap.getMapDirectory()+"/"+gameEngine.gameMap.get_USER_SELECTED_FILE()+".map"; //mac + java.util.Map listOfCountriesResource = mapResouces.getAllCountries(); + List l_lineToWrite =new ArrayList(); + + try(BufferedReader l_reader = new BufferedReader(new FileReader(l_mapLocation));){ + + + String l_line = l_reader.readLine(); + while(l_line!=null && !l_line.toString().equals("[countries]") ){ + System.out.println(l_line); + System.out.println(l_line!="[countries]"); + l_line=l_reader.readLine(); + + } + + l_line=l_reader.readLine(); + for( String countryId : listofCountries){ + + while(l_line.toString().contains(countryId)){ + l_line=l_reader.readLine(); + + } + + try(BufferedWriter writer = new BufferedWriter(new FileWriter(l_mapLocation, true))){ + + + for(String line : l_lineToWrite){ + writer.append(line); + writer.newLine(); + writer.flush(); + } + writer.close(); + + } + catch(IOException e){ + e.printStackTrace(); + + } + + + + l_reader.close(); + + } + + } + + + + catch(IOException e){ + e.printStackTrace(); + + + } + + + + +} + +public void writeNeighborToFile(java.util.Map listofCountries,GameEngine gameEngine)throws IOException{ + + + } +} + diff --git a/src/main/java/project/app/warzone/Utilities/Commands.java b/src/main/java/project/app/warzone/Utilities/Commands.java index 9bd42fc..bd9a7d8 100644 --- a/src/main/java/project/app/warzone/Utilities/Commands.java +++ b/src/main/java/project/app/warzone/Utilities/Commands.java @@ -5,7 +5,15 @@ public enum Commands { SHOWMAP, ADDPLAYER, REMOVEPLAYER, - EDITMAP + EDITMAP, + ADDCONTINENT, + REMOVECONTINENT, + ADDCOUNTRY, + REMOVECOUNTRY, + ADDNEIGHBOUR, + REMOVENEIGHBOUR + + } \ No newline at end of file From 1881ea56b0532aea63c0095ef9b2bace9c749b1f Mon Sep 17 00:00:00 2001 From: Ro18 Date: Wed, 4 Oct 2023 00:11:45 -0400 Subject: [PATCH 02/18] testing javadoc actions --- .github/workflows/actions.yml | 8 ++++ .../warzone/Commands/MapEditorCommands.java | 2 +- .../app/warzone/Features/MapFeatures.java | 3 ++ .../java/project/app/warzone/Model/Order.java | 46 ++++++++++++++++--- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 476be34..e224464 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -41,6 +41,14 @@ jobs: - name: Test Report uses: mikepenz/action-junit-report@v4 if: success() || failure() + - name: Deploy JavaDoc 🚀 + uses: MathieuSoysal/Javadoc-publisher.yml@v2.4.0 + with: + javadoc-branch: javadoc + java-version: 17 + target-folder: javadoc # url will be https://.github.io//javadoc, This can be left as nothing to generate javadocs in the root folder. + project: maven + # subdirectories: moduleA moduleB #for subdirectories support, needs to be run with custom command #./mvnw test -Dtest=WarzoneApplicationTest diff --git a/src/main/java/project/app/warzone/Commands/MapEditorCommands.java b/src/main/java/project/app/warzone/Commands/MapEditorCommands.java index b8e7437..ff8ce87 100644 --- a/src/main/java/project/app/warzone/Commands/MapEditorCommands.java +++ b/src/main/java/project/app/warzone/Commands/MapEditorCommands.java @@ -57,7 +57,7 @@ public String showmap(){ gameEngine.gameMap = mapFeatures.readMap(p_mapLocation); Boolean l_result = mapFeatures.validateEntireGraph(gameEngine); if(!l_result){ - return("PLease try with some other map"); + return("This map is not valid.Please try with some other map"); } else{ return("You can now proceed to add gameplayers"); diff --git a/src/main/java/project/app/warzone/Features/MapFeatures.java b/src/main/java/project/app/warzone/Features/MapFeatures.java index 00f70c6..b9b06d7 100644 --- a/src/main/java/project/app/warzone/Features/MapFeatures.java +++ b/src/main/java/project/app/warzone/Features/MapFeatures.java @@ -263,6 +263,9 @@ public Boolean validateEntireGraph(GameEngine gameEngine){ public boolean validateSubGraph(Continent con, List l_listOfNodes,java.util.Map l_visitedList){ List l_nodesOfContinent = l_listOfNodes.stream().filter(c-> c.getData().getContinent().equals(con)).toList(); + if(l_nodesOfContinent.size() == 0){ + return false; + } l_visitedList =validateByNodes(l_nodesOfContinent,l_visitedList); for( Node n : l_nodesOfContinent){ if(l_visitedList.containsKey(n) && !l_visitedList.get(n)){ diff --git a/src/main/java/project/app/warzone/Model/Order.java b/src/main/java/project/app/warzone/Model/Order.java index bc36a10..46d7821 100644 --- a/src/main/java/project/app/warzone/Model/Order.java +++ b/src/main/java/project/app/warzone/Model/Order.java @@ -1,14 +1,46 @@ package project.app.warzone.Model; +/** + * This class is used to prototye the orders for a player in the game. + */ public class Order { - String d_orderType; - int d_numberOfArmies; - Player PlayerObject; - // Country CountryObject; - void execute() { - // Order will get execute here + /** + * The number of armies to deploy. + */ + private String d_orderType = "DEPLOY"; + /** + * The number of armies and territory to deploy the armies to. + */ + private int d_numberOfArmies; + private Territory d_Territory; + + public void setL_numberOfArmies(int p_numArmies) { + this.d_numberOfArmies = p_numArmies; + } + + public void setL_territory(Territory p_territory) { + this.d_Territory = p_territory; + } + + public int getL_numberOfArmies() { + return this.d_numberOfArmies; + } + + public Territory getL_territory() { + return this.d_Territory; } + /** + * This method executes the order and deploys the armies on the target country. + * @return A string containing whether the order was successfully executed or not. + */ + public String execute() { + if (this.d_orderType == "DEPLOY") { + this.d_Territory.setNumberOfArmies(this.d_Territory.getNumberOfArmies() + this.d_numberOfArmies); + return "Deployed armies successfully"; -} + } + return "Invalid order"; + } +} \ No newline at end of file From 8aabbdbf78ae72ab991e4f834a26b0a337438290 Mon Sep 17 00:00:00 2001 From: Ro18 Date: Wed, 4 Oct 2023 00:13:24 -0400 Subject: [PATCH 03/18] javadoc file correcttion --- .github/workflows/actions.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index e224464..c8ac5ba 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -41,9 +41,9 @@ jobs: - name: Test Report uses: mikepenz/action-junit-report@v4 if: success() || failure() - - name: Deploy JavaDoc 🚀 + - name: Deploy JavaDoc uses: MathieuSoysal/Javadoc-publisher.yml@v2.4.0 - with: + with: javadoc-branch: javadoc java-version: 17 target-folder: javadoc # url will be https://.github.io//javadoc, This can be left as nothing to generate javadocs in the root folder. From eebacc7c762a80270acfbb7425be0f2e767e1ec4 Mon Sep 17 00:00:00 2001 From: Ro18 Date: Wed, 4 Oct 2023 00:51:41 -0400 Subject: [PATCH 04/18] code and java doc --- .../warzone/Commands/MapEditorCommands.java | 41 ++++--- .../app/warzone/Commands/PlayerCommands.java | 19 ++++ .../app/warzone/Features/MapFeatures.java | 101 ++++++------------ .../app/warzone/Features/PlayerFeatures.java | 93 ++++++++-------- 4 files changed, 130 insertions(+), 124 deletions(-) diff --git a/src/main/java/project/app/warzone/Commands/MapEditorCommands.java b/src/main/java/project/app/warzone/Commands/MapEditorCommands.java index ff8ce87..ff8afff 100644 --- a/src/main/java/project/app/warzone/Commands/MapEditorCommands.java +++ b/src/main/java/project/app/warzone/Commands/MapEditorCommands.java @@ -1,18 +1,15 @@ package project.app.warzone.Commands; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; + import java.util.Dictionary; import java.util.HashMap; import java.util.Hashtable; -import java.util.List; + import java.util.Map; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; import project.app.warzone.Features.MapFeatures; -import project.app.warzone.Model.Continent; import project.app.warzone.Features.PlayerFeatures; import project.app.warzone.Model.GameEngine; import project.app.warzone.Utilities.Commands; @@ -37,6 +34,11 @@ public MapEditorCommands(MapFeatures mapFeatures, GameEngine gameEngine, PlayerF } + + /** + * @param p_filename + * @return String + */ @ShellMethod(key= "loadmap", value="Player can create or open an existing map") public String loadMap(@ShellOption String p_filename){ gameEngine.prevUserCommand=Commands.LOADMAP; @@ -50,6 +52,11 @@ public String loadMap(@ShellOption String p_filename){ } } + + + /** + * @return String + */ @ShellMethod(key= "showmap", value="Used to display map continents with terriotories and boundaries") public String showmap(){ String p_mapLocation=gameEngine.gameMap.getMapDirectory()+"\\"+gameEngine.gameMap.get_USER_SELECTED_FILE()+".map"; @@ -144,6 +151,7 @@ public String editcontinent(@ShellOption(value="a",defaultValue=ShellOption.NULL // } } + @ShellMethod(key= "editcountry", prefix = "-", value="This is used to add continents") public String editcountry(@ShellOption(value="a",defaultValue=ShellOption.NULL)String p_editcmd, @ShellOption(value="r",defaultValue=ShellOption.NULL) String p_editremovecmd){ if(gameEngine.prevUserCommand==Commands.EDITMAP){ @@ -192,6 +200,8 @@ public String editcountry(@ShellOption(value="a",defaultValue=ShellOption.NULL)S } + + // @ShellMethod(key= "editcontinent", value="This is used to add or update continents") // public String editcontinent(){ // return "You can edit continents here"; @@ -202,17 +212,27 @@ public String editcountry(@ShellOption(value="a",defaultValue=ShellOption.NULL)S // } + /** + * @return String + */ @ShellMethod(key= "editcountry", value="This is used to add or update countries") public String editcountry(){ return "You can edit countries here"; } + + /** + * @return String + */ @ShellMethod(key= "editneighbor", value="This is used to add or update neighbor") public String editneighbor(){ return "You can edit neighbor here"; } + /** + * @return String + */ @ShellMethod(key= "editmap", value="This is used to add or create map") public String editmap(@ShellOption String p_filename){ @@ -223,14 +243,11 @@ public String editmap(@ShellOption String p_filename){ return("Please use gameplayer -add command to add players in the game"); } else{ - gameEngine.prevUserCommand=Commands.EDITMAP; - //System.out.println("File not found."); - + gameEngine.prevUserCommand=Commands.EDITMAP; gameEngine.gameMap.createNewMapFile(p_filename); - // System.out.println("New File created successfully.."); - System.out.println("\n"); + System.out.println("\n"); System.out.println("Choose one of the below commands to proceed:\n 1.editcontinent 2.editcountry 3.editneighbor"); - System.out.println("\n"); + System.out.println("\n"); System.out.println("Continet list:::: Select the continents you need to add"); System.out.println("----------------------------------------------------------"); mapResources.printMapDetails(mapResources.getAllContinents()); @@ -241,7 +258,7 @@ public String editmap(@ShellOption String p_filename){ mapResources.printMapDetails(mapResources.getAllCountries()); System.out.println("\n"); - return "Please select the add or remove command"; + return "Please select the add or remove command"; diff --git a/src/main/java/project/app/warzone/Commands/PlayerCommands.java b/src/main/java/project/app/warzone/Commands/PlayerCommands.java index 24c10e8..284e70e 100644 --- a/src/main/java/project/app/warzone/Commands/PlayerCommands.java +++ b/src/main/java/project/app/warzone/Commands/PlayerCommands.java @@ -22,6 +22,16 @@ public PlayerCommands(GameEngine gameEngine,PlayerFeatures playerFeatures){ } + + /** + * @param @ShellOption + * @param defaultValue=ShellOption.NULL + * @param p_playerNameOne + * @param @ShellOption(value="r" + * @param defaultValue=ShellOption.NULL + * @param p_playerNameTwo + * @return String + */ @ShellMethod(key= "gameplayer", prefix = "-", value="Player can create or remove a player") public String gamePlayerAdd(@ShellOption(value="a",defaultValue=ShellOption.NULL, arity = 10 ) String p_playerNameOne,@ShellOption(value="r", defaultValue=ShellOption.NULL, arity=10) String p_playerNameTwo){ @@ -74,6 +84,10 @@ public String gamePlayerAdd(@ShellOption(value="a",defaultValue=ShellOption.NULL + + /** + * @return String + */ @ShellMethod(key= "assigncountries", value="This is used to assign countries to players randomly") public String assigncountries(){ @@ -85,6 +99,11 @@ public String assigncountries(){ } + + + /** + * @return String + */ @ShellMethod(key= "showstats", value="Displays players armies and other details") public String showStats(){ System.out.println("========================================"); diff --git a/src/main/java/project/app/warzone/Features/MapFeatures.java b/src/main/java/project/app/warzone/Features/MapFeatures.java index b9b06d7..f4057e6 100644 --- a/src/main/java/project/app/warzone/Features/MapFeatures.java +++ b/src/main/java/project/app/warzone/Features/MapFeatures.java @@ -1,10 +1,8 @@ package project.app.warzone.Features; import java.io.BufferedReader; -import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -19,6 +17,11 @@ @Component public class MapFeatures { + + /** + * @param filename + * @return Map + */ public Map readMap(String filename){ String l_line=""; @@ -86,30 +89,8 @@ public Map readMap(String filename){ } - - // nodesList = gameMap.getNodes(); - - // for(Node c : nodesList){ - // System.out.print(c.getData().getTerritoryName()+":"); - // String borderString =""; - // List listOfBorders = c.getBorders(); - // for(Node border : listOfBorders ){ - - // borderString+=border.getData().getTerritoryName()+"->"; - - // } - // borderString=borderString.substring(0,borderString.length()-2); - // System.out.println(borderString); - - - // } - } - - - - } reader.close(); @@ -130,26 +111,10 @@ public Map readMap(String filename){ } - public void createMap(){ - - } - - // public void saveChangesToFile(String p_filename) throws IOException{ - // BufferedReader reader = new BufferedReader(new FileReader(p_filename)); - // StringBuilder content = new StringBuilder(); - // String line; - // while ((line = reader.readLine())) { - - // line = line.replace("old text", "new text"); - // content.append(line).append("\n"); - // } - - // reader.close(); - - // } - - + /** + * @param gameMap + */ public void printMap(Map gameMap){ System.out.println("------ Map ------"); @@ -183,30 +148,13 @@ public void printMap(Map gameMap){ } } - /** - * @param p_gameMap - * @param p_filename - * @return - */ - //public Map writeToMap( Map p_gameMap, String p_filename){ - // public Map writeToMap( Map p_gameMap, String p_filename){ - - // BufferedReader l_reader = new BufferedReader(new FileReader(filename)); - // String l_continents = "[continents]"; - // String l_countries = "[countries]"; - // String l_borders="[borders]"; - - // if(l_reader.readLine() == ""){ - // System.out.println("Writing to an empty map file"); - // Files.writeString(filename, l_continents); - - - // } - - //} - - + + /** + * @param p_allNodes + * @param l_visitedList + * @return Map + */ public java.util.Map validateByNodes(List p_allNodes, java.util.Map l_visitedList){ for( Node l_currentNode : p_allNodes){ @@ -224,6 +172,12 @@ public java.util.Map validateByNodes(List p_allNodes, java.u } + + + /** + * @param gameEngine + * @return Boolean + */ public Boolean validateEntireGraph(GameEngine gameEngine){ @@ -260,6 +214,14 @@ public Boolean validateEntireGraph(GameEngine gameEngine){ } + + + /** + * @param con + * @param l_listOfNodes + * @param l_visitedList + * @return boolean + */ public boolean validateSubGraph(Continent con, List l_listOfNodes,java.util.Map l_visitedList){ List l_nodesOfContinent = l_listOfNodes.stream().filter(c-> c.getData().getContinent().equals(con)).toList(); @@ -280,6 +242,13 @@ public boolean validateSubGraph(Continent con, List l_listOfNodes,java.uti } + + + /** + * @param currentCountry + * @param l_visitedList + * @return Map + */ private java.util.Map depthFirstSearch(Node currentCountry, java.util.Map l_visitedList){ l_visitedList.put(currentCountry,true); diff --git a/src/main/java/project/app/warzone/Features/PlayerFeatures.java b/src/main/java/project/app/warzone/Features/PlayerFeatures.java index 999bde2..b7e3708 100644 --- a/src/main/java/project/app/warzone/Features/PlayerFeatures.java +++ b/src/main/java/project/app/warzone/Features/PlayerFeatures.java @@ -1,9 +1,6 @@ package project.app.warzone.Features; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Random; @@ -16,6 +13,11 @@ @Component public class PlayerFeatures { + + + /** + * @param allPlayers + */ public void showAllAssignments(List allPlayers){ for( Player p : allPlayers){ @@ -33,6 +35,11 @@ public void showAllAssignments(List allPlayers){ } + + + /** + * @param p_gameEngine + */ public void assignCountries(GameEngine p_gameEngine){ Random l_random = new Random(); @@ -42,64 +49,64 @@ public void assignCountries(GameEngine p_gameEngine){ int randomCountry = l_random.nextInt(p_gameEngine.gameMap.getNodes().size()); - // Map playerIds = new HashMap(); - - // for(int l_i=1;l_i<=p_gameEngine.getPlayers().size();l_i++){ - // playerIds.put(l_i, false); - // } - int randomId = l_random.nextInt(p_gameEngine.getPlayers().size()+1); - // for(int i=0 ; i< p_gameEngine.getPlayers().size() ;i++){ - // while(playerIds.get(randomId) == true){ - // randomId = l_random.nextInt(p_gameEngine.getPlayers().size()+1); - - // } - // p.setL_playerid(randomId); - // playerIds.put(randomId, true); - - // } - while(p_gameEngine.gameMap.getNodes().get(randomCountry).getData().getOwnerId() != 0){ randomCountry = l_random.nextInt(p_gameEngine.gameMap.getNodes().size()+1); - } - - - - - - + } p.setTerritories(p_gameEngine.gameMap.getNodes().get(randomCountry).getData()); } } + + + + /** + * @param p_playerName + * @param gameEngine + */ public void addPlayers(String p_playerName, GameEngine gameEngine){ - int l_size= gameEngine.getPlayers().size(); + int l_playerCount = gameEngine.getPlayers().size(); + Player player= new Player(l_playerCount++,p_playerName); + gameEngine.d_playersList.add(player); + + } + public void setPlayerIds(GameEngine gameEngine){ - //for(String l_playerObjects : p_playerNames){ - Player player= new Player(l_size+1,p_playerName); - gameEngine.d_playersList.add(player); + int i=1; + for(Player l_player : gameEngine.getPlayers()){ + + l_player.setL_playerid(i); + } - //} } + + /** + * @param p_playerName + * @param gameEngine + */ public void removePlayers(String p_playerName, GameEngine gameEngine){ List playerList = gameEngine.getPlayers(); - //for(String l_player: p_playerNames){ - Optional l_playerToRemove= playerList.stream().filter(c->c.getL_playername().equals(p_playerName)).findFirst(); - playerList.remove(l_playerToRemove.get()); + Optional l_playerToRemove= playerList.stream().filter(c->c.getL_playername().equals(p_playerName)).findFirst(); + playerList.remove(l_playerToRemove.get()); + setPlayerIds(gameEngine); - //} } + + + /** + * @param gameEngine + */ public void printAllPlayers(GameEngine gameEngine){ System.out.println("Final players of the game are:"); List players = gameEngine.getPlayers(); @@ -108,17 +115,11 @@ public void printAllPlayers(GameEngine gameEngine){ } } - // public void initializeArmies(GameEngine gameEngine){ - - // List gamePlayers = new ArrayList<>(); - // int noOfPlayers = gamePlayers.size(); - - // for(Player p : gamePlayers){ - // p.initReinforcementArmies(3) - // } - - // } - + + + /** + * @param gameengine + */ public void showStats(GameEngine gameengine){ List listOfPlayers = gameengine.getPlayers(); for(Player p : listOfPlayers){ From ddf2c0fd956e3d1d8f418831b650de7e274cfa44 Mon Sep 17 00:00:00 2001 From: AishwaryaShinde07 Date: Wed, 4 Oct 2023 01:05:25 -0400 Subject: [PATCH 05/18] update fix --- .../warzone/Commands/MapEditorCommands.java | 48 +++++++++---- .../app/warzone/Features/MapFeatures.java | 67 +++++++++++++++++-- 2 files changed, 99 insertions(+), 16 deletions(-) diff --git a/src/main/java/project/app/warzone/Commands/MapEditorCommands.java b/src/main/java/project/app/warzone/Commands/MapEditorCommands.java index 4b4c571..8ac5e9f 100644 --- a/src/main/java/project/app/warzone/Commands/MapEditorCommands.java +++ b/src/main/java/project/app/warzone/Commands/MapEditorCommands.java @@ -79,6 +79,7 @@ public String editcontinent(@ShellOption(value="a",defaultValue=ShellOption.NULL Dictionary continentDict = new Hashtable(); if(p_editcmd != null && p_editcmd != ""){ + gameEngine.prevUserCommand=Commands.ADDCONTINENT; String[] editCmd= p_editcmd.split(","); System.out.println("length of string:"+editCmd.length); System.out.println("Inside edit continent"); @@ -158,11 +159,10 @@ public String editcountry(@ShellOption(value="a",defaultValue=ShellOption.NULL)S //Dictionary countryDict = new Hashtable(); if(gameEngine.prevUserCommand==Commands.ADDCONTINENT|| gameEngine.prevUserCommand==Commands.REMOVECONTINENT || gameEngine.prevUserCommand==Commands.ADDCOUNTRY || gameEngine.prevUserCommand==Commands.REMOVECOUNTRY){ - if(p_editcmd != null && p_editcmd !=""){ - - Map listofCountries= new HashMap(); + Map listofCountries= new HashMap(); + gameEngine.prevUserCommand=Commands.ADDCOUNTRY; String[] editCmd= p_editcmd.split(","); System.out.println("length of string:"+editCmd.length); @@ -235,27 +235,51 @@ public String editcountry(@ShellOption(value="a",defaultValue=ShellOption.NULL)S } } else{ - return "You cannnot add players at this stage.Please enter loadmap command first"; + return "You cannnot add country"; } } - @ShellMethod(key= "editneighbor", value="This is used to add or update neighbor") + @ShellMethod(key= "editneighbor",prefix = "-", value="This is used to add or update neighbor") public String editNeighbor(@ShellOption(value="a",defaultValue=ShellOption.NULL)String p_editcmd, @ShellOption(value="r",defaultValue=ShellOption.NULL) String p_editremovecmd) throws IOException{ if(gameEngine.prevUserCommand==Commands.ADDCOUNTRY || gameEngine.prevUserCommand==Commands.REMOVECOUNTRY || gameEngine.prevUserCommand==Commands.ADDNEIGHBOUR || gameEngine.prevUserCommand==Commands.REMOVENEIGHBOUR){ - if(p_editcmd != null && p_editcmd !=""){ - - - } - + //if(p_editcmd != null && p_editcmd !=""){ + Map listofCountries= new HashMap(); + String[] editCmd= p_editcmd.split(","); + System.out.println("length of string:"+editCmd.length); + System.out.println("Inside edit Country"); + int l_i=0; - + String commandToCheck= "-add"; + while(l_i< editCmd.length){ + System.out.println(editCmd[l_i]+":"+commandToCheck); + if(editCmd[l_i].toString().equals(commandToCheck)){ + l_i++; + continue; + + } + else{ + listofCountries.put(editCmd[l_i], editCmd[l_i+1]); + l_i+=2; + System.out.println("listofCountries" +listofCountries); + } + + } + try{ + mapFeatures.writeCountriesNeighborToFile(listofCountries,gameEngine); - } + } + catch(IOException e){ + e.printStackTrace(); + + } + return "Country borders addded succesfully"; + } + // } return "You can edit neighbor here"; diff --git a/src/main/java/project/app/warzone/Features/MapFeatures.java b/src/main/java/project/app/warzone/Features/MapFeatures.java index cbade7e..0521b12 100644 --- a/src/main/java/project/app/warzone/Features/MapFeatures.java +++ b/src/main/java/project/app/warzone/Features/MapFeatures.java @@ -325,7 +325,6 @@ else if(l_line == "[continents]"){ public void writeCountriesToFile(java.util.Map listofCountries,GameEngine gameEngine)throws IOException{ String l_mapLocation=gameEngine.gameMap.getMapDirectory()+"/"+gameEngine.gameMap.get_USER_SELECTED_FILE()+".map"; //mac - //java.util.Map listOfContinentsResource = mapResouces.getAllContinents(); java.util.Map listOfCountriesResource = mapResouces.getAllCountries(); List l_lineToWrite =new ArrayList(); @@ -339,7 +338,7 @@ public void writeCountriesToFile(java.util.Map listofCountries,G // l_line=l_reader.readLine(); // } // } - boolean l_foundCountries =false; + //boolean l_foundCountries =false; while(l_line!=null && !l_line.toString().equals("[countries]") ){ System.out.println(l_line); System.out.println(l_line!="[countries]"); @@ -375,8 +374,6 @@ public void writeCountriesToFile(java.util.Map listofCountries,G for(String countryId : listofCountries.keySet()){ l_lineToWrite.add(countryId+" "+listOfCountriesResource.get(Integer.parseInt(countryId))+" "+listofCountries.get(countryId)); - //+" "+listOfContinentsResource.get(continentId)); - //System.out.println(listOfContinentsResource.get(Integer.parseInt(continentId))+" "+listofContinents.get(continentId)); System.out.println((listofCountries.get(countryId)+" "+listOfCountriesResource.get(Integer.parseInt(countryId))+" "+listofCountries.get(countryId))); } @@ -396,6 +393,67 @@ public void writeCountriesToFile(java.util.Map listofCountries,G } + public void writeCountriesNeighborToFile(java.util.Map listofCountries,GameEngine gameEngine)throws IOException{ + + String l_mapLocation=gameEngine.gameMap.getMapDirectory()+"/"+gameEngine.gameMap.get_USER_SELECTED_FILE()+".map"; //mac + +//java.util.Map listOfCountriesResource = mapResouces.getAllCountries(); + List l_lineToWrite =new ArrayList(); + + try(BufferedReader l_reader = new BufferedReader(new FileReader(l_mapLocation));){ + + + String l_line = l_reader.readLine(); + while(l_line!=null && !l_line.toString().equals("[borders]") ){ + System.out.println(l_line); + System.out.println(l_line!="[borders]"); + l_line=l_reader.readLine(); + + } + if(l_line==null){ + l_lineToWrite.add(" "); + l_lineToWrite.add("[borders]"); + + + } + + else{ + l_line=l_reader.readLine(); + while(l_line != "" && l_line != null){ + l_line=l_reader.readLine(); + } + } + l_reader.close(); + + } + + catch(IOException e){ + e.printStackTrace(); + + + } + + try(BufferedWriter writer = new BufferedWriter(new FileWriter(l_mapLocation, true))){ + for(String countryId : listofCountries.keySet()){ + + l_lineToWrite.add(countryId+" "+listofCountries.get(countryId)); + System.out.println(countryId+" "+listofCountries.get(countryId)); + } + for(String line : l_lineToWrite){ + writer.append(line); + writer.newLine(); + writer.flush(); + } + writer.close(); + + } + catch(IOException e){ + e.printStackTrace(); + + } + +} + public void removeCountriesFromFile(List listofCountries,GameEngine gameEngine)throws IOException{ String l_mapLocation=gameEngine.gameMap.getMapDirectory()+"/"+gameEngine.gameMap.get_USER_SELECTED_FILE()+".map"; //mac @@ -426,6 +484,7 @@ public void removeCountriesFromFile(List listofCountries,GameEngine game for(String line : l_lineToWrite){ writer.append(line); + writer.newLine(); writer.flush(); } From 5b635ed140e3c5013981058e7b49833e63664fb4 Mon Sep 17 00:00:00 2001 From: Ro18 Date: Wed, 4 Oct 2023 01:07:20 -0400 Subject: [PATCH 06/18] updated pom --- pom.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pom.xml b/pom.xml index d35198d..2873390 100644 --- a/pom.xml +++ b/pom.xml @@ -38,13 +38,6 @@ org.springframework.boot spring-boot-starter-test test - - - junit - junit - 4.13.2 - test -