diff --git a/src/main/java/controls/BotIntelligence.java b/src/main/java/controls/BotIntelligence.java index 903b489..5623208 100644 --- a/src/main/java/controls/BotIntelligence.java +++ b/src/main/java/controls/BotIntelligence.java @@ -53,7 +53,6 @@ for(int j=1;j x !x.getValue()).//その中から非公開のカードを抽出 - map(x -> x.getKey()).toList();//カードのリストからキーのリストへ変換 - for (var i : myHands) { - var cardButton = new CardButton(i.getKey().toString()); - cardButton.setStatus(i.getValue()? CardButton.Status.OPEN: CardButton.Status.MY_CLOSED); - // if (!selectableMyHandKeys.contains(i.getKey())) cardButton.setEnabled(false); - myHandButtons.add(cardButton); - myHandButtonsPanel.add(cardButton, BorderLayout.WEST); - final var a=indexForMyHands;//actionPerformedの中に書くと、クリックされて初めて、回しきったindexForMyHandsを参照してしまうため、ここで一時変数に格納する - cardButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - //if(currentStep!=Step.SelectMyHands)return; - cardButton.setEnabledSelection(true); - var option = JOptionPane.showConfirmDialog(null, "Attack with This Card?", "confirmation", 2); - - cardButton.setEnabledSelection(false); - if (option == JOptionPane.YES_OPTION){ - attacker=a; - isDecidedAttacker=true; - } - } - }); - indexForMyHands++; - } - - //相手のカードに関する処理 - indexForOpponent=0; - for (var i : opponentHands) { - var cardButton = new CardButton(i.getValue() ? i.getKey().toString() :CLOSED_SYMBOL); - - cardButton.setStatus(i.getValue()? CardButton.Status.OPEN: CardButton.Status.CLOSED); - cardButton.setEnabled(false); - cardButton.addActionListener(new ActionListener() { - final int index=indexForOpponent; - public void actionPerformed(ActionEvent e) { - if(!iAttack.isDeckLess())isDecidedAttacker=true; - if(!isDecidedAttacker){ - JOptionPane.showMessageDialog(null, "Select Attacker from Your Hands. ", "Warn", - JOptionPane.WARNING_MESSAGE); - return; - } - var baseColor= cardButton.getBackground(); - // cardButton.setBackground(SELECTED_COLOR); - cardButton.setEnabledSelection(true); - // cardButton.setBorder(new LineBorder(SELECTED_COLOR, 4, true)); - //相手のカードを選択したときに確認用ダイアログを出す - var option = JOptionPane.showConfirmDialog(null, "Select This Card?", "confirmation", 2); - if (option == JOptionPane.YES_OPTION) { - target = opponentHands.size()-index;//画面上,相手の手札も自分の手札と同じように左から右へ並べられているため,それを補正するために反転させている - var t = index; - setStep(Step.Declare); - String[] optionsToChoose = new String[DECK_COUNT]; - for (var i = 0; i < optionsToChoose.length; i++) optionsToChoose[i] = String.valueOf(i); - var getDeclaredNumber = (String) JOptionPane.showInputDialog( - null, - "What number is this card?", - "Declare Number", - JOptionPane.QUESTION_MESSAGE, - null, - optionsToChoose, - optionsToChoose[0]); - if(getDeclaredNumber!=null) { //数字を宣言して、承認したとき - guess = Integer.parseInt(getDeclaredNumber); - var g= Integer.parseInt(getDeclaredNumber); - iAttack.Attack(g, attacker, t); - setStep(Step.Confirm); - - boolean isSucceed=iAttack.isSucceedLatestAttack(); - - String resultMessage="Your Attack Was "; - resultMessage+=isSucceed?"Succeed.":"Failed."; - JOptionPane.showMessageDialog(null,resultMessage); - - //次の画面に遷移する処理 - update(iAttack); - - - }else{ - setStep(Step.SelectOpponentHands); - } - - } - cardButton.setEnabledSelection(false); - Timer timer = new Timer(1000, this); - timer.setDelay(6000); - botBehave(iAttack); - } - }); - opponentHandButtons.add(cardButton); - opponentButtonsPanel.add(cardButton, 0);//見た目の順序が逆になるように,0番目に挿入 - indexForOpponent++; - } - - - add(deckButtonPanel,BorderLayout.WEST); - - //add(myHandButtonsPanel, BorderLayout.SOUTH); - add(myHandButtonsPanel, BorderLayout.SOUTH); - - add(opponentButtonsPanel, BorderLayout.NORTH); - setStep(Step.SelectOpponentHands); - // update(stepScheduler); - //試行錯誤用 ->やりたいことはできた -// myHandButtonsPanel.removeAll(); -// for(var mh : myHandButtons){ -// myHandButtonsPanel.add(mh); -// } } void setStep(Step step) { @@ -250,12 +96,14 @@ selectText = "Bot drew."; }else{ atk= bot.selectAttacker(iAttack); + opponentHandButtons.get(atk).setEnabledSelection(true); selectText = "Bot selected. "; } JOptionPane.showMessageDialog(null,selectText); var targetText = ""; var tar = bot.selectTarget(iAttack); + myHandButtons.get(tar).setEnabledSelection(true); JOptionPane.showMessageDialog(null,"Bot targeted this."); @@ -269,144 +117,16 @@ resultMessage+=isSucceed?"Succeed.":"Failed."; JOptionPane.showMessageDialog(null,resultMessage); + myHandButtons.get(tar).setEnabledSelection(false); repaintField(iAttack); + if(isGameOver(iAttack)){ + finishGame(iAttack); + return; + } iAttack.shiftTurn(); playerBehave(iAttack); } - void update(IAttack iAttack){ - myHandButtonsPanel.removeAll();// - myHandButtons.clear(); - //選択可能な自分のカードのキーを取得 - selectableMyHandKeys = iAttack.getMyHands().stream(). - filter(x -> !x.getValue()).//その中から非公開のカードを抽出 - map(x -> x.getKey()).toList();//カードのリストからキーのリストへ変換 - for (var i : iAttack.getMyHands()) { - var cardButton = new CardButton(i.getKey().toString()); - cardButton.setStatus(i.getValue()? CardButton.Status.OPEN: CardButton.Status.MY_CLOSED); - // if (!selectableMyHandKeys.contains(i.getKey())) cardButton.setEnabled(false); - myHandButtons.add(cardButton); - myHandButtonsPanel.add(cardButton, BorderLayout.WEST); - final var a=indexForMyHands; - cardButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - - //if(currentStep!=Step.SelectMyHands)return; - cardButton.setEnabledSelection(true); - var option = JOptionPane.showConfirmDialog(null, "Attack with This Card?", "confirmation", 2); - cardButton.setEnabledSelection(false); - if (option == JOptionPane.YES_OPTION){ - attacker=a; - isDecidedAttacker=true; - } - } - }); - } - - opponentButtonsPanel.removeAll();// - opponentHandButtons.clear(); - //選択可能な相手のカードのキーを取得 - selectableOpponentHandKeys = iAttack.getOpponentHands().stream(). - filter(x -> !x.getValue()).//その中から非公開のカードを抽出 - map(x -> x.getKey()).toList();//カードのリストからキーのリストへ変換 - indexForOpponent = 0; - for (var i : iAttack.getOpponentHands()) { - - var isOpen=i.getValue(); - // var cardButton = new CardButton(isOpen?i.getKey().toString():CLOSED_SYMBOL); - var cardButton = new CardButton(i.getKey().toString()); - cardButton.setStatus(isOpen? CardButton.Status.OPEN: CardButton.Status.CLOSED); - // if (!selectableMyHandKeys.contains(i.getKey())) cardButton.setEnabled(false); - cardButton.addActionListener(new ActionListener() { - final int index=indexForOpponent; - public void actionPerformed(ActionEvent e) { - - if(!iAttack.isDeckLess())isDecidedAttacker=true; - if(!isDecidedAttacker){ - JOptionPane.showMessageDialog(null, "Select Attacker from Your Hands. ", "Warn", - JOptionPane.WARNING_MESSAGE); - return; - } - var baseColor= cardButton.getBackground(); - // cardButton.setBackground(SELECTED_COLOR); - cardButton.setEnabledSelection(true); - // cardButton.setBorder(new LineBorder(SELECTED_COLOR, 4, true)); - //相手のカードを選択したときに確認用ダイアログを出す - var option = JOptionPane.showConfirmDialog(null, "Select This Card?", "confirmation", 2); - if (option == JOptionPane.YES_OPTION) { - - target = iAttack.getOpponentHands().size()-index;//画面上,相手の手札も自分の手札と同じように左から右へ並べられているため,それを補正するために反転させている - var t = index; - setStep(Step.Declare); - String[] optionsToChoose = new String[DECK_COUNT]; - for (var i = 0; i < optionsToChoose.length; i++) optionsToChoose[i] = String.valueOf(i); - var getDeclaredNumber = (String) JOptionPane.showInputDialog( - null, - "What number is this card?", - "Declare Number", - JOptionPane.QUESTION_MESSAGE, - null, - optionsToChoose, - optionsToChoose[0]); - if(getDeclaredNumber!=null) { //数字を宣言して、承認したとき - guess = Integer.parseInt(getDeclaredNumber); - var g= Integer.parseInt(getDeclaredNumber); - iAttack.Attack(g, attacker, t); - setStep(Step.Confirm); - - boolean isSucceed=iAttack.isSucceedLatestAttack(); - - String resultMessage="Your Attack Was "; - resultMessage+=isSucceed?"Succeed.":"Failed."; - JOptionPane.showMessageDialog(null,resultMessage); - - //次の画面に遷移する処理 - update(iAttack); - - - }else{ - setStep(Step.SelectOpponentHands); - } - - } - cardButton.setEnabledSelection(false); - Timer timer = new Timer(1000, this); - timer.setDelay(6000); - botBehave(iAttack); - } - }); - opponentHandButtons.add(cardButton); - opponentButtonsPanel.add(cardButton, 0);//見た目の順序が逆になるように,0番目に挿入 - indexForOpponent++; - } - - -// for(int i=0,n=opponentHandButtons.size();i{ -// -// var cardButton = new JButton(x.getValue() ? x.getKey().toString() :CLOSED_SYMBOL); -// cardButton.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT)); -// cardButton.setEnabled(true); -// opponentHandButtons.add(cardButton); -// opponentButtonsPanel.add(cardButton, 0);//見た目の順序が逆になるように,0番目に挿入 -// }); - opponentHandButtons.stream().forEach(x -> { - x.setEnabled(true); - }); - - - isDecidedAttacker=false; - // add(opponentButtonsPanel); - validate(); - repaint(); - } void paintDrawCard(IAttack iAttack){ var deckTopCard=iAttack.getTopCard(); @@ -452,6 +172,7 @@ for (var i : myHands) { var cardButton = new CardButton(i.getKey().toString()); cardButton.setStatus(i.getValue()? CardButton.Status.OPEN: CardButton.Status.MY_CLOSED); + myHandButtons.add(cardButton); myHandButtonsPanel.add(cardButton, BorderLayout.WEST); @@ -481,7 +202,6 @@ indexForOpponent=0; for (var i : opponentHands) { var cardButton = new CardButton(i.getValue() ? i.getKey().toString() :CLOSED_SYMBOL); - cardButton.setStatus(i.getValue()? CardButton.Status.OPEN: CardButton.Status.CLOSED); cardButton.setEnabled(!i.getValue()); cardButton.addActionListener(new ActionListener() { @@ -523,11 +243,6 @@ String resultMessage="Your Attack Was "; resultMessage+=isSucceed?"Succeed.":"Failed."; JOptionPane.showMessageDialog(null,resultMessage); - - //次の画面に遷移する処理 - //repaintField(iAttack); - - }else{ setStep(Step.SelectOpponentHands); return; @@ -539,7 +254,10 @@ } cardButton.setEnabledSelection(false); repaintField(iAttack); - + if(isGameOver(iAttack)){ + finishGame(iAttack); + return; + } botBehave(iAttack); } }); @@ -547,9 +265,6 @@ opponentButtonsPanel.add(cardButton, 0);//見た目の順序が逆になるように,0番目に挿入 indexForOpponent++; } - - - //ここまでが相手のカードに関する処理 validate(); repaint(); @@ -572,4 +287,68 @@ if(!iAttack.isDeckLess())JOptionPane.showMessageDialog(null,"Select Card from Opponent`s Hands."); } + + /** + * ゲームが終了しているか + * @param iAttack + */ + boolean isGameOver(IAttack iAttack){ + + if(iAttack.isALose()){ + return true; + }else if(iAttack.isBLose()){ + return true; + } + + return false; + } + void finishGame(IAttack iAttack){ + var myHands = iAttack.getMyHands(); + var opponentHands = iAttack.getOpponentHands(); + + /** + * 初期化処理(する必要があるのかどうかは知らない) + */ + isDecidedAttacker=false; + myHandButtonsPanel.removeAll();// + myHandButtons.clear(); + opponentButtonsPanel.removeAll();// + opponentHandButtons.clear(); + /** + * 自分の手札に関する処理 + */ + indexForMyHands=0; + for (var i : myHands) { + var cardButton = new CardButton(i.getKey().toString()); + cardButton.setStatus(i.getValue()? CardButton.Status.OPEN: CardButton.Status.MY_CLOSED); + myHandButtons.add(cardButton); + myHandButtonsPanel.add(cardButton, BorderLayout.WEST); + + final var a=indexForMyHands;//actionPerformedの中に書くと、クリックされて初めて、回しきったindexForMyHandsを参照してしまうため、ここで一時変数に格納する + indexForMyHands++; + } + //ここまでが自分のカードに関する処理 + + /** + *相手のカードに関する処理 + */ + indexForOpponent=0; + for (var i : opponentHands) { + var cardButton = new CardButton(i.getValue() ? i.getKey().toString() :CLOSED_SYMBOL); + cardButton.setStatus(i.getValue()? CardButton.Status.OPEN: CardButton.Status.CLOSED); + cardButton.setEnabled(!i.getValue()); + opponentHandButtons.add(cardButton); + opponentButtonsPanel.add(cardButton, 0);//見た目の順序が逆になるように,0番目に挿入 + indexForOpponent++; + } + //ここまでが相手のカードに関する処理 + validate(); + repaint(); + if(iAttack.isALose()){ + JOptionPane.showMessageDialog(null,"Bot Win."); + }else if(iAttack.isBLose()){ + JOptionPane.showMessageDialog(null,"You Win."); + } + + } } diff --git a/src/test/java/AlgoTest.java b/src/test/java/AlgoTest.java index 76f97bb..3fdcc61 100644 --- a/src/test/java/AlgoTest.java +++ b/src/test/java/AlgoTest.java @@ -9,7 +9,7 @@ public class AlgoTest { @Test - void ゲーム開始時0から11のカードで構成されたデッキとそこから2枚引いて構成された各プレイヤーの手札が存在する() { + void ゲーム開始時0から10のカードで構成されたデッキとそこから2枚引いて構成された各プレイヤーの手札が存在する() { Algo algo = new Algo(); printAlgoState(algo); assertEquals(2, algo.getHandsA().size());