您好,登錄后才能下訂單哦!
本篇內容介紹了“Unity3D怎么實現簡易五子棋”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
Unity3d部分
對C#源碼進行了改寫簡化:
using UnityEngine;using System.Collections;public class chess : MonoBehaviour{ //四個錨點位置,用于計算棋子落點 public GameObject LeftTop; public GameObject RightTop; public GameObject LeftBottom; public GameObject RightBottom; //主攝像機 public Camera cam; //錨點在屏幕上的映射位置 Vector3 LTPos; Vector3 RTPos; Vector3 LBPos; Vector3 RBPos; Vector3 PointPos;//當前點選的位置 float gridWidth = 1; //棋盤網格寬度 float gridHeight = 1; //棋盤網格高度 float minGridDis; //網格寬和高中較小的一個 Vector2[,] chessPos; //存儲棋盤上所有可以落子的位置 int[,] chessState; //存儲棋盤位置上的落子狀態 enum turn { black, white }; turn chessTurn; //落子順序 public Texture2D white; //白棋子 public Texture2D black; //黑棋子 public Texture2D blackWin; //白子獲勝提示圖 public Texture2D whiteWin; //黑子獲勝提示圖 int winner = 0; //獲勝方,1為黑子,-1為白子 bool isPlaying = true; //是否處于對弈狀態 void Start() { chessPos = new Vector2[15, 15]; chessState = new int[17, 16];/*原來定義是new int[15, 15],這里將原來數組chessState上、下和右邊各加一排數據, 也就相當于在棋盤的上、下和右邊各填加一排隱形的棋道。原因后面解釋*/ chessTurn = turn.black; //計算錨點位置 LTPos = cam.WorldToScreenPoint(LeftTop.transform.position); RTPos = cam.WorldToScreenPoint(RightTop.transform.position); LBPos = cam.WorldToScreenPoint(LeftBottom.transform.position); RBPos = cam.WorldToScreenPoint(RightBottom.transform.position); //計算網格寬度 gridWidth = (RTPos.x - LTPos.x) / 14; gridHeight = (LTPos.y - LBPos.y) / 14; minGridDis = gridWidth < gridHeight ? gridWidth : gridHeight; //計算落子點位置 for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { chessPos[i, j] = new Vector2(LBPos.x + gridWidth * j, LBPos.y + gridHeight * i);//這里和源程序定義稍有不同,這里i定位行,j為列 } } } void Update() { //檢測鼠標輸入并確定落子狀態 if (isPlaying && Input.GetMouseButtonDown(0)) { PointPos = Input.mousePosition; for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { //找到最接近鼠標點擊位置的落子點,如果空則落子 if (Dis(PointPos, chessPos[i, j]) < minGridDis / 2 && chessState[i + 1, j] == 0)/*這里chessState行要加1, 因為上、下和右邊各多加了一排,要空出來,chessPos的i行對應chessState的i+1行*/ { //根據下棋順序確定落子顏色 chessState[i + 1, j] = chessTurn == turn.black ? 1 : -1;//同理 //落子成功,更換下棋順序 chessTurn = chessTurn == turn.black ? turn.white : turn.black; } } } //調用判斷函數,確定是否有獲勝方 int re = result(); if (re == 1) { Debug.Log("黑棋勝"); winner = 1; isPlaying = false; } else if (re == -1) { Debug.Log("白棋勝"); winner = -1; isPlaying = false; } } //按下空格重新開始游戲 if (Input.GetKeyDown(KeyCode.Space)) { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { chessState[i + 1, j] = 0;//同理 } } isPlaying = true; chessTurn = turn.black; winner = 0; } } //計算平面距離函數 float Dis(Vector3 mPos, Vector2 gridPos) { return Mathf.Sqrt(Mathf.Pow(mPos.x - gridPos.x, 2) + Mathf.Pow(mPos.y - gridPos.y, 2)); } void OnGUI() { //繪制棋子 for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (chessState[i + 1, j] == 1)//同理 { GUI.DrawTexture(new Rect(chessPos[i, j].x - gridWidth / 2, Screen.height - chessPos[i, j].y - gridHeight / 2, gridWidth, gridHeight), black); } if (chessState[i + 1, j] == -1)//同理 { GUI.DrawTexture(new Rect(chessPos[i, j].x - gridWidth / 2, Screen.height - chessPos[i, j].y - gridHeight / 2, gridWidth, gridHeight), white); } } } //根據獲勝狀態,彈出相應的勝利圖片 if (winner == 1) { GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), blackWin); } if (winner == -1) GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), whiteWin); } //改寫result函數 /*解釋:C語言中,這樣的表達式:chessState[i]&&chessState[i+1]&&chessState[i+2]&&chessState[i+3]&&chessState[i+4],如果 * chessState[i]為False,則不管B是真是假或者是異常都不會運行,利用這一點,在chessState的右邊、上邊和下邊各加一行為0的數據, * 這樣在判斷連續五個棋子的狀態時,就不用擔心chessState數組的索引值超出范圍。例如:chessState[i+4]的索引值i+4剛好超出范圍, * 通過在原來數組chessState的上、下和右邊個添加一排為0的數,這樣chessState[i+3]==0,于是就可以避免引起異常,從而簡化代碼*/ int result() { int flag = 0; if (chessTurn == turn.white) { for (int i = 1; i <= 15; i++)//這里的i從1開始 { for (int j = 0; j <= 14; j++)//j不用變 { if ((chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)//向右橫向 || (chessState[i, j] == 1 && chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1 && chessState[i + 4, j] == 1)//向上橫向 || (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1 && chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)//向右上斜向 || (chessState[i, j] == 1 && chessState[i - 1, j + 1] == 1 && chessState[i - 2, j + 2] == 1 && chessState[i - 3, j + 3] == 1 && chessState[i - 4, j + 4] == 1))//向右下斜向 { flag = 1; } } } } else if (chessTurn == turn.black) { for (int i = 1; i <= 15; i++)//這里的i從1開始 { for (int j = 0; j <= 14; j++) { if ((chessState[i, j] == -1 && chessState[i, j + 1] == -1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1) || (chessState[i, j] == -1 && chessState[i + 1, j] == -1 && chessState[i + 2, j] == -1 && chessState[i + 3, j] == -1 && chessState[i + 4, j] == -1) || (chessState[i, j] == -1 && chessState[i + 1, j + 1] == -1 && chessState[i + 2, j + 2] == -1 && chessState[i + 3, j + 3] == -1 && chessState[i + 4, j + 4] == -1) || (chessState[i, j] == -1 && chessState[i - 1, j + 1] == -1 && chessState[i - 2, j + 2] == -1 && chessState[i - 3, j + 3] == -1 && chessState[i - 4, j + 4] == -1)) { flag = -1; } } } } return flag; }}
“Unity3D怎么實現簡易五子棋”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。