Sha256: 4482b72376a0385a0c6cd17bb1f7ed1975fd6d9dcb2d3cbb3271ab092dce9b8e

Contents?: true

Size: 1.36 KB

Versions: 263

Compression:

Stored size: 1.36 KB

Contents

using System;
using System.Collections.Generic;

public class Minesweeper
{
    public static string Annotate(string input)
    {
        var results = new List<string>();

        var board = input.Split("\n".ToCharArray());

        for (int i = 0; i < board.Length; i++)
        {
            var result = string.Empty;

            for (int j = 0; j < board[0].Length; j++)
            {
                if (board[i][j] == '*')
                {
                    result += '*';
                }
                else
                {
                    var numMines = GetCountForSquare(board, i, j);
                    result += numMines == 0
                        ? " "
                        : numMines.ToString();
                }
            }

            results.Add(result);
        }

        return string.Join("\n", results);
    }

    private static int GetCountForSquare(string[] board, int x, int y)
    {
        var result = 0;

        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                try
                {
                    if (board[x + i][y + j] == '*')
                    {
                        result++;
                    }
                }
                catch (IndexOutOfRangeException)
                {
                }
            }
        }

        return result;
    }
}

Version data entries

263 entries across 263 versions & 1 rubygems

Version Path
trackler-2.2.1.45 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.44 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.43 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.42 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.41 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.40 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.39 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.38 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.37 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.36 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.35 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.34 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.33 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.32 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.31 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.30 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.29 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.28 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.27 tracks/csharp/exercises/minesweeper/Example.cs
trackler-2.2.1.26 tracks/csharp/exercises/minesweeper/Example.cs