I want to create a match3 game using Unity Engine so that I can enter the number of rows and columns in the inspector. This game is for Android, but it works with the mouse in the editor. Write me a complete code example that uses the software architecture structure and solid principles in its coding. This game is two-dimensional and uses sprites. And the MatchFinder class is developed in it, so that the movement of items is done smoothly.
Assistant
Creating a Match-3 game in Unity while adhering to SOLID principles involves structuring your code into different classes and components, each responsible for a single aspect of the game's functionality. Below is a complete example of how to structure this game, including a grid manager, match finder, and a basic implementation of item movement and matching.
This class represents the individual items (gems) in the game.
csharp
using UnityEngine;
public class Gem : MonoBehaviour
{
public Vector2Int Position { get; set; }
public void Initialize(Vector2Int position)
{
Position = position;
}
public void MoveTo(Vector2Int newPosition)
{
Position = newPosition;
// OPTIONAL: Smooth movement implementation can be added here.
}
}
MatchFinder.cs
This class is responsible for finding matches in the grid.
csharp
using System.Collections.Generic;
using UnityEngine;
public class MatchFinder
{
private readonly int _rows;
private readonly int _columns;
private readonly Gem[,] _grid;
public MatchFinder(int rows, int columns, Gem[,] grid)
{
_rows = rows;
_columns = columns;
_grid = grid;
}
public List<Gem> FindMatches()
{
List<Gem> matchedGems = new List<Gem>();
for (int row = 0; row < _rows; row++)
{
for (int col = 0; col < _columns; col++)
{
Gem currentGem = _grid[row, col];
if (currentGem != null)
{
// Check horizontal matches
if (col <= _columns - 3) CheckMatch(currentGem, matchedGems, row, col, 0);
// Check vertical matches
if (row <= _rows - 3) CheckMatch(currentGem, matchedGems, row, col, 1);
}
}
}
return matchedGems;
}
private void CheckMatch(Gem currentGem, List<Gem> matchedGems, int row, int col, int direction)
{
int count = 1;
while (true)
{
int newRow = row + (direction == 1 ? count : 0);
int newCol = col + (direction == 0 ? count : 0);
if (newRow >= _rows || newCol >= _columns || _grid[newRow, newCol] == null ||
_grid[newRow, newCol].GetType() != currentGem.GetType())
{
break;
}
matchedGems.Add(_grid[newRow, newCol]);
count++;
}
if (count >= 3)
{
matchedGems.Add(currentGem);
}
}
}
GridManager.cs
This class manages the game grid, initializing gems and handling their movement.
csharp
using UnityEngine;
public class GridManager : MonoBehaviour
{
public int rows = 6;
public int columns = 6;
public GameObject gemPrefab;
private Gem[,] grid;
void Start()
{
InitializeGrid();
}
private void InitializeGrid()
{
grid = new Gem[rows, columns];
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
CreateGem(row, col);
}
}
}
private void CreateGem(int row, int col)
{
GameObject gemObject = Instantiate(gemPrefab, new Vector2(col, row), Quaternion.identity);
Gem gem = gemObject.GetComponent<Gem>();
gem.Initialize(new Vector2Int(col, row));
grid[row, col] = gem;
}
public void CheckForMatches()
{
MatchFinder matchFinder = new MatchFinder(rows, columns, grid);
var matchedGems = matchFinder.FindMatches();
if (matchedGems.Count > 0)
{
foreach (var gem in matchedGems)
{
Destroy(gem.gameObject); // Handle matched gems
}
// Refill grid or perform other game management tasks
}
}
}
GameManager.cs
This class can handle user input and integrate different game logic together.
csharp
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GridManager gridManager;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Here you would implement the logic to handle input to swap gems
// For example, you could cast a ray and check which gem was clicked on
// Sample code for checking matches could go here
gridManager.CheckForMatches();
}
}
}
Important Considerations
Smooth Movement: To implement smooth movement for the gems, you can use a coroutine or a tweening library (like DOTween). For simplicity, this example utilizes placeholder methods.
Matching Logic: The matching logic in MatchFinder is basic. You may want to refine it by adding randomization, more gem types, and additional game mechanics.
Input Handling: The input mechanism currently uses mouse input for PC testing. You should extend it to support touch input for mobile by using Input.touchCount and Input.GetTouch().
Design Patterns: You could further improve your architecture by applying the Command or Strategy patterns to handle user input and gem swap logic.
After implementing this structure, ensure you create a prefab for your gems in Unity and assign it to gemPrefab in the GridManager component on a GameObject.
This example should give you a solid foundation to build upon for your Match-3 game. From here, you can work on additional features like animations, scoring, levels, and sound effects.