欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity3D实现简易五子棋源码

程序员文章站 2022-06-21 08:46:05
本文实例为大家分享了unity3d简易五子棋源码,供大家参考,具体内容如下 unity3d部分 对c#源码进行了改写简化: using unityengine; usi...

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

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