From f6f3f4a6a17fd26cee7adfe6aa8dbf38dec297f3 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Mon, 8 May 2023 13:07:03 +1000 Subject: [PATCH] state: isPhaseOver sped up code --- src/comp1110/ass2/State.java | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/comp1110/ass2/State.java b/src/comp1110/ass2/State.java index ac818a6..e18ddfe 100644 --- a/src/comp1110/ass2/State.java +++ b/src/comp1110/ass2/State.java @@ -409,28 +409,20 @@ public class State { * Defaults to simple mode */ public boolean isPhaseOver() { - return isPhaseOver(true); - } - - /** - * is the phase over? - * @param simple boolean don't check all player moves - */ - public boolean isPhaseOver(boolean simple){ boolean resourcesLeft = false; for (Resource r : resources) { if (r.isAvailable() && r.getType() != 'S') resourcesLeft = true; } - boolean moveLeft = false; - if (simple) { - if (getCurrentPlayer().canPlay(this)) moveLeft = true; - } - else - { - for (Player player : players) { - if (player.canPlay(this)) moveLeft = true; + int numSettlers = 30 - ((numPlayers - 2) * 5); + for (Player p: players) { + boolean canPlay = false; + if (p.getVillages().length < 5) canPlay = true; + if (p.getSettlers().length < numSettlers) canPlay = true; + if (canPlay) { + if (p.canPlay(this)) moveLeft = true; } + } return !resourcesLeft || !moveLeft; }