package Matrix2P;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * Author: Max Buegler - max@sarstech.com
 * Date: Sep 28, 2009
 * Time: 11:03:28 PM
 */
public class SaddlePoints implements GameAlgorithm{
    private boolean stop=false;
    public String getDescriptor() {
        return "Saddle points";
    }

    public void stop() {
        stop=true;
    }

    public void settings(ActionListener ul) {
        final JFrame settingsFrame=new JFrame("Saddle points settings");
                settingsFrame.setLayout(new GridBagLayout());
                GridBagConstraints cons=new GridBagConstraints();
                cons.weightx=0.5;
                cons.weighty=0.5;
                cons.gridx=0;
                cons.gridy=0;
                cons.gridheight=1;
                cons.gridwidth=1;
                cons.fill=GridBagConstraints.HORIZONTAL;
                JPanel aboutPanel=new JPanel();
                aboutPanel.setBorder(BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("About"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
                    aboutPanel.setLayout(new GridLayout(1,1));
                    JTextArea about=new JTextArea();
                    about.setEditable(false);
                    about.setCursor(null);
                    about.setOpaque(false);
                    about.setFocusable(false);
                    about.setLineWrap(true);
                    about.setText("Finds the saddle points of a game matrix");
                    aboutPanel.add(about);
                settingsFrame.add(aboutPanel,cons);

                settingsFrame.setSize(300,200);
                settingsFrame.setVisible(true);

    }

    public void run(GameMatrix m,ActionListener fl){
        stop=false;
        boolean found=false;
        for (int p1=0;p1<m.getActionsP1();p1++){
            for (int p2=0;p2<m.getActionsP2();p2++){
                if (stop){
                    fl.actionPerformed(null);
                    return;
                }
                boolean max=true;
                for (int p2p=0;p2p<m.getActionsP2();p2p++)
                    if (m.getP2()[p1][p2p]>m.getP2()[p1][p2])max=false;
                if (max){
                    for (int p1p=0;p1p<m.getActionsP1();p1p++)
                        if (m.getP1()[p1p][p2]>m.getP1()[p1][p2])max=false;
                    if (max){
                        System.out.println("Saddle Point at ("+(p1+1)+","+(p2+1)+")");
                        found=true;
                    }

                }
            }
        }
        if (!found)System.out.println("No Saddle Point");
        fl.actionPerformed(null);
    }
}
