Unity3D實(shí)現(xiàn)簡易五子棋源碼
本文實(shí)例為大家分享了Unity3d簡易五子棋源碼,供大家參考,具體內(nèi)容如下
對C#源碼進(jì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; //棋盤網(wǎng)格寬度
float gridHeight = 1; //棋盤網(wǎng)格高度
float minGridDis; //網(wǎng)格寬和高中較小的一個(gè)
Vector2[,] chessPos; //存儲棋盤上所有可以落子的位置
int[,] chessState; //存儲棋盤位置上的落子狀態(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; //是否處于對弈狀態(tài)
void Start()
{
chessPos = new Vector2[15, 15];
chessState = new int[17, 16];/*原來定義是new int[15, 15],這里將原來數(shù)組chessState上、下和右邊各加一排數(shù)據(jù),
也就相當(dāng)于在棋盤的上、下和右邊各填加一排隱形的棋道。原因后面解釋*/
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()
{
//檢測鼠標(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)樯?、下和右邊各多加了一排,要空出來,chessPos的i行對應(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;
}
}
//按下空格重新開始游戲
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);
}
//改寫result函數(shù)
/*解釋:C語言中,這樣的表達(dá)式:chessState[i]&&chessState[i+1]&&chessState[i+2]&&chessState[i+3]&&chessState[i+4],如果
* chessState[i]為False,則不管B是真是假或者是異常都不會運(yùn)行,利用這一點(diǎn),在chessState的右邊、上邊和下邊各加一行為0的數(shù)據(jù),
* 這樣在判斷連續(xù)五個(gè)棋子的狀態(tài)時(shí),就不用擔(dān)心chessState數(shù)組的索引值超出范圍。例如:chessState[i+4]的索引值i+4剛好超出范圍,
* 通過在原來數(shù)組chessState的上、下和右邊個(gè)添加一排為0的數(shù),這樣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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#枚舉數(shù)值與名稱的轉(zhuǎn)換實(shí)例分享
在應(yīng)用枚舉的時(shí)候,時(shí)常需要將枚舉和數(shù)值相互轉(zhuǎn)換的情況。有時(shí)候還需要轉(zhuǎn)換成相應(yīng)的中文。下面介紹一種方法2013-08-08
C#結(jié)合JS實(shí)現(xiàn)HtmlTable動態(tài)添加行并保存到數(shù)據(jù)庫的流程步驟
在 Web 應(yīng)用項(xiàng)目中,實(shí)現(xiàn)一對多錄入的數(shù)據(jù)管理功能是一項(xiàng)常見的應(yīng)用,因此可以實(shí)現(xiàn)一個(gè)相對輕量化的設(shè)計(jì)實(shí)現(xiàn)表格的錄入,為保證功能的可用性、界面友好性,本文給大家介紹了C#結(jié)合JS實(shí)現(xiàn)HtmlTable動態(tài)添加行并保存到數(shù)據(jù)庫,需要的朋友可以參考下2024-12-12
C# 使用CancellationTokenSource取消多線程
有時(shí)間我們在使用多線程的時(shí)候,需要取消線程的執(zhí)行,可以使用CancellationTokenSource來取消對Task開辟多線程的取消,感興趣的可以了解一下2021-08-08
C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法,結(jié)合實(shí)例形式總結(jié)分析了Json轉(zhuǎn)換DataTable,以及DataTable導(dǎo)出Excel相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
C#實(shí)現(xiàn)延時(shí)并自動關(guān)閉MessageBox的方法
這篇文章主要介紹了C#實(shí)現(xiàn)延時(shí)并自動關(guān)閉MessageBox的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)日期操作類DateTime的方法示例
C#中日期和時(shí)間操作主要通過System.DateTime類實(shí)現(xiàn),提供了創(chuàng)建、格式化、比較和計(jì)算等功能,下面就來具體介紹一下,感興趣的可以了解一下2025-03-03
C#對圖片進(jìn)行馬賽克處理可控制模糊程度的實(shí)現(xiàn)代碼
本文通過實(shí)例代碼給大家介紹了C#對圖片進(jìn)行馬賽克處理可控制模糊程度的實(shí)現(xiàn)方法,代碼超簡單,具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05

