Untitled
unknown
plain_text
4 months ago
5.8 kB
2
Indexable
using UnityEngine; using System.Collections.Generic; public class NumberPuzzle : MonoBehaviour { public GameObject gridCellPrefab; public GameObject targetCellPrefab; public Transform gridParent; public Transform topParent; public Transform leftParent; public int gridSize = 4; // Grid size (4x4) private int[,] gridValues; private int[] rowSums; // Sum for each row private int[] columnSums; // Sum for each column private List<Vector2Int> removableCells = new List<Vector2Int>(); // List to store removable cells private void Start() { GenerateGridValues(); CalculateSums(); DisplayGrid(); DisplayTargets(); RemoveRandomNumbers(); // Remove some numbers to make the puzzle solvable } // Generate the grid values (random numbers between 1 and 9) private void GenerateGridValues() { gridValues = new int[gridSize, gridSize]; for (int row = 0; row < gridSize; row++) { for (int col = 0; col < gridSize; col++) { // Generate random numbers between 1 and 9 for the grid gridValues[row, col] = Random.Range(1, 10); } } } // Calculate the sum for each row and column private void CalculateSums() { rowSums = new int[gridSize]; columnSums = new int[gridSize]; for (int row = 0; row < gridSize; row++) { int rowSum = 0; for (int col = 0; col < gridSize; col++) { rowSum += gridValues[row, col]; } rowSums[row] = rowSum; } for (int col = 0; col < gridSize; col++) { int colSum = 0; for (int row = 0; row < gridSize; row++) { colSum += gridValues[row, col]; } columnSums[col] = colSum; } } // Create and display the target cells (row and column sums) private void DisplayTargets() { // Generate top targets (column sums) for (int col = 0; col < gridSize; col++) { CreateTargetCell(columnSums[col], col, true); // Top row } // Generate left targets (row sums) for (int row = 0; row < gridSize; row++) { CreateTargetCell(rowSums[row], row, false); // Left column } } // Create a target cell private void CreateTargetCell(int value, int index, bool isTop) { GameObject targetCell = Instantiate(targetCellPrefab); Vector3 position; if (isTop) { position = topParent.position + new Vector3(index * 2.0f, 0, 0); } else { position = leftParent.position + new Vector3(0, -index * 2.0f, 0); } targetCell.transform.position = position; TargetCell targetScript = targetCell.GetComponent<TargetCell>(); targetScript.SetValue(value); } // Display the grid with numbers in Unity private void DisplayGrid() { for (int row = 0; row < gridSize; row++) { for (int col = 0; col < gridSize; col++) { GameObject gridCell = Instantiate(gridCellPrefab, gridParent.position + new Vector3(col * 2.0f, -row * 2.0f, 0), Quaternion.identity, gridParent); GridCell cellScript = gridCell.GetComponent<GridCell>(); cellScript.SetValue(gridValues[row, col]); cellScript.SetCoordinates(row, col); // Set the grid coordinates } } } // Remove some random numbers and update sums accordingly private void RemoveRandomNumbers() { // Randomly decide how many cells to remove int totalRemovals = Random.Range(5, 10); // For example, removing 5 to 10 cells for (int i = 0; i < totalRemovals; i++) { int row = Random.Range(0, gridSize); int col = Random.Range(0, gridSize); // Ensure the cell is not already removed if (gridValues[row, col] != 0) { removableCells.Add(new Vector2Int(row, col)); // Mark cell for removal gridValues[row, col] = 0; // Remove the number by setting it to 0 } } RecalculateSumsAfterRemoval(); // Recalculate sums after removing numbers } // Recalculate row and column sums after numbers have been removed private void RecalculateSumsAfterRemoval() { for (int row = 0; row < gridSize; row++) { int rowSum = 0; for (int col = 0; col < gridSize; col++) { rowSum += gridValues[row, col]; } rowSums[row] = rowSum; } for (int col = 0; col < gridSize; col++) { int colSum = 0; for (int row = 0; row < gridSize; row++) { colSum += gridValues[row, col]; } columnSums[col] = colSum; } } private void Update() { if (Input.GetMouseButtonDown(0)) { Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero); if (hit.collider != null) { GridCell clickedCell = hit.collider.GetComponent<GridCell>(); if (clickedCell != null && !removableCells.Contains(new Vector2Int(clickedCell.GetRow(), clickedCell.GetCol()))) { clickedCell.RemoveNumber(); // Remove number from grid RecalculateSumsAfterRemoval(); // Recalculate sums after removal } } } } }
Editor is loading...
Leave a Comment