All pastes #572193 Raw Edit

Backup #1

public java v1 · immutable
·published 2007-06-17 16:12 UTC
rendered paste body
/** * @(#)Minesweeper.java * * * @author Michael Cecconet * @version 1.00 2007/6/5 */import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.Random;public class Minesweeper extends JFrame implements ActionListener, MouseListener {	boolean gameOver = true;	boolean startTime = false;	Timer t = new Timer(1000, this);//Updates every second	int timeTaken = 0;//In seconds	//Tile System (Global) - Consists of an array of Tile Object that saves status, and a sister array of JLabels displaying the status.	Tile[][] grid = new Tile[64][64];	JLabel[][] gridShow = new JLabel[grid.length][grid[0].length];		    	    int xBound = 9;    int yBound = 9;    int mineCount = 10;    int flagCount = mineCount;        //Image Declarations (Global)  	    public static ImageIcon baseTile = new ImageIcon("images/normal.png", "basic tile");    	    public static ImageIcon flagTile = new ImageIcon("images/flag.png", "flagged tile");    public static ImageIcon icon = new ImageIcon("images/smiley.png", "Smiley Face");    public static ImageIcon[] mineImage = new ImageIcon[9];    			//Menu Declarations (Global)     JMenuItem j1 = new JMenuItem("New Game");    JMenuItem j2 = new JMenuItem("Beginner");    JMenuItem j3 = new JMenuItem("Intermediate");    JMenuItem j4 = new JMenuItem("Expert");    JMenuItem j5 = new JMenuItem("High Scores");    JMenuItem j6 = new JMenuItem("Exit");    JMenuItem j7 = new JMenuItem("About Minesweeper");    JMenu m1 = new JMenu("Game");    JMenu m2 = new JMenu("Info");    JMenuBar mb1 = new JMenuBar();        //Layout Declarations (Global)    BorderLayout mainLayout = new BorderLayout();    FlowLayout infoLayout = new FlowLayout();    GridLayout gameGrid = new GridLayout(yBound,xBound, 0, 0);    JPanel informationPane = new JPanel();    JPanel gamePane = new JPanel();        //Component Declarations (Global)    JButton gameStatus = new JButton(icon);        //Tile Class    public class Tile {    	private boolean visibleStatus;    	private boolean containsMine;    	public boolean flagStatus;    	private int number;    	//Constructor    	public Tile(){    		visibleStatus = false;//Hidden    		containsMine = false;//No Mine    		flagStatus = false;//No Flag    		number = 0;    	}    	    	//Initializes a Mine on this Space     	    	public void setMine(){    		containsMine = true;    	}    	//Saves the Number of Mine Surronding    	public void setNumber(int getNumber){    		number = getNumber;    	}    	//Called on Click    	public void setVisible(){    		if (containsMine ==true){    			gameOver = true;    		}    		visibleStatus = true;    	}    }       //CreateAndShowGUI    public Minesweeper(){    	//Window Setup    	super("Minesweeper - RRXIII");    	setSize(260,320);    	//setResizable(false);    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    	//Set Layout    	Container mainWindow = getContentPane();    	mainWindow.setLayout(mainLayout);    	//Set Action Commands (For multiple compenent usage)		j1.setActionCommand("newGame");    	//Set ActionListeners    	j1.addActionListener(this);    	j2.addActionListener(this);    	j3.addActionListener(this);    	j4.addActionListener(this);    	j5.addActionListener(this);    	j6.addActionListener(this);    	j7.addActionListener(this);    	addMouseListener(this);    	m1.add(j1);    	    	m1.addSeparator();    	m1.add(j2);    	m1.add(j3);    	m1.add(j4);    	m1.addSeparator();    	m1.add(j6);    	m2.add(j5);    	m2.add(j7);   	    	    	mb1.add(m1);    	mb1.add(m2);    	    	setJMenuBar(mb1);    	    	//Reset tiles used for this game    	gameReset(xBound, yBound, mineCount);    			//Add in Content		mainWindow.add(informationPane, BorderLayout.PAGE_START);		mainWindow.add(gamePane, BorderLayout.CENTER);		mainWindow.add( new JLabel(" "), BorderLayout.PAGE_END);		mainWindow.add( new JLabel("  "), BorderLayout.LINE_START);		mainWindow.add( new JLabel(" "), BorderLayout.LINE_END);    	    	setVisible(true);     }    //Handle Events	public void actionPerformed(ActionEvent e) {		Object source = e.getSource();		if (e.getActionCommand() == "newGame"){//New Game Same Settings			gameReset(xBound, yBound, mineCount);		}else if (source == j2){//New Game Beginner Settings						xBound = 9;			yBound = 9;			mineCount = 10;			setSize(xBound*20+10,yBound*28+50);			gameReset(xBound, yBound, mineCount);					}else if (source == j3){//New Game Intermediate Settings						xBound = 16;			yBound = 16;			mineCount = 40;			gameReset(xBound, yBound, mineCount);			setSize(xBound*20+10,yBound*28+40);		}else if (source == j4){//New Game Expert Settings						xBound = 30;			yBound = 16;			mineCount = 99;			setSize(xBound*20+10,yBound*28+30);			gameReset(xBound, yBound, mineCount);		}else if (source == j7){//Show About Minesweeper			//JOptionPane.showMessageDialog(null, "Minesweeper v 1.0", "Developed by Michael Cecconet", "June 15, 2007");			showAll();		}else if (source == t){//Timer.			timeTaken ++;		}else if (source == t && gameOver == true){//GameOver Display all mines piggybacks with the timer (Probably more efficent way to accomplish this but I'll worry about it later.)			for (int x = 0; x < xBound; x++){    			for (int y = 0; y < yBound; y++){    				if (grid[x][y].containsMine == true){    					grid[x][y].setVisible();    				}   			    			}			}		}		//All Events will update the window.		repaint();    	validate();  	    }		//Mouse Handling Events   	public void mouseClicked(MouseEvent e){   		if (gameOver == true){   			gameReset(xBound, yBound, mineCount);   			gameOver = false;   		}   	}   	public void mouseEntered(MouseEvent e){   	}   	public void mouseExited(MouseEvent e){   	}   	public void mousePressed(MouseEvent e){  	}   	public void mouseReleased(MouseEvent e){   	}    		   	//Game Methods   	//Defines all the settings for a new game.   	   	public void gameReset(int xBound, int yBound, int mineCount){   		   		int tempX, tempY;   		int tempMineCount = 0;   		Random generator = new Random();    	for (int x = 0; x < xBound; x++){    		for (int y = 0; y < yBound; y++){    			grid[x][y] = new Tile();    			//gridShow[x][y] = new JLabel(x+" "+y,baseTile, 0);    			gridShow[x][y] = new JLabel( baseTile);    		}    	}    	for (int i = 0; i < mineCount; i ++){    		do{    			tempX = generator.nextInt(xBound);    			tempY = generator.nextInt(yBound);    		} while (grid[tempX][tempY].containsMine == true);    		    		grid[tempX][tempY].setMine();    		//System.out.println(tempX+", "+tempY);			    	}    	//Set the Number of mines for all spaces. (Egad, 4 nested for loops, good thing it only runs on creation of a new game)    	for (int x = 0; x < xBound; x++){    		for (int y = 0; y < yBound; y++){    			if (grid[x][y].containsMine == false){    				if ( x != 0 && y != 0 && x != xBound-1 && y != yBound-1){// Make sure it's not at the edge    					for (int xi = 0; xi < 3; xi ++){    						for (int yi = 0; yi < 3; yi ++){    						    							if (grid[x+xi-1][y+yi-1].containsMine == true){    								tempMineCount ++;    							}    							//System.out.println(x+", "+y+" search : "+(x+xi-1)+" x result "+ (y+yi-1)+" y result : "+grid[x+xi-1][y+yi-1].containsMine+ " now total of :"+ tempMineCount);    						}    					}    				}else{ // At edge of board    					int xModifier, yModifier;//Modifier Defaults at 0 (Changes to 1 / -1 as needed)    					xModifier = 0;    					yModifier = 0;    					if (x == 0 || x == xBound){// Turns on X Modifier    						xModifier = x > 0 ? -1 : 1;    					}    					if (y == 0 || y == yBound){// Turns on Y Modifier    						yModifier = y > 0 ? -1 : 1;    					}	    					if (xModifier*yModifier != 0){//Handle Corner Calculation    						for(int xi = 0; xi < 2; xi++){    							for (int yi = 0; yi < 2; yi++){    								System.out.println(x+", "+y+" search : "+(x+xi-1+xModifier)+" x result "+ (y+yi-1+yModifier)+" y result ");    								if (grid[x+xi-1+xModifier][y+yi-1+yModifier].containsMine == true){    									tempMineCount ++;    									    								}    								    							}    						}    					}else{//Handle Border Calculation    						for(int xi = 0; xi < 3 - xModifier; xi++){    							for (int yi = 0; yi < 3 - yModifier; yi++){    								System.out.println(x+", "+y+" search : "+(x+xi-1+xModifier)+" x result "+ (y+yi-1+yModifier)+" y result ");    								//if (grid[x+xi-1+xModifier][y+yi-1+yModifier].containsMine == true){    								//	tempMineCount ++;    								//}    								    							}    						}    					}    				}       			grid[x][y].setNumber(tempMineCount);    			tempMineCount = 0;   				    				    			}    			    		}    	}    	gameGrid = new GridLayout(yBound,xBound, 0, 0);    	flagCount = mineCount;    	//Refresh Information JPanel    	informationPane.removeAll();    	gameStatus.addActionListener(this);    	gameStatus.setActionCommand("newGame");    	informationPane.add(gameStatus);    	//Refresh Game JPanel    	gamePane.removeAll();    	gamePane.setLayout(gameGrid);		for (int y = 0; y < yBound; y++){    		for (int x = 0; x < xBound; x++){    			gamePane.add(gridShow[x][y]);    		}    	}    	//Timer Reset    	startTime = false;    	timeTaken = 0;    	t.restart();    	t.stop();  	   		   	}   	//Fights the evil powers that be that prevent declaring array elements globally.   	public static void saveImages(){   		mineImage[0] = new ImageIcon("images/blank.png", "0 mines surronding");    	mineImage[1] = new ImageIcon("images/1.png", "1 mine surronding");    	mineImage[2] = new ImageIcon("images/2.png", "2 mines surronding");    	mineImage[3] = new ImageIcon("images/3.png", "3 mines surronding");    	mineImage[4] = new ImageIcon("images/4.png", "4 mines surronding");    	mineImage[5] = new ImageIcon("images/5.png", "5 mines surronding");    	mineImage[6] = new ImageIcon("images/6.png", "6 mines surronding");    	mineImage[7] = new ImageIcon("images/7.png", "7 mines surronding");    	mineImage[8] = new ImageIcon("images/8.png", "8 mines surronding");   	}   	//Debug Method - Show All Squares   	public void showAll(){   		gamePane.removeAll();   		for (int y = 0; y < yBound; y++){    		for (int x = 0; x < xBound; x++){    			if (grid[x][y].containsMine == false){    				gridShow[x][y] = new JLabel(mineImage[grid[x][y].number]);    				    				gamePane.add(gridShow[x][y]);    			}else if (grid[x][y].containsMine == true){    				gridShow[x][y] = new JLabel(flagTile);    				gamePane.add(gridShow[x][y]);    			}    			    		}    	}   	}   	   	//Main Method		public static void main(String[] args){						System.out.println("Check");			saveImages();									Minesweeper game = new Minesweeper();	}   }