public static string ToEmojiString(this MinesweeperField field) { var sb = new StringBuilder(); for (int i = 0; i < field.Rows; i++) { for (int j = 0; j < field.Cols; j++) { int count = field.Field[i, j]; sb.Append("||").Append(count == -1 ? Emojis.Bomb : Emojis.Numbers.Get(count)).Append("|| "); } sb.AppendLine(); } return(sb.ToString()); }
public Task ExecuteGroupAsync(CommandContext ctx, [Description("desc-game-ms-rows")] int rows = 9, [Description("desc-game-ms-cols")] int cols = 9, [Description("desc-game-ms-bombs")] int bombs = 10) { if (rows < 4 || rows > 9 || cols < 4 || cols > 12) { throw new InvalidCommandUsageException(ctx, "cmd-err-game-ms-dim", 4, 9, 4, 12); } try { var field = new MinesweeperField(rows, cols, bombs); return(ctx.RespondAsync(field.ToEmojiString())); } catch (ArgumentException) { throw new CommandFailedException(ctx, "cmd-err-game-ms-bombs"); } }
public void Should_Create_Minesweeper_Field() { MinesweeperFieldCreator minesweeperFieldCreator = CreateMinesweeperFieldCreator(); IEnumerable <string> textLines = new string[] { "**...", ".....", ".*..." }; var expectedResult = new MinesweeperField(); expectedResult.Cells = new MinesweeperCell[][] { new MinesweeperCell[] { new MinesweeperCell { ColumnCoord = 0, RowCoord = 0, HasMine = true, NeighbourMineCount = 1 }, new MinesweeperCell { ColumnCoord = 1, RowCoord = 0, HasMine = true, NeighbourMineCount = 1 }, new MinesweeperCell { ColumnCoord = 2, RowCoord = 0, HasMine = false, NeighbourMineCount = 1 }, new MinesweeperCell { ColumnCoord = 3, RowCoord = 0, HasMine = false, NeighbourMineCount = 0 }, new MinesweeperCell { ColumnCoord = 4, RowCoord = 0, HasMine = false, NeighbourMineCount = 0 } }, new MinesweeperCell[] { new MinesweeperCell { ColumnCoord = 0, RowCoord = 1, HasMine = false, NeighbourMineCount = 3 }, new MinesweeperCell { ColumnCoord = 1, RowCoord = 1, HasMine = false, NeighbourMineCount = 3 }, new MinesweeperCell { ColumnCoord = 2, RowCoord = 1, HasMine = false, NeighbourMineCount = 2 }, new MinesweeperCell { ColumnCoord = 3, RowCoord = 1, HasMine = false, NeighbourMineCount = 0 }, new MinesweeperCell { ColumnCoord = 4, RowCoord = 1, HasMine = false, NeighbourMineCount = 0 } }, new MinesweeperCell[] { new MinesweeperCell { ColumnCoord = 0, RowCoord = 2, HasMine = false, NeighbourMineCount = 1 }, new MinesweeperCell { ColumnCoord = 1, RowCoord = 2, HasMine = true, NeighbourMineCount = 0 }, new MinesweeperCell { ColumnCoord = 2, RowCoord = 2, HasMine = false, NeighbourMineCount = 1 }, new MinesweeperCell { ColumnCoord = 3, RowCoord = 2, HasMine = false, NeighbourMineCount = 0 }, new MinesweeperCell { ColumnCoord = 4, RowCoord = 2, HasMine = false, NeighbourMineCount = 0 } } }; MinesweeperField result = minesweeperFieldCreator.CreateMinesweeperField( textLines); result.Should().BeEquivalentTo(expectedResult); }