From 540f465c5649e223bc9f32feaf7205713de979f2 Mon Sep 17 00:00:00 2001 From: Sujith <94170693+Luna-martinez@users.noreply.github.com> Date: Tue, 7 Nov 2023 11:54:37 -0500 Subject: [PATCH 1/4] implemented airlift and bomb in GameEngine.java --- .idea/inspectionProfiles/Project_Default.xml | 1 + log.txt | 45 +++++++- src/main/java/Controller/GameEngine.java | 105 +++++++++++++++++++ 3 files changed, 148 insertions(+), 3 deletions(-) diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index b9003d9..4bedf7b 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -33,6 +33,7 @@ diff --git a/log.txt b/log.txt index 7691d47..1e1b38d 100644 --- a/log.txt +++ b/log.txt @@ -1,6 +1,45 @@ In PreLoad Phase: loadmap Command is being executed -Command given by user:loadmap new.map -new.map Map is valid. Add players -> -In StartUp Phase: \ No newline at end of file +Command given by user:loadmap ronaktest.map +ronaktest.map Map is valid. Add players -> +In StartUp Phase: +gameplayer command entered +gameplayer command entered +Assigning Countries to players +Country: china assigned to player: sk +Country: russia assigned to player: sj +Country: germany assigned to player: sk +Country: india assigned to player: sj +In MainPlay Phase: +assigncountries command entered +Countries allocated randomly amongst Players +Assign reinforcement armies to player sk +Assign reinforcement armies to player sj +Entered Execute Orders Phase: +Entered Execute Orders Phase: +Orders in process of being executed for all players +Total Orders in Queue are : 2 +Order executed for player: sk +Order executed for player: sj +Negotiations reset since turn has ended +Negotiations reset since turn has ended +All Orders executed! +All orders for all players are executed, now assigning NEW Reinforcements! +Reinforcements assigned! Players can start with deploying Orders again! +Country not owned by player or insufficient Army units | please pass to next player +advance command entered +For player sk advance order added to Players OrdersList: advance china india +Country not owned by player or insufficient Army units | please pass to next player +Entered Execute Orders Phase: +Entered Execute Orders Phase: +Orders in process of being executed for all players +Total Orders in Queue are : 3 +Order executed for player: sk +Order executed for player: sj +Order executed for player: sk +Negotiations reset since turn has ended +Negotiations reset since turn has ended +All Orders executed! +All orders for all players are executed, now assigning NEW Reinforcements! +Reinforcements assigned! Players can start with deploying Orders again! \ No newline at end of file diff --git a/src/main/java/Controller/GameEngine.java b/src/main/java/Controller/GameEngine.java index 11624fa..bd474e3 100644 --- a/src/main/java/Controller/GameEngine.java +++ b/src/main/java/Controller/GameEngine.java @@ -549,6 +549,111 @@ public GamePhase parseCommand(Player p_player, String p_givenCommand) { exit(0); } + case "bomb": + System.out.println(l_commandName + " command entered"); + try { + if (!(l_param[1] == null)) { + if (this.isAlphabetic(l_param[1])) { + setPhase(new IssueOrderPhase(this)); + l_countryId = l_param[1]; + boolean l_checkOwnedCountry = p_player.getOwnedCountries() + .containsKey(l_countryId.toLowerCase()); + boolean checkCard = p_player.checkCardExists("Bomb"); + if (l_checkOwnedCountry && checkCard) { + // Create a Bomb order + Bomb bombOrder = new Bomb(p_player, l_countryId); + + // Add the Bomb order to the current player's order list + p_player.addOrder(bombOrder); + + //p_player.issue_order(); + d_Phase.issue_order(p_player); + + System.out.println("For player " + p_player.getPlayerName() + + " Bomb order added to Players OrdersList: " + l_param[0] + " " + + l_param[1]); + d_LogEntry.setMessage("For player " + p_player.getPlayerName() + + " Bomb order added to Players OrdersList: " + l_param[0] + " " + + l_param[1]); + p_player.removeCard("Bomb"); + System.out.println("Bomb card used hence it is now removed from Player's cardList "); + d_LogEntry.setMessage("Bomb card used hence it is now removed from Player's cardList "); + } else { + System.out.println( + "Bomb Card not owned or Country not owned by current player | please pass to next player"); + d_LogEntry.setMessage( + "Bomb Card not owned or Country not owned by the current player | please pass to the next player"); + } + this.d_GamePhase = GamePhase.TAKETURN; + break; + } + } else { + System.out.println("Invalid Command"); + } + } catch (Exception e) { + System.out.println( + "Bomb Card is not Owned or Country not owned by current player | please pass to next player"); + d_LogEntry.setMessage( + "Bomb Card is not Owned or Country not owned by current player | please pass to the next player"); + } + break; + + + case "airlift": + System.out.println(l_commandName + " command entered"); + try { + if (!(l_param[1] == null) && !(l_param[2] == null) && !(l_param[3] == null)) { + if (this.isAlphabetic(l_param[1]) && this.isNumeric(l_param[2]) && this.isNumeric(l_param[3])) { + setPhase(new IssueOrderPhase(this)); + String sourceCountryId = l_param[1]; + int numArmiesToAirlift = Integer.parseInt(l_param[2]); + String targetCountryId = l_param[3]; + + boolean sourceCountryOwned = p_player.getOwnedCountries() + .containsKey(sourceCountryId.toLowerCase()); + boolean targetCountryOwned = p_player.getOwnedCountries() + .containsKey(targetCountryId.toLowerCase()); + boolean checkCard = p_player.checkCardExists("Airlift"); + + if (sourceCountryOwned && targetCountryOwned && checkCard) { + // Create an Airlift order + Airlift airliftOrder = new Airlift(p_player, sourceCountryId, numArmiesToAirlift, targetCountryId); + + // Add the Airlift order to the current player's order list + p_player.addOrder(airliftOrder); + + //p_player.issue_order(); + d_Phase.issue_order(p_player); + + System.out.println("For player " + p_player.getPlayerName() + + " Airlift order added to Players OrdersList: " + l_param[0] + " " + + l_param[1] + " " + l_param[2] + " " + l_param[3]); + d_LogEntry.setMessage("For player " + p_player.getPlayerName() + + " Airlift order added to Players OrdersList: " + l_param[0] + " " + + l_param[1] + " " + l_param[2] + " " + l_param[3]); + p_player.removeCard("Airlift"); + System.out.println("Airlift card used hence it is now removed from Player's cardList "); + d_LogEntry.setMessage("Airlift card used hence it is now removed from Player's cardList "); + } else { + System.out.println( + "Airlift Card not owned or Source/Target country not owned by current player | please pass to next player"); + d_LogEntry.setMessage( + "Airlift Card not owned or Source/Target country not owned by current player | please pass to the next player"); + } + this.d_GamePhase = GamePhase.TAKETURN; + break; + } + } else { + System.out.println("Invalid Command"); + } + } catch (Exception e) { + System.out.println( + "Airlift Card is not Owned or Source/Target country not owned by current player | please pass to next player"); + d_LogEntry.setMessage( + "Airlift Card is not Owned or Source/Target country not owned by current player | please pass to the next player"); + } + break; + case "blockade": System.out.println(l_commandName + " command entered"); try { From a2cee7ee1addac68928541e80986e0e96334bb8c Mon Sep 17 00:00:00 2001 From: Sujith <94170693+Luna-martinez@users.noreply.github.com> Date: Tue, 7 Nov 2023 13:18:05 -0500 Subject: [PATCH 2/4] implemented airlift and bomb in GameEngine.java --- src/main/java/Controller/GameEngine.java | 95 +++++++++++++---------- src/main/java/Model/Bomb.java | 96 +++++++++++++----------- src/main/java/Model/Negotiate.java | 2 +- src/main/java/Model/Player.java | 6 -- 4 files changed, 110 insertions(+), 89 deletions(-) diff --git a/src/main/java/Controller/GameEngine.java b/src/main/java/Controller/GameEngine.java index bd474e3..f8e35bf 100644 --- a/src/main/java/Controller/GameEngine.java +++ b/src/main/java/Controller/GameEngine.java @@ -60,7 +60,7 @@ public boolean isValidMapName(String p_mapName) { } /** - * Checks if passed string just has alphbets + * Checks if passed string just has alphabets * * @param p_string * @return true if string has all alphabets or false @@ -552,58 +552,61 @@ public GamePhase parseCommand(Player p_player, String p_givenCommand) { case "bomb": System.out.println(l_commandName + " command entered"); try { - if (!(l_param[1] == null)) { - if (this.isAlphabetic(l_param[1])) { - setPhase(new IssueOrderPhase(this)); - l_countryId = l_param[1]; - boolean l_checkOwnedCountry = p_player.getOwnedCountries() - .containsKey(l_countryId.toLowerCase()); - boolean checkCard = p_player.checkCardExists("Bomb"); - if (l_checkOwnedCountry && checkCard) { - // Create a Bomb order - Bomb bombOrder = new Bomb(p_player, l_countryId); - - // Add the Bomb order to the current player's order list - p_player.addOrder(bombOrder); - - //p_player.issue_order(); - d_Phase.issue_order(p_player); - - System.out.println("For player " + p_player.getPlayerName() - + " Bomb order added to Players OrdersList: " + l_param[0] + " " - + l_param[1]); - d_LogEntry.setMessage("For player " + p_player.getPlayerName() - + " Bomb order added to Players OrdersList: " + l_param[0] + " " - + l_param[1]); - p_player.removeCard("Bomb"); - System.out.println("Bomb card used hence it is now removed from Player's cardList "); - d_LogEntry.setMessage("Bomb card used hence it is now removed from Player's cardList "); - } else { - System.out.println( - "Bomb Card not owned or Country not owned by current player | please pass to next player"); - d_LogEntry.setMessage( - "Bomb Card not owned or Country not owned by the current player | please pass to the next player"); - } - this.d_GamePhase = GamePhase.TAKETURN; - break; + if (l_param.length == 2 && this.isAlphabetic(l_param[1])) { + setPhase(new IssueOrderPhase(this)); + String countryId = l_param[1]; + + // Get the target player from your game logic; replace "fetchTargetPlayer" with the appropriate method + Player targetPlayer = fetchTargetPlayer(p_player); + + boolean sourceCountryOwned = p_player.getOwnedCountries().containsKey(countryId.toLowerCase()); + boolean checkCard = p_player.checkCardExists("Bomb"); + + if (sourceCountryOwned && checkCard) { + // Create a Bomb order + Bomb bombOrder = new Bomb(p_player, targetPlayer, countryId); + + // Add the Bomb order to the current player's order list + p_player.addOrder(bombOrder); + + // Issue the order (p_player.issue_order() or d_Phase.issue_order(p_player)) + // ... + + System.out.println("For player " + p_player.getPlayerName() + + " Bomb order added to Players OrdersList: " + l_param[0] + " " + + l_param[1]); + d_LogEntry.setMessage("For player " + p_player.getPlayerName() + + " Bomb order added to Players OrdersList: " + l_param[0] + " " + + l_param[1]); + p_player.removeCard("Bomb"); + System.out.println("Bomb card used, hence it is now removed from Player's cardList"); + d_LogEntry.setMessage("Bomb card used, hence it is now removed from Player's cardList"); + } else { + System.out.println( + "Bomb Card not owned or Country not owned by current player | please pass to the next player"); + d_LogEntry.setMessage( + "Bomb Card not owned or Country not owned by the current player | please pass to the next player"); } + this.d_GamePhase = GamePhase.TAKETURN; + break; } else { System.out.println("Invalid Command"); } } catch (Exception e) { System.out.println( - "Bomb Card is not Owned or Country not owned by current player | please pass to next player"); + "Bomb Card is not Owned or Country not owned by the current player | please pass to the next player"); d_LogEntry.setMessage( - "Bomb Card is not Owned or Country not owned by current player | please pass to the next player"); + "Bomb Card is not Owned or Country not owned by the current player | please pass to the next player"); } break; + case "airlift": System.out.println(l_commandName + " command entered"); try { if (!(l_param[1] == null) && !(l_param[2] == null) && !(l_param[3] == null)) { - if (this.isAlphabetic(l_param[1]) && this.isNumeric(l_param[2]) && this.isNumeric(l_param[3])) { + if (this.isAlphabetic(l_param[1]) && this.isNumeric(l_param[2]) && this.isAlphabetic(l_param[3])) { setPhase(new IssueOrderPhase(this)); String sourceCountryId = l_param[1]; int numArmiesToAirlift = Integer.parseInt(l_param[2]); @@ -617,7 +620,7 @@ public GamePhase parseCommand(Player p_player, String p_givenCommand) { if (sourceCountryOwned && targetCountryOwned && checkCard) { // Create an Airlift order - Airlift airliftOrder = new Airlift(p_player, sourceCountryId, numArmiesToAirlift, targetCountryId); + Airlift airliftOrder = new Airlift(p_player, sourceCountryId, targetCountryId, numArmiesToAirlift); // Add the Airlift order to the current player's order list p_player.addOrder(airliftOrder); @@ -654,6 +657,7 @@ public GamePhase parseCommand(Player p_player, String p_givenCommand) { } break; + case "blockade": System.out.println(l_commandName + " command entered"); try { @@ -681,7 +685,7 @@ public GamePhase parseCommand(Player p_player, String p_givenCommand) { System.out.println( "Blockade Card not owned or Country not owned by current player | please pass to next player"); d_LogEntry.setMessage( - "Blockade Card not qwned or Country not owned by current player | please pass to next player"); + "Blockade Card not owned or Country not owned by current player | please pass to next player"); } this.d_GamePhase = GamePhase.TAKETURN; break; @@ -863,4 +867,15 @@ private Player fetchPlayerByName(String p_playerName) { } return null; } + + private Player fetchTargetPlayer(Player currentPlayer) { + // Replace this logic with your actual implementation + for (Player player : d_Players) { + if (!player.getPlayerName().equals(currentPlayer.getPlayerName())) { + // Return the first player that is not the current player as the target player + return player; + } + } + return null; // Return null if no suitable target player is found + } } diff --git a/src/main/java/Model/Bomb.java b/src/main/java/Model/Bomb.java index e3271b3..15d90cc 100644 --- a/src/main/java/Model/Bomb.java +++ b/src/main/java/Model/Bomb.java @@ -1,77 +1,89 @@ package Model; -/** - * Class containing logic for implementation of Bomb order - * @author Alekhya K - * - */ -public class Bomb { - private String d_CountryId; - private Player d_Player; - private Player d_CPlayer; +public class Bomb implements Order { + private Player sourcePlayer; + private Player targetPlayer; + private String countryId; - /** - * This constructor will initialize the order object with deploy details. - * @param p_cplayer source player who will bomb - * @param p_player target player where the bomb will take effect - * @param p_countryId adjacent opponent country where the bomb card will take effect - */ - public Bomb(Player p_cplayer, Player p_player, String p_countryId) { - d_Player = p_player; - d_CPlayer = p_cplayer; - d_CountryId = p_countryId; + public Bomb(Player currentPlayer, Player targetPlayer, String countryId) { + this.sourcePlayer = currentPlayer; + this.targetPlayer = targetPlayer; + this.countryId = countryId; } /** * Method which enacts the order + * * @return true if successful, else false */ public boolean execute() { // Check if Source player is negotiating with the target Player - if (d_CPlayer.getNegotiateList().contains(d_Player)) { - System.out.println(d_CPlayer.getPlayerName() + " has negotiated " + d_Player.getPlayerName()); + if (targetPlayer.getNegotiateList().contains(sourcePlayer)) { + System.out.println(targetPlayer.getPlayerName() + " has negotiated " + sourcePlayer.getPlayerName()); // Skip execution return false; } - Country l_c = d_Player.getOwnedCountries().get(d_CountryId.toLowerCase()); - if (l_c != null) { - int l_existingArmies = l_c.getNumberOfArmies(); - double armies = (double) (l_existingArmies / 2); - l_c.setNumberOfArmies((int) armies); + Country targetCountry = targetPlayer.getOwnedCountries().get(countryId.toLowerCase()); + if (targetCountry != null) { + int existingArmies = targetCountry.getNumberOfArmies(); + double remainingArmies = existingArmies / 2.0; + targetCountry.setNumberOfArmies((int) remainingArmies); return true; } return false; } /** - * Getter for current player - * @return d_Player + * Getter for source player + * + * @return sourcePlayer + */ + public Player getSourcePlayer() { + return sourcePlayer; + } + + /** + * Setter for source player + * + * @param sourcePlayer player + */ + public void setSourcePlayer(Player sourcePlayer) { + this.sourcePlayer = sourcePlayer; + } + + /** + * Getter for target player + * + * @return targetPlayer */ - public Player getD_Player() { - return d_Player; + public Player getTargetPlayer() { + return targetPlayer; } /** - * Setter for current player - * @param d_Player player + * Setter for target player + * + * @param targetPlayer player */ - public void setD_Player(Player d_Player) { - this.d_Player = d_Player; + public void setTargetPlayer(Player targetPlayer) { + this.targetPlayer = targetPlayer; } /** - * Getter for ID of Country - * @return d_CountryId + * Getter for country ID + * + * @return countryId */ - public String getD_CountryId() { - return d_CountryId; + public String getCountryId() { + return countryId; } /** - * Setter for ID of Country - * @param d_CountryId country ID + * Setter for country ID + * + * @param countryId country ID */ - public void setD_CountryId(String d_CountryId) { - this.d_CountryId = d_CountryId; + public void setCountryId(String countryId) { + this.countryId = countryId; } } diff --git a/src/main/java/Model/Negotiate.java b/src/main/java/Model/Negotiate.java index b20fc1a..0cc574c 100644 --- a/src/main/java/Model/Negotiate.java +++ b/src/main/java/Model/Negotiate.java @@ -11,7 +11,7 @@ public class Negotiate implements Order{ private Player d_sourcePlayer, d_targetPlayer; /** * Constructor that initializes class variable. - * @param p_currentPlayer sorce player giving order + * @param p_currentPlayer source player giving order * @param p_targetPlayer target player */ public Negotiate(Player p_currentPlayer,Player p_targetPlayer){ diff --git a/src/main/java/Model/Player.java b/src/main/java/Model/Player.java index d55f51b..211841e 100644 --- a/src/main/java/Model/Player.java +++ b/src/main/java/Model/Player.java @@ -12,13 +12,7 @@ public class Player { private HashMap d_OwnedContinents; private HashMap d_OwnedCountries; private int d_OwnedArmies; - - - private Order d_Order; - - - private Queue d_OrderList; ArrayList d_CardDeck; public List d_NegotiateList; From 648a5b88d61598f90ce776d4403d8dca72a9f187 Mon Sep 17 00:00:00 2001 From: Sujith <94170693+Luna-martinez@users.noreply.github.com> Date: Tue, 7 Nov 2023 13:56:31 -0500 Subject: [PATCH 3/4] implemented airlift and bomb in GameEngine.java --- log.txt | 12 +++++------- src/main/java/Controller/GameEngine.java | 1 - 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/log.txt b/log.txt index 1e1b38d..9a1f42e 100644 --- a/log.txt +++ b/log.txt @@ -1,15 +1,15 @@ In PreLoad Phase: loadmap Command is being executed -Command given by user:loadmap ronaktest.map -ronaktest.map Map is valid. Add players -> +Command given by user:loadmap test2.map +test2.map Map is valid. Add players -> In StartUp Phase: gameplayer command entered gameplayer command entered Assigning Countries to players -Country: china assigned to player: sk -Country: russia assigned to player: sj Country: germany assigned to player: sk +Country: iran assigned to player: sj +Country: pak assigned to player: sk Country: india assigned to player: sj In MainPlay Phase: assigncountries command entered @@ -27,10 +27,8 @@ Negotiations reset since turn has ended All Orders executed! All orders for all players are executed, now assigning NEW Reinforcements! Reinforcements assigned! Players can start with deploying Orders again! -Country not owned by player or insufficient Army units | please pass to next player advance command entered -For player sk advance order added to Players OrdersList: advance china india -Country not owned by player or insufficient Army units | please pass to next player +For player sk advance order added to Players OrdersList: advance germany iran Entered Execute Orders Phase: Entered Execute Orders Phase: Orders in process of being executed for all players diff --git a/src/main/java/Controller/GameEngine.java b/src/main/java/Controller/GameEngine.java index f8e35bf..4916400 100644 --- a/src/main/java/Controller/GameEngine.java +++ b/src/main/java/Controller/GameEngine.java @@ -26,7 +26,6 @@ public class GameEngine { public Phase d_Phase; - public void setD_GamePhase(GamePhase d_GamePhase) { this.d_GamePhase = d_GamePhase; } From 9d9235115578fa7729c560a237564132c69ef8c6 Mon Sep 17 00:00:00 2001 From: Sujith <94170693+Luna-martinez@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:03:29 -0500 Subject: [PATCH 4/4] implemented airlift and bomb in GameEngine.java --- log.txt | 43 ------------------------------------------- 1 file changed, 43 deletions(-) diff --git a/log.txt b/log.txt index 9a1f42e..e69de29 100644 --- a/log.txt +++ b/log.txt @@ -1,43 +0,0 @@ - -In PreLoad Phase: -loadmap Command is being executed -Command given by user:loadmap test2.map -test2.map Map is valid. Add players -> -In StartUp Phase: -gameplayer command entered -gameplayer command entered -Assigning Countries to players -Country: germany assigned to player: sk -Country: iran assigned to player: sj -Country: pak assigned to player: sk -Country: india assigned to player: sj -In MainPlay Phase: -assigncountries command entered -Countries allocated randomly amongst Players -Assign reinforcement armies to player sk -Assign reinforcement armies to player sj -Entered Execute Orders Phase: -Entered Execute Orders Phase: -Orders in process of being executed for all players -Total Orders in Queue are : 2 -Order executed for player: sk -Order executed for player: sj -Negotiations reset since turn has ended -Negotiations reset since turn has ended -All Orders executed! -All orders for all players are executed, now assigning NEW Reinforcements! -Reinforcements assigned! Players can start with deploying Orders again! -advance command entered -For player sk advance order added to Players OrdersList: advance germany iran -Entered Execute Orders Phase: -Entered Execute Orders Phase: -Orders in process of being executed for all players -Total Orders in Queue are : 3 -Order executed for player: sk -Order executed for player: sj -Order executed for player: sk -Negotiations reset since turn has ended -Negotiations reset since turn has ended -All Orders executed! -All orders for all players are executed, now assigning NEW Reinforcements! -Reinforcements assigned! Players can start with deploying Orders again! \ No newline at end of file