Need help for a Minesweeper

Hello,
For my first game im trying to create a minesweeper with unity (I already made one in C++ with Codeblocks on terminal). But I don’t know how to import aleatory bombs and then make cases take +1 when a bombe is across.
I used a double for(…){…} to import cases into the screen but it is possible to script a gameObejct just with her name ?
I would like to import with C# script cases with names (“case(0)”, “case(1)”, … , “case(imax)”), and then aleatory affect a number to all cases with script to define if they contain a bomb, or if a bomb are across them.
My answer is then do it is possible ? If it is not please can you propose my an idea ?

Very sorry for my english, is very bad but I make my better.
Thanks

I don’t quite understand your question.

You can certainly use a double for loop to Instantiate your sprites. Keep track of these with a 2D array, probably of type SpriteRenderer. Then you can assign an appropriate sprite for each cell to show whether it is unknown, a 1-4, a bomb, etc.

I think you should also separate the data from displaying the data. Write a class that just implements the minesweeper board and all the interactions you need to keep track of the state of each cell. Don’t have any graphics whatsoever in this class. Then just add in methods to GetValue(x,y) of a cell. Use this data to display it however you want. This way you can create a MineSweeperCell class that has data about a cell like this:

using System;using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MineSweeperCell {

    public bool bomb = false;
    public bool marked;
    public bool revealed;
    public int neighbors;

    public MineSweeperCell()
    {
        bomb = false;
        flagged = false;
        revealed = false;
        neighbors = 0;
    }
}

Then your MineSweeper class could implement an array of these cells. If a user clicks somewhere, your game informs this class that a click happened and it does all the necessary logic to implement that. Then the game can request information about each cell to display it.

If you do it this way you can box up all the actual “MineSweeper” part into just data, without any regard to graphics. You can then implement whatever graphics you want on top of that (3d, 2d, whatever).

1 Like

takatok I just don’t understand clearly how I have to use the MineSweeper class…
You say that I have to create the MineSweeperCell class like you show me and assign this to my sprite thats it ? And I have to change the appereance when booleans takes another values thats it ? I don’t found how to change the Image in a gameObject.
And the MineSweeper class are a class assign to a empty gameObject in the scene thats it ?

No… the idea is MineSweeper class and MineSweeperCell class wouldn’t have anything to do with sprites at all. They would exist independent of graphics. You would add function to tell it someone pressed spot (x,y) and functions to get the value of Spot(x,y). They would be keeping all their data as true/false and ints.

You would then be in charge of figuring out how to display this data (learning Unity). You would have some other GameObject that had this MineSweeper class and your graphics would use that to display things. Here’s a rough idea of a MineSweeper class using the MineSweeperCell from above:

MineSweeper Class

using System;using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MineSweeper  {

    private MineSweeperCell[,] grid;
    private int width;
    private int height;

    public MineSweeper(int width, int height, int bombs)
    {
        ResetGrid(width, height, bombs);
    }

    public void ResetGrid(int width, int height, int bombs)
    {
        this.width = width;
        this.height = height;

        grid = new MineSweeperCell[width, height];
        for (int i = 0; i < width; ++i)
            for (int j = 0; j < height; ++j)
                grid[i, j] = new MineSweeperCell();


        // this is basically a ShuffleBag
        List<MineSweeperCell> allCells = new List<MineSweeperCell>();
        for (int i = 0; i < width; ++i)
            for (int j = 0; j < height; ++j)
                allCells.Add(grid[i, j]);
        while (bombs > 0)
        {
            // find a random cell for a bomb
            int index = Random.Random(0, allCells.Count);
            allCells[index].bomb = true;
            // remove it from our list of cells
            allCells.Remove(allCells[index]);
            bombs--;
        }
        // Lets setup the neighborCount
        for (int i = 0; i < width; ++i)
        {
            for (int j = 0; j < height++j)
            {
                int bombCount = 0;
                foreach (Tuple<int, int> neighbor in offset)
                {
                    int neighborX = i + neighbor.Item1;
                    int neighborY = i + neighbor.Item2;
                    // check if the neighbor is outside our grid
                    if (neighborX < 0 || neighborX == widght || neighborY < 0 || neighborY == height)
                        continue;
                    if (grid[neighborX, neighborY].bomb == true)
                        bombCount++;
                }
                grid[i, j].neighbors = bombCount;
            }
        }
    }

    public void SetMarked(int x, int y, bool val)
    {
        grid[x, y].marked = val;
    }
   
    public void SetRevealed(int x, int y)
    {
        grid[x, y].revealed = true;
    }

    public bool isBomb(int x, int y)
    {
        return grid[x, y].bomb;
    }

    static Tuple<int, int>[] offset = new Tuple<int, int>[8]
    {
        new Tuple<int,int> {0,1 },
        new Tuple<int,int> {1,1 },
        new Tuple<int,int> {1,0 },
        new Tuple<int,int> {1,-1 },
        new Tuple<int,int> {0,-1 },
        new Tuple<int,int> {-1,-1 },
        new Tuple<int,int> {-1,0 },
        new Tuple<int,int> {-1,1 }
    };
}

So the idea here is this is just data to keep track of whats going on. Now some other code you have that is in charge of graphics would call this code to get data about it so you can display whatever sprites you want.

Now if your question is how to make and display sprites thats a whole differrent question and their are lots of tutorials on it.

Oh ok thank you very much I understand more now.
There is certain functions i don’t know already then I will make my little searches on internet to learn more about… for example i don’t understand the Tuple<…> and the foreach… but thanks you very much, i’ll do my better for the rest and i’ll post my progression.

Probable a good idea to find tutorial on C# then. Foreach is a basic C#. Its like for loop. It takes each item in the list of things and goes through each one.

Tuple is just a way to pair 2 things together.

Yea sure I will see C# tutorials, I tried without tutorials because I know C++ and it’s not much different but finally I need.
Thank you for all.