本文实例为大家分享了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 c语言,Unity3D实现简易五子棋源码相关推荐

  1. [C#|Unity3D学习笔记]简易五子棋源码

    Unity3d简易五子棋源码 Unity3d部分 对C#源码进行了改写简化: using UnityEngine; using System.Collections;public class ches ...

  2. 【易语言】五子棋源码

    简介: 易语言五子棋源码 除了无ai 其他功能基本完善 网盘下载地址: http://kekewl.net/lFI6JNemjbx0 图片:

  3. 自动发邮件的程序 c语言,5分钟!教你用C语言发送邮件:附送源码+教学!

    5分钟!教你用C语言发送邮件:附送源码+教学!-1.jpg (10.71 KB, 下载次数: 0) 2018-9-3 02:21 上传 关注<一碳科技>,获取更多知识! 前言 相信年夜家都 ...

  4. c语言程序设计教程赵乘,《C语言程序设计教程》例题源码.pdf

    <C语言程序设计教程>例题源码 练习一 [例1.1]打印 "常熟理工学院"的汉语拼音. 程序源码如下: voidmain() { printf("chang ...

  5. c语言贪吃蛇游戏编程视频教程,C语言贪吃蛇游戏精典源码 - 视频教程 - VC中文网-VC-MFC编程论坛 - Powered by Discuz!...

    19f700059b660539d5dc (38.08 KB, 下载次数: 0) 2017-11-14 16:28 上传 C语言贪吃蛇游戏精典源码 19f500058fe8fcaf675a (2.98 ...

  6. 类似国外多语言wikipedia百度百科网站源码

    这个程序代码,目前在国内还是比较少的,特别是类似国外多语言wikipedia百度百科网站源码的,之前有一个做百科系统的,不知道什么原因,后来不做了,不过那个界面也比较差一点,不说这么多了,说这个程序代 ...

  7. 易语言lsp劫持_易语言网截插件修复源码

    易语言网截插件修复源码.版本 2 .支持库 shell .支持库 eNetIntercept .子程序 _按钮1_被单击 写到文件 (取特定目录 (10) + "/lsp.bat" ...

  8. 易语言linux数据库模块,易语言ADO数据库对象模块源码

    下面我们对易语言ADO数据库对象模块源码文件阐述相关使用资料和易语言ADO数据库对象模块源码文件的更新信息. 易语言ADO数据库对象模块源码 易语言ADO数据库对象模块源码 系统结构:list,取错误 ...

  9. 超级列表框排序mysql,易语言超级列表框排序源码

    易语言超级列表框排序源码.版本 2 .支持库 iext .程序集 窗口程序集1 .程序集变量 集_中文, 文本型, , "0" .程序集变量 集_中文排序, 整数型, , &quo ...

最新文章

  1. SpringBoot第二十二篇: 创建含有多module的springboot工程
  2. 在iOS端如何使用Charles用作http调试
  3. 谷歌推网页爬虫新标准,开源robots.txt解析器
  4. lucene搜索之facet查询原理和facet查询实例——TODO
  5. 学习Kotlin(一)为什么使用Kotlin
  6. Leetcode-最长回文子串(5)
  7. 闪电网络介绍以及试用 (下)
  8. matlab randomsample,randperm和randsample函数用法对比
  9. python字符串对比两项_Python之字符串比较is、==、__cmp__
  10. iosid不足以修改问题_寻找合作伙伴的技巧足以与您合作
  11. 树莓派竟出微控制器了!Raspberry Pi Pico 只需 4 美元!
  12. D-Bus 性能分析
  13. 使用VBScript和ADSI
  14. 建设医疗人工智能的“四步曲”
  15. 少儿Python编程教程
  16. 代理服务器使用全攻略(转)
  17. 人民日报喊你学数学!实力不允许?8本书带你入门
  18. linux压缩包解压
  19. 双向链表的插入及删除图解
  20. 油菜花王国(并查集)

热门文章

  1. java集合类(collection)
  2. 【名额不多了!】Cocos2d-x沙龙深圳站火热报名中
  3. 生成ResultMap
  4. 康佳电视用鸿蒙系统了吗,继鸿蒙系统之后,华为又将进军这个领域!但业内人称难有大突破...
  5. 经典网页设计:原来404错误页面可以这样设计
  6. Win10系统电脑下Airpods Pro等苹果耳机只能使用hands free无法使用stereo的解决办法_CSDN【调研后总结】
  7. 前端页面颜色选择器推荐
  8. 四种JAD生成器之比较(附生成器下载)
  9. Android 仿UC浏览器详情页评论弹框效果
  10. 自定义Flutter资源变体(variant)