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

public class Sol extends JFrame implements ActionListener {
    // represents the 7 X 7 borad
    private int row = 7;
    private int col = 7;
    private int[][] board = new int[row][col];
    
    public final static int EMPTY=0; // no piece
    public final static int OCCUPIED=1; // occupied
    public final static int NO=-1;
    
    private final static int ALLPIECE = 32;
    private int pieceRemoved = 0;
    
    private final static int WAIT_FOR_PIECE = 0;
    private final static int WAIT_FOR_EMPTY = 1;
    private int state = WAIT_FOR_PIECE;
    
    private int currentI; // row number (i) that is currectly selected by user
    private int currentJ; // col num (j) that is currectly selected by user
    private int nextI; // next button
    private int nextJ;
    private int middleI;
    private int middleJ;
    
    // file and icons of the images
    private String occupiedFile = "occupied.gif"; 
    private String emptyFile = "empty.gif";
    private Icon occupiedPic = new ImageIcon(occupiedFile);
    private Icon emptyPic = new ImageIcon(emptyFile);
    
    // GUI
    private JPanel panel = new JPanel(new GridLayout(row, col));
    private Container contentPane = getContentPane();
    private JButton[][] buttons = new JButton[row][col];
    private Color backgroundColor = Color.green.darker(); // background color
    private Color selectColor = Color.green.brighter(); // select color;
    private Color highlightColor = Color.yellow; 
    
    private JButton restartButton = new JButton("Restart");
    
    public Sol() {
        super("Solitaire");
        // initiate the variables
        
       for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
               buttons[i][j] = new JButton();
               buttons[i][j].setBorder(BorderFactory.createEmptyBorder());
               buttons[i][j].setBackground(Color.green.darker());
               buttons[i][j].setActionCommand("" + i + "" + j);
               buttons[i][j].addActionListener(this);
               panel.add(buttons[i][j]);
            }
       }
       
       contentPane.add(panel);
       
       initBoard();
       setButtons();
       //setUpGUI();
       
       restartButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               initBoard();
               setButtons();
           }
       });
       contentPane.add(restartButton, BorderLayout.SOUTH);
    }
    
    private void initBoard() {
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if((i <= 1 && j <=1) || (i <= 1 && j >=5) || (i >=5 && j <=1) || (i >=5 && j >= 5)) {
                    board[i][j] = NO;
                } else {
                    board[i][j] = OCCUPIED;
                }
            }
        }
       board[3][3] = EMPTY;        
    }
    
    private void setButtons() {
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                buttons[i][j].setBackground(backgroundColor);
                switch (board[i][j]) {
                    case NO:
                          buttons[i][j].setEnabled(false);
                          buttons[i][j].setIcon(null);
                          break;
                   case EMPTY:
                          //buttons[i][j].setEnabled(false);
                          buttons[i][j].setIcon(emptyPic);
                          break;
                    case OCCUPIED:
                          buttons[i][j].setEnabled(true); // default, not necessar
                          buttons[i][j].setIcon(occupiedPic);
                }
            }
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        String action;
        JButton button;
        boolean validMove = false;
        switch(state) {
            case WAIT_FOR_PIECE:
                 button = (JButton)e.getSource();
                action = button.getActionCommand();
                currentI = Integer.parseInt(action.substring(0,1)); // first character
                //System.out.println(currentI);
                currentJ = Integer.parseInt(action.substring(1,2)); // second character
                //System.out.println(currentJ);
                if(board[currentI][currentJ] == OCCUPIED) { // has a piece on it
                      button.setBackground(selectColor);
                      state = WAIT_FOR_EMPTY;
                } else {
                    // beep
                    Toolkit.getDefaultToolkit().beep();
                }
                break;
            case WAIT_FOR_EMPTY:
                button = (JButton)e.getSource();
                action = button.getActionCommand();
                nextI = Integer.parseInt(action.substring(0,1)); // first character
                nextJ = Integer.parseInt(action.substring(1,2)); // second character
                //System.out.println(currentJ);
                if(board[nextI][nextJ] == OCCUPIED) { // has a piece on it
                      // reset
                      buttons[currentI][currentJ].setBackground(backgroundColor);
                      button.setBackground(selectColor);
                      currentI = nextI; // reset the current button
                      currentJ = nextJ; 
                } else if(board[nextI][nextJ] == EMPTY) {
                    validMove = false;
                    if(currentI == nextI) {
                       if(currentJ - nextJ == 2) {
                             middleJ = currentJ - 1;
                             middleI = currentI;
                             validMove = (board[middleI][middleJ]==OCCUPIED);
                       } else if (currentJ - nextJ == -2) {
                             middleJ = currentJ + 1;
                             middleI = currentI;
                             validMove = (board[middleI][middleJ]==OCCUPIED);
                       } 
                    }else if(currentJ == nextJ) {
                            if(currentI - nextI == 2) {
                             middleI = currentI - 1;
                             middleJ = currentJ;
                             validMove = (board[middleI][middleJ]==OCCUPIED);
                       } else if (currentI - nextI == -2) {
                             middleI = currentI + 1;
                             middleJ = currentJ;
                             validMove = (board[middleI][middleJ]==OCCUPIED);
                       }
                    } else {
                        validMove = false;

                    }
                        
                    if(validMove) {
                        board[middleI][middleJ] = EMPTY;
                        board[nextI][nextJ] = OCCUPIED;
                        board[currentI][currentJ] = EMPTY;
                        buttons[currentI][currentJ].setIcon(emptyPic);
                        buttons[currentI][currentJ].setBackground(backgroundColor);
                        buttons[middleI][middleJ].setIcon(emptyPic);
                        buttons[nextI][nextJ].setIcon(occupiedPic);
                        buttons[nextI][nextJ].setBackground(selectColor);
                        currentI = nextI;
                        currentJ = nextJ;
                        pieceRemoved++;
                    } else {
                         // beep
                        Toolkit.getDefaultToolkit().beep();
                    }
                }
                break;
        }
    }
    
    
    public static void main(String[] args) {
        JFrame f = new Sol();
        f.setSize(450,450);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
    
                
                
    
    
        
        
    
