Unity之我寫的象棋

最近,我用了不少時間寫了一個象棋遊戲,旨在鍛鍊我的編寫思維和能力,還有就是慢慢學習那80%的知識(20%的是基礎知識);這個象棋遊戲大體已經完成了,但還是有一些是我現在無法做到的,比如遊戲裏面的電腦,再比如怎樣編寫一個更好更健全的網絡同步體系,這些都是我現在無法做到的;編寫此博客的目的在於把我最近寫這個項目的一些思路分享給大家,讓我以後也可以看看,因爲東西多了,腦子也記不住,需要寫下來才能方便查找,好了廢話說完了,該進入正題:

我的項目資源連接:

鏈接:https://pan.baidu.com/s/1xJOvEl6nhlMGRgnTEdaawg 

提取碼:knre 

在項目資源裏,有一部分是關於象棋聯機的,由於聯機我還沒有學的太明白,在這裏就不做說明了,我也會單獨寫一個關於遊戲聯機的博客,以此來記錄學習知識,在以後方便查閱。

一、思路、方法

我的設計思路是:定義了兩個枚舉類型:第一個是關於棋的顏色的枚舉類型,第二個是關於棋的名字的枚舉類型,有了這兩個枚舉類型,我們就能很好的區分每個棋,這在後面的條件判斷中也能很方便的調用,

//棋名
public enum ChessName
{
    Shuai,//帥
    Shi,//士
    Xiang,//象
    Ma,//馬
    Che,//車
    Pao,//炮
    Bing//兵
}
//隊伍種類
public enum Team
{
    Red,//紅隊
    Black//黑隊
}

分好了棋子,接着就是棋盤,在這裏,我定義了一個GameObject類型的數組,其大小與棋盤一致,棋盤搞好了,就可以在上面擺棋子了,擺棋子當然需要座標,所以你得先把32顆棋子的X、Y座標給記錄下來,這個不難,時間決定,我先把棋子座標用數組記錄下來,然後我們開始設計棋子類,我的想法是這個棋子類包含所有棋子的形態,這樣我們就可以一個對象創建多個實例,而不是多個對象創建多個實例;所以這個棋子類要包含所有棋子的走法,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
//棋名
public enum ChessName
{
    Shuai,//帥
    Shi,//士
    Xiang,//象
    Ma,//馬
    Che,//車
    Pao,//炮
    Bing//兵
}

public class Chess : MonoBehaviour
{
    //圖片數組
    public Sprite[] sprites;

    //子物體Image上的Image組件
    private Image selfImage;

    //該棋子所屬隊伍
    public Team selfTeam { set; get; }

    //該棋子的棋名
    public ChessName selfChessName { set; get; }

    //該場景的第一個遊戲邏輯組件
    private Scene02GameController_1 GameController_1;

    //在棋盤上的座標的x軸
    public int x { set; get; }
    //在棋盤上的座標的y軸
    public int y { set; get; }

    //遊戲對象,作用:顯示棋子能走的地方
    public GameObject canDownChessPoint;

    //盛放canUpChessPoint的容器
    [HideInInspector]
    public List<GameObject> canDownChessPoints = new List<GameObject>();

    //控制canUpChessPoints的操作
    [HideInInspector]
    public bool isClick = false;
    //用於區分楚河上方和下方的棋子,在棋子創建之初,初始化
    public int distinguish { set; get; }

    void Start () {
        //獲取子物體Image上的Image組件
        selfImage = transform.Find("Image").GetComponent<Image>();
        //如果棋的selfTeam屬性爲Red,則是紅棋
        if (selfTeam == Team.Red)
        {
            //根據selfChessName,即棋的名字選擇並切換圖片
            switch (selfChessName)
            {
                case ChessName.Shuai:
                    selfImage.sprite = sprites[7];
                    break;
                case ChessName.Shi:
                    selfImage.sprite = sprites[8];
                    break;
                case ChessName.Xiang:
                    selfImage.sprite = sprites[9];
                    break;
                case ChessName.Ma:
                    selfImage.sprite = sprites[10];
                    break;
                case ChessName.Che:
                    selfImage.sprite = sprites[11];
                    break;
                case ChessName.Pao:
                    selfImage.sprite = sprites[12];
                    break;
                case ChessName.Bing:
                    selfImage.sprite = sprites[13];
                    break;
            }
        }
        //否則,爲黑棋
        else
        {
            //根據selfChessName,即棋的名字選擇並切換圖片
            switch (selfChessName)
            {
                case ChessName.Shuai:
                    selfImage.sprite = sprites[0];
                    break;
                case ChessName.Shi:
                    selfImage.sprite = sprites[1];
                    break;
                case ChessName.Xiang:
                    selfImage.sprite = sprites[2];
                    break;
                case ChessName.Ma:
                    selfImage.sprite = sprites[3];
                    break;
                case ChessName.Che:
                    selfImage.sprite = sprites[4];
                    break;
                case ChessName.Pao:
                    selfImage.sprite = sprites[5];
                    break;
                case ChessName.Bing:
                    selfImage.sprite = sprites[6];
                    break;
            }
        }
        //獲取該場景的第一個遊戲邏輯組件
        GameController_1 = GameObject.FindWithTag("GameController").GetComponent<Scene02GameController_1>();
        //如果開始時y<5
        if (y < 5)
        {
            distinguish = 1;
        }
        else
        {
            distinguish = 2;
        }
    }

    private void Update()
    {
        if (isClick)
        {
            //如果鼠標左鍵按下
            if (Input.GetMouseButtonDown(0))
            {
                //如果canUpChessPoints的長度不爲零
                if (canDownChessPoints.Count != 0)
                {
                    foreach (GameObject game in canDownChessPoints)
                    {
                        //刪除
                        Destroy(game);
                    }
                    //清空canUpChessPoints裏所有的元素
                    canDownChessPoints.Clear();
                }
                //把isClick設置爲flase
                isClick = false;
            }
        }
    }

