Unity3D实现简易五子棋源码

所属分类: 软件编程 / C#教程 阅读数: 127
收藏 0 赞 0 分享

本文实例为大家分享了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;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多