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.