Skip to main content
added 762 characters in body
Source Link

why does a safe tile need to know tis neighbors

A SafeTile is a field without a bomb in any of its neighbor fields. By the rules of "mine sweeper" all connected safe and neighbor fields are uncovered when a safe field is hit.

By having the SafeTile knowing its neighbors (which are either of class SafeTile too or of class NeighborTile, but never bombs) the current SafeTile can call the coverUp() method on its neigbors. Any neighbor which is a SafeTile itself will also propagate the call to its neighbors recursively, causing a chain reaction. keep in mind that my quick shot causes an infinite loop...

and what would be the meaning of cover up?

The method coverUp() is meant to be called by the User Interface (UI) to trigger the state change from "unknown" to "visited". Obviously the subclasses of Tile will behave differently when this method is called on them.


why does a safe tile need to know tis neighbors

A SafeTile is a field without a bomb in any of its neighbor fields. By the rules of "mine sweeper" all connected safe and neighbor fields are uncovered when a safe field is hit.

By having the SafeTile knowing its neighbors (which are either of class SafeTile too or of class NeighborTile, but never bombs) the current SafeTile can call the coverUp() method on its neigbors. Any neighbor which is a SafeTile itself will also propagate the call to its neighbors recursively, causing a chain reaction. keep in mind that my quick shot causes an infinite loop...

and what would be the meaning of cover up?

The method coverUp() is meant to be called by the User Interface (UI) to trigger the state change from "unknown" to "visited". Obviously the subclasses of Tile will behave differently when this method is called on them.

deleted 4 characters in body
Source Link
class GameBoard{
  private final Tile[][] fields;
  GameBoard(int width, int height, int bombCount){
    fields = new Tile[width][height];
    // deploy bombs
    List<Pair> fieldPositions = new ArrayList<>();
    for(int i =0;i<width;i++)
     for(int j=0; j<height;j++)
       fieldPositions.add(new Pair(i,j));
    List<Pair> bombPositions = new ArrayList<>();
    for(int i =0;i< bombCount;i++)
      bombPositions.add(fieldPositions.remove(new Random().nextInt(fieldPositions.size()));
    for(Pair bombPos : bombPositions){
       fields[bombPos.x][bombPos.y]= new TileBomb();
         // set bomb neigbors, corners and edges not yet handled...
         for(int i =-1; i< 2; i++)
           for(int j =-1; j< 2; j++)
             if(null==fields[bombPos.x+i][bombPos.y+j])
               fields[bombPos.x+i][bombPos.y+j]= new NeighborTile();
    }
    // set safe fields    
    for(int i =0;i<width;i++)
     for(int j=0; j<height;j++)
       if(null==fields[i][j])
         fields[i][j]= new NeighborTileSafeTile();
    // set neigbours   
    for(int i =0;i<width-1;i++)
      for(int j=0; j<height-1;j++){                 
          fields[i][j].addNeigbor(fields[i][j+1]);                 
          fields[i][j].addNeigbor(fields[i+1][j+1]);                 
          fields[i][j].addNeigbor(fields[i+1][j]);  
          fields[i][j+1].addNeigbor(fields[i][j]);                 
          fields[i+1][j+1].addNeigbor(fields[i][j]);                 
          fields[i+1][j].addNeigbor(fields[i][j]);       
       }
  }
}
class GameBoard{
  private final Tile[][] fields;
  GameBoard(int width, int height, int bombCount){
    fields = new Tile[width][height];
    // deploy bombs
    List<Pair> fieldPositions = new ArrayList<>();
    for(int i =0;i<width;i++)
     for(int j=0; j<height;j++)
       fieldPositions.add(new Pair(i,j));
    List<Pair> bombPositions = new ArrayList<>();
    for(int i =0;i< bombCount;i++)
      bombPositions.add(fieldPositions.remove(new Random().nextInt(fieldPositions.size()));
    for(Pair bombPos : bombPositions){
       fields[bombPos.x][bombPos.y]= new TileBomb();
         // set bomb neigbors, corners and edges not yet handled...
         for(int i =-1; i< 2; i++)
           for(int j =-1; j< 2; j++)
             if(null==fields[bombPos.x+i][bombPos.y+j])
               fields[bombPos.x+i][bombPos.y+j]= new NeighborTile();
    }
    // set safe fields    
    for(int i =0;i<width;i++)
     for(int j=0; j<height;j++)
       if(null==fields[i][j])
         fields[i][j]= new NeighborTile();
    // set neigbours   
    for(int i =0;i<width-1;i++)
      for(int j=0; j<height-1;j++){                 
          fields[i][j].addNeigbor(fields[i][j+1]);                 
          fields[i][j].addNeigbor(fields[i+1][j+1]);                 
          fields[i][j].addNeigbor(fields[i+1][j]);  
          fields[i][j+1].addNeigbor(fields[i][j]);                 
          fields[i+1][j+1].addNeigbor(fields[i][j]);                 
          fields[i+1][j].addNeigbor(fields[i][j]);       
       }
  }
}
class GameBoard{
  private final Tile[][] fields;
  GameBoard(int width, int height, int bombCount){
    fields = new Tile[width][height];
    // deploy bombs
    List<Pair> fieldPositions = new ArrayList<>();
    for(int i =0;i<width;i++)
     for(int j=0; j<height;j++)
       fieldPositions.add(new Pair(i,j));
    List<Pair> bombPositions = new ArrayList<>();
    for(int i =0;i< bombCount;i++)
      bombPositions.add(fieldPositions.remove(new Random().nextInt(fieldPositions.size()));
    for(Pair bombPos : bombPositions){
       fields[bombPos.x][bombPos.y]= new TileBomb();
         // set bomb neigbors, corners and edges not yet handled...
         for(int i =-1; i< 2; i++)
           for(int j =-1; j< 2; j++)
             if(null==fields[bombPos.x+i][bombPos.y+j])
               fields[bombPos.x+i][bombPos.y+j]= new NeighborTile();
    }
    // set safe fields    
    for(int i =0;i<width;i++)
     for(int j=0; j<height;j++)
       if(null==fields[i][j])
         fields[i][j]= new SafeTile();
    // set neigbours   
    for(int i =0;i<width-1;i++)
      for(int j=0; j<height-1;j++){                 
          fields[i][j].addNeigbor(fields[i][j+1]);                 
          fields[i][j].addNeigbor(fields[i+1][j+1]);                 
          fields[i][j].addNeigbor(fields[i+1][j]);  
          fields[i][j+1].addNeigbor(fields[i][j]);                 
          fields[i+1][j+1].addNeigbor(fields[i][j]);                 
          fields[i+1][j].addNeigbor(fields[i][j]);       
       }
  }
}
added 1249 characters in body
Source Link

Why in the if statement for setting neighbor bombs is there if(null == field...).

Initially there are no objects in the field array, any field refers to null.

After placing a new bomb (b) we want to place all neighbor fields with new objects:

