Newer
Older
Algolike / src / main / java / views / MainPanel.java
package views;

import controls.StepScheduler;
import interfaces.IAttack;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.lang.Integer.parseInt;
import static views.Constants.*;

public class MainPanel extends JPanel {
    private List<CardButton> myHandButtons;

    private List<CardButton> opponentHandButtons;

    private List<Integer> selectableMyHandKeys;
    private List<Integer> selectableOpponentHandKeys;
    Constants.Step currentStep;
    JPanel myHandButtonsPanel;
    JPanel opponentButtonsPanel;
    /**
     * アタックで使用するカードが既に決定しているか
     */
    boolean isDecidedAttacker;
    int guess;
    int attacker;
    int target;
    int indexForMyHands=0;
    int indexForOpponent =0;
    private JPanel deckButtonPanel;

    public MainPanel(StepScheduler stepScheduler) {
        super(new BorderLayout());
        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()){    //デッキが存在する場合にデッキトップのカードを引く処理

            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()).//その中から非公開のカードを抽出
                        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(!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);
                            stepScheduler.Attack(g, attacker, t);
                            setStep(Step.Confirm);

                            boolean isSucceed=stepScheduler.isSucceedLatestAttack();

                            String resultMessage="Your Attack Was ";
                            resultMessage+=isSucceed?"Succeed.":"Failed.";
                            JOptionPane.showMessageDialog(null,resultMessage);

                            //次の画面に遷移する処理
                            update(stepScheduler);
                        }else{
                            setStep(Step.SelectOpponentHands);
                        }
                    }
                    cardButton.setEnabledSelection(false);
                }
            });
            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);

        //試行錯誤用 ->やりたいことはできた
//        myHandButtonsPanel.removeAll();
//        for(var mh : myHandButtons){
//            myHandButtonsPanel.add(mh);
//        }
    }

    void setStep(Step step) {
        currentStep = step;
        switch (step) {
            case SelectMyHands:
                break;
            case SelectOpponentHands:
                opponentHandButtons.stream().filter(x -> x.getText().equals(CLOSED_SYMBOL)).forEach(x -> {
                    x.setEnabled(true);
                });
                break;

            case Declare:
                opponentHandButtons.stream().filter(x -> x.getText().equals(CLOSED_SYMBOL)).forEach(x -> {
                    x.setEnabled(false);
                });
                break;
            case Confirm:

                break;
        }
    }
    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);
        });

        //opponentHandButtons.clear();
        //remove(opponentButtonsPanel);
        for(int i=0,n=opponentHandButtons.size();i<n;i++){
            var rev= n-i-1;
            if(iAttack.getOpponentHands().get(rev).getValue()) {
                opponentHandButtons.get(rev).setText(iAttack.getOpponentHands().get(rev).getKey().toString());
                opponentHandButtons.get(rev).setStatus(CardButton.Status.OPEN);
            }
        }
//        iAttack.getOpponentHands().stream().forEach(x->{
//
//         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);
        });
      //  add(opponentButtonsPanel);
       /// repaint();
    }
}