【Unity3D自学记录】纯GUI实现黑白棋

直接将下面的脚本随意挂在任何GameObject上即可
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        Restart();
    }
    private int rowNum = 10;
    private int colNum = 10;
    private int leftEdge = 10;
    private int topEdge = 100;
    private int size = 10;
    private int cellsep = 5;
    void Restart()
    {
        lTable.Clear();
        for (int i = 0; i < colNum; i++)
        {
            lTable.Add(new List<ChessCell>());
            for (int j = 0; j < rowNum; j++)
            {
                lTable[i].Add(new ChessCell());
                if (i == rowNum / 2 - 1 && j == colNum / 2 - 1)
                {
                    lTable[i][j].iPlayer = 1;
                }
                if (i == rowNum / 2 && j == colNum / 2 - 1)
                {
                    lTable[i][j].iPlayer = 2;
                }
                if (i == rowNum / 2 - 1 && j == colNum / 2)
                {
                    lTable[i][j].iPlayer = 2;
                }
                if (i == rowNum / 2 && j == colNum / 2)
                {
                    lTable[i][j].iPlayer = 1;
                }

            }
        }
        size = ((Screen.width > Screen.height ? (Screen.height - topEdge) : Screen.width) - (2 * leftEdge)) / rowNum - cellsep;

        lasti = -1;
        lastj = -1;

        curPlayer = 1;
    }

    private int curPlayer = 1;
    private void ChangePlayer()
    {
        curPlayer = curPlayer == 1 ? 2 : 1;
    }
    private string curPlayerName
    {
        get
        {
            return curPlayer == 1 ? "O" : "X";
        }
    }

    public bool bPC = true;
    private int lasti = -1;
    private int lastj = -1;
    List<List<ChessCell>> lTable = new List<List<ChessCell>>();
    private int player1count = 2;
    private int player2count = 2;
    void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 200, 30), "Cur Player:" + curPlayerName);
        if (GUI.Button(new Rect(220, 10, 80, 40), "restart"))
        {
            Restart();
        }

        if (lasti >= 0)
        {
            GUI.Box(new Rect(leftEdge + lasti * (size + cellsep), topEdge + lastj * (size + cellsep), size, size), "");
        }
        int most1 = 0;
        for (int ii = 0; ii < colNum; ii++)
        {
            for (int jj = 0; jj < rowNum; jj++)
            {
                if (lTable[ii][jj].iPlayer != 0)
                {
                    continue;
                }
                int icount = CheckValid(jj, ii, false);
                if (most1 < icount)
                {
                    most1 = icount;
                }
            }
        }
        if (most1 == 0)
        {
            ShowResult();
        }
        for (int i = 0; i < colNum; i++)
        {
            for (int j = 0; j < rowNum; j++)
            {
                if (lTable[i][j].iPlayer == 1)
                {
                    GUI.Button(new Rect(leftEdge + i * (size + cellsep), topEdge + j * (size + cellsep), size, size), "O");
                }
                else if (lTable[i][j].iPlayer == 2)
                {
                    GUI.Button(new Rect(leftEdge + i * (size + cellsep), topEdge + j * (size + cellsep), size, size), "X");
                }
                else
                {
                    if (GUI.Button(new Rect(leftEdge + i * (size + cellsep), topEdge + j * (size + cellsep), size, size), ""))
                    {
                        if (!bPC || curPlayer == 1)
                        {
                            CheckValid(j, i, true);
                            if (bPC && curPlayer == 2)
                            {
                                int most = 0;
                                int mii = 0;
                                int mjj = 0;
                                for (int ii = 0; ii < colNum; ii++)
                                {
                                    for (int jj = 0; jj < rowNum; jj++)
                                    {
                                        if (lTable[ii][jj].iPlayer != 0)
                                        {
                                            continue;
                                        }
                                        int icount = CheckValid(jj, ii, false);
                                        if (most < icount)
                                        {
                                            most = icount;
                                            mii = ii;
                                            mjj = jj;
                                        }
                                    }
                                }
                                if (most > 0)
                                {
                                    CheckValid(mjj, mii, true);
                                    lasti = mii;
                                    lastj = mjj;
                                }
                                else
                                {
                                    ShowResult();
                                }
                            }
                        }
                    }
                }
            }
        }

        player1count = 0;
        player2count = 0;
        for (int ii = 0; ii < colNum; ii++)
        {
            for (int jj = 0; jj < rowNum; jj++)
            {
                if (lTable[ii][jj].iPlayer == 1)
                {
                    player1count++;
                }
                else if (lTable[ii][jj].iPlayer == 2)
                {
                    player2count++;
                }
            }
        }

        GUI.Label(new Rect(10, 55, 300, 40), "P1 O:" + player1count + "    P2 X:" + player2count);
    }
    void ShowResult()
    {
        GUI.Box(new Rect(220, 55, 100, 40), "");
        GUI.Button(new Rect(220, 55, 100, 40), player1count > player2count ? "P1 WIN" : "P2 WIN");
    }
    int CheckValid(int row, int col, bool bSet)
    {
        List<ChessCell> lSetCells = new List<ChessCell>();
        List<ChessCell> lPendingCells = new List<ChessCell>();
        //test left
        for (int i = col - 1; i >= 0; i--)
        {
            if (lTable[i][row].iPlayer == 0)
            {
                break;
            }
            if (lTable[i][row].iPlayer != curPlayer)
            {
                lPendingCells.Add(lTable[i][row]);
            }
            if (lTable[i][row].iPlayer == curPlayer)
            {
                lSetCells.AddRange(lPendingCells);
                break;
            }
        }
        //test right
        lPendingCells.Clear();
        for (int i = col + 1; i < colNum; i++)
        {
            if (lTable[i][row].iPlayer == 0)
            {
                break;
            }
            if (lTable[i][row].iPlayer != curPlayer)
            {
                lPendingCells.Add(lTable[i][row]);
            }
            if (lTable[i][row].iPlayer == curPlayer)
            {
                lSetCells.AddRange(lPendingCells);
                break;
            }
        }
        //test up
        lPendingCells.Clear();
        for (int i = row - 1; i >= 0; i--)
        {
            if (lTable[col][i].iPlayer == 0)
            {
                break;
            }
            if (lTable[col][i].iPlayer != curPlayer)
            {
                lPendingCells.Add(lTable[col][i]);
            }
            if (lTable[col][i].iPlayer == curPlayer)
            {
                lSetCells.AddRange(lPendingCells);
                break;
            }
        }
        //test down
        lPendingCells.Clear();
        for (int i = row + 1; i < rowNum; i++)
        {
            if (lTable[col][i].iPlayer == 0)
            {
                break;
            }
            if (lTable[col][i].iPlayer != curPlayer)
            {
                lPendingCells.Add(lTable[col][i]);
            }
            if (lTable[col][i].iPlayer == curPlayer)
            {
                lSetCells.AddRange(lPendingCells);
                break;
            }
        }
        //test lefttop
        lPendingCells.Clear();
        for (int i = col - 1, j = row - 1; i >= 0 && j >= 0; i--, j--)
        {
            if (lTable[i][j].iPlayer == 0)
            {
                break;
            }
            if (lTable[i][j].iPlayer != curPlayer)
            {
                lPendingCells.Add(lTable[i][j]);
            }
            if (lTable[i][j].iPlayer == curPlayer)
            {
                lSetCells.AddRange(lPendingCells);
                break;
            }
        }
        //test righttop
        lPendingCells.Clear();
        for (int i = col + 1, j = row - 1; i < colNum && j >= 0; i++, j--)
        {
            if (lTable[i][j].iPlayer == 0)
            {
                break;
            }
            if (lTable[i][j].iPlayer != curPlayer)
            {
                lPendingCells.Add(lTable[i][j]);
            }
            if (lTable[i][j].iPlayer == curPlayer)
            {
                lSetCells.AddRange(lPendingCells);
                break;
            }
        }
        //test leftbottom
        lPendingCells.Clear();
        for (int i = col - 1, j = row + 1; i >= 0 && j < rowNum; i--, j++)
        {
            if (lTable[i][j].iPlayer == 0)
            {
                break;
            }
            if (lTable[i][j].iPlayer != curPlayer)
            {
                lPendingCells.Add(lTable[i][j]);
            }
            if (lTable[i][j].iPlayer == curPlayer)
            {
                lSetCells.AddRange(lPendingCells);
                break;
            }
        }
        //test rightbottom
        lPendingCells.Clear();
        for (int i = col + 1, j = row + 1; i < colNum && j < rowNum; i++, j++)
        {
            if (lTable[i][j].iPlayer == 0)
            {
                break;
            }
            if (lTable[i][j].iPlayer != curPlayer)
            {
                lPendingCells.Add(lTable[i][j]);
            }
            if (lTable[i][j].iPlayer == curPlayer)
            {
                lSetCells.AddRange(lPendingCells);
                break;
            }
        }

        if (lSetCells.Count > 0)
        {
            if (bSet)
            {
                lTable[col][row].iPlayer = curPlayer;
                foreach (ChessCell cc in lSetCells)
                {
                    cc.iPlayer = curPlayer;
                }
                ChangePlayer();
            }
        }
        return lSetCells.Count;
    }
}

class ChessCell
{
    public int iPlayer = 0;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值