전체 글
- [GitHub] Sprites-Outline 2024.05.20
- 2D 3Matchpuzzle - 블록 위치 찾기 테스트 2024.05.16
- 2D 3Matchpuzzle - 빈공간 찾기 2024.05.14
[GitHub] Sprites-Outline
2024. 5. 20. 13:01
2D 3Matchpuzzle - 블록 위치 찾기 테스트
2024. 5. 16. 13:10
완전 탐색을 하여 2중 for문을 돌린다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MatchTest : MonoBehaviour
{
private static readonly int Rows = 4;
private static readonly int Cols = 4;
private int[,] board =
{
{1, 1, 2, 0},
{1, 1, 1, 1},
{2, 1, 0, 1},
{0, 0, 1, 1}
};
void Start()
{
FindMatchingBlocks();
}
private void FindMatchingBlocks()
{
HashSet<(int, int)> matchedBlocks = new HashSet<(int, int)>();
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
CheckHorizontalMatch(i, j, matchedBlocks);
CheckVerticalMatch(i, j, matchedBlocks);
}
}
Debug.Log("Matching Blocks:");
foreach (var block in matchedBlocks)
{
Debug.Log($"({block.Item1}, {block.Item2})");
}
}
private void CheckHorizontalMatch(int row, int col, HashSet<(int, int)> matchedBlocks)
{
if (col + 2 < Cols &&
board[row, col] == board[row, col + 1] &&
board[row, col] == board[row, col + 2])
{
matchedBlocks.Add((row, col));
matchedBlocks.Add((row, col + 1));
matchedBlocks.Add((row, col + 2));
}
}
private void CheckVerticalMatch(int row, int col, HashSet<(int, int)> matchedBlocks)
{
if (row + 2 < Rows &&
board[row, col] == board[row + 1, col] &&
board[row, col] == board[row + 2, col])
{
matchedBlocks.Add((row, col));
matchedBlocks.Add((row + 1, col));
matchedBlocks.Add((row + 2, col));
}
}
}
'산대특' 카테고리의 다른 글
플레이콘솔 리더보드에 점수 올리기 (0) | 2024.06.26 |
---|---|
2D 3Matchpuzzle - 매치 시 파괴 및 로드 + 힌트 (0) | 2024.05.20 |
2D 3Matchpuzzle - 빈공간 찾기 (0) | 2024.05.14 |
Download Python (0) | 2024.05.08 |
Learn Firebase (0) | 2024.05.02 |
2D 3Matchpuzzle - 빈공간 찾기
2024. 5. 14. 13:08
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.U2D;
using Random = UnityEngine.Random;
public class Test : MonoBehaviour
{
public Board board;
private void Start()
{
board.CreateBoard();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 1);
RaycastHit2D raycastHit2D = Physics2D.Raycast(ray.origin, ray.direction);
if (raycastHit2D.collider != null)
{
Block block = raycastHit2D.collider.GetComponent<Block>();
Debug.Log($"[{block.row}, {block.col}] ({block.transform.position.x}, {block.transform.position.y}), {block.blockType}");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class Block : MonoBehaviour
{
public enum BlockType
{
Rabbit, Pig, Rat, Monkey, Cat, Chick, Puppy
}
public BlockType blockType;
public SpriteRenderer spriteRenderer;
public TMP_Text debugText;
public int row;
public int col;
public void Init(BlockType blockType)
{
this.blockType = blockType;
//이미지 변경
ChangeSprite(blockType);
}
public void ChangeSprite(BlockType blockType)
{
Sprite sp =
AtlasManager.instance.blockAtlas.GetSprite(blockType.ToString());
spriteRenderer.sprite = sp;
}
public void SetPosition(Vector2 pos)
{
transform.position = pos;
var index = Position2Index(pos);
row = index.row;
col = index.col;
debugText.text = $"[{index.row},{index.col}]";
}
public static (int row, int col) Position2Index(Vector2 pos)
{
return ((int)pos.y, (int)pos.x);
}
public static (int x, int y) Index2Position(Vector2 index)
{
return ((int)index.x, (int)index.y);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Block))]
public class BlockEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Block block = target as Block;
if (GUILayout.Button("제거"))
{
Debug.Log($"[{block.row},{block.col}]을 제거 합니다.");
Board.instance.RemoveBlock(block.row, block.col);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Random = UnityEngine.Random;
public class Board : MonoBehaviour
{
public static Board instance; //싱글톤
private Block[,] board; //2차원 배열이 Block들을 관리
public GameObject blockPrefab;
public int totalHeight = 14;
public int height = 7;
public int width = 7;
private void Awake()
{
instance = this;
}
public void CreateBoard()
{
//크기가 7개인 Block타입의 2차원 배열을 만들기
board = new Block[totalHeight, width];
for (int i = 0; i < height; i++) //행
{
for (int j = 0; j < width; j++) //열
{
CreateBlock(i, j);
}
}
PrintBoard();
}
public void CreateBlock(int row, int col)
{
Vector2 pos = new Vector2(col, row);
Block.BlockType blockType = (Block.BlockType)Random.Range(0, 7); //0 ~ 6
GameObject blockGo = Instantiate(blockPrefab, this.transform);
Block block = blockGo.GetComponent<Block>();
block.Init(blockType);
block.SetPosition(pos);
//배열의 요소에 블록 넣기
board[row, col] = block;
}
public void RemoveBlock(int row, int col)
{
Block block = board[row, col];
Destroy(block.gameObject); //게임오브젝트 파괴
//배열의 요소를 비워줌
board[row, col] = null;
}
public void PrintBoard()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < height; i++) //행
{
for (int j = 0; j < width; j++) //열
{
Block block = board[i, j];
string strElement = (block == null) ? "null" : block.blockType.ToString();
// 각 엘리먼트를 고정된 너비로 출력
sb.AppendFormat("[{0},{1}] = {2,-10}", i, j, strElement);
}
sb.AppendLine();
}
Debug.Log(sb.ToString());
}
private int[] arrEmptySpaceCol; //빈공간이 있는 컬럼 배열
public void FindEmptySpaceFromColumn()
{
arrEmptySpaceCol = new int[width]; //배열 초기화
//0 : 행, 1: 열
for (int i = 0; i < width; i++) //열
{
int existBlockCnt = 0;
for (int j = 0; j < totalHeight; j++) //행
{
Block block = board[j, i];
if (block != null) existBlockCnt++;
}
int emptySpace = height - existBlockCnt;
arrEmptySpaceCol[i] = emptySpace;
Debug.Log($"{i} 열의 빈공간은 {emptySpace}개 입니다.");
}
}
public void CreateNewBlocks()
{
// arrEmptySpaceCol가 null이거나 모든 요소가 0이면 빈공간이 없음
if (arrEmptySpaceCol == null || arrEmptySpaceCol.All(x => x == 0))
{
Debug.Log("빈공간이 없습니다.");
}
else
{
for (int i = 0; i < arrEmptySpaceCol.Length; i++)
{
int cnt = arrEmptySpaceCol[i]; //i(col)에 몇개 빈공간이 있는가?
if (cnt > 0)
{
Debug.Log($"{i}열에 {cnt}만큼 블록 생성 필요");
CreateBlockFromColumn(i, cnt);
}
}
}
arrEmptySpaceCol = null;
}
private void CreateBlockFromColumn(int col, int cnt)
{
int startRow = height; //행
for (int i = 0; i < cnt; i++)
{
//[7, col] -> [8, col] -> [9,col]
CreateBlock(startRow, col);
startRow++;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Board))]
public class BoardEditor : Editor
{
private int currentColIdx = 0;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Board board = target as Board;
if (GUILayout.Button("배열 요소 출력"))
{
board.PrintBoard(); ;
}
GUILayout.Space(5);
if (GUILayout.Button("각 열에 빈공간 찾기"))
{
board.FindEmptySpaceFromColumn();
}
GUILayout.Space(5);
if (GUILayout.Button("새로운 블록들 생성하기"))
{
board.CreateNewBlocks();
}
GUILayout.Space(5);
if (GUILayout.Button($"현재 행({currentColIdx})에 있는 모든 블록 내려보내기"))
{
Debug.Log($"{currentColIdx}행에 있는 모든 블록 내려 보냅니다.");
currentColIdx++;
if (currentColIdx > 6)
{
currentColIdx = 0;
}
}
GUILayout.Space(5);
if (GUILayout.Button("리셋 행"))
{
currentColIdx = 0;
}
}
}
'산대특' 카테고리의 다른 글
2D 3Matchpuzzle - 매치 시 파괴 및 로드 + 힌트 (0) | 2024.05.20 |
---|---|
2D 3Matchpuzzle - 블록 위치 찾기 테스트 (0) | 2024.05.16 |
Download Python (0) | 2024.05.08 |
Learn Firebase (0) | 2024.05.02 |
2D Vertical Shooting Game 모작 최종 (0) | 2024.04.08 |