//  Scott DeRuiter          1/23/01
//  Minefield.java
//  Use the left, right, up, and down keys to navigate around the
//  minefield.
//	3/13/2013 Greenstein: changed applet to JFrame


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


public class Minefield implements KeyListener, FocusListener, MouseListener   
{
	private MinefieldCell [][] board;
	private int x = 0, y = 0;
	private int numberofbad = 6;
	private boolean done = false, firsttime;
	
	private JFrame frame;
	private PaintPanel canvas;

	public Minefield ( )   
	{
		SetUpBoard ( );
	}
	
	public static void main(String[] args) 
	{
		Minefield mf = new Minefield();
		mf.Run();
	}

	public void SetUpBoard ( )  
	{
		x = y = 0;
		numberofbad = 6;
		done = false;
		firsttime = true;
		board = new MinefieldCell[8][8];

		for ( int i = 0; i < 8; i++ )   
			for ( int k = 0; k < 8; k++ ) 
				board[i][k] = new MinefieldCell();
		PlaceBadCells ( );
		for ( int i = 0; i < 8; i++ )   
			for ( int k = 0; k < 8; k++ )   
				CountBadNeighbors (i, k);
	}

	public void Run ()  
	{ 
		// Create a frame to hold everything
		frame = new JFrame ("Minefield");
		frame.setSize(600, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//frame.setBackground(Color.black);	// only needed if frame is larger than panel
		
		// Define panel to draw on
		canvas = new PaintPanel();		// create a panel to draw on
      		canvas.setBackground(Color.gray);
		canvas.addFocusListener(this);
		canvas.addKeyListener(this);
		canvas.addMouseListener(this);	// connects the MouseListerner to the panel window
		
		// Put frame together
		frame.getContentPane().add(canvas);	// puts panel on frame
		frame.setVisible(true);
	}
   
	class PaintPanel extends JPanel 
	{

		public void paintComponent(Graphics g) 
		{
			super.paintComponent(g);
			if ( firsttime )   
			{
				GiveDirections ( g );
			}
			else   
			{
				for ( int i = 0; i < 8; i++ )   
					for ( int k = 0; k < 8; k++ )   
						board[i][k].drawCell( g,  i * 40 + 117, k * 40 + 17, 36, 36 ); 
				board[x][y].changeToOnCell(  ); 
				board[x][y].changeToCellVisited(  ); 
				board[x][y].drawCell( g,  x * 40 + 117, y * 40 + 17, 36, 36 ); 
			}
		}
	}

	public void focusGained(FocusEvent evt) 
	{
		firsttime = false;
		canvas.repaint();
	}
   
	public void focusLost(FocusEvent evt) 
	{
		firsttime = true;
		canvas.repaint();
	}

	public void mousePressed(MouseEvent evt) 
	{
		canvas.requestFocus();
	}   
   
   	public void mouseEntered(MouseEvent evt) { }
	public void mouseExited(MouseEvent evt) { }
	public void mouseReleased(MouseEvent evt) { }
	public void mouseClicked(MouseEvent evt) { }
   
	public void keyTyped ( KeyEvent e )    {}

	public void keyPressed ( KeyEvent e )    
	{
		Graphics g = canvas.getGraphics();
		int value = e.getKeyCode();
		if ( value == KeyEvent.VK_SPACE )   
		{
			SetUpBoard ( );
			firsttime = false;
			canvas.repaint ( );
		}
		else if (( value == KeyEvent.VK_RIGHT  || value == KeyEvent.VK_LEFT || 
				value == KeyEvent.VK_UP || value == KeyEvent.VK_DOWN ) && !done ) 
		{
			board[x][y].changeToOffCell(  ); 
			board[x][y].drawCell( g,  x * 40 + 117, y * 40 + 17, 36, 36 ); 
			if ( value == KeyEvent.VK_RIGHT && x < 7 ) 
					x++;
			else if ( value == KeyEvent.VK_LEFT && x > 0 )
					x--;
			else if ( value == KeyEvent.VK_UP && y > 0 )    
					y--;
			else if ( value == KeyEvent.VK_DOWN && y < 7 ) 
					y++;
			board[x][y].changeToOnCell(  ); 
			board[x][y].changeToCellVisited(  );
			board[x][y].drawCell( g,  x * 40 + 117, y * 40 + 17, 36, 36 ); 
			if ( board[x][y].checkForBadCell ( ) || (x == 7 && y == 7) ) {
				done = true;
				DrawFinalBoard (g);
		}
	}

	public void keyReleased ( KeyEvent e )   {}

	public void GiveDirections ( Graphics g )   
	{
		Font direct  = new Font ( "Serif", Font.BOLD, 40 );
		g.setFont ( direct );
		g.setColor ( Color.blue );
		g.drawString ( "CLICK ON THE APPLET", 50, 100 );
		g.drawString ( "TO BEGIN OR RESUME", 55, 200 );
		g.drawString ( "THE GAME", 180, 300 );
	}

	public void PlaceBadCells ( )  
	{
		int badx, bady; 
		for ( int v = 1; v <= numberofbad; v++ )  
		{
			badx = (int)( Math.random() * 8 );
			bady = (int)( Math.random() * 8 );
			if ( (badx != 0 || bady != 0) && !board[badx][bady].checkForBadCell() &&
					(badx != 7 || bady != 7) )  
				board[badx][bady].changeToBadCell(  );
			else
				v--; 
		}
	}

	public void CountBadNeighbors (int i, int k)  
	{
		for ( int j = -1; j < 2; j++ )
			for ( int m = -1; m < 2; m++ )  
				if ( i + j >= 0 && i + j <= 7 && k + m >= 0 && k + m <= 7 )
					if ( board[i+j][k+m].checkForBadCell( ) )
						board[i][k].incrementBadNeighbors( );
	}

	public void DrawFinalBoard ( Graphics g ) 
	{
		for ( int i = 0; i < 8; i++ )   
			for ( int k = 0; k < 8; k++ )   
		{
				board[i][k].changeToCellVisited ( );
				board[i][k].drawCell( g,  i * 40 + 117, k * 40 + 17, 36, 36 ); 
				WinOrLoseMessage ( g );
		}
	}

	public void WinOrLoseMessage ( Graphics g )  
	{
		g.setColor ( Color.red );
		Font letters  = new Font("Serif", Font.BOLD, 20);
		g.setFont(letters);
		if ( x == 7 && y == 7 )  
		{
			g.drawString ( "YOU", 20, 40 );
			g.drawString ( "ARE", 20, 70 );
			g.drawString ( "A", 20, 100 );
			g.drawString ( "WINNER!", 20, 130 );
		}
		else  
		{
			g.drawString ( "Sorry,",20, 40 );
			g.drawString ( "but",20, 70 );
			g.drawString ( "this",20, 100 );
			g.drawString ( "time",20, 130 );
			g.drawString ( "you",20, 160 );
			g.drawString ( "lost.",20, 190 );
		}
		g.drawString ( "Hit", 460, 40 );
		g.drawString ( "Spacebar", 460, 70 );
		g.drawString ( "to", 460, 100 );
		g.drawString ( "play", 460, 130 );
		g.drawString ( "again.", 460, 160 );
	}
}

class MinefieldCell        
{
	private int badneighbors;
	private boolean visited;
	private boolean oncell;
	private boolean badcell;

	public MinefieldCell ()  
	{
		badneighbors = 0;
		visited = false;
		oncell = false;
		badcell = false;
	}

	public void drawCell ( Graphics g, int xvalue, int yvalue, int width, int height )   
	{
		g.setColor ( Color.blue );
		if ( oncell )
			g.setColor ( Color.red );
		else if ( visited )
			g.setColor ( Color.white );
		g.fillRect ( xvalue, yvalue, width, height );
		if ( oncell || visited )   
		{
			g.setColor ( Color.black );
			Font letters  = new Font("Serif", Font.BOLD, height);
			g.setFont(letters);
			if ( badcell )
				g.drawString ( "*", xvalue + width / 4, yvalue + 11 * height  / 12 );
			else
				g.drawString ( "" + badneighbors, xvalue + width / 4, yvalue + 11 * height  / 12 );
		}
	}
	
	public void changeToOnCell ( )   
	{	       
		oncell = true;         
	}
	
	public void changeToOffCell ( )   
	{         
		oncell = false;        
	}

	public void changeToCellVisited ( )    
	{   
		visited = true;         
	}

	public void changeToBadCell ( )    
	{        
		badcell = true;       
	}

	public void incrementBadNeighbors ( )   
	{ 
		badneighbors++;     
	}

	public boolean checkForBadCell ( )   
	{
		if ( badcell )
			return true;
		else
			return false;
	}
}

Back to Lesson 26 Examples

Back to Java Main Page