unity3d 入门——实现井字棋

代码部分是用c#实现的,作为有过c,c++基础的程序员,c#上手的感觉还可以,也没有到什么高级的特性。
作为3d游戏编程课程的第一个作业,还有师兄的博客的参考,整体难度并不大。
实现中主要用到了
行为对象属性中的:
OnGui()//渲染处理GUI事件,没一帧调用一次
GUI标签
GUI.Label() //在屏幕上创建一个文本或者纹理标签。
GUI.Button()//创建一个单次按下按钮。用户点击按钮事件立即触发。当用户点击按钮的时候返回true。
以下是整体实现的代码。如果有bug欢迎指出哟~

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mybutton : MonoBehaviour {
    //显示棋盘内容
    private string[] info = new string [9];
    //记录棋盘的状态,state 0代表没有按下,1代表X按下, 2代表O按下
    private int[] state = new int[9];
    //1代表X的回合,0代表O的回合
    private int turn =0;
    private string result="";
    private int i;
    public Texture buttonTexture;
    private int temp;
    private int temp1;
    void Start () {
        init();
    }
    void init()
    {
        for (i = 0; i < 9; i++)
        {
            info[i] = "";

            state[i] = 0;
        }
        turn = 0;
    }
    // Update is called once per frame
    void Update () {

    }
     void OnGUI()
    {
        int x, y, num;
        GUI.Label(new Rect(40, 320, 50, 50), result);
        if(GUI.Button(new Rect(200,320, 100, 40), "reStart"))
        {
            init();
            result = "";
        }
        GUI.color = Color.yellow;
        GUI.backgroundColor = Color.red;
        //游戏结束后只能操作reStart健
        if (isend() )
        {
             x = 20;
             y = 20;
             num = 0;
            //保留游戏结果的情况
            for (temp = 0; temp < 3; temp++)
            {
                for (temp1 = 0; temp1 < 3; temp1++)
                {
                    GUI.Button(new Rect(x, y, 80, 80), info[num]);

                    x += 80;
                    num++;
                }
                y += 80;
                x = 20;
            }

            if (GUI.Button(new Rect(200, 320, 100, 40), "reStart"))
            {
                init();
                result = "";
            }
            return;
        }

        //检测玩家操作
         x = 20;
         y = 20;
        num = 0;
        for(temp=0;temp<3;temp++)
        {
            for(temp1=0;temp1<3;temp1++)
            {
                if(GUI.Button(new Rect(x, y, 80, 80), info[num]))
                {
                    if(state[num]==0&&turn==0)
                    {
                        info[num] = "O";
                        state[num] = 2;

                    } else if(state[num]==0&&turn==1)
                    {
                        info[num] = "X";
                        state[num] = 1;
                    }
                    if(turn==0)
                    {
                        turn = 1;
                    } else if(turn==1)
                    {
                        turn = 0;
                    }
                }
                x += 80;
                num++;
            }
            y += 80;
            x = 20;
        }

    }
    bool isend()
    {
        //检查横排
        int  j = 0;
        for (j = 0; j < 7; j = j + 3)
        {
            if (state[j] == state[j + 1] && state[j + 1] == state[j + 2])
            {
                if (state[j] == 1)
                {
                    result = "X wins by row";
                    return true;
                }
                else if (state[j] == 2)
                {
                    result = "O wins by row";
                    return true;
                }
            }
        }
        //检查竖排
        for(j=0;j<3;j++)
        {
            if(state[j]==state[j+3]&&state[j+3]==state[j+6])
            {
                if (state[j] == 1)
                {
                    result = "X wins by column";
                    //print("x win");
                    return true;
                }
                else if (state[j] == 2)
                {
                    result = "O wins by column";
                    //print("O win");
                    return true;
                }
            }
        }
        //检查对角线

           if (state[0] == state[4] && state[ 4] == state[8])
            {
                if (state[0] == 1)
                {
                    result = "X wins by diagonal";
                     return true;
            }
                else if (state[0] == 2)
                {
                    result = "O wins by diagonal";
                    return true;
            }
            }
        if (state[2] == state[4] && state[4] == state[6])
        {
            if (state[2] == 1)
            {
                result = "X wins by diagonal";
                return true;
            }
            else if (state[2] == 2)
            {
                result = "O wins by diagonal";
                return true;
            }
        }
        //检查是否平局
        int flag = 0;
        for(j=0;j<9;j++)
        {
            if(state[j]==0)
            {
                flag = 1;
                break;
            }

        }
        if(flag==0)
        {
            result = "a draw";
            return true;
        }
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值