Unity3D實現(xiàn)簡易五子棋源碼
本文實例為大家分享了Unity3d簡易五子棋源碼,供大家參考,具體內(nèi)容如下
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; //棋盤網(wǎng)格寬度
float gridHeight = 1; //棋盤網(wǎng)格高度
float minGridDis; //網(wǎng)格寬和高中較小的一個
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ù),
也就相當于在棋盤的上、下和右邊各填加一排隱形的棋道。原因后面解釋*/
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);
//計算網(wǎng)格寬度
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()
{
//檢測鼠標輸入并確定落子狀態(tài)
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行*/
{
//根據(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;
}
}
//計算平面距離函數(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),彈出相應的勝利圖片
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語言中,這樣的表達式:chessState[i]&&chessState[i+1]&&chessState[i+2]&&chessState[i+3]&&chessState[i+4],如果
* chessState[i]為False,則不管B是真是假或者是異常都不會運行,利用這一點,在chessState的右邊、上邊和下邊各加一行為0的數(shù)據(jù),
* 這樣在判斷連續(xù)五個棋子的狀態(tài)時,就不用擔心chessState數(shù)組的索引值超出范圍。例如:chessState[i+4]的索引值i+4剛好超出范圍,
* 通過在原來數(shù)組chessState的上、下和右邊個添加一排為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)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
欄 目:C#教程
本文地址:http://m.jygsgssxh.com/a1/C_jiaocheng/4648.html
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠程關(guān)閉計算機或重啟計算機的方法
- 01-10C#自定義簽名章實現(xiàn)方法
- 01-10C#文件斷點續(xù)傳實現(xiàn)方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?


