Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Guillermo
committed
Aug 22, 2011
1 parent
48c0259
commit 921f5f0
Showing
10 changed files
with
141 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,72 @@ | ||
package minesweeper | ||
|
||
class Field { | ||
public static final String SAFE_TILE = "." | ||
public static final String MINE_TILE = "*" | ||
int width, height | ||
def tiles = [] | ||
|
||
void addTile(String tile) { | ||
tiles << tile | ||
} | ||
|
||
int getPosition(int x, int y) { | ||
x + y * width | ||
} | ||
|
||
int countMinesAt(int x, int y) { | ||
def mines = 0 | ||
mines += isPositionMined(x - 1, y - 1) ? 1 : 0 | ||
mines += isPositionMined(x, y - 1) ? 1 : 0 | ||
mines += isPositionMined(x + 1, y - 1) ? 1 : 0 | ||
mines += isPositionMined(x - 1, y) ? 1 : 0 | ||
mines += isPositionMined(x + 1, y) ? 1 : 0 | ||
mines += isPositionMined(x - 1, y + 1) ? 1 : 0 | ||
mines += isPositionMined(x, y + 1) ? 1 : 0 | ||
mines += isPositionMined(x + 1, y + 1) ? 1 : 0 | ||
return mines | ||
} | ||
|
||
boolean isPositionSafe(int x, int y) { | ||
SAFE_TILE == getTileAtPosition(x, y) | ||
} | ||
|
||
boolean isPositionMined(int x, int y) { | ||
MINE_TILE == getTileAtPosition(x, y) | ||
} | ||
|
||
def getTileAtPosition(int x, int y) { | ||
if (isOutOfBounds(x, y)) | ||
return null | ||
return tiles[getPosition(x, y)] | ||
} | ||
|
||
private boolean isOutOfBounds(int x, int y) { | ||
return x < 0 || x >= width || y < 0 || y >= height | ||
} | ||
|
||
void setMinesAt(int x, int y, int mines) { | ||
tiles[getPosition(x, y)] = mines | ||
} | ||
static final SAFE_TILE = "." | ||
static final MINE_TILE = "*" | ||
def cols, rows | ||
def tiles = [] | ||
|
||
def addTile(tile) { | ||
tiles << tile | ||
} | ||
|
||
def reveal() { | ||
(0..rows - 1).each { revealRow it } | ||
} | ||
|
||
def revealRow(row) { | ||
(0..cols - 1).each { revealTileAt it, row } | ||
} | ||
|
||
def revealTileAt(col, row) { | ||
if (isSafeAt(col, row)) | ||
setMinesNextTo col, row, countMinesAt(col, row) | ||
} | ||
|
||
def isSafeAt(col, row) { | ||
SAFE_TILE == getTileAtPosition(col, row) | ||
} | ||
|
||
def setMinesNextTo(col, row, mines) { | ||
tiles[getPosition(col, row)] = mines | ||
} | ||
|
||
def countMinesAt(col, row) { | ||
def mines = 0 | ||
mines += isMineAt(col - 1, row - 1) ? 1 : 0 | ||
mines += isMineAt(col, row - 1) ? 1 : 0 | ||
mines += isMineAt(col + 1, row - 1) ? 1 : 0 | ||
mines += isMineAt(col - 1, row) ? 1 : 0 | ||
mines += isMineAt(col + 1, row) ? 1 : 0 | ||
mines += isMineAt(col - 1, row + 1) ? 1 : 0 | ||
mines += isMineAt(col, row + 1) ? 1 : 0 | ||
mines += isMineAt(col + 1, row + 1) ? 1 : 0 | ||
return mines | ||
} | ||
|
||
def isMineAt(col, row) { | ||
MINE_TILE == getTileAtPosition(col, row) | ||
} | ||
|
||
def getTileAtPosition(col, row) { | ||
if (isOutOfBounds(col, row)) | ||
return null | ||
return tiles[getPosition(col, row)] | ||
} | ||
|
||
def isOutOfBounds(col, row) { | ||
return col < 0 || col >= cols || row < 0 || row >= rows | ||
} | ||
|
||
def getPosition(col, row) { | ||
return col + row * cols | ||
} | ||
|
||
def render() { | ||
return (0..rows - 1).inject("") { solution, row -> solution += "${renderRow(row)}\n" } | ||
} | ||
|
||
def renderRow(row) { | ||
return (0..cols - 1).inject("") { solution, col -> solution += getTileAtPosition(col, row) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
package minesweeper | ||
|
||
class FieldRepository { | ||
static fields = [] | ||
static fields = [] | ||
|
||
static Field newField() { | ||
def field = new Field() | ||
fields << field | ||
return field | ||
} | ||
static createField() { | ||
def field = new Field() | ||
fields << field | ||
return field | ||
} | ||
|
||
static getAt(index) { | ||
fields[index] | ||
} | ||
static getAt(index) { | ||
fields[index] | ||
} | ||
|
||
static reset() { | ||
fields = [] | ||
} | ||
static reset() { | ||
fields = [] | ||
} | ||
|
||
static getAll() { | ||
return fields | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package minesweeper | ||
|
||
interface Line { | ||
public static EOF_DEFINITION = "0 0" | ||
void visit(Field field) | ||
static EOF_DEFINITION = "0 0" | ||
|
||
boolean isEOF() | ||
def visit(field) | ||
def isEOF() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package minesweeper | ||
|
||
class LineFactory { | ||
static minesweeper.Line create(contents) { | ||
if (contents.contains(DimensionsLine.DIMENSIONS_SEPARATOR)) | ||
return new DimensionsLine(contents) | ||
return new TilesLine(contents) | ||
} | ||
static minesweeper.Line create(contents) { | ||
if (isADimensionsLine(contents)) | ||
return new DimensionsLine(contents) | ||
return new TilesLine(contents) | ||
} | ||
|
||
static isADimensionsLine(contents) { | ||
contents.contains DimensionsLine.DIMENSIONS_SEPARATOR | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,35 @@ | ||
package minesweeper | ||
|
||
class MineSweeper { | ||
void solve(input) { | ||
parseLines(input) | ||
revealFields() | ||
} | ||
def solve(input) { | ||
def lines = parseLines(input) | ||
createFieldsWith(lines) | ||
revealAllFields() | ||
} | ||
|
||
void parseLines(ArrayList<String> input) { | ||
def field | ||
for (line in input.collect { LineFactory.create(it) }) { | ||
if (line.isEOF()) | ||
break | ||
if (line instanceof DimensionsLine) | ||
field = FieldRepository.newField() | ||
line.visit(field) | ||
} | ||
} | ||
def parseLines(input) { | ||
input.collect { LineFactory.create(it) } | ||
} | ||
|
||
void revealFields() { | ||
FieldRepository.fields.eachWithIndex { field, number -> | ||
revealTiles(field) | ||
} | ||
} | ||
def createFieldsWith(lines) { | ||
def field | ||
for (line in lines) { | ||
if (line.isEOF()) | ||
break | ||
if (line instanceof DimensionsLine) | ||
field = FieldRepository.createField() | ||
line.visit field | ||
} | ||
} | ||
|
||
void revealTiles(Field field) { | ||
for (def y = 0; y < field.height; y++) { | ||
for (def x = 0; x < field.width; x++) { | ||
revealTile(x, y, field) | ||
} | ||
} | ||
} | ||
def revealAllFields() { | ||
FieldRepository.all.each { field -> field.reveal() } | ||
} | ||
|
||
void revealTile(int x, int y, Field field) { | ||
if (field.isPositionSafe(x, y)) | ||
field.setMinesAt(x, y, field.countMinesAt(x, y)) | ||
} | ||
|
||
String getAllSolutions() { | ||
def solution = "" | ||
FieldRepository.fields.eachWithIndex { field, number -> | ||
solution += "Field #${number + 1}:\n" | ||
solution += getSolution(field) | ||
solution += "\n" | ||
} | ||
return solution | ||
} | ||
|
||
String getSolution(Field field) { | ||
String solution = "" | ||
for (def y = 0; y < field.height; y++) { | ||
for (def x = 0; x < field.width; x++) { | ||
solution += field.getTileAtPosition(x, y) | ||
} | ||
solution += "\n" | ||
} | ||
return solution | ||
} | ||
def renderSolutions() { | ||
def number = 1 | ||
return FieldRepository.all.inject("") { solution, field -> | ||
solution += "Field #${number++}:\n${field.render()}\n" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters