diff --git a/src/main/java/controls/StepScheduler.java b/src/main/java/controls/StepScheduler.java index 19ed6e5..bd541c5 100644 --- a/src/main/java/controls/StepScheduler.java +++ b/src/main/java/controls/StepScheduler.java @@ -11,29 +11,29 @@ Algo algo; int turnCount=0; boolean isATurn; + boolean isSucceedLatestAttack; public StepScheduler(){ algo = new Algo(); isATurn=true; } - public Constants.Step getStep(){ - return null; - } - @Override public void Attack(int guess, int attacker, int target) { if(isATurn){ if(isDeckLess()){ algo.inputSelectA(guess, attacker, target); - + isSucceedLatestAttack = algo.getSucceedSelectA().getKey(); }else{ algo.inputDrawA(guess, target); + isSucceedLatestAttack = algo.getSucceedDrawA().getKey(); } }else { if(isDeckLess()){ algo.inputSelectB(guess, attacker, target); + isSucceedLatestAttack = algo.getSucceedSelectB().getKey(); }else{ algo.inputDrawB(guess, target); + isSucceedLatestAttack = algo.getSucceedDrawB().getKey(); } } turnCount++; @@ -51,12 +51,16 @@ @Override public Map.Entry getTopCard() { - return null; + if(isDeckLess())return null; + return algo.getDeck().get(0); } public boolean isDeckLess(){ return algo.getDeck().size()==0; } + public boolean isSucceedLatestAttack(){ + return isSucceedLatestAttack; + } public boolean getIsATurn(){ return isATurn; } diff --git a/src/main/java/interfaces/IAttack.java b/src/main/java/interfaces/IAttack.java index 691afcd..ccca842 100644 --- a/src/main/java/interfaces/IAttack.java +++ b/src/main/java/interfaces/IAttack.java @@ -1,5 +1,7 @@ package interfaces; +import views.Constants; + import java.util.List; import java.util.Map; @@ -13,5 +15,5 @@ Map.Entry getTopCard(); boolean isDeckLess(); - + boolean isSucceedLatestAttack(); } diff --git a/src/main/java/models/HandsA.java b/src/main/java/models/HandsA.java index f3f62d9..d7d8c46 100644 --- a/src/main/java/models/HandsA.java +++ b/src/main/java/models/HandsA.java @@ -22,7 +22,7 @@ if (succeedSelectA.getKey()) { temp_if3 = this.value; } else { - this.value.set(succeedSelectA.getValue().getKey(),new AbstractMap.SimpleEntry<>(this.value.get(succeedSelectA.getValue().getValue()).getKey(), true)); + this.value.set(succeedSelectA.getValue().getValue(),new AbstractMap.SimpleEntry<>(this.value.get(succeedSelectA.getValue().getValue()).getKey(), true)); temp_if3 = this.value; } value = temp_if3; diff --git a/src/main/java/models/HandsB.java b/src/main/java/models/HandsB.java index 0593382..c9e3f5f 100644 --- a/src/main/java/models/HandsB.java +++ b/src/main/java/models/HandsB.java @@ -60,7 +60,7 @@ if (succeedSelectB.getKey()) { temp_if5 = this.value; } else { - this.value.set(succeedSelectB.getValue().getKey(),new AbstractMap.SimpleEntry<>(this.value.get(succeedSelectB.getValue().getValue()).getKey(), true)); + this.value.set(succeedSelectB.getValue().getValue(),new AbstractMap.SimpleEntry<>(this.value.get(succeedSelectB.getValue().getValue()).getKey(), true)); temp_if5 = this.value; } value = temp_if5; diff --git a/src/main/java/views/CardButton.java b/src/main/java/views/CardButton.java new file mode 100644 index 0000000..3f96a32 --- /dev/null +++ b/src/main/java/views/CardButton.java @@ -0,0 +1,64 @@ +package views; + +import javax.swing.*; +import javax.swing.border.LineBorder; + +import java.awt.*; + +import static views.Constants.*; +import static views.Constants.CARD_HEIGHT; + +public class CardButton extends JButton { + private Status status; + final int UNSELECTED_THICKNESS=1; + final int SELECTED_THICKNESS=4; + public CardButton(String text) { + this.setText(text); + this.setBorder(new LineBorder(UNSELECTED_COLOR, UNSELECTED_THICKNESS, true)); + this.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT)); + this.setFont(new Font("MS ゴシック", Font.BOLD, 14)); + } + //選択状態の切り替え + public void setEnabledSelection(boolean enabledSelection){ + if(enabledSelection){ + this.setBorder(new LineBorder(SELECTED_COLOR, SELECTED_THICKNESS,true)); + }else { + this.setBorder(new LineBorder(UNSELECTED_COLOR, UNSELECTED_THICKNESS, true)); + } + } + public void setStatus(Status status){ + switch (status){ + case OPEN -> { + this.setBackground(OPEN_COLOR); + this.setForeground(CLOSED_COLOR); + } + case CLOSED -> { + this.setBackground(CLOSED_COLOR); + this.setForeground(OPEN_COLOR); + } + case MY_CLOSED -> { + this.setContentAreaFilled(false); + this.setFocusPainted(false); // used for demonstration + this.setForeground(OPEN_COLOR); + } + } + } + @Override + protected void paintComponent(Graphics g) { + final Graphics2D g2 = (Graphics2D) g.create(); + g2.setPaint(new GradientPaint( + new Point(0, 0), + OPEN_COLOR, + new Point(0, getHeight()), + CLOSED_COLOR)); + g2.fillRect(0, 0, getWidth(), getHeight()); + g2.dispose(); + + super.paintComponent(g); + } + public enum Status{ + OPEN, //開示されている + CLOSED, //開示されていない + MY_CLOSED //開示されていない(ただし自分専用) + } +} diff --git a/src/main/java/views/Constants.java b/src/main/java/views/Constants.java index 26c9e78..4d61b87 100644 --- a/src/main/java/views/Constants.java +++ b/src/main/java/views/Constants.java @@ -1,5 +1,6 @@ package views; +import javax.swing.*; import java.awt.*; public class Constants { @@ -10,9 +11,23 @@ SelectMyHands, SelectOpponentHands, Declare, - Confirm + Confirm, + OpponentSelect, + OpponentDeclare, + OpponentJudge, + OpponentConfirm } public static String CLOSED_SYMBOL="?"; public static int DECK_COUNT=11; public static Color SELECTED_COLOR=Color.CYAN; + public static Color ATTACKER_COLOR=Color.RED; + public static Color UNSELECTED_COLOR=Color.BLACK; + public static Color OPEN_COLOR=Color.WHITE; + public static Color CLOSED_COLOR=Color.BLACK; + public static JButton cardPaint=new JButton(){ + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + } + }; } diff --git a/src/main/java/views/MainPanel.java b/src/main/java/views/MainPanel.java index 9da1f54..c7c7ae1 100644 --- a/src/main/java/views/MainPanel.java +++ b/src/main/java/views/MainPanel.java @@ -4,6 +4,7 @@ import interfaces.IAttack; import javax.swing.*; +import javax.swing.border.LineBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -15,17 +16,23 @@ import static views.Constants.*; public class MainPanel extends JPanel { - private List myHandButtons; + private List myHandButtons; - private List opponentHandButtons; + private List opponentHandButtons; private List selectableMyHandKeys; private List selectableOpponentHandKeys; + Constants.Step currentStep; JPanel myHandButtonsPanel; - JPanel opponentButtonsPanel; + JPanel opponentButtonsPanel; + /** + * アタックで使用するカードが既に決定しているか + */ + boolean isDecidedAttacker; int guess; int attacker; int target; + int indexForMyHands=0; int indexForOpponent =0; private JPanel deckButtonPanel; @@ -34,48 +41,90 @@ myHandButtons = new ArrayList<>(); opponentHandButtons = new ArrayList<>(); selectableOpponentHandKeys = new ArrayList<>(); - + currentStep = Step.SelectMyHands; setButtons(stepScheduler); } void setButtons(IAttack stepScheduler) { + deckButtonPanel = new JPanel(); myHandButtonsPanel = new JPanel(); opponentButtonsPanel = new JPanel(); + // myHandButtonsPanel.setLayout(new BorderLayout()); + // myHandButtonsPanel.setLayout(null); var deckTopCard=stepScheduler.getTopCard(); var myHands = stepScheduler.getMyHands(); var opponentHands = stepScheduler.getOpponentHands(); + isDecidedAttacker=false; - if(!stepScheduler.isDeckLess()){ + if(!stepScheduler.isDeckLess()){ //デッキが存在する場合にデッキトップのカードを引く処理 + + var cardButton =new CardButton(deckTopCard.getKey().toString()); + cardButton.setBounds(0,100,CARD_WIDTH,CARD_HEIGHT); + myHandButtons.add(cardButton); + myHandButtonsPanel.add(cardButton, BorderLayout.WEST); + isDecidedAttacker=true; + }else{ //デッキが存在しない場合、アタックに使用するカードを選択する処理 + + } + + if(!stepScheduler.isDeckLess()){ //デッキが存在する場合 + + var top=stepScheduler.getTopCard(); var cardButton =new JButton("deck"); cardButton.setPreferredSize(new Dimension(CARD_HEIGHT,CARD_WIDTH)); deckButtonPanel.add(cardButton); + }else { + } - + indexForMyHands=0; //選択可能な自分のカードのキーを取得 selectableMyHandKeys = myHands.stream(). - filter(x -> !x.getValue()).//非公開のカードを抽出 + filter(x -> !x.getValue()).//その中から非公開のカードを抽出 map(x -> x.getKey()).toList();//カードのリストからキーのリストへ変換 for (var i : myHands) { - var cardButton = new JButton(i.getKey().toString()); - if (!selectableMyHandKeys.contains(i.getKey())) cardButton.setEnabled(false); - cardButton.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT)); + 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 JButton(i.getValue() ? i.getKey().toString() :CLOSED_SYMBOL); - cardButton.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT)); + 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(!isDecidedAttacker){ + JOptionPane.showMessageDialog(null, "Select Attacker from Your Hands. ", "Warn", + JOptionPane.WARNING_MESSAGE); + return; + } var baseColor= cardButton.getBackground(); - cardButton.setBackground(SELECTED_COLOR); + // 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) { @@ -92,13 +141,13 @@ null, optionsToChoose, optionsToChoose[0]); - if(getDeclaredNumber!=null) { + if(getDeclaredNumber!=null) { //数字を宣言して、承認したとき guess = Integer.parseInt(getDeclaredNumber); var g= Integer.parseInt(getDeclaredNumber); stepScheduler.Attack(g, attacker, t); setStep(Step.Confirm); - boolean isSucceed=stepScheduler.getOpponentHands().get(t).getKey()==g; + boolean isSucceed=stepScheduler.isSucceedLatestAttack(); String resultMessage="Your Attack Was "; resultMessage+=isSucceed?"Succeed.":"Failed."; @@ -110,7 +159,7 @@ setStep(Step.SelectOpponentHands); } } - cardButton.setBackground(baseColor); + cardButton.setEnabledSelection(false); } }); opponentHandButtons.add(cardButton); @@ -121,12 +170,21 @@ add(deckButtonPanel,BorderLayout.WEST); + //add(myHandButtonsPanel, BorderLayout.SOUTH); add(myHandButtonsPanel, BorderLayout.SOUTH); + add(opponentButtonsPanel, BorderLayout.NORTH); setStep(Step.SelectOpponentHands); + + //試行錯誤用 ->やりたいことはできた +// myHandButtonsPanel.removeAll(); +// for(var mh : myHandButtons){ +// myHandButtonsPanel.add(mh); +// } } void setStep(Step step) { + currentStep = step; switch (step) { case SelectMyHands: break; @@ -147,17 +205,40 @@ } } void update(IAttack iAttack){ + myHandButtonsPanel.removeAll(); + //選択可能な自分のカードのキーを取得 + 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); + 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); + } + }); + } myHandButtons.stream().forEach(x -> { - x.setEnabled(false); + // x.setEnabled(false); }); //opponentHandButtons.clear(); //remove(opponentButtonsPanel); for(int i=0,n=opponentHandButtons.size();i{ //