Skip to content

Commit

Permalink
Refactorizaciones a go go
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo committed Aug 22, 2011
1 parent 48c0259 commit 921f5f0
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 136 deletions.
2 changes: 1 addition & 1 deletion ggalmazor/groovy/script/spike.groovy
Expand Up @@ -29,7 +29,7 @@ System.in.withReader { prompt ->
throw new Exception("Wrong row count: ${row} (only ${field.h} declared)")
}
if (line.size() != field["w"]) {
throw new Exception("Wrong width at row ${row}: ${line.length()}")
throw new Exception("Wrong cols at row ${row}: ${line.length()}")
}
line.each { tile ->
if ("." != tile && "*" != tile)
Expand Down
2 changes: 1 addition & 1 deletion ggalmazor/groovy/src/minesweeper/BaseLine.groovy
Expand Up @@ -3,7 +3,7 @@ package minesweeper
abstract class BaseLine implements minesweeper.Line {
def contents

boolean isEOF() {
def isEOF() {
EOF_DEFINITION == contents
}
}
6 changes: 3 additions & 3 deletions ggalmazor/groovy/src/minesweeper/DimensionsLine.groovy
Expand Up @@ -7,9 +7,9 @@ class DimensionsLine extends BaseLine {
this.contents = contents
}

void visit(Field field) {
def visit(field) {
def space = contents.indexOf(DIMENSIONS_SEPARATOR)
field.setWidth contents[space + 1..-1].toInteger()
field.setHeight contents[0..space - 1].toInteger()
field.setCols contents[space + 1..-1].toInteger()
field.setRows contents[0..space - 1].toInteger()
}
}
115 changes: 68 additions & 47 deletions ggalmazor/groovy/src/minesweeper/Field.groovy
@@ -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) }
}
}
28 changes: 16 additions & 12 deletions ggalmazor/groovy/src/minesweeper/FieldRepository.groovy
@@ -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
}
}
6 changes: 3 additions & 3 deletions ggalmazor/groovy/src/minesweeper/Line.groovy
@@ -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()
}
14 changes: 9 additions & 5 deletions ggalmazor/groovy/src/minesweeper/LineFactory.groovy
@@ -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
}
}
78 changes: 27 additions & 51 deletions ggalmazor/groovy/src/minesweeper/MineSweeper.groovy
@@ -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"
}
}
}
2 changes: 1 addition & 1 deletion ggalmazor/groovy/src/minesweeper/TilesLine.groovy
Expand Up @@ -5,7 +5,7 @@ class TilesLine extends BaseLine {
this.contents = contents
}

void visit(Field field) {
def visit(field) {
contents.each { tile -> field.addTile tile }
}
}
24 changes: 12 additions & 12 deletions ggalmazor/groovy/test/minesweeper/MineSweeperTests.groovy
Expand Up @@ -6,10 +6,10 @@ import static org.junit.Assert.assertThat
import static org.hamcrest.CoreMatchers.is

class MineSweeperTests {
@Before
void resetFieldRepository() {
FieldRepository.reset()
}
@Before
void "Resetear el repositorio de tableros"() {
FieldRepository.reset()
}

@Test
void "Crea un campo de minas de 1 por 1"() {
Expand All @@ -20,8 +20,8 @@ class MineSweeperTests {
]
def mineSweeper = new MineSweeper()
mineSweeper.solve(input)
assertThat FieldRepository[0].width, is(1)
assertThat FieldRepository[0].height, is(1)
assertThat FieldRepository[0].cols, is(1)
assertThat FieldRepository[0].rows, is(1)
}

@Test
Expand All @@ -36,8 +36,8 @@ class MineSweeperTests {
]
def mineSweeper = new MineSweeper()
mineSweeper.solve(input)
assertThat FieldRepository[0].width, is(1)
assertThat FieldRepository[1].height, is(2)
assertThat FieldRepository[0].cols, is(1)
assertThat FieldRepository[1].rows, is(2)
}

@Test
Expand All @@ -58,7 +58,7 @@ Field #2:
"""
def mineSweeper = new MineSweeper()
mineSweeper.solve(input)
assertThat mineSweeper.getAllSolutions(), is(solution)
assertThat mineSweeper.renderSolutions(), is(solution)
}

@Test
Expand All @@ -79,7 +79,7 @@ Field #2:
"""
def mineSweeper = new MineSweeper()
mineSweeper.solve(input)
assertThat mineSweeper.getAllSolutions(), is(solution)
assertThat mineSweeper.renderSolutions(), is(solution)
}

@Test
Expand Down Expand Up @@ -108,7 +108,7 @@ Field #2:
"""
def mineSweeper = new MineSweeper()
mineSweeper.solve(input)
assertThat mineSweeper.getAllSolutions(), is(solution)
assertThat mineSweeper.renderSolutions(), is(solution)
}

@Test
Expand Down Expand Up @@ -139,7 +139,7 @@ Field #2:
"""
def mineSweeper = new MineSweeper()
mineSweeper.solve(input)
assertThat mineSweeper.getAllSolutions(), is(solution)
assertThat mineSweeper.renderSolutions(), is(solution)
}
}

Expand Down

0 comments on commit 921f5f0

Please sign in to comment.