    //當指針移入該控件時調用
    public void OnPointerEnter()
    {
        //如果遊戲沒有結束了
        if (!GameController_1.isGameOver && !GameController_1.isPause)
        {
            //如果該棋子是我方棋子
            if (selfTeam == GameController_1.selfTeam)
            {
                //則加深棋子整體的顏色
                selfImage.color = new Color((float)186 / 255, (float)186 / 255, (float)186 / 255);

            }
        }
    }
    //當指針移出該控件時調用
    public void OnPointerExit()
    {
        //設置棋子整體的顏色
        selfImage.color = new Color(1, 1, 1);
    }
    //當指針在該控件上按下時調用
    public void OnPointerDown()
    {
        //如果遊戲沒有結束了
        if (!GameController_1.isGameOver && !GameController_1.isPause)
        {
            //如果你選擇的顏色與棋子的顏色一致
            if (selfTeam == GameController_1.selfTeam)
            {
                if (!isClick)
                {
                    JudgeWalking();
                    //延遲執行本屬於OnPointerDownDelay方法裏代碼
                    Invoke("OnPointerDownDelay", 0.1f);
                }
            }
        }
    }
    //判斷行走
    public void JudgeWalking()
    {
        //如果他是帥
        if (selfChessName == ChessName.Shuai)
        {
            //向右摸索前進一格
            if (x + 1 <= 5)
            {
                ShuaiShiBingGo(x + 1, y);
            }
            //向左摸索前進一格
            if (x - 1 >= 3)
            {
                ShuaiShiBingGo(x - 1, y);
            }
            //如果是楚河下方的帥
            if (y > 5)
            {
                //向下摸索前進一格
                if (y + 1 < 10)
                {
                    ShuaiShiBingGo(x, y + 1);
                }
                //向上摸索前進一格
                if (y - 1 >= 7)
                {
                    ShuaiShiBingGo(x, y - 1);
                }
            }
            //如果是楚河上方的帥
            else
            {
                //向下摸索前進一格
                if (y + 1 <= 2)
                {
                    ShuaiShiBingGo(x, y + 1);
                }
                //向上摸索前進一格
                if (y - 1 >= 0)
                {
                    ShuaiShiBingGo(x, y - 1);
                }
            }
        }
        //如果他是士
        else if (selfChessName == ChessName.Shi)
        {
            //如果是楚河下方的的士
            if (y > 5)
            {
                //向右下方摸索前進
                if (x + 1 <= 5 && y + 1 < 10)
                {
                    ShuaiShiBingGo(x + 1, y + 1);
                }
                //向右上方摸索前進
                if (x + 1 <= 5 && y - 1 >= 7)
                {
                    ShuaiShiBingGo(x + 1, y - 1);
                }
                //向左下方摸索前進
                if (x - 1 >= 3 && y + 1 < 10)
                {
                    ShuaiShiBingGo(x - 1, y + 1);
                }
                //向右上方摸索前進
                if (x - 1 >= 3 && y - 1 >= 7)
                {
                    ShuaiShiBingGo(x - 1, y - 1);
                }
            }
            //否則,士楚河上方的士
            else
            {
                //向右下方摸索前進
                if (x + 1 <= 5 && y + 1 <= 2)
                {
                    ShuaiShiBingGo(x + 1, y + 1);
                }
                //向右上方摸索前進
                if (x + 1 <= 5 && y - 1 >= 0)
                {
                    ShuaiShiBingGo(x + 1, y - 1);
                }
                //向左下方摸索前進
                if (x - 1 >= 3 && y + 1 <= 2)
                {
                    ShuaiShiBingGo(x - 1, y + 1);
                }
                //向右上方摸索前進
                if (x - 1 >= 3 && y - 1 >= 0)
                {
                    ShuaiShiBingGo(x - 1, y - 1);
                }
            }
        }
        //如果他是象
        else if (selfChessName == ChessName.Xiang)
        {
            //如果distinguish爲一
            if (distinguish == 1)
            {
                //向右下方走
                if (x + 2 < 9 && y + 2 < 5)
                {
                    //表示是否能走
                    bool isGo = false;
                    //判斷是否有棋子阻擋
                    MaXiangGo(x + 1, y + 1, ref isGo);
                    if (isGo)
                    {
                        MaXiangGo(x + 2, y + 2, ref isGo);
                    }
                }
                //向右上方走
                if (x + 2 < 9 && y - 2 >= 0)
                {
                    bool isGo = false;
                    MaXiangGo(x + 1, y - 1, ref isGo);
                    if (isGo)
                    {
                        MaXiangGo(x + 2, y - 2, ref isGo);
                    }
                }
                //向左下方走
                if (x - 2 >= 0 && y + 2 < 5)
                {
                    bool isGo = false;
                    MaXiangGo(x - 1, y + 1, ref isGo);
                    if (isGo)
                    {
                        MaXiangGo(x - 2, y + 2, ref isGo);
                    }
                }
                //向右上方走
                if (x - 2 >= 0 && y - 2 >= 0)
                {
                    bool isGo = false;
                    MaXiangGo(x - 1, y - 1, ref isGo);
                    if (isGo)
                    {
                        MaXiangGo(x - 2, y - 2, ref isGo);
                    }
                }
            }
            //否則:
            else
            {
                //向右下方走
                if (x + 2 < 9 && y + 2 < 10)
                {
                    bool isGo = false;
                    MaXiangGo(x + 1, y + 1, ref isGo);
                    if (isGo)
                    {
                        MaXiangGo(x + 2, y + 2, ref isGo);
                    }
                }
                //向右上方走
                if (x + 2 < 9 && y - 2 >= 5)
                {
                    bool isGo = false;
                    MaXiangGo(x + 1, y - 1, ref isGo);
                    if (isGo)
                    {
                        MaXiangGo(x + 2, y - 2, ref isGo);
                    }
                }
                //向左下方走
                if (x - 2 >= 0 && y + 2 < 10)
                {
                    bool isGo = false;
                    MaXiangGo(x - 1, y + 1, ref isGo);
                    if (isGo)
                    {
                        MaXiangGo(x - 2, y + 2, ref isGo);
                    }
                }
                //向右上方走
                if (x - 2 >= 0 && y - 2 >= 5)
                {
                    bool isGo = false;
                    MaXiangGo(x - 1, y - 1, ref isGo);
                    if (isGo)
                    {
                        MaXiangGo(x - 2, y - 2, ref isGo);
                    }
                }
            }
        }
        //如果他是馬
        else if (selfChessName == ChessName.Ma)
        {
            //向右摸索
            if (x + 2 < 9)
            {
                //isGo是表示該方向的前一格有沒有棋子
                //如果有就爲false,表示不能通過
                //如果沒有就爲true,表示可以通過
                bool isGo = false;
                //開始檢查
                MaXiangGo(x + 1, y, ref isGo);
                if (isGo)//結果判斷
                {
                    //馬走的兩個路線
                    if (y + 1 < 10)
                    {
                        MaXiangGo(x + 2, y + 1, ref isGo);
                    }
                    if (y - 1 >= 0)
                    {
                        MaXiangGo(x + 2, y - 1, ref isGo);
                    }
                }
            }
            //向左摸索
            if (x - 2 >= 0)
            {
                //isGo是表示該方向的前一格有沒有棋子
                //如果有就爲false,表示不能通過
                //如果沒有就爲true,表示可以通過
                bool isGo = false;
                //開始檢查
                MaXiangGo(x - 1, y, ref isGo);
                if (isGo)//結果判斷
                {
                    //馬走的兩個路線
                    if (y + 1 < 10)
                    {
                        MaXiangGo(x - 2, y + 1, ref isGo);
                    }
                    if (y - 1 >= 0)
                    {
                        MaXiangGo(x - 2, y - 1, ref isGo);
                    }
                }
            }
            //向下摸索
            if (y + 2 < 10)
            {
                bool isGo = false;

                MaXiangGo(x, y + 1, ref isGo);
                if (isGo)
                {
                    if (x + 1 < 9)
                    {
                        MaXiangGo(x + 1, y + 2, ref isGo);
                    }
                    if (x - 1 >= 0)
                    {
                        MaXiangGo(x - 1, y + 2, ref isGo);
                    }
                }
            }
            //向上摸索
            if (y - 2 >= 0)
            {
                bool isGo = false;

                MaXiangGo(x, y - 1, ref isGo);
                if (isGo)
                {
                    if (x + 1 < 9)
                    {
                        MaXiangGo(x + 1, y - 2, ref isGo);
                    }
                    if (x - 1 >= 0)
                    {
                        MaXiangGo(x - 1, y - 2, ref isGo);
                    }
                }
            }
        }
        //如果他是車
        else if (selfChessName == ChessName.Che)
        {
            //向右摸索前進
            for (int i = 1; x + i < 9; i++)
            {
                if (CheGo(x + i, y))
                {
                    break;
                }
            }
            //向左摸索前進
            for (int i = 1; x - i >= 0; i++)
            {
                if (CheGo(x - i, y))
                {
                    break;
                }
            }
            //向下摸索前進
            for (int i = 1; y + i < 10; i++)
            {
                if (CheGo(x, y + i))
                {
                    break;
                }
            }
            //向上摸索前進
            for (int i = 1; y - i >= 0; i++)
            {
                if (CheGo(x, y - i))
                {
                    break;
                }
            }
        }
        //如果他是炮
        else if (selfChessName == ChessName.Pao)
        {
            //向右摸索前進
            {
                bool fire = false;//是否開炮
                for (int i = 1; x + i < 9; i++)
                {
                    if (PaoGo(x + i, y, ref fire))
                    {
                        break;
                    }
                }
            }

            //向左摸索前進
            {
                bool fire = false;//是否開炮
                for (int i = 1; x - i >= 0; i++)
                {
                    if (PaoGo(x - i, y, ref fire))
                    {
                        break;
                    }
                }
            }

            //向下摸索前進
            {
                bool fire = false;//是否開炮
                for (int i = 1; y + i < 10; i++)
                {
                    if (PaoGo(x, y + i, ref fire))
                    {
                        break;
                    }
                }
            }

            //向上摸索前進
            {
                bool fire = false;//是否開炮
                for (int i = 1; y - i >= 0; i++)
                {
                    if (PaoGo(x, y - i, ref fire))
                    {
                        break;
                    }
                }
            }
        }
        //如果他是兵
        else if (selfChessName == ChessName.Bing)
        {
            //控制兵是否能左右行走
            bool around = false;
            //如果這是楚河上方的兵
            if (distinguish == 1)
            {
                //向前摸索行走
                if (y + 1 < 10)
                {
                    ShuaiShiBingGo(x, y + 1);
                }
                //如果這個兵過了楚河
                if (y >= 5)
                {
                    around = true;
                }
            }
            //否則是楚河下方的兵
            else
            {
                //向前摸索行走
                if (y - 1 >= 0)
                {
                    ShuaiShiBingGo(x, y - 1);
                }
                //如果這個兵過了楚河
                if (y <= 4)
                {
                    around = true;
                }
            }
            //這個兵是否能左右行走
            if (around)
            {
                //向右摸索前進一格
                if (x + 1 < 9)
                {
                    ShuaiShiBingGo(x + 1, y);
                }
                //向左摸索前進一格
                if (x - 1 >= 0)
                {
                    ShuaiShiBingGo(x - 1, y);
                }
            }
        }
    }

