match 3 game code chatgpt.txt
public GameObject[,] gameBoard; public GameObject gamePiecePrefab; public int boardWidth = 8; public int boardHeight = 8;
void Start() { gameBoard = new GameObject[boardWidth, boardHeight]; for (int x = 0; x < boardWidth; x++) { for (int y = 0; y < boardHeight; y++) { gameBoard[x, y] = Instantiate(gamePiecePrefab, new Vector2(x, y), Quaternion.identity); } } } void CheckMatches() { for (int x = 0; x < boardWidth; x++) { for (int y = 0; y < boardHeight; y++) { GameObject currentPiece = gameBoard[x, y];
// Check for horizontal matches if (x + 2 < boardWidth) { GameObject matchPiece1 = gameBoard[x + 1, y]; GameObject matchPiece2 = gameBoard[x + 2, y]; if (currentPiece.tag == matchPiece1.tag && currentPiece.tag == matchPiece2.tag) { Destroy(currentPiece); Destroy(matchPiece1); Destroy(matchPiece2); // Add score, play sound, etc. } }
// Check for vertical matches if (y + 2 < boardHeight) { GameObject matchPiece1 = gameBoard[x, y + 1]; GameObject matchPiece2 = gameBoard[x, y + 2]; if (currentPiece.tag == matchPiece1.tag && currentPiece.tag == matchPiece2.tag) { Destroy(currentPiece); Destroy(matchPiece1); Destroy(matchPiece2); // Add score, play sound, etc. } } } } } void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit) { // Get the clicked game piece GameObject clickedPiece = hit.collider.gameObject;
// Check for adjacent game pieces GameObject adjacentPiece = null; if (hit.collider.tag == "GamePiece") { int x = (int)hit.collider.transform.position.x; int y = (int)hit.collider.transform.position.y; if (x > 0 && gameBoard[x - 1, y] != null) { adjacentPiece = gameBoard[x - 1, y]; } if (x < boardWidth - 1 && gameBoard[x + 1, y] != null) { adjacentPiece = gameBoard[x + 1, y]; } if (y > 0 && gameBoard[x, y - 1] != null) { adjacentPiece = gameBoard[x, y - 1]; } if (y < boardHeight - 1 && gameBoard[x, y + 1] != null) { adjacentPiece = game