Skip to content

Commit

Permalink
Source Files
Browse files Browse the repository at this point in the history
  • Loading branch information
developerjeffreywong authored Jan 29, 2019
1 parent 6df2314 commit 533e0e3
Show file tree
Hide file tree
Showing 4 changed files with 541 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/DrawingPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Drawing Panel Class<br />
* This is the class that is used to draw things on the
* screen.
*
* @author Jeffrey Wong
*
* MIT LICENSE
* Copyright (c) 2016 Jeffrey Wong ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class DrawingPanel extends JPanel implements MouseListener{
// the grid we are drawing
private MyGrid mg;

/**
* Default constructor for DrawingPanel<br />
* we create the grid we are using here and add a
* MouseListener and set this drawing panel to focus
*/
public DrawingPanel()
{
super();

// set this drawing panel to focused
// so we can get mouse events
setFocusable(true);
requestFocusInWindow();

// add our mouse listener to the drawing panel
addMouseListener(this);

// create the grid
mg=new MyGrid();
}

/**
* the method we use to draw things into our panel
* the only thing we draw is the grid
* @param g the graphics object we will be using
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
mg.draw(g);
}

//
public void mouseClicked(MouseEvent m)
{
// convert to grid coordinates
int row = m.getY()/(mg.getRectHeight()+mg.getMargin());
int col = m.getX()/(mg.getRectWidth() + mg.getMargin());
String gameOverMessage = "";
boolean gameOver = false;
//System.out.println(m.getX() + " " + m.getY() + " " + col + " " + row);
//mg.test(row, col);

// update the grid
mg.updateGrid(row, col);

// update the screen
repaint();

// check if dead
if(mg.checkDead(row, col))
{
gameOverMessage="You Lose!";
gameOver = true;
}

// check if win
if(mg.playerWin())
{
gameOverMessage="You Win!";
gameOver = true;
}

// end game if game over
if(gameOver)
{
JOptionPane.showMessageDialog(null, gameOverMessage);
System.exit(0);
}
}

// useless mouse interface methods that we must have
// due to using an interface
public void mouseEntered(MouseEvent m){}
public void mouseExited(MouseEvent m){}
public void mousePressed(MouseEvent m){}
public void mouseReleased(MouseEvent m){}
}
66 changes: 66 additions & 0 deletions src/MineSweeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* The MineSweeper Main Class
*
* @author Jeffrey Wong
*
* MIT LICENSE
* Copyright (c) 2016 Jeffrey Wong ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import java.awt.Container;
import javax.swing.JFrame;
public class MineSweeper extends JFrame{

// width and height of the window
public static final int WIDTH = 500;
public static final int HEIGHT = 500;

// position of the window on the screen
public static final int XPOS = 0;
public static final int YPOS = 0;


// our DrawingPanel to display the grid
private DrawingPanel dp;

// the constructor
public MineSweeper()
{
// call the original JFrame constructor
super();

// create a new DrawingPanel
dp = new DrawingPanel();
// get the JFrame container that holds all
// the elements
Container c = getContentPane();

// add our Drawing Panel to the JFrame
c.add(dp);
}

public static void main(String [] args)
{
MineSweeper window = new MineSweeper();
window.setBounds(XPOS, YPOS, WIDTH, HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
82 changes: 82 additions & 0 deletions src/MyGrid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import java.awt.Color;
public class MyGrid extends TheGrid{

// the grid is modelled by a 3D array
// refer to documentation about TheGrid.java
private int [][][] grid;

// YOU DO NOT NEED ANY OTHER GLOBAL VARIABLES //

/**
* Default Constructor <br />
* You only need to use this constructor<br />
* This constructor will create the grid from TheGrid class
* with the bombs but you need to fill in the grid with the
* correct numbers as hints so the player can play the game
*/
public MyGrid()
{
/* because MyGrid class extends TheGrid class
* we have to first make TheGrid object using
* the the parent constructor
*
* you can only use super once, so comment the
* one you are not using
*
* refer to TheGrid documentation*/

// default constructor of TheGrid class
//super();

// second constructor of TheGrid class
super(10,10,25,4);

// make our own copy of the grid for this class
grid = getGrid();

/* figure out the correct numbers (hints)
* to place in the grid below
*
* you can also modify how the grid looks here
*/
TESTING=false; // turn on or off testing mode
}

/**
* checks to see if the player clicked on a bomb
* @param row the row that was clicked
* @param col the col that was clicked
* @return true if player clicks on a bomb
*/
public boolean checkDead(int x, int y)
{
return false;
}

/**
* checks to see if the player has won the game <br />
* the player wins when only the rectangles with the
* bombs are hidden
* @return true if the player wins
*/
public boolean playerWin()
{
return false;
}

/**
* Update the Grid<br />
* the status of the rectangle clicked by the user should
* change to show<br />
* if the value of the grid is 0, it should keep unhiding
* all rectangles around it until there is a number
* @param r the row that was clicked
* @param c the column that was clicked
*/
public void updateGrid(int r, int c)
{
// change status of the rectangle clicked to SHOW
grid[r][c][1]=SHOW;
}

}
Loading

0 comments on commit 533e0e3

Please sign in to comment.