    //延遲執行本屬於OnPointerDownDelay方法裏代碼
    public void OnPointerDownDelay()
    {
        //設置isClick爲true
        isClick = true;
    }

    //帥、士、兵的走法
    private void ShuaiShiBingGo(int a, int b)
    {
        //如果棋盤上的(b,a)座標爲空
        if (GameController_1.checkerboard[b, a] != null)
        {
            //如果該座標上有棋子,且爲敵方棋子
            if (GameController_1.checkerboard[b, a].GetComponent<Chess>().selfTeam != selfTeam)
            {
                //則該座標可以作爲下棋的點
                GameObject game = Instantiate(canDownChessPoint,
                    transform.position + new Vector3((a - x) * 0.5f, (y - b) * 0.5f, 0), Quaternion.identity);
                //設置canUpChessPoint實例對象的x和y
                game.GetComponent<CanDownChessPoint>().x = a;
                game.GetComponent<CanDownChessPoint>().y = b;
                //將自身傳給game
                game.GetComponent<CanDownChessPoint>().chess = transform.gameObject;
                //加入到該容器中
                canDownChessPoints.Add(game);
            }
        }
        //否則
        else
        {
            //該座標可以作爲下棋的點
            GameObject game = Instantiate(canDownChessPoint,
                transform.position + new Vector3((a - x) * 0.5f, (y - b) * 0.5f, 0), Quaternion.identity);
            //設置canUpChessPoint實例對象的x和y
            game.GetComponent<CanDownChessPoint>().x = a;
            game.GetComponent<CanDownChessPoint>().y = b;
            //將自身傳給game的CanUpChessPoint的Chess
            game.GetComponent<CanDownChessPoint>().chess = transform.gameObject;
            //加入到該容器中
            canDownChessPoints.Add(game);
        }
    }

    //車的走法
    private bool CheGo(int a, int b)
    {
        //如果棋盤上的(b,a)座標爲空
        if (GameController_1.checkerboard[b, a] != null)
        {
            //如果該座標上有棋子,且爲敵方棋子
            if (GameController_1.checkerboard[b, a].GetComponent<Chess>().selfTeam != selfTeam)
            {
                //則該座標可以作爲下棋的點
                GameObject game = Instantiate(canDownChessPoint,
                    transform.position + new Vector3((a - x) * 0.5f, (y - b) * 0.5f, 0), Quaternion.identity);
                //設置canUpChessPoint實例對象的x和y
                game.GetComponent<CanDownChessPoint>().x = a;
                game.GetComponent<CanDownChessPoint>().y = b;
                //將自身傳給game
                game.GetComponent<CanDownChessPoint>().chess = transform.gameObject;
                //加入到該容器中
                canDownChessPoints.Add(game);
            }
            //運行到此行,說明前方有我方棋子,因被我方棋子擋住,就不能前進
            //返回true,讓上面執行break退出for循環
            return true;
        }
        //否則
        else
        {
            //該座標可以作爲下棋的點
            GameObject game = Instantiate(canDownChessPoint,
                transform.position + new Vector3((a - x) * 0.5f, (y - b) * 0.5f, 0), Quaternion.identity);
            //設置canUpChessPoint實例對象的x和y
            game.GetComponent<CanDownChessPoint>().x = a;
            game.GetComponent<CanDownChessPoint>().y = b;
            //將自身傳給game
            game.GetComponent<CanDownChessPoint>().chess = transform.gameObject;
            //加入到該容器中
            canDownChessPoints.Add(game);
            //運行到此行,說明該點無棋子,可以行走,可以繼續向前摸索
            //返回false,讓上面不執行break繼續for循環
            return false;
        }
    }

    //炮的走法
    private bool PaoGo(int a, int b, ref bool fire)
    {
        //如果棋盤上的(b,a)座標爲空
        if (GameController_1.checkerboard[b, a] != null)
        {
            //如果fire爲true,則表示進入開炮狀態
            if (fire)
            {
                //如果該座標上有棋子,且爲敵方棋子
                if (GameController_1.checkerboard[b, a].GetComponent<Chess>().selfTeam != selfTeam)
                {
                    //則該座標可以作爲下棋的點
                    GameObject game = Instantiate(canDownChessPoint,
                        transform.position + new Vector3((a - x) * 0.5f, (y - b) * 0.5f, 0), Quaternion.identity);
                    //設置canUpChessPoint實例對象的x和y
                    game.GetComponent<CanDownChessPoint>().x = a;
                    game.GetComponent<CanDownChessPoint>().y = b;
                    //將自身傳給game
                    game.GetComponent<CanDownChessPoint>().chess = transform.gameObject;
                    //加入到該容器中
                    canDownChessPoints.Add(game);
                }
                //運行到此行,說明前方有我方棋子,因被我方棋子擋住,就不能前進
                //返回true,讓上面執行break退出for循環
                return true;
            }
            //否則
            else
            {
                //fire設爲true,表示可以打炮
                fire = true;
                //運行到此行,說明剛剛開始隔棋打棋,雖然該點有棋子,但是要作爲支點
                //返回false,讓上面不執行break,繼續for循環
                return false;
            }
        }
        //否則
        else
        {
            //如果fire爲true,則表示進入開炮狀態
            if (fire)
            {
                //運行到此行,說明已經隔棋打棋,不過該點沒有棋子
                //返回false,讓上面不執行break,繼續for循環
                return false;
            }
            //否則
            else
            {
                //該座標可以作爲下棋的點
                GameObject game = Instantiate(canDownChessPoint,
                    transform.position + new Vector3((a - x) * 0.5f, (y - b) * 0.5f, 0), Quaternion.identity);
                //設置canUpChessPoint實例對象的x和y
                game.GetComponent<CanDownChessPoint>().x = a;
                game.GetComponent<CanDownChessPoint>().y = b;
                //將自身傳給game
                game.GetComponent<CanDownChessPoint>().chess = transform.gameObject;
                //加入到該容器中
                canDownChessPoints.Add(game);
                //運行到此行,說明該點無棋子,可以行走,可以繼續向前摸索
                //返回false,讓上面不執行break,繼續for循環
                return false;
            }
        }
    }

    //馬、象的走法
    private void MaXiangGo(int a, int b, ref bool isGo)
    {
        //如果棋盤上的(b,a)座標爲空
        if (GameController_1.checkerboard[b, a] != null)
        {
            //能否通行
            if (isGo)
            {
                //如果該座標上有棋子,且爲敵方棋子
                if (GameController_1.checkerboard[b, a].GetComponent<Chess>().selfTeam != selfTeam)
                {
                    //則該座標可以作爲下棋的點
                    GameObject game = Instantiate(canDownChessPoint,
                        transform.position + new Vector3((a - x) * 0.5f, (y - b) * 0.5f, 0), Quaternion.identity);
                    //設置canUpChessPoint實例對象的x和y
                    game.GetComponent<CanDownChessPoint>().x = a;
                    game.GetComponent<CanDownChessPoint>().y = b;
                    //將自身傳給game
                    game.GetComponent<CanDownChessPoint>().chess = transform.gameObject;
                    //加入到該容器中
                    canDownChessPoints.Add(game);
                }
            }
        }
        //否則
        else
        {
            //能否通行
            if (isGo)
            {
                //該座標可以作爲下棋的點
                GameObject game = Instantiate(canDownChessPoint,
                    transform.position + new Vector3((a - x) * 0.5f, (y - b) * 0.5f, 0), Quaternion.identity);
                //設置canUpChessPoint實例對象的x和y
                game.GetComponent<CanDownChessPoint>().x = a;
                game.GetComponent<CanDownChessPoint>().y = b;
                //將自身傳給game
                game.GetComponent<CanDownChessPoint>().chess = transform.gameObject;
                //加入到該容器中
                canDownChessPoints.Add(game);
            }
            else
            {
                //設置爲true,就是能通行
                isGo = true;
            }
        }
    }
}

以上就是我設計的棋子類,除了棋子類,我還設計了一個棋子可行走區域的類,他在遊戲是用一塊半透明的黑布來作爲本身,他的作用:是表示可行走區域,玩家點擊之後,棋子可以移動到點擊點;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class CanDownChessPoint : MonoBehaviour
{
    //綁定的棋子chess
    public GameObject chess { get; set; }

    //該場景的第一個遊戲邏輯組件
    private Scene02GameController_1 GameController_1;

    //在棋盤上的座標的x軸
    public int x { set; get; }
    //在棋盤上的座標的y軸
    public int y { set; get; }
    //黑棋贏的Text遊戲對象
    private GameObject blackWinText;
    //紅棋贏的Text遊戲對象
    private GameObject redWinText;

    void Start () {
        //獲取該場景的第一個遊戲邏輯組件
        GameController_1 = GameObject.Find("GameController").GetComponent<Scene02GameController_1>();
        //獲取黑棋贏的Text遊戲對象
        blackWinText = GameObject.Find("Canvas").transform.Find("BlackWinText").gameObject;
        //獲取紅棋贏的Text遊戲對象
        redWinText = GameObject.Find("Canvas").transform.Find("RedWinText").gameObject;
        //在這裏獲取移動的棋子
        GameController_1.moveChess = chess;
    }

    //當指針在該控件上按下
    public void OnPointerDown()
    {
        //獲取chess原來的座標x、y
        int temporaryX = chess.GetComponent<Chess>().x;
        int temporaryY = chess.GetComponent<Chess>().y;
        //獲取要被吃的棋子,如果沒有就爲null
        GameObject game = null;
        //棋盤數組上的原來位置設爲空
        GameController_1.checkerboard[chess.GetComponent<Chess>().y, chess.GetComponent<Chess>().x] = null;
        //如果棋盤上[y,x]的位置有他方棋子,則吃掉它
        if (GameController_1.checkerboard[y, x] != null)
        {
            //獲取要被吃的棋子
            game = GameController_1.checkerboard[y, x];
            //隱藏要被吃的棋子
            game.SetActive(false);
        }
        //在棋盤上[y,x]的位置設置該棋子對象
        GameController_1.checkerboard[y, x] = chess;
        //更新棋子對象的x和y
        chess.GetComponent<Chess>().x = x;
        chess.GetComponent<Chess>().y = y;
        //執行絕殺方法
        GameController_1.Lonely();
        //如果絕殺isLonely爲true,則棋盤上有棋將軍
        if (GameController_1.isLonely)
        {
            //如果被將軍方與下棋方相同
            if (GameController_1.lonelyObjectTeam == chess.GetComponent<Chess>().selfTeam)
            {
                //則不能下棋
                //把要移動的棋子移回原來的位置
                GameController_1.checkerboard[temporaryY, temporaryX] = chess;
                //如果他吃了棋子
                if (game != null)
                {
                    //就把該位置給吃掉的棋子,並顯示他
                    GameController_1.checkerboard[y, x] = game;
                    game.SetActive(true);
                }
                //否則,該位置就爲null
                else
                {
                    GameController_1.checkerboard[y, x] = null;
                }
                //重置棋子對象的x和y
                chess.GetComponent<Chess>().x = temporaryX;
                chess.GetComponent<Chess>().y = temporaryY;
            }
            //否則
            else
            {
                //吃掉棋子
                Destroy(game);
                //移動要移動的棋子
                chess.transform.position = transform.position;
                //切換下棋
                if (GameController_1.selfTeam == Team.Red)
                {
                    GameController_1.selfTeam = Team.Black;
                }
                else
                {
                    GameController_1.selfTeam = Team.Red;
                }
            }
            GameController_1.isLonely = false;
        }
        //否則
        else
        {
            //吃掉棋子
            Destroy(game);
            //移動要移動的棋子
            chess.transform.position = transform.position;
            //切換下棋
            if (GameController_1.selfTeam == Team.Red)
            {
                GameController_1.selfTeam = Team.Black;
            }
            else
            {
                GameController_1.selfTeam = Team.Red;
            }
        }
    }
}

