Skip to content

Commit

Permalink
BugFix and new features for virus.
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsaltinisik committed Dec 20, 2020
1 parent 6cfaced commit 4f2534f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 43 deletions.
23 changes: 14 additions & 9 deletions src/controllers/modelcontrollers/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class GameManager {
private static int NUMBER_OF_REGIONS = 40;
private boolean diceRolled = false;
private Card currentChanceCard = null;
private City currentInfectedCity = null;


public static GameManager getInstance(){
Expand Down Expand Up @@ -52,15 +51,11 @@ public ArrayList<Region> getRegions(){

// infects a random city in a game
public void infectRandomCity() {
if(currentInfectedCity != null){
currentInfectedCity.infect(false);
}
int random = (int) (this.game.getRegionNumber() * Math.random());
while( !(this.game.getRegion(random) instanceof City) ){
random = (int) (this.game.getRegionNumber() * Math.random());
}
((City) this.game.getRegion(random)).infect(true);
currentInfectedCity = ((City) this.game.getRegion(random));
}

// moves current player count number of steps
Expand Down Expand Up @@ -180,6 +175,9 @@ private boolean performRegionAction(){
if(((City) currentRegion).isInfected()){
currentPlayer.infect(true);
}
else if(currentPlayer.isInfected()) {
((City) currentRegion).infect(true);
}
performed = checkAgreements();
if(!performed){
GameSceneController.handleBuyCityPopup();
Expand Down Expand Up @@ -236,10 +234,8 @@ public void endTurn(){
this.offerAgreement();
this.game.checkVirus();

if(tourCounter == 0) {
// TODO remove infected cities infection
infectRandomCity();
}
infectRandomCity();

turnCounter++;
turnCounter = turnCounter % this.game.getPlayerNumber();
if(turnCounter == 0) {
Expand Down Expand Up @@ -275,8 +271,17 @@ public int getTour(){
return tourCounter;
}

public int getTurn(){
return turnCounter;
}

public Game getGame() { return this.game; }

public void setDiceRolled(boolean bool){
diceRolled = bool;
}


// Private methods

private boolean checkPandemic(){
Expand Down
22 changes: 17 additions & 5 deletions src/controllers/scenecontrollers/GameSceneController.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,15 @@ public void handleRollDiceButton() throws IOException{
}
//GameManager.getInstance().getCurrentPlayer().quarantine(true); // DEBUG
GameManager.getInstance().moveForward(dice[2]);
// GameManager.getInstance().moveForward(1); // DEBUG
//GameManager.getInstance().moveForward(14); // DEBUG
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.schedule(GameManager.getInstance()::runPerformRegionAction, dice[2] * 300, TimeUnit.MILLISECONDS);
rollDiceButton.setVisible(false);
rollDiceLabel.setVisible(false);

// repeat turn
if(dice[0] != dice[1]){
rollDiceButton.setVisible(false);
rollDiceLabel.setVisible(false);
}
}

public void handleEndTurnButton(){
Expand All @@ -197,8 +201,16 @@ public void handleEndTurnButton(){
else{
System.out.println("Cannot end turn without rolling a dice.");
}
rollDiceButton.setVisible(true);
rollDiceLabel.setVisible(true);
// if new player bankrupted, disable roll dice
if(GameManager.getInstance().getCurrentPlayer().isBankrupted()){
// set isDiceRolled true
GameManager.getInstance().setDiceRolled(true);
}
else{
rollDiceButton.setVisible(true);
rollDiceLabel.setVisible(true);
}

}

public void handleSaveGameButton(){
Expand Down
19 changes: 19 additions & 0 deletions src/models/City.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class City extends Region {
private String name;
private final double MORGAGE_PENALTY = 0.55;
private final double MORGAGE_RATE = 0.5;
private int infectTurnCounter = 99999;
private final int MAX = 99999;

public City(double price, double[] rents, String name, int id) {
super(id);
Expand Down Expand Up @@ -51,10 +53,27 @@ public boolean removeBuilding(int count) {
}

public void infect(boolean bool) {
if(bool){
if(!isInfected){
infectTurnCounter = GameManager.getInstance().getTour();
}
}
else{
infectTurnCounter = MAX;
}
isInfected = bool;
this.notifyAllObservers();
}

public boolean checkInfection(){
if(Math.abs(GameManager.getInstance().getTurn() - infectTurnCounter) >= 1){
infect(false);
infectTurnCounter = MAX;
return true;
}
return false;
}

public boolean isInfected() {
return isInfected;
}
Expand Down
5 changes: 5 additions & 0 deletions src/models/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public void checkVirus() {
player.checkInfection();
}
}
for( Region region : regions){
if(region instanceof City){
((City) region).checkInfection();
}
}
}

public ArrayList<Agreement> getAgreements(){
Expand Down
59 changes: 30 additions & 29 deletions src/models/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public class Player extends Observable implements Serializable {
private boolean isBankrupted;
private int location;
private boolean isInfected;
private int quarantineTourCounter = -1;
private int infectTourCounter = -1;
private int quarantineTourCounter = 99999;
private int infectTourCounter = 99999;
private final int MAX = 99999;

public Player(String name, String color, String pawn, int id)
{
Expand Down Expand Up @@ -46,41 +47,21 @@ public void quarantine(boolean bool)
}
}
else{
quarantineTourCounter = -1;
quarantineTourCounter = MAX;
}
isInQuarantine = bool;
this.notifyGameLogObserver("quarantine", bool ? 1 : 0);
}

public boolean checkQuarantine(){
if(GameManager.getInstance().getTour() - quarantineTourCounter >= 2){
isInQuarantine = false;
quarantineTourCounter = -1;
quarantine(false);
quarantineTourCounter = MAX;
return true;
}
return false;
}

public void addMoney(double money)
{
this.money = this.money + money;
if(isBankrupted && money > 0){
isBankrupted = false;
}
this.notifyAllObservers();
this.notifyGameLogObserver("money", money);
}

public void removeMoney(double money)
{
this.money = this.money - money;
if(this.money < 0){
isBankrupted = true;
}
this.notifyAllObservers();
this.notifyGameLogObserver("money", -money);
}

public void infect(boolean bool)
{
if(bool){
Expand All @@ -89,22 +70,42 @@ public void infect(boolean bool)
}
}
else{
infectTourCounter = -1;
infectTourCounter = MAX;
}
isInfected = bool;
this.notifyAllObservers();
this.notifyGameLogObserver("infect", bool ? 1 : 0);
}

public boolean checkInfection(){
if(GameManager.getInstance().getTour() - infectTourCounter >= 3){
isInfected = false;
infectTourCounter = -1;
if(GameManager.getInstance().getTour() - infectTourCounter >= 2){
infect(false);
infectTourCounter = MAX;
return true;
}
return false;
}

public void addMoney(double money)
{
this.money = this.money + money;
if(isBankrupted && money > 0){
isBankrupted = false;
}
this.notifyAllObservers();
this.notifyGameLogObserver("money", money);
}

public void removeMoney(double money)
{
this.money = this.money - money;
if(this.money < 0){
isBankrupted = true;
}
this.notifyAllObservers();
this.notifyGameLogObserver("money", -money);
}

public boolean removeCity(City city){
if (cities.remove(city)){
this.notifyAllObservers();
Expand Down

0 comments on commit 4f2534f

Please sign in to comment.