Skip to content

Commit

Permalink
Merge pull request #59 from RancyKaur/build2-cpy
Browse files Browse the repository at this point in the history
AIRLIFT AND BOMB
  • Loading branch information
Sujith-Kumar-2003 authored Nov 7, 2023
2 parents b84e472 + 9d92351 commit 254fa5d
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 58 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions log.txt
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@

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:
125 changes: 122 additions & 3 deletions src/main/java/Controller/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class GameEngine {
public Phase d_Phase;



public void setD_GamePhase(GamePhase d_GamePhase) {
this.d_GamePhase = d_GamePhase;
}
Expand Down Expand Up @@ -60,7 +59,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
Expand Down Expand Up @@ -549,6 +548,115 @@ public GamePhase parseCommand(Player p_player, String p_givenCommand) {
exit(0);
}

case "bomb":
System.out.println(l_commandName + " command entered");
try {
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 the current player | please pass to the next player");
d_LogEntry.setMessage(
"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.isAlphabetic(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, targetCountryId, numArmiesToAirlift);

// 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 {
Expand Down Expand Up @@ -576,7 +684,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;
Expand Down Expand Up @@ -758,4 +866,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
}
}
96 changes: 54 additions & 42 deletions src/main/java/Model/Bomb.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion src/main/java/Model/Negotiate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/Model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ public class Player {
private HashMap<String, Continent> d_OwnedContinents;
private HashMap<String, Country> d_OwnedCountries;
private int d_OwnedArmies;



private Order d_Order;



private Queue<Order> d_OrderList;
ArrayList<GameCard> d_CardDeck;
public List<Player> d_NegotiateList;
Expand Down

0 comments on commit 254fa5d

Please sign in to comment.