Unity3D實(shí)現(xiàn)簡(jiǎn)易五子棋源碼
更新時(shí)間:2019年09月17日 09:48:49 投稿:lijiao
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)簡(jiǎn)易五子棋源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Unity3d簡(jiǎn)易五子棋源碼,供大家參考,具體內(nèi)容如下
對(duì)C#源碼進(jìn)行了改寫(xiě)簡(jiǎn)化:
using UnityEngine; using System.Collections; public class chess : MonoBehaviour { //四個(gè)錨點(diǎn)位置,用于計(jì)算棋子落點(diǎn) public GameObject LeftTop; public GameObject RightTop; public GameObject LeftBottom; public GameObject RightBottom; //主攝像機(jī) public Camera cam; //錨點(diǎn)在屏幕上的映射位置 Vector3 LTPos; Vector3 RTPos; Vector3 LBPos; Vector3 RBPos; Vector3 PointPos;//當(dāng)前點(diǎn)選的位置 float gridWidth = 1; //棋盤(pán)網(wǎng)格寬度 float gridHeight = 1; //棋盤(pán)網(wǎng)格高度 float minGridDis; //網(wǎng)格寬和高中較小的一個(gè) Vector2[,] chessPos; //存儲(chǔ)棋盤(pán)上所有可以落子的位置 int[,] chessState; //存儲(chǔ)棋盤(pán)位置上的落子狀態(tài) 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; //是否處于對(duì)弈狀態(tài) void Start() { chessPos = new Vector2[15, 15]; chessState = new int[17, 16];/*原來(lái)定義是new int[15, 15],這里將原來(lái)數(shù)組chessState上、下和右邊各加一排數(shù)據(jù), 也就相當(dāng)于在棋盤(pán)的上、下和右邊各填加一排隱形的棋道。原因后面解釋*/ chessTurn = turn.black; //計(jì)算錨點(diǎn)位置 LTPos = cam.WorldToScreenPoint(LeftTop.transform.position); RTPos = cam.WorldToScreenPoint(RightTop.transform.position); LBPos = cam.WorldToScreenPoint(LeftBottom.transform.position); RBPos = cam.WorldToScreenPoint(RightBottom.transform.position); //計(jì)算網(wǎng)格寬度 gridWidth = (RTPos.x - LTPos.x) / 14; gridHeight = (LTPos.y - LBPos.y) / 14; minGridDis = gridWidth < gridHeight ? gridWidth : gridHeight; //計(jì)算落子點(diǎn)位置 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() { //檢測(cè)鼠標(biāo)輸入并確定落子狀態(tài) if (isPlaying && Input.GetMouseButtonDown(0)) { PointPos = Input.mousePosition; for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { //找到最接近鼠標(biāo)點(diǎn)擊位置的落子點(diǎn),如果空則落子 if (Dis(PointPos, chessPos[i, j]) < minGridDis / 2 && chessState[i + 1, j] == 0)/*這里chessState行要加1, 因?yàn)樯?、下和右邊各多加了一排,要空出?lái),chessPos的i行對(duì)應(yīng)chessState的i+1行*/ { //根據(jù)下棋順序確定落子顏色 chessState[i + 1, j] = chessTurn == turn.black ? 1 : -1;//同理 //落子成功,更換下棋順序 chessTurn = chessTurn == turn.black ? turn.white : turn.black; } } } //調(diào)用判斷函數(shù),確定是否有獲勝方 int re = result(); if (re == 1) { Debug.Log("黑棋勝"); winner = 1; isPlaying = false; } else if (re == -1) { Debug.Log("白棋勝"); winner = -1; isPlaying = false; } } //按下空格重新開(kāi)始游戲 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; } } //計(jì)算平面距離函數(shù) 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); } } } //根據(jù)獲勝狀態(tài),彈出相應(yīng)的勝利圖片 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); } //改寫(xiě)result函數(shù) /*解釋:C語(yǔ)言中,這樣的表達(dá)式:chessState[i]&&chessState[i+1]&&chessState[i+2]&&chessState[i+3]&&chessState[i+4],如果 * chessState[i]為False,則不管B是真是假或者是異常都不會(huì)運(yùn)行,利用這一點(diǎn),在chessState的右邊、上邊和下邊各加一行為0的數(shù)據(jù), * 這樣在判斷連續(xù)五個(gè)棋子的狀態(tài)時(shí),就不用擔(dān)心chessState數(shù)組的索引值超出范圍。例如:chessState[i+4]的索引值i+4剛好超出范圍, * 通過(guò)在原來(lái)數(shù)組chessState的上、下和右邊個(gè)添加一排為0的數(shù),這樣chessState[i+3]==0,于是就可以避免引起異常,從而簡(jiǎn)化代碼*/ int result() { int flag = 0; if (chessTurn == turn.white) { for (int i = 1; i <= 15; i++)//這里的i從1開(kāi)始 { 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開(kāi)始 { 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; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
C#定制Excel界面并實(shí)現(xiàn)與數(shù)據(jù)庫(kù)交互的方法
這篇文章主要介紹了C#定制Excel界面并實(shí)現(xiàn)與數(shù)據(jù)庫(kù)交互的方法的相關(guān)資料,需要的朋友可以參考下2015-11-11C#基于WebSocket實(shí)現(xiàn)聊天室功能
這篇文章主要為大家詳細(xì)介紹了C#基于WebSocket實(shí)現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C#中倒計(jì)時(shí)功能的優(yōu)化方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了當(dāng)C#重復(fù)使用一段代碼倒計(jì)時(shí)時(shí),如何使用普通類(lèi)和靜態(tài)方法,實(shí)現(xiàn)簡(jiǎn)單的代碼封裝性、可擴(kuò)展性、可維護(hù)性,感興趣的可以了解下2024-01-01C#窗體讀取EXCEL并存入SQL數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#窗體讀取EXCEL并存入SQL數(shù)據(jù)庫(kù)的方法,實(shí)例簡(jiǎn)述了實(shí)現(xiàn)讀取excel及寫(xiě)入SQL數(shù)據(jù)庫(kù)的原理與技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01基于C#實(shí)現(xiàn)設(shè)置桌面背景功能
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置桌面背景功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12