Minesweeper

N

Nyk

Gast
Hallo, hier mein Code:

Code:
import java.util.ArrayList;
import java.util.Collection;


public class Board {
    
    private Collection<Field> felder = new ArrayList<Field>();
   
    private Field[][] spielfeld;
    private int mineCount;
    private int width;
    private int height;
    private int flagCount;
    public Board(int width, int height, Collection<Coordinate> mines) {
        this.spielfeld = new Field[width][height];
        this.mineCount = mines.size();
        this.width = width;
        this.height = height;
        // Felder initialisieren...
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                spielfeld[x][y] = new Field(new Coordinate(x, y));
            }
        }
        // Spielfeld mit Minen versehen
        for (Coordinate c : mines) {
            spielfeld[c.getX()][c.getY()].setHasMine(true);
        }
        // NeighboursMineCount setzen
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                spielfeld[x][y].setNeighbourMineCount(checkNeighbours(x, y));
            }
        }
    }
    public boolean hasWon() {
        if (getRemainingMines() == 0) {
            return true;
        }
        return false;
    }
    public void flagMine(Coordinate coord) {
        if (!spielfeld[coord.getX()][coord.getY()].isOpened()) {
            if (spielfeld[coord.getX()][coord.getY()].hasFlag()) {
                spielfeld[coord.getX()][coord.getY()].setHasFlag(false);
            } else {
                spielfeld[coord.getX()][coord.getY()].setHasFlag(true);
            }
        }
    }
    public int getRemainingMines() {
        flagCount = 0;
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (spielfeld[x][y].hasFlag()) {
                    flagCount++;
                }
            }
        }
        return mineCount - flagCount;
    }
    public Collection<Field> getMines() {
        Collection<Field> mines = new ArrayList<Field>();
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (spielfeld[x][y].hasMine()) {
                    mines.add(spielfeld[x][y]);
                }
            }
        }
        return mines;
    }
    public Collection<Field> getFields() {
        Collection<Field> temp = new ArrayList<Field>();
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                temp.add(spielfeld[x][y]);
            }
        }
        return temp;
    }
    public RevealFieldsResult revealFields(Coordinate coord) {
        Collection<Field> offeneFelder = new ArrayList<Field>();
        spielfeld[coord.getX()][coord.getY()].setOpened(true);
        offeneFelder.add(spielfeld[coord.getX()][coord.getY()]);
        if (spielfeld[coord.getX()][coord.getY()].hasMine()) {
            return new RevealFieldsResult(offeneFelder);
        }
       if (spielfeld[coord.getX()][coord.getY()].getNeighbourMineCount() == 0) {
               offeneFelder.addAll(openFields(coord.getX(), coord.getY(), false));
             
              
           }
        return new RevealFieldsResult(offeneFelder);
    }
    public RevealFieldsResult revealMultiClickFields(Coordinate coord) {
        Collection<Field> checkedFields = new ArrayList<Field>();
        Field thisField = spielfeld[coord.getX()][coord.getY()];
//         Ist das Feld noch nicht geöffnet oder ist das Feld geöffnet und es grenzt an keine Mine an, passiert nichts
//         (FIELD_NOT_REVEALED).
        if (!thisField.isOpened() || (thisField.isOpened() && thisField.getNeighbourMineCount() == 0)) {
            return new RevealFieldsResult(checkedFields);
        }
//        Stimmt die Anzahl der Flaggen auf den Nachbarfeldern mit der Anzahl der Minen
//        überein, werden alle Felder die KEINE Flagge haben aufgedeckt (siehe revealFields-Methode).
        int neighbourFlags = countFlags(coord.getX(), coord.getY());
        if (neighbourFlags == thisField.getNeighbourMineCount()) {
            thisField.setOpened(true);
            checkedFields.add(thisField);
         
            checkedFields.addAll(openFields(coord.getX(), coord.getY(), true));
           
        }
//        Stimmt die Anzahl der Flaggen auf den Nachbarfeldern nicht mit der Anzahl der Minen
//        überein, passiert nichts (FIELD_NOT_REVEALED)
        if (neighbourFlags != thisField.getNeighbourMineCount()) {
            return new RevealFieldsResult(checkedFields);
        }
        return new RevealFieldsResult(checkedFields);
    }
    private int checkNeighbours(int x, int y) {
        int counter = 0;
        for (int hx = x - 1; hx <= x + 1; hx++) {
            for (int hy = y - 1; hy <= y + 1; hy++) {
                if (!(hx == x && hy == y) && hx >= 0 && hy >= 0 && hx < width
                        && hy < height) {
                    if (spielfeld[hx][hy].hasMine()) {
                        counter++;
                    }
                }
            }
        }
        return counter;
    }
    private int countFlags(int x, int y) {
        int counter = 0;
        for (int hx = x - 1; hx <= x + 1; hx++) {
            for (int hy = y - 1; hy <= y + 1; hy++) {
                if (!(hx == x && hy == y) && hx >= 0 && hy >= 0 && hx < width
                        && hy < height) {
                    if (spielfeld[hx][hy].hasFlag()) {
                        counter++;
                    }
                }
            }
        }
        return counter;
    }
    private Collection<Field> openFields(int x, int y, Boolean checkFlag) {
        Collection<Field> felder = new ArrayList<Field>();
        for (int hx = x - 1; hx <= x + 1; hx++) {
            for (int hy = y - 1; hy <= y + 1; hy++) {
                if (!(hx == x && hy == y) && hx >= 0 && hy >= 0 && hx < width
                        && hy < height) {
                    if (checkFlag) {
                        if (spielfeld[hx][hy].hasFlag()) {
                            continue;
                        }
                    }
                    spielfeld[hx][hy].setOpened(true);
                    felder.add(spielfeld[hx][hy]);
                }
            }
        }
        return felder;
    }
    private Collection<Field> openFieldNoFlag(int x, int y, Collection<Field> felder) {
        for (int hx = x - 1; hx <= x + 1; hx++) {
            for (int hy = y - 1; hy <= y + 1; hy++) {
                if (hx != x && hy != y && hx >= 0 && hy >= 0 && hx < width
                        && hy < height) {
                    if (!spielfeld[hx][hy].hasMine()) {
                        spielfeld[hx][hy].setOpened(true);
                        felder.add(spielfeld[hx][hy]);
                    }
                }
            }
        }
        return felder;
    }
}


