I want to create a match3 game using Unity Engine, where 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 2D.
Assistant
User
please implement and develop MatchFinder class too.
Assistant
Creating a Match 3 game in Unity with solid architecture principles can be broken down into several components. Below is a comprehensive example, encompassing essential elements of a basic Match 3 game. This has been organized in accordance with SOLID principles to maintain readability, maintainability, and scalability.
Project Structure
Scripts
GameManager.cs
BoardManager.cs
Tile.cs
InputHandler.cs
MatchFinder.cs
Prefabs
Tile Prefab
Scenes
Main Scene
Code Implementation
1. GameManager.cs
csharp
using UnityEngine;
public class GameManager : MonoBehaviour
{
public int rows;
public int columns;
private BoardManager boardManager;
private void Awake()
{
boardManager = GetComponent<BoardManager>();
}
private void Start()
{
boardManager.SetupBoard(rows, columns);
}
}
2. BoardManager.cs
csharp
using UnityEngine;
public class BoardManager : MonoBehaviour
{
public GameObject tilePrefab;
private Tile[,] tiles;
public void SetupBoard(int rows, int columns)
{
tiles = new Tile[rows, columns];
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < columns; y++)
{
GameObject newTile = Instantiate(tilePrefab, new Vector2(x, y), Quaternion.identity);
newTile.name = $"Tile {x} {y}";
tiles[x, y] = newTile.GetComponent<Tile>();
tiles[x, y].SetupTile(this, x, y);
}
}
}
public Tile GetTile(int x, int y)
{
if (x >= 0 && x < tiles.GetLength(0) && y >= 0 && y < tiles.GetLength(1))
{
return tiles[x, y];
}
return null;
}
}
3. Tile.cs
csharp
using UnityEngine;
public class Tile : MonoBehaviour
{
private BoardManager boardManager;
private int xIndex;
private int yIndex;
public void SetupTile(BoardManager boardManager, int x, int y)
{
this.boardManager = boardManager;
this.xIndex = x;
this.yIndex = y;
GetComponent<SpriteRenderer>().color = new Color(Random.value, Random.value, Random.value); // example color
}
private void OnMouseDown()
{
InputHandler.Instance.OnTileClick(this);
}
public void OnSelected()
{
// Highlight tile or some feedback
GetComponent<SpriteRenderer>().color = Color.red; // example visual feedback
}
public void ResetColor()
{
GetComponent<SpriteRenderer>().color = new Color(Random.value, Random.value, Random.value); // reset color
}
public int GetXIndex() => xIndex;
public int GetYIndex() => yIndex;
}
4. InputHandler.cs
csharp
using UnityEngine;
public class InputHandler : MonoBehaviour
{
public static InputHandler Instance;
private Tile selectedTile;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public void OnTileClick(Tile tile)
{
if (selectedTile == null)
{
selectedTile = tile;
tile.OnSelected();
}
else
{
// Swap logic or match finding can go here
SwapTiles(selectedTile, tile);
selectedTile.ResetColor();
selectedTile = null;
}
}
private void SwapTiles(Tile tile1, Tile tile2)
{
// Basic swap logic between tiles
int tempX = tile1.GetXIndex();
int tempY = tile1.GetYIndex();
tile1.SetupTile(tile1.GetComponent<BoardManager>(), tile2.GetXIndex(), tile2.GetYIndex());
tile2.SetupTile(tile2.GetComponent<BoardManager>(), tempX, tempY);
}
}
5. MatchFinder.cs (optional; to be implemented if required)
csharp
// Not fully implemented but could be used to find matches after swaps
public class MatchFinder : MonoBehaviour
{
// Logic for finding matches should be defined here
}
Setup the Unity Scene
Create a new 2D Unity project.
Create a new empty GameObject in the Scene named "Game Manager".
Attach the GameManager.cs script to the "Game Manager" GameObject.
Create a new GameObject with a SpriteRenderer to represent tiles, and save it as a Prefab (named "Tile").
Add the Tile.cs script to the Tile Prefab, and set up a default sprite.
Link the Tile Prefab to the BoardManager component in the GameManager inspector.
Conclusion
This code provides a fundamental structure for your Match 3 game, aligning well with the SOLID principles. The architecture supports clean separation of concerns, making future modifications easier, such as implementing match finding, scoring, and other game mechanics. Remember that this is a scaffolding structure; you'll need to expand upon match-finding logic, game-over conditions, animations, and additional features to create a fully-fledged game.
Certainly! The MatchFinder class will be responsible for finding matches of tiles once they are swapped. We'll implement a basic match-finding algorithm that checks horizontally and vertically for three or more contiguous tiles of the same "type" (which in this case can be represented by the color).
Here is how you can implement the MatchFinder class:
MatchFinder.cs
csharp
using System.Collections.Generic;
using UnityEngine;
public class MatchFinder : MonoBehaviour
{
private BoardManager boardManager;
private void Awake()
{
boardManager = GetComponent<BoardManager>();
}
public List<Tile> FindMatches(int x, int y)
{
List<Tile> matchedTiles = new List<Tile>();
// Check horizontal matches
matchedTiles.AddRange(CheckHorizontalMatches(x, y));
// Check vertical matches
matchedTiles.AddRange(CheckVerticalMatches(x, y));
return matchedTiles;
}
private List<Tile> CheckHorizontalMatches(int x, int y)
{
List<Tile> horizontalMatches = new List<Tile>();
Tile startingTile = boardManager.GetTile(x, y);
Color startingColor = startingTile.GetComponent<SpriteRenderer>().color;
// Check left
for (int i = x - 1; i >= 0; i--)
{
Tile tile = boardManager.GetTile(i, y);
if (tile != null && tile.GetComponent<SpriteRenderer>().color == startingColor)
{
horizontalMatches.Add(tile);
}
else
{
break;
}
}
// Check right
for (int i = x + 1; i < boardManager.tiles.GetLength(0); i++)
{
Tile tile = boardManager.GetTile(i, y);
if (tile != null && tile.GetComponent<SpriteRenderer>().color == startingColor)
{
horizontalMatches.Add(tile);
}
else
{
break;
}
}
// Include the starting tile and return if there are at least two adjacent tiles
horizontalMatches.Add(startingTile);
return horizontalMatches.Count >= 3 ? horizontalMatches : new List<Tile>();
}
private List<Tile> CheckVerticalMatches(int x, int y)
{
List<Tile> verticalMatches = new List<Tile>();
Tile startingTile = boardManager.GetTile(x, y);
Color startingColor = startingTile.GetComponent<SpriteRenderer>().color;
// Check down
for (int i = y - 1; i >= 0; i--)
{
Tile tile = boardManager.GetTile(x, i);
if (tile != null && tile.GetComponent<SpriteRenderer>().color == startingColor)
{
verticalMatches.Add(tile);
}
else
{
break;
}
}
// Check up
for (int i = y + 1; i < boardManager.tiles.GetLength(1); i++)
{
Tile tile = boardManager.GetTile(x, i);
if (tile != null && tile.GetComponent<SpriteRenderer>().color == startingColor)
{
verticalMatches.Add(tile);
}
else
{
break;
}
}
// Include the starting tile and return if there are at least two adjacent tiles
verticalMatches.Add(startingTile);
return verticalMatches.Count >= 3 ? verticalMatches : new List<Tile>();
}
public void RemoveMatches(List<Tile> matchedTiles)
{
foreach (Tile tile in matchedTiles)
{
Destroy(tile.gameObject);
// Additional logic can be implemented here to handle score updates
}
}
}
Modifications in Other Classes
To integrate the MatchFinder class into the existing structure, you need to modify the InputHandler class to invoke MatchFinder after swapping tiles.
Updated InputHandler.cs
csharp
using UnityEngine;
using System.Collections.Generic;
public class InputHandler : MonoBehaviour
{
public static InputHandler Instance;
private Tile selectedTile;
private MatchFinder matchFinder;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
matchFinder = GetComponent<MatchFinder>();
}
public void OnTileClick(Tile tile)
{
if (selectedTile == null)
{
selectedTile = tile;
tile.OnSelected();
}
else
{
SwapTiles(selectedTile, tile);
selectedTile.ResetColor();
selectedTile = null;
// Check for matches after swap
List<Tile> matches = matchFinder.FindMatches(tile.GetXIndex(), tile.GetYIndex());
if (matches.Count > 0)
{
matchFinder.RemoveMatches(matches);
// You may also want to refill the board or implement other game logic here
}
}
}
private void SwapTiles(Tile tile1, Tile tile2)
{
// Basic swap logic between tiles
int tempX = tile1.GetXIndex();
int tempY = tile1.GetYIndex();
tile1.SetupTile(tile1.GetComponent<BoardManager>(), tile2.GetXIndex(), tile2.GetYIndex());
tile2.SetupTile(tile2.GetComponent<BoardManager>(), tempX, tempY);
}
}
Additional Steps
Destroying Tiles: The above logic destroys the matched tiles. You might want to handle the board refilling logic after removing matches. This can be implemented in the GameManager or BoardManager to maintain the grid state.
Scoring System: As part of expanding your game, consider implementing a scoring system to keep track of the player's score when tiles are matched and removed.
Animations: For a better user experience, you might want to implement animations for tile movements and matches.
Refilling the Board: After matches are removed, you will want to implement logic to refill the empty spaces – either by spawning new tiles or moving down existing ones.
This implementation provides a basic framework for a Match 3 game in Unity, maintaining a clean and organized architecture using SOLID principles. You can expand upon this by implementing additional features and mechanics to make your game more engaging!