/*
TicTacToe 1.0
random ai, 2 player human vs human
This is a Case study to integrate what I have learned about:
	*JMenus
	*JFrames, Panels, Buttons, etc......
	*Images
	*Splash Screens
	*and lots of other cool stuff! (arrays, listeners, and so on...etc....)
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Random;

public class KillBox implements ActionListener {
    JFrame mainFrame, 			//the mainFrame holds everything,
    	   dialogFrame;			//the dialogFrame is for "About"
    	   
    JPanel mainPanel, 			//mainPanel is the playing board	
    	   winPanel, 			//winPanel holds the images for the winner
    	   countPanel;			//countPanel holds the scores
    	   
    JButton box[][];			//this makes an array of JButtons for the boxes
    
    ImageIcon xwins,			//these ImageIcons hold the x and y winning banner 
    		  owins;
    		   
    JButton winLabel;			//this holds the winning player labels (ie. xwins)
    
    JLabel xCount, 				//these are the score holders
    	   oCount;
        
    JMenuBar menuBar;			//this makes the menuBar
    JMenu file, help;			//these are the main menus (File   Help)
    JMenuItem exit, about;		//these go within the menus
    JCheckBoxMenuItem ai;		//this is a special menuItem that has a checkBox 
    							//next to it
    
	Random rand = new Random();	//random numbers make for a genius ai (hmmm...)
	
    //used for counting
    int row, col, 
    	xCountRow, 				//keeps track of the checked lines
    	oCountRow,
    	xCountCol,
    	oCountCol,
    	xCountDiag,
    	oCountDiag; 
 	
 	int curButton, 				//what is the current button played
    	curPlayer, 				//who is the current player
    	xWinCount = 0,			//these keep each player's score
    	oWinCount = 0;
    
    final int X = 1;			//used for winning line checking
    final int O = 2;
	
	int i, n, done, checked = 0;			//used for working with numbers
	
	//here are some variables used for the dialog boxes 
	//to set ai and other stuff
	int aiPlayer = 0;
	
    //constructor
    public KillBox() {
 
    	//make the mainFrame
		mainFrame = new JFrame("KillBox");
		mainFrame.getContentPane().setLayout(new BorderLayout());
		
		//standard tic tac toe board
		mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(3, 3));		//3x3 tic tac toe board 
				
		//this holds the images
		winPanel = new JPanel();
		
		//this panel holds the counts of winning
		countPanel = new JPanel();
		
		
		//who is currently playing
		curPlayer = 0;
		
		buildMenu();	
		buildUI();
		
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.pack();
		mainFrame.setVisible(true);
    }
    
    void buildMenu(){
		//make the menubar
		menuBar = new JMenuBar();
		mainFrame.setJMenuBar(menuBar);

		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();
    		}	
		});
		ai = new JCheckBoxMenuItem("AI?");
		ai.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
        		if(checked == 0){	
        			aiPlayer = 1;
        			clearVars();
        			checked = 1;
        	} else {
        		aiPlayer = 0;
        		clearVars();
        		checked = 0;
        		}
        	}	
		});
		
		//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(ai);
		file.add(exit);
		
		help.add(about);
}
    
    void buildUI(){
    	xwins = new ImageIcon("xwins.gif", "boom");
	    owins = new ImageIcon("owins.gif", "boom");
	    	    
	    xCount = new JLabel("X Score: ");
	    oCount = new JLabel("O Score: ");
	    
	    //make 9 boxes
	    //addActionListener
	    //add it to mainPanel (ticTacToe board)
	    box = new JButton[3][3];
   		for(int row = 0; row < 3; row++){
			for(int col = 0; col < 3; col++){
	    		box[row][col] = new JButton("     ");
	    		box[row][col].setSize(new Dimension(10,10));
	   			box[row][col].addActionListener(this);
	   			mainPanel.add(box[row][col]);
			}
    	}
		

		winLabel = new JButton();
		winLabel.addActionListener(this);
		
		countPanel.add(xCount);	
		countPanel.add(oCount);	
		winPanel.add(winLabel);
		mainFrame.getContentPane().add(countPanel, BorderLayout.NORTH);
		mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER);
		mainFrame.getContentPane().add(winPanel, BorderLayout.SOUTH);
    }
    
    void XWins(){
    	winLabel.setIcon(xwins);
    	xCount.setText("X Score: " + String.valueOf(xWinCount));
    	//System.out.println("xscore: "+xWinCount);
    	xWinCount++;

    }

    void OWins(){
		oWinCount++;
		winLabel.setIcon(owins);
		oCount.setText("O Score: " + String.valueOf(oWinCount));
		//System.out.println("oscore: "+oWinCount);
    }

    public void actionPerformed(ActionEvent e) {
		for(row = 0; row <3; row++){
			for(col = 0; col < 3; col++){
				if(e.getSource() == box[row][col]){
					if(!(box[row][col].getText().equals("X")||box[row][col].getText().equals("O"))){
						if(curPlayer == 0){
							box[row][col].setText("X");
							curPlayer = 1;
					    	if(aiPlayer == 1){
								aiMove();
								curPlayer = 0;
							} 
						} else {
							box[row][col].setText("O");
							curPlayer = 0;
						} 
					}
				}
			}
    	}
    	if(e.getSource() == winLabel){
    		clearVars();
    		winLabel.setIcon(null);
    	}
		checkWinner();
	}

	void aiMove(){
		i = rand.nextInt(3);
		n = rand.nextInt(3);
		done = 0;
		while(done == 0){
			if(!box[i][n].getText().equals("O")){
				if(!box[i][n].getText().equals("X")){
					box[i][n].setText("O");
					curPlayer = 0;
					done = 1;
				}
			} else {
				i = rand.nextInt(3);
				n = rand.nextInt(3);
			}
		}
	}		
									
	void clearVars(){
    		for(row = 0; row < 3; row++){
    			for(col = 0; col < 3; col++){
    				box[row][col].setText("");
    				curPlayer = 0;
    				xCountRow=0; 
	    			oCountRow=0;
   					xCountCol=0;
   	 				oCountCol=0;
    				xCountDiag=0;
    				oCountDiag=0; 
    			}
    		}
	}
		
	void checkWinner(){
		//check the rows for x's and see if it wins
		for(row = 0; row < 3; ++row){
			xCountRow = 0;
			oCountRow = 0;
			for(col = 0; col < 3; col++){
				if(box[row][col].getText().equals("X"))
					xCountRow++;
				if(box[row][col].getText().equals("O"))
					oCountRow++;
			}
			if(xCountRow == 3)
				XWins();
			if(oCountRow == 3)
				OWins();
		}
		//check the columns for a winner
		//I switched the i and j to switch the code from
		//checking rows to collums
		for(row = 0; row < 3; ++row){
			xCountCol = 0;
			oCountCol = 0;

			for(col = 0; col < 3; col++){
				if(box[col][row].getText().equals("X"))
					xCountCol++;
				if(box[col][row].getText().equals("O"))
					oCountCol++;
			}
			if(xCountCol == 3)
				XWins();
			if(oCountCol == 3)
				OWins();
		}
		//check the diagonals for a winner
		//since there are only 2 diagonals the
		//loop will change to make the buttons checked
		//always go one to the side for a diagonal
		//and an extra loop for checking the only two diagonals
			xCountDiag = 0;
			oCountDiag = 0;
			for(col = 0; col < 3; col++){
				if(box[col][col].getText().equals("X"))
					xCountDiag++;
				if(box[col][col].getText().equals("O"))
					oCountDiag++;
			}
				
		
		
			if(xCountDiag == 3)
				XWins();
			if(oCountDiag == 3)
				OWins();
			
			//the second diagonal is mean so I just test the buttons
			//manually in the if statements for  O and X			
				if(box[2][0].getText().equals("X")&&
				   box[1][1].getText().equals("X")&&
				   box[0][2].getText().equals("X"))
					xCountDiag=3;
				if(box[2][0].getText().equals("O")&&
				   box[1][1].getText().equals("O")&&
				   box[0][2].getText().equals("O"))
				   	oCountDiag=3;
				
		
		
			if(xCountDiag == 3)
				XWins();
			if(oCountDiag == 3)
				OWins();
	}	
	
	//exit the program
	void leave(){
		System.exit(0);
	}
	
	//shows a plain dialog with name and program
	void aboutDialog(){
		dialogFrame = new JFrame();
		JOptionPane.showMessageDialog(dialogFrame,
    			"TicTacToe 1.0\nMade by Alex Perez",
    			"About",
    			JOptionPane.PLAIN_MESSAGE);
    		}


	public static void main(String s[]) {
  		//here is the code for the splash screen initialization
  		Frame splashFrame = new JFrame();
		SplashWindow splash = new SplashWindow("splashicon.gif", splashFrame, 2000);
		KillBox panel = new KillBox();
    }
}


