wszib-kolo / sapper

sapper
0 stars 2 forks source link

B - Bridge between GUI and Saper Alghoritm #4

Closed job-lukasz closed 10 years ago

job-lukasz commented 10 years ago
ulysses1900 commented 10 years ago

Uszat: what do you mean by "get position of asked field"?

Kacper3331 commented 10 years ago

Maybe it means, that for example he will ask about field (2,3) and your answer: there is mine or there is one nearest mine somewhere. But you see your task is to "send information about field - is there a mine or information about the nearest mines' so I think it's explained very clearly.

job-lukasz commented 10 years ago

yeah, Kacper is right

To A send information about field - is there a mine or information about the nearest mines I think it should return enum type:

public enum MineNumberWinLose{
    ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, WIN, LOSE;
}

If the asked field is near to no mine return ZERO, with one - One, two - Two etc, if there is no field without mines left return WIN or if there is mine return LOSE

Sory for that I comment it so late but i hope there isn`t much changes.

mkasztelnik commented 10 years ago

Are you sure that information about WIN, LOSE should be inside field?

job-lukasz commented 10 years ago

Not in field but this class should give me information if it was the last field without mines or not - so if I`ve won or not yet, or if is there a mine. If there is then send me thet I lose

----edit-- ^^^^wrong info

mkasztelnik commented 10 years ago

Please remember that class should have only one concern to change :) Why sapper algorithm is not responsible for checking if user wins/looses game?

job-lukasz commented 10 years ago

You are right, the class C should be rosponsible for it, but this class could translate that information for this enum.

ulysses1900 commented 10 years ago

Hi, I did not have code to work on, so I've written Sapper class along with test. I have also written one of two things I'm supposed to prepare. I'll amend the code to match code sent by Uszat. Anyway, my till-now code is below. Any comment is welcome ;-)

**************** Test class **************************
package saper1;
import junit.framework.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class SaperTest {
    @DataProvider(name = "SaperInputOutputSizeTwoByTwoNoMines")
    public Object[][] getSaperInputOutputNoMines() {
        return new Object[][] { { 0, 0, "0" }, { 0, 1, "0" }, { 1, 0, "0" },
                { 1, 1, "0" } };
    }
    @DataProvider(name = "SaperInputOutputSizeTwoByTwoAllMines")

    public Object[][] getSaperInputOutputAllMines() {
        return new Object[][] { { 0, 0, "Mine" }, { 0, 1, "Mine" }, { 1, 0, "Mine" },
                { 1, 1, "Mine" } };
    }
    Saper s = new Saper(2, 2, 1);

    @Test(dataProvider = "SaperInputOutputSizeTwoByTwoNoMines")
    public void returnsZeroAsMinesCount(int verticalCoord,
            int horizontalCoord, String expected) {
        s.gameBoard.setField(0,0,false,0);
        s.gameBoard.setField(0,1,false,0);
        s.gameBoard.setField(1,0,false,0);
        s.gameBoard.setField(1,1,false,0);

        Assert.assertEquals(
                s.gameBoard.getField(verticalCoord, horizontalCoord)
                        .getFieldValue(), expected);
    }

    @Test(dataProvider = "SaperInputOutputSizeTwoByTwoAllMines")
    public void returnsMine(int verticalCoord,
            int horizontalCoord, String expected) {
        s.gameBoard.setField(0,0,true,3);
        s.gameBoard.setField(0,1,true,3);
        s.gameBoard.setField(1,0,true,3);
        s.gameBoard.setField(1,1,true,3);

        Assert.assertEquals(
                s.gameBoard.getField(verticalCoord, horizontalCoord)
                        .getFieldValue(), expected);
    }
}

**************** Saper class **************************
package saper1;

public class Saper {
    Board gameBoard;

    public Saper(int boardWidth, int boardHeight, int boardMinesCount) {
        gameBoard = new Board(boardWidth, boardHeight, boardMinesCount);
    }
}

class Board {
    private Field[][] fields;
    private int minesCount;

    public Board(int width, int height, int minesCount) {
        fields = new Field[width][height];
        this.minesCount = minesCount;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                fields[i][j] = new Field();
            }
        }
    }

    public Field getField(int verticalCoord, int horizontalCoord) {
        return fields[verticalCoord][horizontalCoord];
    }

    public int getMinesCount() {
        return minesCount;
    }

    public void setField(int verticalCoord, int horizontalCoord, boolean isMine, int adjacentMinesCount) {
        getField(verticalCoord, horizontalCoord).setMine(isMine);
        getField(verticalCoord, horizontalCoord).setAdjacentMinesCount(adjacentMinesCount);
    }
}

class Field {
    private boolean mine; 
    private int adjacentMinesCount;

    public Field() {
        mine = false;
        adjacentMinesCount = 0;
    }

    public boolean getMine() {
        return mine;
    }

    public int getAdjacentMinesCount() {
        return adjacentMinesCount;
    }

    public String getFieldValue() {
        if (mine == true) {
            return "Mine";
        } else if (adjacentMinesCount >= 0) {
            return Integer.toString(getAdjacentMinesCount());
        } else
            return "Error: not a \"Mine\" nor a non-negative number";
    }

    public void setMine(boolean isMine) {
        mine = isMine;
    }

    public void setAdjacentMinesCount(int adjacentMinesCount) {
        this.adjacentMinesCount = adjacentMinesCount;
    }
}
ulysses1900 commented 10 years ago

One more thing: If I get information about whole board from C why should I send information about size and number of mines on the board is need to generate to C ?? I don't get it. Please explain

unijewski commented 10 years ago

You send to us an information about size of the board and number of mines from A and in reply we return you all board (fields with mines and their neighbours). All of this you give back to GUI.

Kacper3331 commented 10 years ago

Yeah, Grzesiek is right and I think you don't have to follow the order of this list so carefully.

ulysses1900 commented 10 years ago

OK, I get it now. thnx