add collapsing

noStream
agp8x 2017-04-07 13:44:04 +02:00
parent 5416627d0d
commit dcb0ac452d
4 changed files with 84 additions and 0 deletions

View File

@ -6,4 +6,6 @@ package org.agp8x.android.games.fillgrid.data;
public interface Cell {
String getTableSymbol();
void markCollapsed();
}

View File

@ -1,6 +1,8 @@
package org.agp8x.android.games.fillgrid.data;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -43,6 +45,61 @@ public class GridBoard<G extends GridObject<C>, C extends Cell> implements Grid<
contents.clear();
}
/**
* Find lines with all cells filled and mark them as collapsed
*
* @return number of collapsed cells
*/
public int collapseFilledLines() {
List<Offset> deletionQueue = findFullLines();
deletionQueue.stream().map(contents::get).forEach(Cell::markCollapsed);
return deletionQueue.size();
}
public List<Offset> findFullLines() {
List<Offset> offsets = new LinkedList<>();
offsets.addAll(findFullLines(dimensions.x, dimensions.y, new HorizontalLine()));
offsets.addAll(findFullLines(dimensions.y, dimensions.x, new VerticalLine()));
return offsets;
}
private List<Offset> findFullLines(int limit0, int limit1, OffsetLineGenerator offset) {
List<Offset> offsets = new LinkedList<>();
for (int i = 0; i < limit0; i++) {
List<Offset> line = new LinkedList<>();
for (int j = 0; j < limit1; j++) {
Offset key = offset.build(i, j);
if (contents.containsKey(key) && contents.get(key) != null) {
line.add(key);
} else {
break;
}
}
if (line.size() == dimensions.y) {
offsets.addAll(line);
}
}
return offsets;
}
private interface OffsetLineGenerator {
Offset build(int x, int y);
}
private class HorizontalLine implements OffsetLineGenerator {
@Override
public Offset build(int x, int y) {
return new Offset(y, x);
}
}
private class VerticalLine implements OffsetLineGenerator {
@Override
public Offset build(int x, int y) {
return new Offset(x, y);
}
}
/**
* Check whether a given object can be placed on a given origin
*

View File

@ -30,4 +30,9 @@ public class StringCell implements Cell {
public String getTableSymbol() {
return value.substring(0,1);
}
@Override
public void markCollapsed() {
}
}

View File

@ -38,5 +38,25 @@ public class GridBoardTest {
System.out.println(board.symbolTable());
board.drop(new Offset(3,0), block1);
System.out.println(board.symbolTable());
System.out.println(board.findFullLines());
GridBlock<StringCell> block2 = new GridBlock<>(new HashMap<>());
block2.getObjects().put(new Offset(1,0), new StringCell("F"));
board.drop(new Offset(0,0), block2);
System.out.println(board.symbolTable());
System.out.println(board.findFullLines());
}
@Test
public void testFullLines() throws Exception {
GridBlock<StringCell> block1 = new GridBlock<>(new HashMap<>());
block1.getObjects().put(new Offset(0,0), new StringCell("1"));
block1.getObjects().put(new Offset(0,1), new StringCell("2"));
block1.getObjects().put(new Offset(0,2), new StringCell("3"));
block1.getObjects().put(new Offset(0,3), new StringCell("4"));
board.drop(new Offset(0,0), block1);
System.out.println(board.symbolTable());
System.out.println(board.findFullLines());
System.out.println(board.collapseFilledLines());
}
}