Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1. The game starts from the point where all the numbers are shown to the user, but not the positions of the mines. I do this step.
2. The user should enter two numbers.I do this step.
3. If the user enters the coordinates of an empty cell (without a number because cells with numbers are guaranteed to be free of mines), the program should set a special mark on this cell which means that the user thinks a mine is located
there.
4. If the user enters the coordinates of a cell with a number, the program should ask the user again and don't print the field.
5. If the user enters the coordinates where a special mark is located, the program should remove the mark. This is necessary because the user can place marks anywhere except for cells with numbers, but the game ends only if all the marks are correct.
6. When the user marks all the mines without marking any empty cells, the game ends.

import java.util.Random;
import java.util.Scanner;

public class Minesweeper {

public static void main(String[] args) {
    Minesweeper minesweeper = new Minesweeper();
    minesweeper.randomX();
    minesweeper.changeMinesweeper();
}

char[][] minesweeper = new char[9][9];
Random randNum = new Random();
Scanner sc = new Scanner(System.in);

public Minesweeper() {
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            minesweeper[i][j] = '.';
        }
    }
}

public void changeMinesweeper() {
    while (true) {

        System.out.print("Set/delete mines marks (x and y coordinates): ");
        int n = sc.nextInt();
        int m = sc.nextInt();
        int x = n - 1;
        int y = m - 1;
    }
}

public void printMinesweeper() {
    System.out.println(" " + "|" + "123456789" + "|");
    System.out.println("-" + "|" + "---------" + "|");
    for (int i = 0; i < 9; i++) {
        System.out.print(i + 1 + "|");
        for (int j = 0; j < 9; j++) {
            if (minesweeper[i][j] == 'X') {
                System.out.print('.');
            } else {
                System.out.print(getCharAt(i, j));
            }
        }
        System.out.println("|");
    }
    System.out.println("-" + "|" + "---------" + "|");
}

private String getCharAt(int i, int j) {
    if (mineAt(i, j)) {
        return "X";
    }

    int minesNear = countMinesNear(i, j);
    if (minesNear == 0) {
        return ".";
    } else {
        return Integer.toString(minesNear);
    }
}

private boolean mineAt(int i, int j) {
    return minesweeper[i][j] == 'X';
}

private int countMinesNear(int i, int j) {
    int mines = 0;
    for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
            if (x + i >= 0 && x + i < minesweeper.length && y + j >= 0 && y + j < minesweeper.length) {
                if (minesweeper[x + i][y + j] == 'X') {
                    mines++;
                }
            }
        }
    }
    return mines;
}

public void randomX() {
    System.out.print("How many mines do you want on the field?: ");
    int numberOfMines = sc.nextInt();
    int i = 0;
    while (i < numberOfMines) {
        int x = randNum.nextInt(9);
        int y = randNum.nextInt(9);
        if (minesweeper[x][y] == '.') {
            minesweeper[x][y] = 'X';
            i++;
        }
    }
    printMinesweeper();
}
}


What I have tried:

This code print a table 9X9 with mines and number a mines around mines, now I need to can set/delete mines and empty space with any symbol and say to user if in cell is a nubmer
Posted
Updated 12-Sep-20 22:46pm

1 solution

You have read the coordinates from the user:
Java
public void changeMinesweeper() {
    while (true) {

        System.out.print("Set/delete mines marks (x and y coordinates): ");
        int n = sc.nextInt();
        int m = sc.nextInt();
        int x = n - 1;
        int y = m - 1;
    }
}
So you can tell what the cells contains in the same way you do for all your other code:
Java
minesweeper[i][j]
Gives you the value.
So all you need to do is set that to your "I think this is a mine" symbol (a "?" perhaps") and allow for that in your "print the whole map" code.


Can I suggest a change that makes things easier? Instead of having a 9x9 "play area", add a single character border on each side, so you use a 11x11 map. Make all the new cells always empty, use the user indexes as one-based and the "how many mines around this" calculation becomes simpler as you don't have to test for "out of range" values. (x,y) always looks at
(x - 1, y - 1), (x    , y - 1), (x + 1, y - 1), 
(x - 1, y    ),                 (x + 1, y    ), 
(x - 1, y + 1), (x    , y + 1), (x + 1, y + 1)
and it doesn't have to care where in the map x and y are.

[edit]
I'd also suggest keeping two maps: the "Mines map" and the "guesses map" - so you can update the user inputs without touching the "real map"
Random fill the "Mines map", and then count the "next to" values into the "guesses map" and allow the user to set his "mine here" on that one.
Then it's easy to check when he wants to: compare x and y on the two maps.

[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900