以上就是這個類的全部代碼,在我這個遊戲裏除了這兩個類,還有一個遊戲邏輯類,遊戲邏輯類用來處理遊戲開始時、遊戲進行時、遊戲結束時的一些事件;比如將軍、絕殺等象棋裏的一些事件,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//隊伍種類
public enum Team
{
    Red,//紅隊
    Black//黑隊
}

public class Scene02GameController_1 : MonoBehaviour {
    //棋子預製體
    public GameObject chess;
    
    [HideInInspector]
    public List<GameObject> chessBox= new List<GameObject>();//我方棋子容器

    [HideInInspector]
    public GameObject[,] checkerboard = new GameObject[10,9];//整個棋盤數組

    //開始時棋子所在的點
    public Vector2[] pointBox;
    //紅方帥遊戲對象
    private GameObject redShuai;
    //黑方帥遊戲對象
    private GameObject blackShuai;
    //我選擇的棋的顏色
    public Team selfTeam = Team.Red;
    [HideInInspector]
    public bool isGameOver = false;//控制遊戲結束
    [HideInInspector]
    public bool isPause = false;//控制遊戲暫停
    //黑棋贏的Text遊戲對象
    private GameObject blackWinText;
    //紅棋贏的Text遊戲對象
    private GameObject redWinText;
    //遊戲設置對象
    private GameObject setting;
    //是否絕殺
    public bool isLonely { set; get; }
    //被將軍的帥的顏色
    public Team lonelyObjectTeam { set; get; }
    //移動的棋子對象
    public GameObject moveChess { set; get; }

