Properly Call FillBoard

This commit is contained in:
2025-12-14 11:01:04 +08:00
parent 6abccbe6d8
commit 68046b8960
5 changed files with 593 additions and 591 deletions

View File

@@ -1,188 +1,188 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Enums;
//Done, moved to MatchService and GameBoard
public class GameBoard
{
// - sets size of the grid
// - handles the score
// - holds the current matches
#region Variables
private int height = 0;
public int Height { get { return this.height; } }
private int width = 0;
public int Width { get { return this.width; } }
private SC_Gem[,] allGems;
// public Gem[,] AllGems { get { return allGems; } }
private int score = 0;
public int Score
{
get { return this.score; }
set {
this.score = value; }
}
private List<SC_Gem> currentMatches = new List<SC_Gem>();
public List<SC_Gem> CurrentMatches { get { return this.currentMatches; } }
#endregion
public GameBoard(int _Width, int _Height)
{
this.height = _Height;
this.width = _Width;
this.allGems = new SC_Gem[this.width, this.height];
}
//checks if there are 3 gems of the same type next to each other
//used during setup to avoid matches on game start
//MatchService
public bool MatchesAt(Vector2Int _PositionToCheck, SC_Gem _GemToCheck)
{
if (_PositionToCheck.x > 1)
{
if (this.allGems[_PositionToCheck.x - 1, _PositionToCheck.y].type == _GemToCheck.type && this.allGems[_PositionToCheck.x - 2, _PositionToCheck.y].type == _GemToCheck.type)
return true;
}
if (_PositionToCheck.y > 1)
{
if (this.allGems[_PositionToCheck.x, _PositionToCheck.y - 1].type == _GemToCheck.type && this.allGems[_PositionToCheck.x, _PositionToCheck.y - 2].type == _GemToCheck.type)
return true;
}
return false;
}
//places the gem in the 2d array
public void SetGem(int _X, int _Y, SC_Gem _Gem)
{
this.allGems[_X, _Y] = _Gem;
}
public SC_Gem GetGem(int _X,int _Y)
{
return this.allGems[_X, _Y];
}
//MatchService
public void FindAllMatches()
{
this.currentMatches.Clear();
for (int x = 0; x < this.width; x++)
for (int y = 0; y < this.height; y++)
{
SC_Gem currentGem = this.allGems[x, y];
if (currentGem != null)
{
if (x > 0 && x < this.width - 1)
{
SC_Gem leftGem = this.allGems[x - 1, y];
SC_Gem rightGem = this.allGems[x + 1, y];
//checking no empty spots
if (leftGem != null && rightGem != null)
{
//Match
if (leftGem.type == currentGem.type && rightGem.type == currentGem.type)
{
currentGem.isMatch = true;
leftGem.isMatch = true;
rightGem.isMatch = true;
this.currentMatches.Add(currentGem);
this.currentMatches.Add(leftGem);
this.currentMatches.Add(rightGem);
}
}
}
if (y > 0 && y < this.height - 1)
{
SC_Gem aboveGem = this.allGems[x, y - 1];
SC_Gem bellowGem = this.allGems[x, y + 1];
//checking no empty spots
if (aboveGem != null && bellowGem != null)
{
//Match
if (aboveGem.type == currentGem.type && bellowGem.type == currentGem.type)
{
currentGem.isMatch = true;
aboveGem.isMatch = true;
bellowGem.isMatch = true;
this.currentMatches.Add(currentGem);
this.currentMatches.Add(aboveGem);
this.currentMatches.Add(bellowGem);
}
}
}
}
}
if (this.currentMatches.Count > 0) this.currentMatches = this.currentMatches.Distinct().ToList();
CheckForBombs();
}
public void CheckForBombs()
{
for (int i = 0; i < this.currentMatches.Count; i++)
{
SC_Gem gem = this.currentMatches[i];
int x = gem.posIndex.x;
int y = gem.posIndex.y;
if (gem.posIndex.x > 0)
{
if (this.allGems[x - 1, y] != null && this.allGems[x - 1, y].type == GemType.Bomb)
MarkBombArea(new Vector2Int(x - 1, y), this.allGems[x - 1, y].blastSize);
}
if (gem.posIndex.x + 1 < this.width)
{
if (this.allGems[x + 1, y] != null && this.allGems[x + 1, y].type == GemType.Bomb)
MarkBombArea(new Vector2Int(x + 1, y), this.allGems[x + 1, y].blastSize);
}
if (gem.posIndex.y > 0)
{
if (this.allGems[x, y - 1] != null && this.allGems[x, y - 1].type == GemType.Bomb)
MarkBombArea(new Vector2Int(x, y - 1), this.allGems[x, y - 1].blastSize);
}
if (gem.posIndex.y + 1 < this.height)
{
if (this.allGems[x, y + 1] != null && this.allGems[x, y + 1].type == GemType.Bomb)
MarkBombArea(new Vector2Int(x, y + 1), this.allGems[x, y + 1].blastSize);
}
}
}
public void MarkBombArea(Vector2Int bombPos, int _BlastSize)
{
string _print = "";
for (int x = bombPos.x - _BlastSize; x <= bombPos.x + _BlastSize; x++)
{
for (int y = bombPos.y - _BlastSize; y <= bombPos.y + _BlastSize; y++)
{
if (x >= 0 && x < this.width && y >= 0 && y < this.height)
{
if (this.allGems[x, y] != null)
{
_print += "(" + x + "," + y + ")" + System.Environment.NewLine;
this.allGems[x, y].isMatch = true;
this.currentMatches.Add(this.allGems[x, y]);
}
}
}
}
this.currentMatches = this.currentMatches.Distinct().ToList();
}
}
// using System.Collections;
// using System.Collections.Generic;
// using System.Linq;
// using UnityEngine;
// using Enums;
//
// //Done, moved to MatchService and GameBoard
// public class GameBoard
// {
// // - sets size of the grid
// // - handles the score
// // - holds the current matches
// #region Variables
//
// private int height = 0;
// public int Height { get { return this.height; } }
//
// private int width = 0;
// public int Width { get { return this.width; } }
//
// private SC_Gem[,] allGems;
// // public Gem[,] AllGems { get { return allGems; } }
//
// private int score = 0;
// public int Score
// {
// get { return this.score; }
// set {
// this.score = value; }
// }
//
// private List<SC_Gem> currentMatches = new List<SC_Gem>();
// public List<SC_Gem> CurrentMatches { get { return this.currentMatches; } }
// #endregion
//
// public GameBoard(int _Width, int _Height)
// {
// this.height = _Height;
// this.width = _Width;
// this.allGems = new SC_Gem[this.width, this.height];
// }
//
// //checks if there are 3 gems of the same type next to each other
// //used during setup to avoid matches on game start
// //MatchService
// public bool MatchesAt(Vector2Int _PositionToCheck, SC_Gem _GemToCheck)
// {
// if (_PositionToCheck.x > 1)
// {
// if (this.allGems[_PositionToCheck.x - 1, _PositionToCheck.y].type == _GemToCheck.type && this.allGems[_PositionToCheck.x - 2, _PositionToCheck.y].type == _GemToCheck.type)
// return true;
// }
//
// if (_PositionToCheck.y > 1)
// {
// if (this.allGems[_PositionToCheck.x, _PositionToCheck.y - 1].type == _GemToCheck.type && this.allGems[_PositionToCheck.x, _PositionToCheck.y - 2].type == _GemToCheck.type)
// return true;
// }
//
// return false;
// }
//
// //places the gem in the 2d array
// public void SetGem(int _X, int _Y, SC_Gem _Gem)
// {
// this.allGems[_X, _Y] = _Gem;
// }
//
// public SC_Gem GetGem(int _X,int _Y)
// {
// return this.allGems[_X, _Y];
// }
//
// //MatchService
// public void FindAllMatches()
// {
// this.currentMatches.Clear();
//
// for (int x = 0; x < this.width; x++)
// for (int y = 0; y < this.height; y++)
// {
// SC_Gem currentGem = this.allGems[x, y];
// if (currentGem != null)
// {
// if (x > 0 && x < this.width - 1)
// {
// SC_Gem leftGem = this.allGems[x - 1, y];
// SC_Gem rightGem = this.allGems[x + 1, y];
// //checking no empty spots
// if (leftGem != null && rightGem != null)
// {
// //Match
// if (leftGem.type == currentGem.type && rightGem.type == currentGem.type)
// {
// currentGem.isMatch = true;
// leftGem.isMatch = true;
// rightGem.isMatch = true;
// this.currentMatches.Add(currentGem);
// this.currentMatches.Add(leftGem);
// this.currentMatches.Add(rightGem);
// }
// }
// }
//
// if (y > 0 && y < this.height - 1)
// {
// SC_Gem aboveGem = this.allGems[x, y - 1];
// SC_Gem bellowGem = this.allGems[x, y + 1];
// //checking no empty spots
// if (aboveGem != null && bellowGem != null)
// {
// //Match
// if (aboveGem.type == currentGem.type && bellowGem.type == currentGem.type)
// {
// currentGem.isMatch = true;
// aboveGem.isMatch = true;
// bellowGem.isMatch = true;
// this.currentMatches.Add(currentGem);
// this.currentMatches.Add(aboveGem);
// this.currentMatches.Add(bellowGem);
// }
// }
// }
// }
// }
//
// if (this.currentMatches.Count > 0) this.currentMatches = this.currentMatches.Distinct().ToList();
//
// CheckForBombs();
// }
//
// public void CheckForBombs()
// {
// for (int i = 0; i < this.currentMatches.Count; i++)
// {
// SC_Gem gem = this.currentMatches[i];
// int x = gem.posIndex.x;
// int y = gem.posIndex.y;
//
// if (gem.posIndex.x > 0)
// {
// if (this.allGems[x - 1, y] != null && this.allGems[x - 1, y].type == GemType.Bomb)
// MarkBombArea(new Vector2Int(x - 1, y), this.allGems[x - 1, y].blastSize);
// }
//
// if (gem.posIndex.x + 1 < this.width)
// {
// if (this.allGems[x + 1, y] != null && this.allGems[x + 1, y].type == GemType.Bomb)
// MarkBombArea(new Vector2Int(x + 1, y), this.allGems[x + 1, y].blastSize);
// }
//
// if (gem.posIndex.y > 0)
// {
// if (this.allGems[x, y - 1] != null && this.allGems[x, y - 1].type == GemType.Bomb)
// MarkBombArea(new Vector2Int(x, y - 1), this.allGems[x, y - 1].blastSize);
// }
//
// if (gem.posIndex.y + 1 < this.height)
// {
// if (this.allGems[x, y + 1] != null && this.allGems[x, y + 1].type == GemType.Bomb)
// MarkBombArea(new Vector2Int(x, y + 1), this.allGems[x, y + 1].blastSize);
// }
// }
// }
//
// public void MarkBombArea(Vector2Int bombPos, int _BlastSize)
// {
// string _print = "";
// for (int x = bombPos.x - _BlastSize; x <= bombPos.x + _BlastSize; x++)
// {
// for (int y = bombPos.y - _BlastSize; y <= bombPos.y + _BlastSize; y++)
// {
// if (x >= 0 && x < this.width && y >= 0 && y < this.height)
// {
// if (this.allGems[x, y] != null)
// {
// _print += "(" + x + "," + y + ")" + System.Environment.NewLine;
// this.allGems[x, y].isMatch = true;
// this.currentMatches.Add(this.allGems[x, y]);
// }
// }
// }
// }
//
// this.currentMatches = this.currentMatches.Distinct().ToList();
// }
// }
//