Oh sorry here’s the full script:
using UnityEngine;
using UnityEngine.SceneManagement;
public class Game : MonoBehaviour
{
public GameObject losescreen;
public GameObject settingspanel;
public GameObject menubutton;
public GameObject menubuttonn;
public GameObject winnerscreen;
public int width = 16;
public int height = 16;
public int mineCount = 32;
private Board board;
private Cell[,] state;
private bool gameover;
private void OnValidate()
{
mineCount = Mathf.Clamp(mineCount, 0, width * height);
}
private void Awake()
{
//DontDestroyOnLoad(this);
Application.targetFrameRate = 60;
board = GetComponentInChildren();
}
private void Start()
{
NewGame();
}
private void NewGame()
{
state = new Cell[width, height];
gameover = false;
GenerateCells();
GenerateMines();
GenerateNumbers();
//Camera.main.transform.position = new Vector3(width / 2f, height / 2f, -10f);
board.Draw(state);
}
private void GenerateCells()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Cell cell = new Cell();
cell.position = new Vector3Int(x, y, 0);
cell.type = Cell.Type.Empty;
state[x, y] = cell;
}
}
}
private void GenerateMines()
{
for (int i = 0; i < mineCount; i++)
{
int x = Random.Range(0, width);
int y = Random.Range(0, height);
while (state[x, y].type == Cell.Type.Mine)
{
x++;
if (x >= width)
{
x = 0;
y++;
if (y >= height)
{
y = 0;
}
}
}
state[x, y].type = Cell.Type.Mine;
}
}
private void GenerateNumbers()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Cell cell = state[x, y];
if (cell.type == Cell.Type.Mine)
{
continue;
}
cell.number = CountMines(x, y);
if (cell.number > 0)
{
cell.type = Cell.Type.Number;
}
state[x, y] = cell;
}
}
}
private int CountMines(int cellX, int cellY)
{
int count = 0;
for (int adjacentX = -1; adjacentX <= 1; adjacentX++)
{
for (int adjacentY = -1; adjacentY <= 1; adjacentY++)
{
if (adjacentX == 0 && adjacentY == 0)
{
continue;
}
int x = cellX + adjacentX;
int y = cellY + adjacentY;
if (GetCell(x, y).type == Cell.Type.Mine)
{
count++;
}
}
}
return count;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
NewGame();
}
else if (!gameover)
{
if (Input.GetMouseButtonDown(1))
{
Flag();
}
else if (Input.GetMouseButtonDown(0))
{
Reveal();
}
}
}
private void Flag()
{
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int cellPosition = board.tilemap.WorldToCell(worldPosition);
Cell cell = GetCell(cellPosition.x, cellPosition.y);
// Cannot flag if already revealed
if (cell.type == Cell.Type.Invalid || cell.revealed)
{
return;
}
cell.flagged = !cell.flagged;
state[cellPosition.x, cellPosition.y] = cell;
board.Draw(state);
}
private void Reveal()
{
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int cellPosition = board.tilemap.WorldToCell(worldPosition);
Cell cell = GetCell(cellPosition.x, cellPosition.y);
// Cannot reveal if already revealed or while flagged
if (cell.type == Cell.Type.Invalid || cell.revealed || cell.flagged)
{
return;
}
switch (cell.type)
{
case Cell.Type.Mine:
Explode(cell);
break;
case Cell.Type.Empty:
Flood(cell);
CheckWinCondition();
break;
default:
cell.revealed = true;
state[cellPosition.x, cellPosition.y] = cell;
CheckWinCondition();
break;
}
board.Draw(state);
}
private void Flood(Cell cell)
{
// Recursive exit conditions
if (cell.revealed) return;
if (cell.type == Cell.Type.Mine || cell.type == Cell.Type.Invalid) return;
// Reveal the cell
cell.revealed = true;
state[cell.position.x, cell.position.y] = cell;
// Keep flooding if the cell is empty, otherwise stop at numbers
if (cell.type == Cell.Type.Empty)
{
Flood(GetCell(cell.position.x - 1, cell.position.y));
Flood(GetCell(cell.position.x + 1, cell.position.y));
Flood(GetCell(cell.position.x, cell.position.y - 1));
Flood(GetCell(cell.position.x, cell.position.y + 1));
}
}
private void Explode(Cell cell)
{
Debug.Log(“Game Over!”);
gameover = true;
// Set the mine as exploded
cell.exploded = true;
cell.revealed = true;
state[cell.position.x, cell.position.y] = cell;
// Reveal all other mines
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
cell = state[x, y];
if (cell.type == Cell.Type.Mine)
{
cell.revealed = true;
state[x, y] = cell;
}
}
}
if (gameover = true)
{
if (losescreen.activeInHierarchy == false)
{
losescreen.SetActive(true);
}
}
}
private void CheckWinCondition()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Cell cell = state[x, y];
// All non-mine cells must be revealed to have won
if (cell.type != Cell.Type.Mine && !cell.revealed)
{
return; // no win
}
}
}
if (gameover = true)
{
if (winnerscreen.activeInHierarchy == false)
{
winnerscreen.SetActive(true);
}
}
Debug.Log(“Winner!”);
gameover = true;
// Flag all the mines
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Cell cell = state[x, y];
if (cell.type == Cell.Type.Mine)
{
cell.flagged = true;
state[x, y] = cell;
}
}
}
}
private Cell GetCell(int x, int y)
{
if (IsValid(x, y))
{
return state[x, y];
}
else
{
return new Cell();
}
}
private bool IsValid(int x, int y)
{
return x >= 0 && x < width && y >= 0 && y < height;
}
public void whenButtonCli()
{
NewGame();
if (winnerscreen.activeInHierarchy == true)
{
winnerscreen.SetActive(false);
}
if (losescreen.activeInHierarchy == true)
{
losescreen.SetActive(false);
}
}
public void OnClick()
{
gameover = true;
}
public void OnClic()
{
gameover = false;
}
public void OnClickied()
{
gameover = true;
}
public void OnClick1()
{
gameover = false;
}
public void OnClick2()
{
if (menubuttonn.activeInHierarchy == false)
menubuttonn.SetActive(true);
if (menubutton.activeInHierarchy == true)
menubutton.SetActive(false);
}
public void OnClick3()
{
if (menubuttonn.activeInHierarchy == true)
menubuttonn.SetActive(false);
if (menubutton.activeInHierarchy == false)
menubutton.SetActive(true);
}
public void OnClick5()
{
if (settingspanel.activeInHierarchy == false)
settingspanel.SetActive(true);
}
public void OnClick6()
{
if (settingspanel.activeInHierarchy == true)
settingspanel.SetActive(false);
}
public void Size16x16()
{
int[ ] width = new int[16];
int[ ] height = new int[16];
int[ ] mineCount = new int[32];
//SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
public void Size16()
{
width = 16;
height = 16;
mineCount = 32;
Debug.Log(“Size updated to 16x16”);
}
}