Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions src/nick/sweeper/main/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import nick.sweeper.ai.AILogic;
import nick.sweeper.main.Tile.Type;
Expand Down Expand Up @@ -39,6 +40,8 @@ public class Grid {
private final MineSweeper game;

private Tile highlight;

private BufferStrategy bs;

public Grid(final int xSize, final int ySize, final int mines, final MineSweeper gameObj) {

Expand All @@ -51,6 +54,7 @@ public Grid(final int xSize, final int ySize, final int mines, final MineSweeper
numMines = mines;
game = gameObj;


grid = new Tile[sizeX][sizeY];
initGrid( );
}
Expand All @@ -71,11 +75,27 @@ private final void clearEmptyNeighbors(final Tile t) {
}
}

public void draw(final Graphics g) {
public void addGraphics(BufferStrategy bs) {
this.bs = bs;

// Needs to be called multiple times to fill the buffer
// Not quite perfect and doesn't work all the time
draw();
draw();
draw();
}

public void draw() {
Graphics g = bs.getDrawGraphics();
setOffsets(game.getWidth( ) / 2, game.getHeight( ) / 2);
update( );

g.setColor(Color.BLACK);
g.fillRect(0, 0, game.getWidth( ), game.getHeight( ));

drawTileGrid(g);

if (highlight != null) {
if (highlight != null && drawHighlight) {
// Tile X and Y
final int tX = highlight.getX( ) - 1;
final int tY = highlight.getY( ) - 1;
Expand Down Expand Up @@ -103,6 +123,11 @@ public void draw(final Graphics g) {

g.setColor(Color.GREEN);
g.drawString(win, rX, rY);

g.dispose( );
bs.show( );
// game.stop(false);
game.restart();
} else if (hitMine) {
g.setFont(new Font("Courier New", Font.BOLD, squareDrawSize));
final String lose = "Hit a Mine!";
Expand All @@ -116,7 +141,20 @@ public void draw(final Graphics g) {

g.setColor(Color.RED);
g.drawString(lose, rX, rY);


g.dispose( );
bs.show( );
// game.stop(true);
game.restart();
}

final String basePrint = MineSweeper.name+" (" + sizeX( ) + ", " + sizeY( ) + ") | Flags Used: " + flagsUsed( ) + " | Mines: " + numMines( ) + " | " + String.format("%.2f", percentComplete( )) + "% Complete | AI Engaged: " + MineSweeper.getAI().isAlive( );

game.setTitle(basePrint);

g.dispose( );
bs.show( );
}

private final void drawTileGrid(final Graphics g) {
Expand Down Expand Up @@ -148,9 +186,13 @@ private void initGrid( ) {
int minesToPlace = numMines;
for (int x = 0; x < sizeX; x++) {
for (int y = 0; y < sizeY; y++) {
if(x < 2 && y < 2) {
grid[x][y] = new Tile(this, x, y, false);
continue;
}
final int spotsLeft = (totalSquares( ) - y) - (x * sizeY);

final double chanceOfMine = (double) (minesToPlace) / spotsLeft;
final double chanceOfMine = (double) (minesToPlace) / (spotsLeft - 4);
if (Math.random( ) <= chanceOfMine) {
grid[x][y] = new Tile(this, x, y, true);
minesToPlace--;
Expand Down Expand Up @@ -229,7 +271,6 @@ public void onClick(final int pX, final int pY, final boolean flag) {
if (s != null) {
onClick(s, flag);
}

}

public void onClick(final Tile s, final boolean flag) {
Expand Down Expand Up @@ -264,6 +305,8 @@ public void onClick(final Tile s, final boolean flag) {
}

clearEmptyNeighbors(s);

draw();
}

public float percentComplete( ) {
Expand All @@ -280,7 +323,6 @@ public float percentMines( ) {
}

public Dimension renderSize( ) {

return new Dimension(sizeX * squareDrawSize, sizeY * squareDrawSize);
}

Expand Down Expand Up @@ -357,14 +399,9 @@ public void update( ) {
setHighLight(tileAt(tX, tY));
}
}
if (numKnown( ) == totalSquares( )) {
if (numKnown( ) == totalSquares( ) || numKnown() - flagsUsed() == totalSquares() - numMines()) {
completed = true;
}

if (hitMine) {
game.stop(true);
}

}

}
44 changes: 37 additions & 7 deletions src/nick/sweeper/main/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import nick.sweeper.ai.AILogic;
import radar.sweeper.draw.MousePath;

public class Input implements MouseListener, MouseMotionListener, KeyListener {

private static int mX, mY;
Expand All @@ -27,18 +30,30 @@ public static int mouseY( ) {
return mY;
}

private final Grid g;

public Input(final Grid g) {
private Grid g;

private MousePath mousePath;

public Input() {
}

public void setGrid(Grid g) {
this.g = g;
}


public void setMousePath(MousePath mp) {
this.mousePath = mp;
}

@Override
public void keyPressed(final KeyEvent e) {

if (e.getKeyCode( ) == KeyEvent.VK_A) {
MineSweeper.toggleAI( );
if(!MineSweeper.getAI().isAlive()) {
MineSweeper.getAI().start();
}else {
AILogic.halt();
}
}
}

Expand All @@ -55,7 +70,7 @@ public void keyTyped(final KeyEvent e) {
@Override
public void mouseClicked(final MouseEvent e) {

g.onClick(e.getX( ), e.getY( ), e.getButton( ) != MouseEvent.BUTTON1);
// g.onClick(e.getX( ), e.getY( ), e.getButton( ) != MouseEvent.BUTTON1);

}

Expand All @@ -81,11 +96,26 @@ public void mouseMoved(final MouseEvent e) {

mX = e.getX( );
mY = e.getY( );

if(mousePath != null) {
mousePath.hoveredLocation(mX, mY);
}

if(Grid.drawHighlight) {
g.draw();
}
}

@Override
public void mousePressed(final MouseEvent arg0) {
public void mousePressed(final MouseEvent e) {

if(e.getButton() == MouseEvent.BUTTON1) {
g.onClick(e.getX( ), e.getY( ), false);
return;
}
if(e.getButton() == MouseEvent.BUTTON3) {
g.onClick(e.getX( ), e.getY( ), true);
}
}

@Override
Expand Down
Loading