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

import controls.StepScheduler;
import interfaces.IAttack;

import javax.swing.*;
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<JButton> myHandButtons;

    private List<JButton> opponentHandButtons;

    private List<Integer> selectableMyHandKeys;
    private List<Integer> selectableOpponentHandKeys;
    JPanel myHandButtonsPanel;
            JPanel opponentButtonsPanel;
    int guess;
    int attacker;
    int target;
    int indexForOpponent =0;
    private JPanel deckButtonPanel;

    public MainPanel(StepScheduler stepScheduler) {
        super(new BorderLayout());
        myHandButtons = new ArrayList<>();
        opponentHandButtons = new ArrayList<>();
        selectableOpponentHandKeys = new ArrayList<>();

        setButtons(stepScheduler);
    }

    void setButtons(IAttack stepScheduler) {
        deckButtonPanel = new JPanel();
        myHandButtonsPanel = new JPanel();
        opponentButtonsPanel = new JPanel();
        var deckTopCard=stepScheduler.getTopCard();
        var myHands = stepScheduler.getMyHands();
        var opponentHands = stepScheduler.getOpponentHands();

        if(!stepScheduler.isDeckLess()){
            var cardButton =new JButton("deck");
            cardButton.setPreferredSize(new Dimension(CARD_HEIGHT,CARD_WIDTH));
            deckButtonPanel.add(cardButton);
        }


        //選択可能な自分のカードのキーを取得
        selectableMyHandKeys = myHands.stream().
                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));
            myHandButtons.add(cardButton);
            myHandButtonsPanel.add(cardButton, BorderLayout.WEST);
        }

        //相手のカードに関する処理
        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));
            cardButton.setEnabled(false);
            cardButton.addActionListener(new ActionListener() {
                final int index=indexForOpponent;
                public void actionPerformed(ActionEvent e) {
                    var baseColor= cardButton.getBackground();
                    cardButton.setBackground(SELECTED_COLOR);
                    //相手のカードを選択したときに確認用ダイアログを出す
                    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.getOpponentHands().get(t).getKey()==g;

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

                            //次の画面に遷移する処理
                            update(stepScheduler);
                        }else{
                            setStep(Step.SelectOpponentHands);
                        }
                    }
                    cardButton.setBackground(baseColor);
                }
            });
            opponentHandButtons.add(cardButton);
            opponentButtonsPanel.add(cardButton, 0);//見た目の順序が逆になるように,0番目に挿入
            indexForOpponent++;
        }


        add(deckButtonPanel,BorderLayout.WEST);

        add(myHandButtonsPanel, BorderLayout.SOUTH);
        add(opponentButtonsPanel, BorderLayout.NORTH);
        setStep(Step.SelectOpponentHands);
    }

    void setStep(Step 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){

        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());
        }
//        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();
    }
}