Huhu. Ich bin dabei Minesweeper in java zu schreiben. Ich wollte nun die anzahl der minen im umfeld in das angeklickte feld zu schreiben. der array user[][] ist das feld welches der user angeklickt hat und in mienen[][] sind die 8 mienen gespeichert, welche vorhanden sind. hier ist meine überlegung , wie ich herausfinden kann, wie viele mienen im umfeld sind:
diese 8 felder müssen überprüft werden:
user[i-1][j-1]
user[j-1]
user[i+1][j-1]
user[i-1][j]
user[i+1][j]
user[i-1][j+1]
user[j+1]
user[i+1][j+1]
quellcode:
was haltet ihr davon? habt ihr sonst eine andere idee? .. bitte alles langsam erklären. bin totaler anfänger in Java ^^
Probelme : er macht halt nicht die felder die außen liegen, da er ja nicht -1 arrays haben kann und er macht überall eine 1 hin auch wenn vorher eine 0 da wa ^^..
diese 8 felder müssen überprüft werden:
user[i-1][j-1]
user[j-1]
user[i+1][j-1]
user[i-1][j]
user[i+1][j]
user[i-1][j+1]
user[j+1]
user[i+1][j+1]
quellcode:
Java:
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;
public class minesweeper implements AnimationListener {
private static final int WIDTH = 640;
private static final int HEIGHT = 480;
private static final int COORDINATES_COUNT = 3;
int[] coordX = new int[COORDINATES_COUNT];
int[] coordY = new int[COORDINATES_COUNT];
boolean[] directionsX = new boolean[COORDINATES_COUNT];
boolean[] directionsY = new boolean[COORDINATES_COUNT];
/**
* Constructor
*/
int[][] user = new int[8][8];
int[][] mienen = new int[8][8];
public minesweeper() {
new AnimatedWindow("My Animation!", WIDTH, HEIGHT, this, AnimatedWindow.PERIODIC_UPDATE_25_FPS);
Random rand = new Random();
for (int i=0;i<8;i++) {
int x = rand.nextInt(8 );
int y = rand.nextInt(8 );
if (mienen[x][y]) {
i--;
} else {
mienen[x][y] = 1;
}
}
}
/**
* Callback: Prepare the set of coordinates for the next frame.
*/
public void prepareNextFrame() {
}
/**
* Callback: Draw the animation using the current set of coordinates calculated by
* prepareNextFrame() using the given Graphics context.
*
* @param g Graphics The given Graphics context to draw to.
*/
public void drawFrame(Graphics2D g) {
g.setColor(Color.DARK_GRAY );
g.fillRect(0,0,WIDTH,HEIGHT);
g.setColor(Color.BLACK);
int w = WIDTH;
int h = HEIGHT;
for (int i=1;i<8;i++) {
g.drawLine(i*w/8,0,i*w/8,h);
}
for (int i=1;i<8;i++) {
g.drawLine(0,i*h/8,w,i*h/8);
}
int dw = w/8;
int dh = h/8;
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
if (user[i][j]==1) {
if (mienen[i][j]) {
g.setColor(Color.LIGHT_GRAY );
g.fillRect(i*(w/8),j*(h/8), w/8-1, h/8-1);
g.setColor(Color.BLACK);
g.drawString("Miene",dw*i+20,dh*j+20);
} else {
g.setColor(Color.LIGHT_GRAY );
g.fillRect(i*(w/8),j*(h/8), w/8-1, h/8-1);
g.setColor(Color.BLACK);
int anzahl = 0;
if (user[i-1][j-1]==1) anzahl=+1; else if (user[i][j-1]==1) anzahl=+1; else
if (user[i+1][j-1]==1) anzahl=+1; else if (user[i-1][j]==1) anzahl=+1; else
if (user[i+1][j]==1) anzahl=+1; else if (user[i-1][j+1]==1) anzahl=+1; else
if (user[i][j+1]==1) anzahl=+1; else if (user[i+1][j+1]==1) anzahl=+1;
g.drawString(" "+ anzahl +" ",dw*i+20,dh*j+20);
anzahl=0;
}
} else if (user[i][j]==2) {
g.setColor(Color.LIGHT_GRAY );
g.fillRect(i*(w/8),j*(h/8), w/8-1, h/8-1);
g.setColor(Color.BLACK);
g.drawString("Flagge",dw*i+20,dh*j+20);
}
else if (user[i][j]==3) {
g.setColor(Color.LIGHT_GRAY );
g.fillRect(i*(w/8),j*(h/8), w/8-1, h/8-1);
g.setColor(Color.BLACK);
g.drawString("?",dw*i+20,dh*j+20);
}
}
}
}
public void mousePressed(int posX, int posY) {
int x = posX*8/WIDTH;
int y = posY*8/HEIGHT;
user[x][y] = (user[x][y]+1)%4;
}
public void mouseReleased(int posX, int posY) {
// TODO Auto-generated method stub
}
public void mouseMoved(int posX, int posY) {
// TODO Auto-generated method stub
}
public void mouseDragged(int posX, int posY) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new minesweeper();
}
}
was haltet ihr davon? habt ihr sonst eine andere idee? .. bitte alles langsam erklären. bin totaler anfänger in Java ^^
Probelme : er macht halt nicht die felder die außen liegen, da er ja nicht -1 arrays haben kann und er macht überall eine 1 hin auch wenn vorher eine 0 da wa ^^..
Zuletzt bearbeitet: