Thanks for reading my question. I am currently taking a Java class on Coursera, and was asked to write a program on minesweeper for the assignment. My code creates the correct result, but my grade was deducted greatly because my code is ”excessively complex, with a cyclomatic complexity of 60“ according to the auto-grader. I understand that there are too many conditionals and loops, but I had a hard time trying to make it more simple.
Here is my code. It takes 3 integer command-line arguments m, n, and k to create an m-by-n grid with k mines in random locations. I use "5" to mark the mines instead of "" because the highest a number in a tile can get is 4 (since a tile has 4 sides). If two mines are located side by side, extra values might be added to its marker of "5". So I make all the values >= 5 become "" when I print them out. Each value is separated by two spaces.
public class Minesweeper {
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
int k = Integer.parseInt(args[2]);
int[][] mine = new int[m][n];
//put the mines
for(int z = 0; z < k; z++) {
int randomX = (int) (Math.random() * m);
int randomY = (int) (Math.random() * n);
mine[randomX][randomY] = 5;
}
for(int y = 0; y < n; y++) {
for(int x = 0; x < m; x++) {
//first row of the grid
if(y == 0) {
//upper left corner
if(x == 0) {
if(mine[x + 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y + 1] >= 5) {
mine[x][y] += 1;
}
}
//upper right corner
else if(x == m - 1) {
if(mine[x - 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y + 1] >= 5) {
mine[x][y] += 1;
}
}
//mid of first row
else {
if(mine[x - 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x + 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y + 1] >= 5) {
mine[x][y] += 1;
}
}
}
//mid rows
else if(y > 0 && y < n - 1) {
//left side
if(x == 0) {
if(mine[x][y - 1] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y + 1] >= 5) {
mine[x][y] += 1;
}
if(mine[x + 1][y] >= 5) {
mine[x][y] += 1;
}
}
//right side
else if(x == m - 1) {
if(mine[x][y - 1] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y + 1] >= 5) {
mine[x][y] += 1;
}
if(mine[x - 1][y] >= 5) {
mine[x][y] += 1;
}
}
//mid
else {
if(mine[x][y - 1] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y + 1] >= 5) {
mine[x][y] += 1;
}
if(mine[x - 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x + 1][y] >= 5) {
mine[x][y] += 1;
}
}
}
//bottom row
else if(y == n - 1) {
//bottom left corner
if(x == 0) {
if(mine[x + 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y - 1] >= 5) {
mine[x][y] += 1;
}
}
//bottom right corner
else if(x == m - 1) {
if(mine[x - 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y - 1] >= 5) {
mine[x][y] += 1;
}
}
//middle of the bottom row
else {
if(mine[x + 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x - 1][y] >= 5) {
mine[x][y] += 1;
}
if(mine[x][y - 1] >= 5) {
mine[x][y] += 1;
}
}
}
}
}
//print out the grid
for(int y = 0; y < n; y++) {
for(int x = 0; x < m; x++) {
//println at the right edge of the grid
if(x == m - 1) {
if(mine[x][y] >= 5) {
System.out.println("*");
}
else {
System.out.println(mine[x][y]);
}
}
//other tiles, no need to switch lines
else {
if(mine[x][y] >= 5) {
System.out.print("* ");
}
else {
System.out.print(mine[x][y] + " ");
}
}
}
}
}
}
Thank you for your time, and I'd really appreciate any suggestions.