Skip to content

Commit

Permalink
Merge pull request #40 from ro18/Ash-build3
Browse files Browse the repository at this point in the history
Ash build3
  • Loading branch information
ro18 authored Nov 30, 2023
2 parents 18a12df + a3b2cbb commit 2eea995
Show file tree
Hide file tree
Showing 18 changed files with 405 additions and 100 deletions.
55 changes: 45 additions & 10 deletions src/main/java/project/app/warzone/Commands/MapEditorCommands.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package project.app.warzone.Commands;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.springframework.shell.standard.ShellComponent;
Expand All @@ -7,7 +10,10 @@

import project.app.warzone.Features.MapFeatures;
import project.app.warzone.Features.PlayerFeatures;
import project.app.warzone.Model.ConquestFileReader;
import project.app.warzone.Model.GameEngine;
import project.app.warzone.Model.Map;
import project.app.warzone.Model.MapFeatureAdapter;
import project.app.warzone.Utilities.Commands;
import project.app.warzone.Utilities.MapResources;

Expand Down Expand Up @@ -53,22 +59,51 @@ public void loadMap(@ShellOption String p_filename) {
* Display the game map
*
* @return String returns message about the map validation
* @throws IOException
*/
@ShellMethod(key = "showmap", value = "Used to display map continents with terriotories and boundaries")
public void showmap() {
d_gameEngine.getGamePhase().showMap();
}
String p_mapLocation = d_gameEngine.gameMap.getMapDirectory() + "/"
+ d_gameEngine.gameMap.get_USER_SELECTED_FILE() + ".map";
String l_line="";

try{
BufferedReader reader = new BufferedReader(new FileReader(p_mapLocation));
MapFeatures mapFeatures= MapFeatures.getInstance();
l_line=reader.readLine();
if(l_line.equals("[Map]")){
mapFeatures = new MapFeatureAdapter(new ConquestFileReader());
// newmMapFeatures.readMap(p_mapLocation);
}
else{
mapFeatures = MapFeatures.getInstance();
// mapFeatures.readMap(p_mapLocation);
}




d_gameEngine.getGamePhase().showMap(mapFeatures);

/**
* @return Map
*/
public project.app.warzone.Model.Map returnMap() {
String p_mapLocation = d_gameEngine.gameMap.getMapDirectory() + "/"
+ d_gameEngine.gameMap.get_USER_SELECTED_FILE() + ".map";
dMap = dMapFeatures.readMap(p_mapLocation);
return dMap;
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found----");
}
catch (IOException e) {
System.out.println("IOException----");
}
}

// public project.app.warzone.Model.Map returnMap() {
// String p_mapLocation = d_gameEngine.gameMap.getMapDirectory() + "/"
// + d_gameEngine.gameMap.get_USER_SELECTED_FILE() + ".map";



// dMap = dMapFeatures.readMap(p_mapLocation);
// return dMap;
// }

/**
* command for editing continent
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/project/app/warzone/Features/MapFeatures.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ public MapFeatures(MapResources mapResouces){
MapFeatures.mapResouces = mapResouces;

}

//Empty Constructor
public MapFeatures(){

}
/**
* used for getting instance
* @return MapFeatures
*/
public static synchronized MapFeatures getInstance()
{
if (d_singleInstance == null)
d_singleInstance = new MapFeatures(mapResouces);
d_singleInstance = new MapFeatures(new MapResources());

return d_singleInstance;
}
Expand All @@ -61,6 +66,7 @@ public static synchronized MapFeatures getInstance()
* @return Map returns gamemap
*/
public Map readMap(String filename){
System.out.println("Inside Domination::MapFeatures: readMap()");
LogObject l_logObject = new LogObject();
l_logObject.setD_command("showmap");
l_logEntryBuffer.addObserver(this);
Expand Down Expand Up @@ -165,7 +171,7 @@ public Map readMap(String filename){
* @param gameMap used for storing game map
*/
public void printMap(Map gameMap){

System.out.println("------Domination Map ------");
System.out.println("------ Map ------");
System.out.println();
String continent ="";
Expand Down Expand Up @@ -230,7 +236,7 @@ public java.util.Map<Node,Boolean> validateByNodes(List<Node> p_allNodes, java.u
* @param gameEngine storing gameEngine
* @return Boolean returns the status of validating
*/
public Boolean validateEntireGraph(GameEngine gameEngine){
public boolean validateEntireGraph(GameEngine gameEngine){


System.out.println();
Expand Down Expand Up @@ -270,7 +276,7 @@ public Boolean validateEntireGraph(GameEngine gameEngine){
public boolean validateSubGraph(Continent con, List<Node> l_listOfNodes,java.util.Map<Node,Boolean> l_visitedList){

List<Node> l_nodesOfContinent = l_listOfNodes.stream().filter(c-> c.getData().getContinent().equals(con)).toList();
if(l_nodesOfContinent.size() == 0){
if(l_nodesOfContinent.isEmpty()){
return false;
}
l_visitedList =validateByNodes(l_nodesOfContinent,l_visitedList);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/project/app/warzone/Model/AttackOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ public void Negotiate(Player p_playerToNegotiate,Player currentPlayer)
public void update(java.util.Observable p_obj, Object p_arg) {
LogObject l_logObject = (LogObject) p_arg;
if (p_arg instanceof LogObject) {
try {
BufferedWriter l_writer = new BufferedWriter(
new FileWriter(System.getProperty("logFileLocation"), true));
try (BufferedWriter l_writer = new BufferedWriter(
new FileWriter(System.getProperty("logFileLocation"), true))){
l_writer.newLine();
l_writer.append(LogObject.d_logLevel + " " + l_logObject.d_command + "\n" + "Time: " + l_logObject.d_timestamp + "\n" + "Status: "
+ l_logObject.d_statusCode + "\n" + "Description: " + l_logObject.d_message);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/project/app/warzone/Model/Cards.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ public String getCardType() {
public void setCardType(String cardType) {
cardType = cardType.toUpperCase();

if (cardType.equals("BOMB") || cardType.equals("REINFORCEMENT") || cardType.equals("BLOCKADE") || cardType.equals("AIRLIFT") || cardType.equals("NEGOTIATE")) {
if (isValidCardType(cardType)) {
this.cardType = cardType;
} else {
System.out.println("Invalid card type");
}
}
public boolean isValidCardType(String cardType) {
return List.of("BOMB", "REINFORCEMENT", "BLOCKADE", "AIRLIFT", "NEGOTIATE").contains(cardType);
}

}
166 changes: 166 additions & 0 deletions src/main/java/project/app/warzone/Model/ConquestFileReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package project.app.warzone.Model;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observer;

import project.app.warzone.Utilities.LogObject;

public class ConquestFileReader {
private LogEntryBuffer l_logEntryBuffer = new LogEntryBuffer();

//Empty Constructor
public ConquestFileReader(){

}

public Map readMap(String filename){
// System.out.println("Inside ConquestFileReader: readMap()");
// LogObject l_logObject = new LogObject();
// l_logObject.setD_command("showmap");
// l_logEntryBuffer.addObserver((Observer) this);

String l_line="";
List<Continent> continentsList = new ArrayList<>();
List<Node> nodesList = new ArrayList<>();
Map gameMap = new Map();
try{

BufferedReader reader = new BufferedReader(new FileReader(filename));
l_line=reader.readLine();
while ( l_line !=null ) {

if(l_line.equals("[Map]")){
do{
l_line=reader.readLine();

}while(!l_line.equals(""));
}
if(l_line.equals("[Continents]")){
l_line=reader.readLine();

while(!l_line.equals("")){
String[] continentDetails =l_line.split("=");
gameMap.createContinent(continentDetails[0], Integer.parseInt((continentDetails[1])));
l_line=reader.readLine();
};
continentsList= gameMap.getListOfContinents();
}

if(l_line.equals("[Territories]")){

l_line=reader.readLine();
int i=0;
int countryId = -1;
// int k=1;
String[][] listOfCountries= new String[28][20];
// This loop is used to create the countries
while(l_line != null){
String[] countryDetails = l_line.split(",");
for(int j=0; j<countryDetails.length; j++){
listOfCountries[i][j]=countryDetails[j];

for(int k=0; k<continentsList.size(); k++){
if(continentsList.get(k).getContinentName().equals(countryDetails[3])){
countryId=k;
break;
}
}

}
gameMap.createAndInsertCountry(i++,countryDetails[0],continentsList.get(countryId) );


l_line= reader.readLine();
}
nodesList = gameMap.getNodes();
for(int k=0; k<listOfCountries.length; k++){

List<Node> bordersToConnect = new ArrayList<>();
Node currentTerritory = nodesList.get(k); //setting an id for borders
for(int j=0; j < listOfCountries[k].length ;j++)
{
// listOfCountries[k][j] is the name of the country
for(i=0; i<listOfCountries.length; i++){
if(listOfCountries[i][0].equals(listOfCountries[k][j])){
Node node=nodesList.get(i);
bordersToConnect.add(node);
break;
}
}
gameMap.addEdgesOfCountry(currentTerritory, bordersToConnect);
}
}

}

l_line=reader.readLine();


}

reader.close();
// l_logObject.setStatus(true, "User printed map " + filename.split("/")[filename.split("/").length-1].split(".map")[0]);
// l_logEntryBuffer.notifyClasses(l_logObject);
printMap(gameMap);
return gameMap;

}
catch(FileNotFoundException e){
// l_logObject.setStatus(false, "Error: File not found");
// l_logEntryBuffer.notifyClasses(l_logObject);
e.printStackTrace();
return gameMap;

}
catch (IOException e) {
// l_logObject.setStatus(false, "IO Exception");
// l_logEntryBuffer.notifyClasses(l_logObject);
e.printStackTrace();
return gameMap;

}

}


public void printMap(Map gameMap){
//System.out.println("Inside ConquestFileReader: printMap()");
System.out.println("------Conquest Map ------");
System.out.println();
String continent ="";
List<Node> nodeList = gameMap.getNodes();
for(Node c : nodeList){

if(c.getData().getContinent().getContinentName() != continent ){
System.out.println("Continent:"+c.getData().getContinent().getContinentName());
continent = c.getData().getContinent().getContinentName();
System.out.println("=======================================================================");
}
System.out.print(c.getData().getCountryName()+" : ");
String borderString ="";
if(c.getBorders() != null && c.getBorders().size()> 0 ){
List<Node> listOfBorders = c.getBorders();

for(Node border : listOfBorders ){

borderString+=border.getData().getCountryName()+" -> ";

}
borderString=borderString.substring(0,borderString.length()-4);
System.out.println(borderString);
System.out.println();
}



}
}



}
17 changes: 8 additions & 9 deletions src/main/java/project/app/warzone/Model/Edit.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package project.app.warzone.Model;

public abstract class Edit extends Phase {

/**
* @param p_ge gameengine
*/
Edit(GameEngine p_ge) {
import project.app.warzone.Features.MapFeatures;

public abstract class Edit extends Phase{
//
Edit(GameEngine p_ge) {
super(p_ge);
}

Expand Down Expand Up @@ -45,9 +44,9 @@ public void fortify() {
public void endGame() {
printInvalidCommandMessage();
}

public void showMap() {
printInvalidCommandMessage();
public void showMap(MapFeatures newmapFeatures) {
printInvalidCommandMessage();
}


Expand Down
6 changes: 4 additions & 2 deletions src/main/java/project/app/warzone/Model/End.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package project.app.warzone.Model;

import project.app.warzone.Features.MapFeatures;

public class End extends Phase {

End(GameEngine p_ge) {
Expand All @@ -13,8 +15,8 @@ public void loadMap(String p_filename) {
printInvalidCommandMessage();
}

public void showMap() {
printInvalidCommandMessage();
public void showMap(MapFeatures newmapFeatures) {
printInvalidCommandMessage();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/project/app/warzone/Model/MapFeatureAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package project.app.warzone.Model;

import project.app.warzone.Features.MapFeatures;
public class MapFeatureAdapter extends MapFeatures {
ConquestFileReader conquestReader;

public MapFeatureAdapter(ConquestFileReader conquestReader){
this.conquestReader= conquestReader;

}

public Map readMap(String fileName)
{
return conquestReader.readMap(fileName);
}
}
Loading

0 comments on commit 2eea995

Please sign in to comment.