und hier mein Problem:

Code:
java.lang.AssertionError: Revealing field at coordinates (2/1) gave result with wrong number of fields. Board before:

3 3

| | | |
| | | |
| |m|m|

|#|#|#|
|#|#| |
|#|f|f|

Board after:

3 3

| | | |
| | | |
| |m|m|

Status:
|#| | |
|#|2|2|
|#|f|f|
Expected: <5>
     but: was <4>
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at org.junit.Assert.assertThat(Assert.java:865)
    at de.uniwue.jpp.minesweeper.tests.model.required.BoardTest.testMultiClickRightGuess(BoardTest.java:421)

Kann mir jemand Helfen wie ich das Problem beheben kann? =/
 

oerrs

Mitglied
lol das ist genau mein Code den ich heute nacht abgegeben hatte, nachdem er endlich funktioniert.
Meine Frage ist, wie bist du an den rangekommen?
Du hast die allergleichen Methodennamen und die gleichen Kommentare, die gleichen Argumente die den Hilfsmethoden uebergeben werden.. Kann nicht sein :D Schreib dir einen eigenen Code
 

oerrs

Mitglied
Ja bin auch drauf gekommen. Wollte kurz zwischenspeichern als ich nochmal alles von neu überarbeiten wollte, war mein Fehler das auf pastebin zu machen. Sorry mein Fehler. Mittlerweile ist der Code sowieso anders..
 

MWin123

Bekanntes Mitglied
Pastebin kannst du schon verwenden, du musst aber "Paste Exposure" auf unlisted (oder private) umstellen.
Sonst wird der Code veröffentlicht und man kann ihn über Google finden.

Hier hat wohl noch einer deiner Kollegen seinen Code veröffentlicht:
http://pastebin.com/yw9vCBbb
BY: ZOGGA007
t.gif
ON AUG 20TH, 2015 | SYNTAX: JAVA | SIZE: 7.96 KB | VIEWS: 186 | EXPIRES: IN 13 DAYS
 

Ähnliche Java Themen

Neue Themen


Oben