Created
March 29, 2024 14:40
-
-
Save pjmagee/780c3d84b51675f14f53f1eafa259f34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Have you ever played Minesweeper? Well, the goal of the game is to find all the mines within an MxN field. To help you, | |
| the game shows a number in a square which tells you how many mines there are adjacent to that square. For instance, take | |
| the following 4x4 field with 2 mines (which are represented by an * character): | |
| * . . . | |
| . . . . | |
| . * . . | |
| . . . . | |
| The same field including the hint numbers described above would look like this: | |
| * 1 0 0 | |
| 2 2 1 0 | |
| 1 * 1 0 | |
| 1 1 1 0 | |
| */ | |
| char[,] field = | |
| { | |
| { '*', '.', '.', '.' }, | |
| { '.', '.', '.', '.' }, | |
| { '.', '*', '.', '.' }, | |
| { '.', '.', '.', '.' } | |
| }; | |
| int[,] output = new int[4, 4]; | |
| for (int row = 0; row < field.GetLength(0); row++) | |
| { | |
| for (int col = 0; col < field.GetLength(1); col++) | |
| { | |
| if (field[row, col] == '*') | |
| { | |
| // Below row | |
| if (row + 1 < field.GetLength(0)) | |
| output[row + 1, col]++; | |
| // Right column | |
| if (col + 1 < field.GetLength(1)) | |
| output[row, col + 1]++; | |
| // Diagonal bottom-right | |
| if (row + 1 < field.GetLength(0) && col + 1 < field.GetLength(1)) | |
| output[row + 1, col + 1]++; | |
| // Left column | |
| if (col - 1 >= 0) | |
| output[row, col - 1]++; | |
| // Above row | |
| if (row - 1 >= 0) | |
| output[row - 1, col]++; | |
| // Diagonal top-left | |
| if (row - 1 >= 0 && col - 1 >= 0) | |
| output[row - 1, col - 1]++; | |
| // Diagonal bottom-left | |
| if (row + 1 < field.GetLength(0) && col - 1 >= 0) | |
| output[row + 1, col - 1]++; | |
| // Diagonal top-right | |
| if (row - 1 >= 0 && col + 1 < field.GetLength(1)) | |
| output[row - 1, col + 1]++; | |
| } | |
| } | |
| } | |
| output.Dump(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment