首先在網上找一張棋盤的圖片(16x16),導入unity,圖片類型設置為Sprite(2D and UI),作為背景;
新建 2D 物體 sprite 在Sprite Render 內的Sprite 中指定之前導入的圖片。
通過Scale調整背景的大小 使得邊界落子位置能夠處在一個較為工整的坐標位置。
創建空物體 GameManager 用於掛載 GameManager。
創建空物體Gird 用於存放棋子。(坐標重置)
創建關於棋子的類 PointItem.cs
棋子類型分為空類型,白,黑。通過調節 color的rgba值來實現,游戲開始所有的棋子都為無色。
該類掛載在棋子預支體上
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 public enum PointType 6 { 7 Empty, 8 White, 9 Black 10 } 11 12 public class PointItem :MonoBehaviour 13 { 14 PointType pointType; 15 SpriteRenderer sr; 16 public int X, Y; 17 18 void Start() 19 { 20 sr = this.gameObject.GetComponent<SpriteRenderer>(); 21 } 22 23 public PointType PointType 24 { 25 get { return pointType; } 26 set 27 { 28 pointType = value; 29 if (pointType == PointType.White) 30 sr.color = new Color(1, 1, 1, 1); 31 if (pointType == PointType.Black) 32 sr.color = new Color(0, 0, 0, 1); 33 } 34 } 35 36 public PointItem(int x,int y) 37 { 38 this.X = x; 39 this.Y = y; 40 this.pointType = PointType.Empty; 41 } 42 }
預制體的創建只需要用基本圖形創建圓形的2d sprite就可以了(初始設置為全透明) 設置好 tag 和 layer 即可
GameManager.cs如下 算法很爛 但是感覺思路應該問題不大
每次落子的時候(先進行橫向匹配),先向左遍歷,如果左邊第一子與落子顏色相同,繼續向左遍歷,如果左邊第二子顏色不同或為空,
則直接判斷右邊三子(3個)的顏色是否與落子相同,如果相同則落子勝 否則為不勝。
可以理解為左一同,則右三同可勝
左二同,則右二同可勝
。。。
同理可以做出縱向匹配和斜向匹配。(注意好索引溢出的問題就好了)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameManager : MonoBehaviour { PointItem[,] points; public GameObject go; GameObject gird; private PointType State = PointType.White; void Start () { points = new PointItem[16,16]; gird = GameObject.FindGameObjectWithTag("Gird"); for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { Vector3 position = new Vector3(-7.5f + Mathf.Floor(j), 7.5f - Mathf.Floor(i), 0f); GameObject goo = Instantiate(go,position,Quaternion.identity); goo.transform.SetParent(gird.transform); PointItem p = goo.transform.GetComponent<PointItem>(); p.X = j; p.Y = i; points[j,i] = p; } } } void Update() { if (Input.GetMouseButtonDown(0)) { //Debug.Log("點擊了"); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; PointItem p; if(Physics.Raycast(ray,out hit)) { if (hit.transform.gameObject.tag == "trigger") { p = hit.transform.GetComponent<PointItem>(); Choose(p.X,p.Y); // Debug.Log(new Vector2(p.X, p.Y)); } } } } public void Choose(int x,int y) { if (points[x,y].PointType != PointType.Empty) return; points[x,y].PointType = State; Comfirm(x, y, State); if (State == PointType.White) State = PointType.Black; else State = PointType.White; } public void Comfirm(int x, int y, PointType p) { //橫向匹配 if (x - 1 > -1 && points[x - 1, y].PointType == p) { if (x - 2 > -1 && points[x - 2, y].PointType == p) { if (x - 3 > -1 && points[x - 3, y].PointType == p) { if (x - 4 > -1 && points[x - 4, y].PointType == p) { Debug.Log(p + "贏了"); } else if (x + 1 < 16) { if (points[x + 1, y].PointType == p) Debug.Log(p + "贏了"); } } else if (x + 2 < 16) { if (points[x + 1, y].PointType == p && points[x + 2, y].PointType == p) Debug.Log(p + "贏了"); } } else if (x + 3 < 16) { if (points[x + 1, y].PointType == p && points[x + 2, y].PointType == p && points[x + 3, y].PointType == p) Debug.Log(p + "贏了"); } } else if (x + 4 < 16) { if (points[x + 1, y].PointType == p && points[x + 2, y].PointType == p && points[x + 3, y].PointType == p && points[x + 4, y].PointType == p) Debug.Log(p + "贏了"); } //縱向匹配 if (y - 1 > -1 && points[x, y - 1].PointType == p) { if (y - 2 > -1 && points[x, y - 2].PointType == p) { if (y - 3 > -1 && points[x, y - 3].PointType == p) { if (y - 4 > -1 && points[x, y - 4].PointType == p) { Debug.Log(p + "贏了"); } else if (y + 1 < 16) { if (points[x, y + 1].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 2 < 16) { if (points[x, y + 1].PointType == p && points[x, y + 2].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 3 < 16) { if (points[x, y + 1].PointType == p && points[x, y + 2].PointType == p && points[x, y + 3].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 4 < 16) { if (points[x, y + 1].PointType == p && points[x, y + 2].PointType == p && points[x, y + 3].PointType == p && points[x, y + 4].PointType == p) Debug.Log(p + "贏了"); } //左上右下匹配 if (y - 1 > -1 && x - 1 > -1 && points[x - 1, y - 1].PointType == p) { if (y - 2 > -1 && x - 2 > -1 && points[x - 2, y - 2].PointType == p) { if (y - 3 > -1 && x - 3 > -1 && points[x - 3, y - 3].PointType == p) { if (y - 4 > -1 && x - 4 > -1 && points[x - 4, y - 4].PointType == p) { Debug.Log(p + "贏了"); } else if (y + 1 < 16 && x + 1 < 16) { if (points[x + 1, y + 1].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 2 < 16 && x + 2 < 16) { if (points[x + 1, y + 1].PointType == p && points[x + 2, y + 2].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 3 < 16 && x + 3 < 16) { if (points[x + 1, y + 1].PointType == p && points[x + 2, y + 2].PointType == p && points[x + 3, y + 3].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 4 < 16 && x + 4 < 16) { if (points[x + 1, y + 1].PointType == p && points[x + 2, y + 2].PointType == p && points[x + 3, y + 3].PointType == p && points[x + 4, y + 4].PointType == p) Debug.Log(p + "贏了"); } //右上左下匹配 if (y - 1 > -1 && x + 1 < 16 && points[x + 1, y - 1].PointType == p) { if (y - 2 > -1 && x + 2 < 16 && points[x + 2, y - 2].PointType == p) { if (y - 3 > -1 && x + 3 < 16 && points[x + 3, y - 3].PointType == p) { if (y - 4 > -1 && x + 4 < 16 && points[x + 4, y - 4].PointType == p) { Debug.Log(p + "贏了"); } else if (y + 1 < 16 && x - 1 > -1) { if (points[x - 1, y + 1].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 2 < 16 && x - 2 > -1) { if (points[x - 1, y + 1].PointType == p && points[x - 2, y + 2].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 3 < 16 && x - 3 > -1) { if (points[x - 1, y + 1].PointType == p && points[x - 2, y + 2].PointType == p && points[x - 3, y + 3].PointType == p) Debug.Log(p + "贏了"); } } else if (y + 4 < 16 && x - 4 > -1) { if (points[x - 1, y + 1].PointType == p && points[x - 2, y + 2].PointType == p && points[x - 3, y + 3].PointType == p && points[x - 4, y + 4].PointType == p) Debug.Log(p + "贏了"); } } }
算法感覺很笨拙 而且即使前面勝利 后面的if語句還是會執行。
我的解決思路是將這4個匹配做成4個方法里 返回bool值
再通過或運算來得到結果。
不知道還有沒有什么更好的辦法。
感覺應該有更省事的寫法,不知道有沒有大神指點一下。