unity小游戏系列之黑白棋

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

public class black_white : 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=1, white=-1 } ;

    turn chessTurn; //落子顺序
    public Texture2D white; //白棋子
    public Texture2D black; //黑棋子
    public Texture2D blackWin; //白子获胜提示图
    public Texture2D whiteWin; //黑子获胜提示图
    public GameObject Tips;
    int winner = 0; //获胜方,1为黑子,-1为白子
    bool isPlaying = true; //是否处于对弈状态

    // Use this for initialization
    void Start () {
        
        chessPos = new Vector2[14, 14];
        chessState = new int[14, 14];
        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 < 14; i++)
        {
            for (int j = 0; j < 14; j++)
            {
                chessPos[i, j] = new Vector2((LBPos.x + gridWidth * i + gridWidth / 2), (LBPos.y + gridHeight * j + gridHeight / 2));
            }
        }
        ReSet();
    }

    void ReSet() 
    {
        for (int i = 0; i < 14; i++)
        {
            for (int j = 0; j < 14; j++)
            {
                chessState[i, j] = 0;
            }
        }
        isPlaying = true;
        chessTurn = turn.black;
        winner = 0;
        chessState[6, 6] = 1;
        chessState[7, 7] = 1;
        chessState[6, 7] = -1;
        chessState[7, 6] = -1;
        Tips.gameObject.SetActive(false);
    }
    
    // Update is called once per frame
    void Update () {
        //检测鼠标输入并确定落子状态
        if (isPlaying && Input.GetMouseButtonDown(0))
        {
            PointPos = Input.mousePosition;
            int curr_x = 0, curr_y = 0;
            for (int i = 0; i < 14; i++)
            {
                for (int j = 0; j < 14; j++)
                {
                    //找到最接近鼠标点击位置的落子点,如果空则落子
                    if (Dis(PointPos, chessPos[i, j]) < minGridDis/2 && chessState[i, j] == 0)
                    {
                        //根据下棋顺序确定落子颜色
                        chessState[i, j] = chessTurn == turn.black ? 1 : -1;
                        curr_x = i;
                        curr_y = j;
                        result(curr_x, curr_y, chessTurn);
                        //落子成功,更换下棋顺序
                        chessTurn = chessTurn == turn.black ? turn.white : turn.black;
                    }
                }
            }
            int re = isWin();
            //调用判断函数,确定是否有获胜方
            if (re == 1)
            {
                Debug.Log("黑棋胜");
                winner = 1;
                isPlaying = false;
            }
            else if (re == -1)
            {
                Debug.Log("白棋胜");
                winner = -1;
                isPlaying = false;
            }
        }
        //按下空格重新开始游戏
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ReSet();
        }  
    }
    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 < 14; i++)
        {
            for (int j = 0; j < 14; j++)
            {
                if (chessState[i, 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, j] == -1)
                {
                    GUI.DrawTexture(new Rect(chessPos[i, j].x - gridWidth/2 , Screen.height - chessPos[i, j].y - gridHeight/2 , gridWidth, gridHeight), white);
                }
            }
        }
        if ((int)chessTurn == 1)
        {
            GUI.DrawTexture(new Rect(0, Screen.height * 0.5f, gridWidth, gridHeight), black);
        }
        else 
        {
            GUI.DrawTexture(new Rect(0, Screen.height * 0.5f, 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);
            Tips.gameObject.SetActive(true);
        }
        else if (winner == -1)
        {
            GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), whiteWin);
            Tips.gameObject.SetActive(true);
        }

    }

    int isWin()
    {
        int black_count = 0, white_count = 0;
        bool is_null = false;
        foreach (int a in chessState) 
        {
            if (a == 0) 
            {
                is_null = true;
            }
            else if (a == 1) 
            {
                black_count++;
            }
            else if (a == -1) 
            {
                white_count++;
            }
        }
        if (!is_null)
        {
            return black_count > white_count ? 1 : -1;
        }
        else
        {
            if (black_count == 0 || white_count == 0)
            {
                return black_count > white_count ? 1 : -1;
            }
            else 
            {
                return 2;
            }
        }
    }

    void result(int x, int y, turn curr_turn)
    {
        //黑白子当前数量
        int Black_num = 0, White_num = 0;
        int serial_right = 0;
        int serial_left = 0;
        int serial_top = 0;
        int serial_bottom = 0;

        int serial_obliqueRightTop = 0;
        int serial_obliqueLeftBottom = 0;
        int serial_obliqueLeftTop = 0;
        int serial_obliqueRightBottom = 0;

        //判断右边            
        if (x + 1 <= 13 && chessState[x + 1, y] != 0)
        {
            List<int> chess_list = new List<int>();
            for (int i = x + 1; i < 14; i++)
            {
                if (chessState[i, y] != 0)
                {
                    serial_right++;
                    chess_list.Add(chessState[i, y]);                    
                }
                else
                {                   
                    break;
                }
            }

            if (!chess_list.Contains((int)curr_turn))
            {
                serial_right = 0;
            }
            else
            {
                int curr_index = 0;
                for (int l = x; l <= x + serial_right; l++)
                {
                    if (chessState[l, y] == (int)curr_turn && l - x >= 2)
                    {
                        if (l - x > curr_index)
                        {
                            curr_index = l - x;
                        }
                    }
                }
                serial_right = curr_index;
            }

            for (int i = x; i < x + serial_right; i++)
            {
                chessState[i, y] = (int)curr_turn;
            }
        }
        //判断左边 
        if (x - 1 >= 0 && chessState[x - 1, y] != 0)
        {
            List<int> chess_list = new List<int>();
            for (int i = x - 1; i >= 0; i--)
            {
                if (chessState[i, y] != 0)
                {
                    serial_left++;
                    chess_list.Add(chessState[i, y]);
                }
                else
                {
                    break;
                }
            }
            if (!chess_list.Contains((int)curr_turn))
            {
                serial_left = 0;
            }
            else
            {
                int curr_index = 0;
                for (int l = x; l >= x - serial_left; l--)
                {
                    if (chessState[l, y] == (int)curr_turn && x - l >= 2)
                    {
                        if (x - l > curr_index)
                        {
                            curr_index = x - l;
                        }
                    }
                }
                serial_left = curr_index;
            }
            for (int i = x; i > x - serial_left; i--)
            {
                chessState[i, y] = (int)curr_turn;
            }
        }
        //判断上
        if (y + 1 <= 13 && chessState[x, y + 1] != 0)
        {
            List<int> chess_list = new List<int>();
            for (int i = y + 1; i < 14; i++)
            {
                if (chessState[x, i] != 0)
                {
                    serial_top++;
                    chess_list.Add(chessState[x, i]);
                }
                else
                {
                    break;
                }
            }
            if (!chess_list.Contains((int)curr_turn))
            {
                serial_top = 0;
            }
            else
            {
                int curr_index = 0;
                for (int l = y; l <= y + serial_top; l++)
                {
                    if (chessState[x, l] == (int)curr_turn && l - y >= 2)
                    {
                        if (l - y > curr_index)
                        {
                            curr_index = l - y;
                        }
                    }
                }
                serial_top = curr_index;
            }
            for (int i = y; i < y + serial_top; i++)
            {
                chessState[x, i] = (int)curr_turn;
            }
        }
        //判断下
        if (y - 1 >= 0 && chessState[x, y - 1] != 0)
        {
            List<int> chess_list = new List<int>();
            for (int i = y - 1; i >= 0; i--)
            {
                if (chessState[x, i] != 0)
                {
                    serial_bottom++;
                    chess_list.Add(chessState[x, i]);
                }
                else
                {
                    break;
                }
            }
            if (!chess_list.Contains((int)curr_turn))
            {
                serial_bottom = 0;
            }
            else
            {
                int curr_index = 0;
                for (int l = y; l >= y - serial_bottom; l--)
                {
                    if (chessState[x, l] == (int)curr_turn && y - l >= 2)
                    {
                        if (y - l > curr_index)
                        {
                            curr_index = y - l;
                        }
                    }
                }
                serial_bottom = curr_index;
            }
            for (int i = y; i > y - serial_bottom; i--)
            {
                chessState[x, i] = (int)curr_turn;
            }
        }
        //斜右上线/
        if (x + 1 <= 13 && y + 1 <= 13 && chessState[x + 1, y + 1] != 0)
        {
            List<int> chess_list = new List<int>();
            for (int i = 1; (i + x < 14) && (i + y < 14); i++)
            {
                if (chessState[x + i, y + i] != 0)
                {
                    serial_obliqueRightTop++;
                    chess_list.Add(chessState[x + i, y + i]);
                }
                else
                {
                    break;
                }
            }
            if (!chess_list.Contains((int)curr_turn))
            {
                serial_obliqueRightTop = 0;
            }
            else
            {
                int curr_index = 0;
                for (int l = 0; l <= serial_obliqueRightTop; l++)
                {
                    if (chessState[x + l, y + l] == (int)curr_turn && l >= 2)
                    {
                        if (l > curr_index)
                        {
                            curr_index = l;
                        }
                    }
                }
                serial_obliqueRightTop = curr_index;
            }
            for (int i = 0; i < serial_obliqueRightTop; i++)
            {
                chessState[x + i, y + i] = (int)curr_turn;
            }
        }
        //斜左下线/
        if (x - 1 >= 0 && y - 1 >= 0 && chessState[x - 1, y - 1] != 0)
        {
            List<int> chess_list = new List<int>();
            for (int i = 1; (x - i >= 0) && (y - i >= 0); i++)
            {
                if (chessState[x - i, y - i] != 0)
                {
                    serial_obliqueLeftBottom++;
                    chess_list.Add(chessState[x - i, y - i]);
                }
                else
                {
                    break;
                }
            }
            if (!chess_list.Contains((int)curr_turn))
            {
                serial_obliqueLeftBottom = 0;
            }
            else
            {
                int curr_index = 0;
                for (int l = 0; l <= serial_obliqueLeftBottom; l++)
                {
                    if (chessState[x - l, y - l] == (int)curr_turn && l >= 2)
                    {
                        if (l > curr_index)
                        {
                            curr_index = l;
                        }
                    }
                }
                serial_obliqueLeftBottom = curr_index;
            }
            for (int i = 0; i < serial_obliqueLeftBottom; i++)
            {
                chessState[x - i, y - i] = (int)curr_turn;
            }
        }

        //斜左上线
        if (x - 1 >= 0 && y + 1 < 14 && chessState[x - 1, y + 1] != 0)
        {
            List<int> chess_list = new List<int>();
            for (int i = 1; (x - i >= 0) && (y + i < 14); i++)
            {
                if (chessState[x - i, y + i] != 0)
                {
                    serial_obliqueLeftTop++;
                    chess_list.Add(chessState[x - i, y + i]);                   
                }
                else
                {
                    break;
                }
            }
            if (!chess_list.Contains((int)curr_turn))
            {
                serial_obliqueLeftTop = 0;
            }
            else
            {
                int curr_index = 0;
                for (int l = 0; l <= serial_obliqueLeftTop; l++)
                {
                    if (chessState[x - l, y + l] == (int)curr_turn && l >= 2)
                    {
                        if (l > curr_index)
                        {
                            curr_index = l;
                        }
                    }
                }
                serial_obliqueLeftTop = curr_index;
            }
            for (int i = 0; i < serial_obliqueLeftTop; i++)
            {
                chessState[x - i, y + i] = (int)curr_turn;
            }
        }

        //斜右下线
        if (x + 1 < 14 && y - 1 >= 0 && chessState[x + 1, y - 1] != 0)
        {
            List<int> chess_list = new List<int>();
            for (int i = 1; (x + i < 14) && (y - i >= 0); i++)
            {
                if (chessState[x + i, y - i] != 0)
                {
                    serial_obliqueRightBottom++;
                    chess_list.Add(chessState[x + i, y - i]);
                }
                else
                {
                    break;
                }
            }
            if (!chess_list.Contains((int)curr_turn))
            {
                serial_obliqueRightBottom = 0;
            }
            else
            {
                int curr_index = 0;
                for (int l = 0; l <= serial_obliqueRightBottom; l++)
                {
                    if (chessState[x + l, y - l] == (int)curr_turn && l >= 2)
                    {
                        if (l > curr_index)
                        {
                            curr_index = l;
                        }
                    }
                }
                serial_obliqueRightBottom = curr_index;
            }
            for (int i = 0; i < serial_obliqueRightBottom; i++)
            {
                chessState[x + i, y - i] = (int)curr_turn;
            }
        }
    }

    public void close() 
    {
        Application.Quit();
    }
}

主要算法是先判断落子处的某个方向是否旁边有棋子,如果没有就可以跳出,如果有就继续统计其连续有多少棋子,再在其连续的棋子中判断有没有和当前落子类型一样的棋子,没有就跳出,有就继续判断离当前落子处最远的一颗同类型的棋子,中间只要大于一颗棋子的距离,就可以把这些该变色的棋子统计出来

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,047评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,807评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,501评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,839评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,951评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,117评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,188评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,929评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,372评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,679评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,837评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,536评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,168评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,886评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,129评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,665评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,739评论 2 351

推荐阅读更多精彩内容