Created
October 24, 2018 17:46
-
-
Save feehe21/604c7307a641c35cbd0e15540a065a70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
import java.awt.Dimension; | |
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.event.MouseListener; | |
import java.awt.event.MouseEvent; | |
import java.awt.Font; | |
import java.awt.FontMetrics; | |
import java.awt.*; | |
public class MineSweeper2 { | |
private JFrame frame; | |
public MineSweeper2() { | |
frame = new JFrame("Mine Sweeper"); | |
frame.setSize(600, 400); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.setPreferredSize(frame.getSize()); | |
frame.add(new MineDraw(frame.getSize())); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public static void main(String... argv) { | |
new MineSweeper2(); | |
} | |
public static class MineDraw extends JPanel implements MouseListener { | |
int startX = 10; | |
int startY = 10; | |
int side = 40; | |
int turn = 0; | |
int yPlace = 0; | |
int[][] grid = new int[8][8]; | |
boolean[][] show = new boolean[8][8]; | |
int max = 8; | |
int min = 0; | |
int randomNum = 0; | |
int randomNum2 = 0; | |
boolean start = true; | |
boolean first = true; | |
boolean bomb = false; | |
int cleared = 0; | |
public MineDraw(Dimension dimension) { | |
setSize(dimension); | |
setPreferredSize(dimension); | |
addMouseListener(this); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
Graphics2D g2 = (Graphics2D)g; | |
Dimension d = getSize(); | |
startX = 0; | |
startY = 0; | |
cleared = 0; | |
//1) draw grid of squares | |
//2) draw numbers in the squares | |
/* | |
* g2.setColor(Color.red); | |
g2.setFont (new Font("TimesRoman", Font.PLAIN, 20)); | |
g2.drawString(""+grid[row][col] , x,y); | |
3) assign -1 to mines. Randomly assign this to ten spots | |
*/ | |
for (int row = 0; row < grid.length; row++) { | |
for (int col = 0; col < grid[0].length; col++) { | |
if(!show[row][col]){ | |
g2.setColor(Color.gray); | |
g2.fillRect(startX,startY,side,side); | |
g2.setColor(Color.black); | |
g2.drawRect(startX,startY,side,side); | |
}else{ | |
g2.setColor(Color.white); | |
g2.fillRect(startX,startY,side,side); | |
g2.setColor(Color.black); | |
g2.drawRect(startX,startY,side,side); | |
} | |
//g2.setColor(Color.black); | |
//g2.drawRect(startX,startY,side,side); | |
g2.setColor(Color.red); | |
if(show[row][col] && grid[row][col] != 0){ | |
g2.setFont (new Font("TimesRoman", Font.PLAIN, 20)); | |
g2.drawString("" + grid[row][col], startX + side/2,startY + side - side/3); | |
} | |
startX += side; | |
if(show[row][col] && grid[row][col] > -1){ | |
cleared ++; | |
} | |
} | |
startX = 0; | |
startY += side; | |
} | |
if(cleared == 54){ | |
System.out.println("You win!"); | |
} | |
} | |
public void randomAssign(int x,int y){ | |
for(int i = 0; i < 10; i ++){ | |
randomNum = (int)(Math.random() * ((max - min) )) + min; | |
randomNum2 = (int)(Math.random() * ((max - min) )) + min; | |
if(grid[randomNum][randomNum2] == -1){ | |
i--; | |
}else if(x == randomNum2 && y == randomNum){ | |
i--; | |
}else{ | |
grid[randomNum][randomNum2] = -1; | |
assignNumbers(randomNum2, randomNum); | |
} | |
} | |
} | |
public void assignNumbers(int x, int y){ | |
if(x > 0 && y > 0 && grid[y-1][x-1]!=-1){ | |
grid[y-1][x-1]++; | |
} | |
if(x > 0 && grid[y][x-1]!=-1){ | |
grid[y][x-1]++; | |
} | |
if(y > 0 && grid[y-1][x]!=-1){ | |
grid[y-1][x]++; | |
} | |
if(x < grid[0].length-1 && y < grid.length-1 && grid[y+1][x+1]!=-1){ | |
grid[y+1][x+1]++; | |
} | |
if(y < grid.length-1 && grid[y+1][x]!=-1){ | |
grid[y+1][x]++; | |
} | |
if(x < grid[0].length-1 && grid[y][x+1]!=-1){ | |
grid[y][x+1]++; | |
} | |
if(x < grid[0].length-1 && y > 0 && grid[y-1][x+1]!=-1){ | |
grid[y-1][x+1]++; | |
} | |
if(x > 0 && y < grid.length-1 && grid[y+1][x-1]!=-1){ | |
grid[y+1][x-1]++; | |
} | |
} | |
public void mousePressed(MouseEvent e) { | |
int x = e.getX()/side;//column | |
int y = e.getY()/side;//row | |
// System.out.println(x + " " + y); | |
if(x<grid.length && y <grid[0].length && start){ | |
randomAssign(x,y); | |
start = false; | |
} | |
// | |
if(!bomb){ | |
repaint(); | |
} | |
} | |
public void mouseReleased(MouseEvent e) { | |
int x = e.getX()/side;//column | |
int y = e.getY()/side;//row | |
//System.out.println("mouse released"); | |
show[y][x] = true; | |
if(first){ | |
clearNextTo(x, y); | |
first = false; | |
} | |
if(grid[y][x] == 0){ | |
clearNextTo(x,y); | |
} | |
if(grid[y][x] == -1){ | |
System.out.println("You hit a bomb! You lose!"); | |
bomb = true; | |
} | |
if(!bomb){ | |
repaint(); | |
} | |
} | |
public void clearNextTo(int x, int y){ | |
if(x > 0 && y > 0 && grid[y-1][x-1]!=-1){ | |
show[y-1][x-1] = true; | |
} | |
if(x > 0 && grid[y][x-1]!=-1){ | |
show[y][x-1] = true; | |
} | |
if(y > 0 && grid[y-1][x]!=-1){ | |
show[y-1][x] = true; | |
} | |
if(x < grid[0].length-1 && y < grid.length-1 && grid[y+1][x+1]!=-1){ | |
show[y+1][x+1] = true; | |
} | |
if(y < grid.length-1 && grid[y+1][x]!=-1){ | |
show[y+1][x] = true; | |
} | |
if(x < grid[0].length-1 && grid[y][x+1]!=-1){ | |
show[y][x+1] = true; | |
} | |
if(x < grid[0].length-1 && y > 0 && grid[y-1][x+1]!=-1){ | |
show[y-1][x+1] = true; | |
} | |
if(x > 0 && y < grid.length-1 && grid[y+1][x-1]!=-1){ | |
show[y+1][x-1] = true; | |
} | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
public void mouseClicked(MouseEvent e) { | |
} | |
//not used | |
public void search0s (int x, int y){ | |
System.out.println("search 0s"); | |
boolean keepGoing = true; | |
if(clearNextTo(x,y, keepGoing)){ | |
System.out.println("clearNextTo = true"); | |
while(keepGoing){ | |
System.out.println("loop"); | |
for (int row = 0; row < grid.length; row++) { | |
for (int col = 0; col < grid[0].length; col++) { | |
if(show[row][col] && grid[row][col] == 0){ | |
System.out.println("shown 0"); | |
if(clearNextTo(x,y, keepGoing)){ | |
row = grid.length; | |
col = grid[0].length; | |
}else{ | |
row = grid.length; | |
col = grid[0].length; | |
keepGoing = false; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
//not used | |
public boolean clearNextTo(int x, int y, boolean go){ | |
boolean newZero = false; | |
System.out.println("clearNextTo"); | |
if(x > 0 && y > 0 && grid[y-1][x-1]!=-1){ | |
show[y-1][x-1] = true; | |
if(grid[y-1][x-1] == 0){ | |
newZero = true; | |
} | |
} | |
if(x > 0 && grid[y][x-1]!=-1){ | |
show[y][x-1] = true; | |
if(grid[y][x-1] == 0){ | |
newZero = true; | |
} | |
} | |
if(y > 0 && grid[y-1][x]!=-1){ | |
show[y-1][x] = true; | |
if(grid[y-1][x] == 0){ | |
newZero = true; | |
} | |
} | |
if(x < grid[0].length-1 && y < grid.length-1 && grid[y+1][x+1]!=-1){ | |
show[y+1][x+1] = true; | |
if(grid[y+1][x+1] == 0){ | |
newZero = true; | |
} | |
} | |
if(y < grid.length-1 && grid[y+1][x]!=-1){ | |
show[y+1][x] = true; | |
if(grid[y+1][x] == 0){ | |
newZero = true; | |
} | |
} | |
if(x < grid[0].length-1 && grid[y][x+1]!=-1){ | |
show[y][x+1] = true; | |
if(grid[y][x+1] == 0){ | |
newZero = true; | |
} | |
} | |
if(x < grid[0].length-1 && y > 0 && grid[y-1][x+1]!=-1){ | |
show[y-1][x+1] = true; | |
if(grid[y-1][x+1] == 0){ | |
newZero = true; | |
} | |
} | |
if(x > 0 && y < grid.length-1 && grid[y+1][x-1]!=-1){ | |
show[y+1][x-1] = true; | |
if(grid[y+1][x-1] == 0){ | |
newZero = true; | |
} | |
} | |
System.out.println("clearNextTo returns " + newZero); | |
return newZero; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment