/*
 ConnectFour
 04/15/2004
 Short Description:
 Connect four done good!
*/
 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ConnectFour extends JFrame implements MouseListener{
    JMenuBar menuBar;			//this makes the menuBar
    JMenu file, game, help;			//these are the main menus (File Game Help)
    JMenuItem exit, reset, about;		//these go within the menus
	
	JPanel dropPanel,
		   gamePanel,
		   statPanel;
	
	JLabel gameBoard[][];
	
    Container cp = getContentPane();
	
	JButton resetButton, fillButton;
	
	ImageIcon blackChip = new ImageIcon("blackchip.gif");
	ImageIcon redChip = new ImageIcon("redchip.gif");
	ImageIcon blackSquare = new ImageIcon("blacksquare.gif");
	ImageIcon redSquare = new ImageIcon("redsquare.gif");
	ImageIcon clearSquare = new ImageIcon("clearsquare.gif");
	
    boolean curPlayer = true,
    		gameWon = false;
    
    int rows = 7, columns = 6, maxHeight = rows - 1;
    //constructor
    public ConnectFour() {
    	super("ConnectFour");
    	cp.setLayout(new BorderLayout());
    	setResizable(false);
    	
		buildGame();
		buildMenu();
			
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
    }
    
    void buildGame(){
    	
    	//make the panels
    	dropPanel = new JPanel(new GridLayout(0,columns));
    	gamePanel = new JPanel(new GridLayout(0,columns));
    	statPanel = new JPanel(new GridLayout(1,0));
    	
    	//make the gameboard
    	gameBoard = new JLabel[columns][rows];
    	for(int row = 0; row < rows; row++){
    		for(int col = 0; col < columns; col++){
    			//gameBoard[col][row] = new JLabel(col+" "+row);
    			gameBoard[col][row] = new JLabel(clearSquare);
    			gameBoard[col][row].addMouseListener(this);
    			gamePanel.add(gameBoard[col][row]);
    		}
    	}
    	
    	fillButton = new JButton("Fill");
		fillButton.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
        		fill();
    		}	
		});
    	
    	resetButton = new JButton("Reset");
		resetButton.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
        		reset();
    		}	
		});
    	
    	statPanel.add(resetButton);
    	statPanel.add(fillButton);
    	
    	cp.add(dropPanel, BorderLayout.NORTH);
    	cp.add(gamePanel, BorderLayout.CENTER);
    	cp.add(statPanel, BorderLayout.SOUTH);
    }
    
    void buildMenu(){
		//make the menubar
		menuBar = new JMenuBar();

		file = new JMenu("File");
		help = new JMenu("Help");
		menuBar.add(file);
		menuBar.add(help);
	
		exit = new JMenuItem("Exit");
		exit.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
        		leave();
    		}	
		});
		
		//make the about menu item
		about = new JMenuItem("About");
		//this makes it that when you click it the aboutDialog comes up
		about.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
        		aboutDialog();
    		}	
		});
		
		//add the menuItems			
		file.add(exit);
		help.add(about);
		setJMenuBar(menuBar);
	}

	void aboutDialog(){
		JFrame dialogFrame = new JFrame();
		JOptionPane.showMessageDialog(dialogFrame,
    			"ConnectFour 1.0\nMade by Alex Perez",
    			"About",
    			JOptionPane.PLAIN_MESSAGE);
    }
	
	void leave(){
		System.exit(0);
	}
	
	void reset(){
    	for(int row = 0; row < rows; row++){
    		for(int col = 0; col < columns; col++){
    			gameBoard[col][row].setIcon(clearSquare);
    		}
    	}
	}		
	void fill(){
    	for(int row = 0; row < rows; row++){
    		for(int col = 0; col < columns; col++){
    			gameBoard[col][row].setIcon(redSquare);
    		}
    	}
	}		
	
	boolean checkWinner(boolean player){
		ImageIcon testIcon;
		int winCount = 0;
		
		if(curPlayer){
			testIcon = blackSquare;
		} else {
			testIcon = redSquare;
		}
		
    	for(int row = 0; row < rows; row++){
    		for(int col = 0; col < columns; col++){
    			if(gameBoard[col][row].getIcon().equals(testIcon)){
    				winCount++;
    				System.out.println(winCount);
    			} else {
    				winCount = 0;
    				System.out.println(winCount);
    			}
    		}
    		System.out.println("");
    	}
    	if(winCount >= 4){
    		System.out.println(testIcon+" wins");
    		return true;
    	} else {
			return false;
		}
	}	
	
	
	//loop until the right gameBoard[][] JLabel is fount (first two 'for's and if)
	//if found, then loop throught the columns, each time testing if the next JLabel
	//is empty (has emptySquare in it), and if it is, then give it the respective color
	//then set the current player (opposite what it is right now)
	public void mouseClicked(MouseEvent e){
    	if(!gameWon){
	    	for(int row = 0; row < rows; row++){
	    		for(int col = 0; col < columns; col++){
	    			if(e.getSource() == gameBoard[col][row]){
	    				for(int i = 0; i < rows; i++){
	    					if(gameBoard[col][i].getIcon().equals(clearSquare)){
			    				if(curPlayer){		    					
			    					gameBoard[col][i].setIcon(blackSquare);
			    					if(i > 0){
			    						gameBoard[col][i-1].setIcon(clearSquare);
			    					}
			    				} else {
			    					gameBoard[col][i].setIcon(redSquare);
			    					if(i > 0){
			    						gameBoard[col][i-1].setIcon(clearSquare);
			    					}
			    				}
			    			} else {
			    				break;
			    			}
						}
					}
				}
			}
			checkWinner(curPlayer);
		}
		curPlayer = !curPlayer; 
	}
	
	public void mouseEntered(MouseEvent e){
    	for(int row = 0; row < rows; row++){
    		for(int col = 0; col < columns; col++){
    			if(e.getSource() == gameBoard[col][row]){
					//stuff here
    			}
    		}
    	}
	}
	
	public void mouseExited(MouseEvent e){
    	for(int row = 0; row < rows; row++){
    		for(int col = 0; col < columns; col++){
    			if(e.getSource() == gameBoard[col][row]){
					//stuff here
    			}
    		}
    	}
	}
	public void mousePressed(MouseEvent evt){}
	public void mouseReleased(MouseEvent evt){}


  public static void main(String s[]) {
	ConnectFour panel = new ConnectFour();
    }
}