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

C#实现五子棋小游戏

程序员文章站 2022-05-25 13:53:08
...

主界面如图:

C#实现五子棋小游戏

各种结果判断:

if (IsBegin)
            {
                try
                {
                    int x = (e.X + MianSet.CellWidth / 2) / MianSet.CellWidth;
                    int y = (e.Y + MianSet.CellWidth / 2) / MianSet.CellWidth;
                    if (ChessBack[x, y] != 0)
                        return;
                    Chess.DrawChess(type, pictureBox1, graphic, e);
                    ChessBack[x, y] = type ? 1 : 2;
                    if (IsFull() && !Victory(x, y))
                    {
                        if (MessageBox.Show("游戏结束,平局") == DialogResult.OK)
                            InitalizeQP();
                        return;
                    }
                    if (Victory(x, y))
                    {
                        string victer = type ? "红" : "蓝";
                        if (MessageBox.Show(victer + "方胜利!", "结果") == DialogResult.OK)
                            InitalizeQP();
                        return;
                    }
                    type = !type;//换人
                    labMes.Text = type ? "红方行动" : "蓝方行动";
                }
                catch { }
            }

结果判断算法:

private bool LeanVic(int x, int y)//斜着同色五连珠
        {
            int L1 = x - 4;// (x - 4) > 0 ? x - 4 : 0;
            int L2 = y - 4;// (y - 4) > 0 ? y - 4 : 0;
            int val = ChessBack[x, y];
            for (int i = L1, j = L2; i < 16 && j < 16; i++, j++)
            {
                if (ChessBack[x, y] == val && ChessBack[x + 1, y + 1] == val && ChessBack[x + 2, y + 2] == val && ChessBack[x + 3, y + 3] == val && ChessBack[x + 4, y+ 4] == val)
                    return true;
            }
            for (int i = L1, j = L2; i < 16 && j < 16; i++, j++)
            {
                if (ChessBack[x, y] == val && ChessBack[x + 1, y - 1] == val && ChessBack[x + 2, y - 2] == val && ChessBack[x + 3, y - 3] == val && ChessBack[x + 4, y - 4] == val)
                    return true;
            }
            for (int i = L1, j = L2; i < 16 && j < 16; i++, j++)
            {
                if (ChessBack[x, y] == val && ChessBack[x - 1, y + 1] == val && ChessBack[x - 2, y + 2] == val && ChessBack[x - 3, y + 3] == val && ChessBack[x - 4, y + 4] == val)
                    return true;
            }
            return false;
        }
        private bool ColVic(int x, int y)//一列同色五连珠
        {
            int col = (y - 4) > 0 ? y - 4 : 0;
            int val = ChessBack[x, y];
            for (int i = col; i < 16; i++)
            {
                if (ChessBack[x, i] == val && ChessBack[x, i + 1] == val && ChessBack[x, i + 2] == val && ChessBack[x, i + 3] == val && ChessBack[x, i + 4] == val)
                    return true;
            }
            return false;
        }

        private bool RowVic(int x, int y)//一行同色五连珠
        {
            int row = (x - 4) > 0 ? x - 4 : 0;
            int val = ChessBack[x, y];
            for (int i = row; i < 16; i++)
            {
                if (ChessBack[i, y] == val && ChessBack[i + 1, y] == val && ChessBack[i + 2, y] == val && ChessBack[i + 3, y] == val && ChessBack[i + 4, y] == val)
                    return true;
            }
            return false;
        }

        private bool IsFull()//填满棋盘
        {
            bool full = true;
            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    if (ChessBack[i, j] == 0)
                    {
                        full = false;
                    }
                }
            }
            return full;
        }
源码地址
相关标签: C#五子棋