 - - -      n n n
 - b -  ->  n b n
 - - -      n n n

The two inner loops iterate the sequence -1, 0, 1. This means we get all 9 possible combinations. But at x+0 - y+0 is the bomb object we don't want to overwrite. We could have checked for k and l not being both 0 which would have worked for the current bomb position. But what if we have placed another bomb (B) in a field nearby in some previous iteration?

 N N N -
 N B N -
 N N b -
 - - - -

By checking the possible neighbor field for being empty (referencing to null) we do not need a sophisticated logic to avoid replacing the actual or any previously placed bomb with a neighbor object by accident. We also don't replace any already placed neighbor field (except with a bomb) but that is less important.

On the other hand a new bomb can (and should) replace a previously placed neighbor. Therefore we do not check when placing a bomb.


Why in the if statement for setting neighbor bombs is there if(null == field...).

Initially there are no objects in the field array, any field refers to null.

After placing a new bomb (b) we want to place all neighbor fields with new objects:

 - - -      n n n
 - b -  ->  n b n
 - - -      n n n

The two inner loops iterate the sequence -1, 0, 1. This means we get all 9 possible combinations. But at x+0 - y+0 is the bomb object we don't want to overwrite. We could have checked for k and l not being both 0 which would have worked for the current bomb position. But what if we have placed another bomb (B) in a field nearby in some previous iteration?

 N N N -
 N B N -
 N N b -
 - - - -

By checking the possible neighbor field for being empty (referencing to null) we do not need a sophisticated logic to avoid replacing the actual or any previously placed bomb with a neighbor object by accident. We also don't replace any already placed neighbor field (except with a bomb) but that is less important.

On the other hand a new bomb can (and should) replace a previously placed neighbor. Therefore we do not check when placing a bomb.

added 38 characters in body
Source Link
Loading
added 38 characters in body
Source Link
Loading
Source Link
Loading