Skip to content

Commit

Permalink
Solve sample cases two and three
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Apr 12, 2014
1 parent b53e5a2 commit 9b6354c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
44 changes: 44 additions & 0 deletions Gcj2014/Minesweeper/Minesweeper.Tests/MasterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,49 @@ public void SolveSimpleCase()
Assert.AreEqual("...*", lines[2]);
Assert.AreEqual("****", lines[3]);
}

[TestMethod]
public void SolveSampleCaseTwo()
{
Master master = new Master();

var result = master.Solve(4, 7, 3);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(IList<string>));

var lines = (IList<string>)result;

Assert.AreEqual(4, lines.Count);
Assert.AreEqual("c......", lines[0]);
Assert.AreEqual(".......", lines[1]);
Assert.AreEqual("......*", lines[2]);
Assert.AreEqual(".....**", lines[3]);
}

[TestMethod]
public void SolveSampleCaseThree()
{
Master master = new Master();

var result = master.Solve(10, 10, 82);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(IList<string>));

var lines = (IList<string>)result;

Assert.AreEqual(10, lines.Count);
Assert.AreEqual("c.********", lines[0]);
Assert.AreEqual("..********", lines[1]);
Assert.AreEqual("..********", lines[2]);
Assert.AreEqual("..********", lines[3]);
Assert.AreEqual("..********", lines[4]);
Assert.AreEqual("..********", lines[5]);
Assert.AreEqual("..********", lines[6]);
Assert.AreEqual("..********", lines[7]);
Assert.AreEqual("..********", lines[8]);
Assert.AreEqual("**********", lines[9]);
}
}
}
5 changes: 5 additions & 0 deletions Gcj2014/Minesweeper/Minesweeper/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public void SetEmpty(int nr, int nc)
this.cells[k, j] = '.';
}

public void SetEmptyCell(int nr, int nc)
{
this.cells[nr, nc] = '.';
}

public void SetClick(int nr, int nc)
{
this.cells[nr, nc] = 'c';
Expand Down
27 changes: 26 additions & 1 deletion Gcj2014/Minesweeper/Minesweeper/Master.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public object Solve(int nrows, int ncols, int nmines)
if (rest == 1)
continue;

return Generate(nrows, ncols, k, j, rest);
if (rest < k * (ncols - j))
return Generate(nrows, ncols, k, j, rest);
}

return "Impossible";
Expand All @@ -38,6 +39,30 @@ private object Generate(int nrows, int ncols, int height, int width, int rest)
Board board = new Board(nrows, ncols);
board.SetEmpty(height, width);
board.SetClick(0, 0);

int y = 0;
int x = width;

while (rest >= 2 && y + 1 < height)
{
board.SetEmptyCell(y, x);
board.SetEmptyCell(y + 1, x);
rest -= 2;
x++;

if (x >= ncols)
{
y += 2;
x = width;

if (y >= height)
break;
}
}

if (rest > 0 && y < height)
board.SetEmptyCell(y, x);

return board.ToLines();
}
}
Expand Down

0 comments on commit 9b6354c

Please sign in to comment.