    void Start () {
        //獲取黑棋贏的Text遊戲對象
        blackWinText = GameObject.Find("Canvas").transform.Find("BlackWinText").gameObject;
        //獲取紅棋贏的Text遊戲對象
        redWinText = GameObject.Find("Canvas").transform.Find("RedWinText").gameObject;
        //獲取遊戲設置對象
        setting = GameObject.Find("Canvas").transform.Find("Setting").gameObject;
        //創建32顆棋子
        CreateChess();
        //設置絕殺bool值
        isLonely = false;
    }
    private void Update()
    {
        //如果誰輸了
        if (isGameOver)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                RenewStart();
            }
        }
        //如果按了Esc鍵
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            //第二次按關閉,設置爲false
            if (isPause)
            {
                //隱藏Setting遊戲對象
                setting.SetActive(false);
                //是否暫停關閉
                isPause = false;
                
            }
            //第一次按打開,設置爲true
            else
            {
                //顯示Setting遊戲對象
                setting.SetActive(true);
                //是否暫停打開
                isPause = true;
            }
        }
    }
    //重新開始遊戲
    public void RenewStart()
    {
        //禁止顯示Text遊戲對象
        blackWinText.SetActive(false);
        redWinText.SetActive(false);
        //清空棋盤數組
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                if (checkerboard[i, j] != null)
                {
                    Destroy(checkerboard[i, j]);
                }
            }
        }
        //初始化棋盤數組
        checkerboard = new GameObject[10, 9];
        //創建32顆棋子
        CreateChess();
        //遊戲沒有結束
        isGameOver = false;
    }
    //創建32顆棋子
    public void CreateChess()
    {
        //總共要創建32顆棋子
        for (int i = 0; i < pointBox.Length; i++)
        {
            //創建一顆棋子
            GameObject game = Instantiate(chess, new Vector3(-2 + pointBox[i].x * 0.5f, 2.25f - pointBox[i].y * 0.5f, 0), Quaternion.identity);
            //如果我選的時紅棋
            if (selfTeam == Team.Red)
            {
                //把前16個棋子設爲黑棋
                if (i < 16)
                {
                    game.GetComponent<Chess>().selfTeam = Team.Black;
                }
                //把後16個棋子設爲紅棋
                else
                {
                    game.GetComponent<Chess>().selfTeam = Team.Red;
                    //把該棋子加入到我方棋子容器
                    chessBox.Add(game);
                }
            }
            //否則,我選的是黑棋
            else
            {
                //把前16個棋子設爲紅棋
                if (i < 16)
                {
                    game.GetComponent<Chess>().selfTeam = Team.Red;
                }
                //把後16個棋子設爲黑棋
                else
                {
                    game.GetComponent<Chess>().selfTeam = Team.Black;
                    //把該棋子加入到我方棋子容器
                    chessBox.Add(game);
                }
            }
            //設置每一顆棋的selfChessName,即棋上面的漢字
            switch (i)
            {
                case 0:
                    game.GetComponent<Chess>().selfChessName = ChessName.Che;
                    break;
                case 1:
                    game.GetComponent<Chess>().selfChessName = ChessName.Ma;
                    break;
                case 2:
                    game.GetComponent<Chess>().selfChessName = ChessName.Xiang;
                    break;
                case 3:
                    game.GetComponent<Chess>().selfChessName = ChessName.Shi;
                    break;
                case 4:
                    game.GetComponent<Chess>().selfChessName = ChessName.Shuai;
                    break;
                case 5:
                    game.GetComponent<Chess>().selfChessName = ChessName.Shi;
                    break;
                case 6:
                    game.GetComponent<Chess>().selfChessName = ChessName.Xiang;
                    break;
                case 7:
                    game.GetComponent<Chess>().selfChessName = ChessName.Ma;
                    break;
                case 8:
                    game.GetComponent<Chess>().selfChessName = ChessName.Che;
                    break;
                case 9:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 10:
                    game.GetComponent<Chess>().selfChessName = ChessName.Pao;
                    break;
                case 11:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 12:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 13:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 14:
                    game.GetComponent<Chess>().selfChessName = ChessName.Pao;
                    break;
                case 15:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 16:
                    game.GetComponent<Chess>().selfChessName = ChessName.Che;
                    break;
                case 17:
                    game.GetComponent<Chess>().selfChessName = ChessName.Ma;
                    break;
                case 18:
                    game.GetComponent<Chess>().selfChessName = ChessName.Xiang;
                    break;
                case 19:
                    game.GetComponent<Chess>().selfChessName = ChessName.Shi;
                    break;
                case 20:
                    game.GetComponent<Chess>().selfChessName = ChessName.Shuai;
                    break;
                case 21:
                    game.GetComponent<Chess>().selfChessName = ChessName.Shi;
                    break;
                case 22:
                    game.GetComponent<Chess>().selfChessName = ChessName.Xiang;
                    break;
                case 23:
                    game.GetComponent<Chess>().selfChessName = ChessName.Ma;
                    break;
                case 24:
                    game.GetComponent<Chess>().selfChessName = ChessName.Che;
                    break;
                case 25:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 26:
                    game.GetComponent<Chess>().selfChessName = ChessName.Pao;
                    break;
                case 27:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 28:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 29:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
                case 30:
                    game.GetComponent<Chess>().selfChessName = ChessName.Pao;
                    break;
                case 31:
                    game.GetComponent<Chess>().selfChessName = ChessName.Bing;
                    break;
            }
            //把棋子放到棋盤數組中
            checkerboard[(int)pointBox[i].y, (int)pointBox[i].x] = game;
            //讓棋子獲得它在棋盤中的位置,數組位置
            game.GetComponent<Chess>().x = (int)pointBox[i].x;
            game.GetComponent<Chess>().y = (int)pointBox[i].y;
            //獲取紅方、黑方的帥
            if (game.GetComponent<Chess>().selfChessName == ChessName.Shuai)
            {
                if (game.GetComponent<Chess>().selfTeam == Team.Red)
                {
                    redShuai = game;
                }
                else
                {
                    blackShuai = game;
                }
            }
        }
    }
    //當指針點擊Quit時調用
    public void OnPointerQuit()
    {
        //跳到場景一
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(0);
    }
    //當指針點擊Again時調用
    public void OnPointerAgain()
    {
        //隱藏Setting遊戲對象
        setting.SetActive(false);
        //是否暫停打開
        isPause = false;
        //重新開始遊戲
        RenewStart();
    }

    public void Lonely()
    {
        //這是對所有棋的檢測
        foreach(GameObject game in checkerboard)
        {
            if (game != null)
            {
                JudgeWalking(game);
                if (isLonely)
                {
                    break;
                }
            }
        }
        //下面是對帥的檢測
        //如果兩個帥在同一行
        if (redShuai.GetComponent<Chess>().x == blackShuai.GetComponent<Chess>().x)
        {
            //此代碼塊的控制值
            int a = 0;
            //掃描這一列
            for (int i = 0; i < 10; i++)
            {
                //如果棋盤這個位置有棋子
                if (checkerboard[i, redShuai.GetComponent<Chess>().x] != null)
                {
                    //如果該棋子爲帥
                    if (checkerboard[i, redShuai.GetComponent<Chess>().x].GetComponent<Chess>().selfChessName == ChessName.Shuai)
                    {
                        a++;
                        if (a == 2)
                        {
                            //絕殺bool值爲true,意爲:將軍
                            isLonely = true;
                            //獲取被將軍的帥的顏色
                            lonelyObjectTeam = moveChess.GetComponent<Chess>().selfTeam;
                            break;
                        }
                    }
                    //否則
                    else
                    {
                        if (a != 0)
                        {
                            break;
                        }
                    }
                }
            }
        }
    }

    //判斷行走
    public void JudgeWalking(GameObject game)
    {
        Chess chess = game.GetComponent<Chess>();

        //如果他是帥
        if (chess.selfChessName == ChessName.Shuai)
        {
            //向右摸索前進一格
            if (chess.x + 1 <= 5)
            {
                ShuaiShiBingGo(chess.x + 1, chess.y, chess);
            }
            //向左摸索前進一格
            if (chess.x - 1 >= 3)
            {
                ShuaiShiBingGo(chess.x - 1, chess.y, chess);
            }
            //如果是楚河下方的帥
            if (chess.y > 5)
            {
                //向下摸索前進一格
                if (chess.y + 1 < 10)
                {
                    ShuaiShiBingGo(chess.x, chess.y + 1, chess);
                }
                //向上摸索前進一格
                if (chess.y - 1 >= 7)
                {
                    ShuaiShiBingGo(chess.x, chess.y - 1, chess);
                }
            }
            //否則是楚河上方的帥
            else
            {
                //向下摸索前進一格
                if (chess.y + 1 <= 2)
                {
                    ShuaiShiBingGo(chess.x, chess.y + 1, chess);
                }
                //向上摸索前進一格
                if (chess.y - 1 >= 0)
                {
                    ShuaiShiBingGo(chess.x, chess.y - 1, chess);
                }
            }
            
        }
        //如果他是士
        else if (chess.selfChessName == ChessName.Shi)
        {
            //如果是楚河下方的的士
            if (chess.y > 5)
            {
                //向右下方摸索前進
                if (chess.x + 1 <= 5 && chess.y + 1 < 10)
                {
                    ShuaiShiBingGo(chess.x + 1, chess.y + 1, chess);
                }
                //向右上方摸索前進
                if (chess.x + 1 <= 5 && chess.y - 1 >= 7)
                {
                    ShuaiShiBingGo(chess.x + 1, chess.y - 1, chess);
                }
                //向左下方摸索前進
                if (chess.x - 1 >= 3 && chess.y + 1 < 10)
                {
                    ShuaiShiBingGo(chess.x - 1, chess.y + 1, chess);
                }
                //向右上方摸索前進
                if (chess.x - 1 >= 3 && chess.y - 1 >= 7)
                {
                    ShuaiShiBingGo(chess.x - 1, chess.y - 1, chess);
                }
            }
            //否則,士楚河上方的士
            else
            {
                //向右下方摸索前進
                if (chess.x + 1 <= 5 && chess.y + 1 <= 2)
                {
                    ShuaiShiBingGo(chess.x + 1, chess.y + 1, chess);
                }
                //向右上方摸索前進
                if (chess.x + 1 <= 5 && chess.y - 1 >= 0)
                {
                    ShuaiShiBingGo(chess.x + 1, chess.y - 1, chess);
                }
                //向左下方摸索前進
                if (chess.x - 1 >= 3 && chess.y + 1 <= 2)
                {
                    ShuaiShiBingGo(chess.x - 1, chess.y + 1, chess);
                }
                //向右上方摸索前進
                if (chess.x - 1 >= 3 && chess.y - 1 >= 0)
                {
                    ShuaiShiBingGo(chess.x - 1, chess.y - 1, chess);
                }
            }
        }
        //如果他是象
        else if (chess.selfChessName == ChessName.Xiang)
        {
            //如果distinguish爲一
            if (chess.distinguish == 1)
            {
                //向右下方走
                if (chess.x + 2 < 9 && chess.y + 2 < 5)
                {
                    //表示是否能走
                    bool isGo = false;
                    //判斷是否有棋子阻擋
                    MaXiangGo(chess.x + 1, chess.y + 1, ref isGo, chess);
                    if (isGo)
                    {
                        MaXiangGo(chess.x + 2, chess.y + 2, ref isGo, chess);
                    }
                }
                //向右上方走
                if (chess.x + 2 < 9 && chess.y - 2 >= 0)
                {
                    bool isGo = false;
                    MaXiangGo(chess.x + 1, chess.y - 1, ref isGo, chess);
                    if (isGo)
                    {
                        MaXiangGo(chess.x + 2, chess.y - 2, ref isGo, chess);
                    }
                }
                //向左下方走
                if (chess.x - 2 >= 0 && chess.y + 2 < 5)
                {
                    bool isGo = false;
                    MaXiangGo(chess.x - 1, chess.y + 1, ref isGo, chess);
                    if (isGo)
                    {
                        MaXiangGo(chess.x - 2, chess.y + 2, ref isGo, chess);
                    }
                }
                //向右上方走
                if (chess.x - 2 >= 0 && chess.y - 2 >= 0)
                {
                    bool isGo = false;
                    MaXiangGo(chess.x - 1, chess.y - 1, ref isGo, chess);
                    if (isGo)
                    {
                        MaXiangGo(chess.x - 2, chess.y - 2, ref isGo, chess);
                    }
                }
            }
            //否則:
            else
            {
                //向右下方走
                if (chess.x + 2 < 9 && chess.y + 2 < 10)
                {
                    bool isGo = false;
                    MaXiangGo(chess.x + 1, chess.y + 1, ref isGo, chess);
                    if (isGo)
                    {
                        MaXiangGo(chess.x + 2, chess.y + 2, ref isGo, chess);
                    }
                }
                //向右上方走
                if (chess.x + 2 < 9 && chess.y - 2 >= 5)
                {
                    bool isGo = false;
                    MaXiangGo(chess.x + 1, chess.y - 1, ref isGo, chess);
                    if (isGo)
                    {
                        MaXiangGo(chess.x + 2, chess.y - 2, ref isGo, chess);
                    }
                }
                //向左下方走
                if (chess.x - 2 >= 0 && chess.y + 2 < 10)
                {
                    bool isGo = false;
                    MaXiangGo(chess.x - 1, chess.y + 1, ref isGo, chess);
                    if (isGo)
                    {
                        MaXiangGo(chess.x - 2, chess.y + 2, ref isGo, chess);
                    }
                }
                //向右上方走
                if (chess.x - 2 >= 0 && chess.y - 2 >= 5)
                {
                    bool isGo = false;
                    MaXiangGo(chess.x - 1, chess.y - 1, ref isGo, chess);
                    if (isGo)
                    {
                        MaXiangGo(chess.x - 2, chess.y - 2, ref isGo, chess);
                    }
                }
            }
        }
        //如果他是馬
        else if (chess.selfChessName == ChessName.Ma)
        {
            //向右摸索
            if (chess.x + 2 < 9)
            {
                //isGo是表示該方向的前一格有沒有棋子
                //如果有就爲false,表示不能通過
                //如果沒有就爲true,表示可以通過
                bool isGo = false;
                //開始檢查
                MaXiangGo(chess.x + 1, chess.y, ref isGo, chess);
                if (isGo)//結果判斷
                {
                    //馬走的兩個路線
                    if (chess.y + 1 < 10)
                    {
                        MaXiangGo(chess.x + 2, chess.y + 1, ref isGo, chess);
                    }
                    if (chess.y - 1 >= 0)
                    {
                        MaXiangGo(chess.x + 2, chess.y - 1, ref isGo, chess);
                    }
                }
            }
            //向左摸索
            if (chess.x - 2 >= 0)
            {
                //isGo是表示該方向的前一格有沒有棋子
                //如果有就爲false,表示不能通過
                //如果沒有就爲true,表示可以通過
                bool isGo = false;
                //開始檢查
                MaXiangGo(chess.x - 1, chess.y, ref isGo, chess);
                if (isGo)//結果判斷
                {
                    //馬走的兩個路線
                    if (chess.y + 1 < 10)
                    {
                        MaXiangGo(chess.x - 2, chess.y + 1, ref isGo, chess);
                    }
                    if (chess.y - 1 >= 0)
                    {
                        MaXiangGo(chess.x - 2, chess.y - 1, ref isGo, chess);
                    }
                }
            }
            //向下摸索
            if (chess.y + 2 < 10)
            {
                bool isGo = false;

                MaXiangGo(chess.x, chess.y + 1, ref isGo, chess);
                if (isGo)
                {
                    if (chess.x + 1 < 9)
                    {
                        MaXiangGo(chess.x + 1, chess.y + 2, ref isGo, chess);
                    }
                    if (chess.x - 1 >= 0)
                    {
                        MaXiangGo(chess.x - 1, chess.y + 2, ref isGo, chess);
                    }
                }
            }
            //向上摸索
            if (chess.y - 2 >= 0)
            {
                bool isGo = false;

                MaXiangGo(chess.x, chess.y - 1, ref isGo, chess);
                if (isGo)
                {
                    if (chess.x + 1 < 9)
                    {
                        MaXiangGo(chess.x + 1, chess.y - 2, ref isGo, chess);
                    }
                    if (chess.x - 1 >= 0)
                    {
                        MaXiangGo(chess.x - 1, chess.y - 2, ref isGo, chess);
                    }
                }
            }
        }
        //如果他是車
        else if (chess.selfChessName == ChessName.Che)
        {
            //向右摸索前進
            for (int i = 1; chess.x + i < 9; i++)
            {
                if (CheGo(chess.x + i, chess.y, chess))
                {
                    break;
                }
            }
            //向左摸索前進
            for (int i = 1; chess.x - i >= 0; i++)
            {
                if (CheGo(chess.x - i, chess.y, chess))
                {
                    break;
                }
            }
            //向下摸索前進
            for (int i = 1; chess.y + i < 10; i++)
            {
                if (CheGo(chess.x, chess.y + i, chess))
                {
                    break;
                }
            }
            //向上摸索前進
            for (int i = 1; chess.y - i >= 0; i++)
            {
                if (CheGo(chess.x, chess.y - i, chess))
                {
                    break;
                }
            }
        }
        //如果他是炮
        else if (chess.selfChessName == ChessName.Pao)
        {
            //向右摸索前進
            {
                bool fire = false;//是否開炮
                for (int i = 1; chess.x + i < 9; i++)
                {
                    if (PaoGo(chess.x + i, chess.y, ref fire, chess))
                    {
                        break;
                    }
                }
            }
            //向左摸索前進
            {
                bool fire = false;//是否開炮
                for (int i = 1; chess.x - i >= 0; i++)
                {
                    if (PaoGo(chess.x - i, chess.y, ref fire, chess))
                    {
                        break;
                    }
                }
            }
            //向下摸索前進
            {
                bool fire = false;//是否開炮
                for (int i = 1; chess.y + i < 10; i++)
                {
                    if (PaoGo(chess.x, chess.y + i, ref fire, chess))
                    {
                        break;
                    }
                }
            }
            //向上摸索前進
            {
                bool fire = false;//是否開炮
                for (int i = 1; chess.y - i >= 0; i++)
                {
                    if (PaoGo(chess.x, chess.y - i, ref fire, chess))
                    {
                        break;
                    }
                }
            }
        }
        //如果他是兵
        else if (chess.selfChessName == ChessName.Bing)
        {
            //控制兵是否能左右行走
            bool around = false;
            //如果這是楚河上方的兵
            if (chess.distinguish == 1)
            {
                //向前摸索行走
                if (chess.y + 1 < 10)
                {
                    ShuaiShiBingGo(chess.x, chess.y + 1, chess);
                }
                //如果這個兵過了楚河
                if (chess.y >= 5)
                {
                    around = true;
                }
            }
            //否則是楚河下方的兵
            else
            {
                //向前摸索行走
                if (chess.y - 1 >= 0)
                {
                    ShuaiShiBingGo(chess.x, chess.y - 1, chess);
                }
                //如果這個兵過了楚河
                if (chess.y <= 4)
                {
                    around = true;
                }
            }
            //這個兵是否能左右行走
            if (around)
            {
                //向右摸索前進一格
                if (chess.x + 1 < 9)
                {
                    ShuaiShiBingGo(chess.x + 1, chess.y, chess);
                }
                //向左摸索前進一格
                if (chess.x - 1 >= 0)
                {
                    ShuaiShiBingGo(chess.x - 1, chess.y, chess);
                }
            }
        }
    }

    //帥、士、兵的走法
    private void ShuaiShiBingGo(int a, int b, Chess chess)
    {
        //如果棋盤上的(b,a)座標爲空
        if (checkerboard[b, a] != null)
        {
            //如果該座標上有棋子,且爲敵方棋子
            if (checkerboard[b, a].GetComponent<Chess>().selfTeam != chess.selfTeam)
            {
                if(checkerboard[b, a].GetComponent<Chess>().selfChessName == ChessName.Shuai)
                {
                    //絕殺bool值爲true,意爲:將軍
                    isLonely = true;
                    //獲取被將軍的帥的顏色
                    lonelyObjectTeam = checkerboard[b, a].GetComponent<Chess>().selfTeam;
                }
            }
        }
    }

    //車的走法
    private bool CheGo(int a, int b, Chess chess)
    {
        //如果棋盤上的(b,a)座標爲空
        if (checkerboard[b, a] != null)
        {
            //如果該座標上有棋子,且爲敵方棋子
            if (checkerboard[b, a].GetComponent<Chess>().selfTeam != chess.selfTeam)
            {
                if (checkerboard[b, a].GetComponent<Chess>().selfChessName == ChessName.Shuai)
                {
                    //絕殺bool值爲true,意爲:將軍
                    isLonely = true;
                    //獲取被將軍的帥的顏色
                    lonelyObjectTeam = checkerboard[b, a].GetComponent<Chess>().selfTeam;
                }
            }
            //運行到此行,說明前方有我方棋子,因被我方棋子擋住,就不能前進
            //返回true,讓上面執行break退出for循環
            return true;
        }
        //否則
        else
        {
            //運行到此行,說明該點無棋子,可以行走,可以繼續向前摸索
            //返回false,讓上面不執行break繼續for循環
            return false;
        }
    }

    //炮的走法
    private bool PaoGo(int a, int b, ref bool fire, Chess chess)
    {
        //如果棋盤上的(b,a)座標爲空
        if (checkerboard[b, a] != null)
        {
            //如果fire爲true,則表示進入開炮狀態
            if (fire)
            {
                //如果該座標上有棋子,且爲敵方棋子
                if (checkerboard[b, a].GetComponent<Chess>().selfTeam != chess.selfTeam)
                {
                    if (checkerboard[b, a].GetComponent<Chess>().selfChessName == ChessName.Shuai)
                    {
                        //絕殺bool值爲true,意爲:將軍
                        isLonely = true;
                        //獲取被將軍的帥的顏色
                        lonelyObjectTeam = checkerboard[b, a].GetComponent<Chess>().selfTeam;
                    }
                }
                //運行到此行,說明前方有我方棋子,因被我方棋子擋住,就不能前進
                //返回true,讓上面執行break退出for循環
                return true;
            }
            //否則
            else
            {
                //fire設爲true,表示可以打炮
                fire = true;
                //運行到此行,說明剛剛開始隔棋打棋,雖然該點有棋子,但是要作爲支點
                //返回false,讓上面不執行break,繼續for循環
                return false;
            }
        }
        //否則
        else
        {
            //運行到此行,說明該點無棋子,可以行走,可以繼續向前摸索
            //返回false,讓上面不執行break,繼續for循環
            return false;
        }
    }

    //馬、象的走法
    private void MaXiangGo(int a, int b, ref bool isGo, Chess chess)
    {
        //如果棋盤上的(b,a)座標爲空
        if (checkerboard[b, a] != null)
        {
            //能否通行
            if (isGo)
            {
                //如果該座標上有棋子,且爲敵方棋子
                if (checkerboard[b, a].GetComponent<Chess>().selfTeam != chess.selfTeam)
                {
                    if (checkerboard[b, a].GetComponent<Chess>().selfChessName == ChessName.Shuai)
                    {
                        //絕殺bool值爲true,意爲:將軍
                        isLonely = true;
                        //獲取被將軍的帥的顏色
                        lonelyObjectTeam = checkerboard[b, a].GetComponent<Chess>().selfTeam;
                    }
                }
            }
        }
        //否則
        else
        {
            //能否通行
            if (!isGo)
            {
                isGo = true;
            }
        }
    }
}

在這個象棋遊戲裏,絕殺我還沒有寫,就是輸贏我沒有寫,這個以後會把它補上;

以上是單人模式的代碼,但是由於沒有電腦,所以我這個遊戲是不完善的,原因是我現在還沒有能力寫出一個好的象棋AI,這是我在做遊戲方面所欠缺的,

好了!記錄到此爲止。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章