 artemkorsakov's solution

to Minesweeper in the C# Track

Published at Apr 21 2019 · 0 comments
Instructions
Test suite
Solution

Add the numbers to a minesweeper board.

Minesweeper is a popular game where the user has to find the mines using numeric hints that indicate how many mines are directly adjacent (horizontally, vertically, diagonally) to a square.

In this exercise you have to create some code that counts the number of mines adjacent to a square and transforms boards like this (where * indicates a mine):

+-----+
| * * |
|  *  |
|  *  |
|     |
+-----+

into this:

+-----+
|1*3*1|
|13*31|
| 2*2 |
| 111 |
+-----+

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit Minesweeper.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

MinesweeperTest.cs

// This file was auto-generated based on version 1.1.0 of the canonical data.

using System;
using Xunit;

public class MinesweeperTest
{
[Fact]
public void No_rows()
{
var minefield = Array.Empty<string>();
var expected = Array.Empty<string>();
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void No_columns()
{
var minefield = new[]
{
""
};
var expected = new[]
{
""
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void No_mines()
{
var minefield = new[]
{
"   ",
"   ",
"   "
};
var expected = new[]
{
"   ",
"   ",
"   "
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Minefield_with_only_mines()
{
var minefield = new[]
{
"***",
"***",
"***"
};
var expected = new[]
{
"***",
"***",
"***"
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Mine_surrounded_by_spaces()
{
var minefield = new[]
{
"   ",
" * ",
"   "
};
var expected = new[]
{
"111",
"1*1",
"111"
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Space_surrounded_by_mines()
{
var minefield = new[]
{
"***",
"* *",
"***"
};
var expected = new[]
{
"***",
"*8*",
"***"
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Horizontal_line()
{
var minefield = new[]
{
" * * "
};
var expected = new[]
{
"1*2*1"
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Horizontal_line_mines_at_edges()
{
var minefield = new[]
{
"*   *"
};
var expected = new[]
{
"*1 1*"
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Vertical_line()
{
var minefield = new[]
{
" ",
"*",
" ",
"*",
" "
};
var expected = new[]
{
"1",
"*",
"2",
"*",
"1"
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Vertical_line_mines_at_edges()
{
var minefield = new[]
{
"*",
" ",
" ",
" ",
"*"
};
var expected = new[]
{
"*",
"1",
" ",
"1",
"*"
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Cross()
{
var minefield = new[]
{
"  *  ",
"  *  ",
"*****",
"  *  ",
"  *  "
};
var expected = new[]
{
" 2*2 ",
"25*52",
"*****",
"25*52",
" 2*2 "
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}

[Fact(Skip = "Remove to run test")]
public void Large_minefield()
{
var minefield = new[]
{
" *  * ",
"  *   ",
"    * ",
"   * *",
" *  * ",
"      "
};
var expected = new[]
{
"1*22*1",
"12*322",
" 123*2",
"112*4*",
"1*22*2",
"111111"
};
Assert.Equal(expected, Minesweeper.Annotate(minefield));
}
}
﻿using System.Collections.Generic;
using System.Linq;

public static class Minesweeper
{
public static string[] Annotate(string[] inputBoard)
{
var result = inputBoard.Select(r => r.ToCharArray()).ToList();
for (int i = 0; i < result.Count; i++)
{
for (int j = 0; j < result[i].Length; j++)
{
if (result[i][j] == '*')
{
result = IncreaseSquare(result, i, j);
}
}
}
return result.Select(ch => new string(ch)).ToArray();
}

private static List<char[]> IncreaseSquare(List<char[]> square, int row, int col)
{
if (row > 0)
{
square[row - 1] = IncreaseRow(square[row - 1], col);
}
square[row] = IncreaseRow(square[row], col);
if (row < square.Count - 1)
{
square[row + 1] = IncreaseRow(square[row + 1], col);
}
return square;
}

private static char[] IncreaseRow(char[] row, int col)
{
if (col > 0)
{
row[col - 1] = IncreaseItem(row[col - 1]);
}
row[col] = IncreaseItem(row[col]);
if (col < row.Length - 1)
{
row[col + 1] = IncreaseItem(row[col + 1]);
}
return row;
}

private static char IncreaseItem(char c)
{
if (c == '*')
{
return c;
}
if (c == ' ')
{
return '1';
}
int num = (int)char.GetNumericValue(c) + 1;
return (char)(num + '0');
}
}