Minesweeper

Status
Nicht offen für weitere Antworten.

Icke

Mitglied
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:

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:

Marco13

Top Contributor
Den Teil
Code:
                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;
kannst du mal im "Java Quiz" posten, mit der Frage "Was wird dort gemacht?" :D

Ansonsten müßtest du die Frage etwas präzisieren.

BTW: Hast du den Punkt bedacht, dass der Benutzer die Mine (es heißt Mine, nicht Miene...) bei (0,0) sucht? (ArrayIndexOutOfBoundsException muß verhindert werden!)
 

chri

Mitglied
[JAVA=84]
if (user[i-1][j-1]==1) anzahl=+1;
else if (user[j-1]==1) anzahl=+1;
else if (user[i+1][j-1]==1) anzahl=+1;
...
[/code]
heißt soviel wie
ist irgend ein feld rundherum schon geöffnet worden ist die anzahl 1 - und nicht mehr.
richtiger wäre anzahl += 1 aber immer noch nicht ganz wegen den elses

d.h. nicht die geöffneten felder abfragen sondern die minen (ich nehme an es sollen keine smilies drunter liegen ;) )
und diese mit ifs ohne elses abfragen.

So in etwa habe ichs damals gelöst
Java:
//relative position zum zählenden Feld
		int[][] rundherum = {{-1,-1},{-1,0},...,{0,-1}};
		for(int n=0;n<8;n++)
		{
			int xPos = i+rundherum[n][0];
			if(xPos <0 || xPos> 8) continue;
			int yPos = i+rundherum[n][1];
			if(yPos <0 || yPos> 8) continue;
			
			if(minen[xPos][yPos] == 1) anzahl++;
		}


außerdem würde ich nicht jedesmal neuzeichnen alle felder neu berechnen sondern zB im user[][] die anzahl speichern, 0-8 = anzahl, 9=flag, 10=?, 11= mine. Dann wird nur mehr beim aufdecken einmal gezählt und im draw reicht ein switch.
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben