diff --git a/src/main/java/controls/PhaseController.java b/src/main/java/controls/PhaseController.java index 929c670..5a30228 100644 --- a/src/main/java/controls/PhaseController.java +++ b/src/main/java/controls/PhaseController.java @@ -1,44 +1,151 @@ package controls; -import interfaces.ICardButtonEnabler; +import interfaces.IGameView; +import resources.Algo; -public class PhaseController extends Thread{ - ICardButtonEnabler iCardButtonEnabler; +public class PhaseController { + IGameView iGameView; Phase currentPhase; int guess; + boolean isDecidedAttacker; int attacker; + boolean isDecidedTarget; int target; - public PhaseController(ICardButtonEnabler iCardButtonEnabler){ - this.iCardButtonEnabler=iCardButtonEnabler; + Algo algo; + AbstractGameState abstractGameState; + TurnPlayer turnPlayer; + TurnBot turnBot; + public PhaseController(Algo algo){ + this.algo = algo; + isDecidedAttacker = false; + turnPlayer = new TurnPlayer(algo); + turnBot = new TurnBot(algo); + abstractGameState = turnPlayer; currentPhase=Phase.Selection; + //iGameView.onFinishedEachAttack(abstractGameState); + // iGameView.onStartPlayerTurn(abstractGameState); + } + public void bindGameView(IGameView iGameView){ + this.iGameView = iGameView; } public void setSelection(int attacker){ - this.attacker = attacker; - iCardButtonEnabler.setEnableSelection(attacker); + changePhase(Phase.Selection, attacker); +// switch (currentPhase){ +// case Selection: +// if(abstractGameState.isDeckLess()){ +// currentPhase = Phase.Target; +// }else{ +// this.attacker = attacker; +// isDecidedAttacker = true; +// currentPhase = Phase.Target; +// iGameView.onDecidedSelection(attacker); +// } +// break; +// case Target: +// this.attacker = attacker; +// isDecidedAttacker = true; +// currentPhase = Phase.Target; +// iGameView.onDecidedSelection(attacker); +// break; +// } + } - public void setTarget(int target){ - this.target = target; - iCardButtonEnabler.setEnableTarget(target); + public void setTarget(int target){ + changePhase(Phase.Target, target); + } - @Override - public void run(){ - switch (currentPhase){ + public boolean isDecidedAttacker(){ + return isDecidedAttacker; + } + /** + * + * @param phase 遷移先の状態 + * @param params 遷移先に遷移するために必要な引数 + */ + private void changePhase(Phase phase, int... params) { + + switch (phase){ + case StartPlayerTurn: + abstractGameState=turnPlayer; + isDecidedAttacker = !abstractGameState.isDeckLess(); + iGameView.repaintBoard(abstractGameState); + iGameView.onStartPlayerTurn(abstractGameState); + break; case Selection: + + this.attacker = params[0]; + isDecidedAttacker = true; + currentPhase = Phase.Target; + iGameView.onDecidedSelection(attacker); + + break; case Target: + this.target = params[0]; + currentPhase = phase; + iGameView.onDecidedTarget(target); + currentPhase = Phase.Declaration; + + break; case Declaration: + this.guess = params[0]; + abstractGameState.attack(this.guess,this.attacker,this.target); + currentPhase = Phase.StartBotTurn; + var isSucceed = abstractGameState.isSucceedLatestAttack(); + iGameView.repaintBoard(abstractGameState); + iGameView.onFinishedPlayerAttack(guess, isSucceed); + if(judgeGameOver())return; + changePhase(Phase.StartBotTurn, 0); + break; + case StartBotTurn: + + currentPhase = Phase.StartBotTurn; + abstractGameState = turnBot; + abstractGameState.updateTurn(); + iGameView.onStartBotAttack(turnBot); + + break; + case BotAttack: + abstractGameState.updateTurn(); + abstractGameState.attack(params[0],params[1],params[2]); + iGameView.onFinishedBotAttack(params[0], abstractGameState.isSucceedLatestAttack()); + if(judgeGameOver())return; + changePhase(Phase.StartPlayerTurn); + break; + } + } + + public void setDeclaration(int guess) { + changePhase(Phase.Declaration, guess); + } + + public void startBotTurn(){ + changePhase(Phase.StartBotTurn, 0); + } + public void botAttack(int guess, int attacker, int target){ + changePhase(Phase.BotAttack,guess,attacker,target); + } - public void playerAttack(){ + boolean judgeGameOver(){ + if(algo.getLoseA()){ + iGameView.onFinishedGame(abstractGameState, true); + return true; + }else if(algo.getLoseB()){ + iGameView.onFinishedGame(abstractGameState,false); + return true; + } + return false; } - public void botAttack(){ - } enum Phase{ + StartPlayerTurn, Selection, Target, - Declaration + Declaration, + StartBotTurn, + BotAttack } }