C#窗体程序编写简单五子棋

一、前言

        简单C#窗体程序学习·······keep runing······

        文章末尾处提供全面源码。

二、程序实现

       C#窗体程序编写五子棋,可想而知,比Java会便捷很多,比较可以直接图形化拖拉界面。Java还需要安装插件才行。

【1】简单五子棋嘛,一个棋盘界面就可以了,所以先拖一个窗体界面,大体如下图

【2】拖控件:从控件里面拖出两个 panel 控件、三个 button 控件、一个 label控件

【3】设属性:为两个 panel 控件 dock 属性分别 选择  FILL  和  右沾满(属性根据自己喜欢来设置)

设置背景颜色,在右边的panel中设置,三个按钮分别命名为    开始、重来、退出(这个随便)

一个 label 控件,用来表示游戏状态。

扫描二维码关注公众号,回复: 5911177 查看本文章

【4】所用私有成员、初始化函数、窗体加载等等····

        //游戏是否开始
        private bool start;
        //下的是黑子还是白子
        private bool type = true;
        //棋盘大小
        private const int size = 15;
        //是否为空,空为0,不为空为1、2
        //private int[,] ChessCheck = new int[11, 11];
        private int[,] ChessCheck = new int[size, size];

        public Form1()
        {
            InitializeComponent();
        }

        //设置游戏窗体大小
        private void Form1_Load(object sender, EventArgs e)
        {
            start = false;
            button1.Enabled = true;
            button2.Enabled = false;
            this.Width = MainSize.Wid;
            this.Height = MainSize.Hei;
            this.Location = new Point(260, 75);//设置窗体起始位置
        }


        //初始化
        private void InitializeThis()
        {
            //棋盘存储数组 函数
            for (int i = 0; i < size; i++)
                for (int j = 0; j < size; j++)
                    ChessCheck[i, j] = 0;
            start = false;//是否开始游戏
            this.panel1.Invalidate();//棋盘重新加载(画)
            type = true;//默认黑棋先下
        }

【5】辅助类1——MainSize类(来说明棋盘、棋子、窗体的大小等)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 五子棋Forms
{
    //以下是MainSize类

    class MainSize
    {
        //主框体大小
        public static int Wid { get { return 710; } }
        public static int Hei { get { return 640; } }
        //棋盘大小
        public static int CBWid { get { return 600; } }
        public static int CBHei { get { return 600; } }
        //棋盘宽度
        public static int CBGap { get { return 40; } }
        //棋子直径
        public static int ChessRadious
        {
            get { return 34; }
        }
    }
}

【6】辅助类2——ChessBoard类(就一个静态函数、画棋盘)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;


namespace 五子棋Forms
{
    //以下是ChessBoard类
    class ChessBoard
    {
        public static void DrawCB(Graphics g)
        {
            int num = MainSize.CBWid / MainSize.CBGap - 1;
            int gap = MainSize.CBGap;
            g.Clear(Color.Gold);
            for (int i = 0; i < num + 1; i++)
            {
                g.DrawLine(new Pen(Color.Black), 20, 20 + i * gap, 20 + num * gap, 20 + i * gap);
                g.DrawLine(new Pen(Color.Black), 20 + gap * i, 20, 20 + i * gap, 20 + gap * num);
            }
        }
    }
}

【7】辅助类3——Chess类(两个静态函数,一个用来画鼠标点击处的棋子,一个当棋盘重画的时,把当前已经下在棋盘上的棋子也重新全部画出来——防止棋盘刷新,把棋子图像抹去)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace 五子棋Forms
{
    //以下是Chess类
    class Chess
    {
        public static void DrawC(Panel p, bool type, MouseEventArgs e)
        {
            Graphics g = p.CreateGraphics();
            //确定棋子的中心位置
            int x1 = (e.X) / MainSize.CBGap;
            int x2 = x1 * MainSize.CBGap + 20 - 17;
            int y1 = (e.Y) / MainSize.CBGap;
            int y2 = y1 * MainSize.CBGap + 20 - 17;
            if (type)
            {
                g.FillEllipse(new SolidBrush(Color.Black), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
            }
            else
            {
                g.FillEllipse(new SolidBrush(Color.White), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
            }
        }

        //当界面被重新聚焦的时候,把棋盘上的棋子重新加载(画)出来
        public static void ReDrawC(Panel p, int[,] ChessCheck)
        {
            Graphics g = p.CreateGraphics();
            for (int i = 0; i < ChessCheck.GetLength(0); i++)
                for (int j = 0; j < ChessCheck.GetLength(1); j++)
                {
                    //MessageBox.Show("ReDrawC", "信息提示!", MessageBoxButtons.OK);
                    int type=ChessCheck[i, j] ;
                    if (type!= 0)
                    {
                        //确定棋子的中心位置
                        int x2 = i * MainSize.CBGap + 20 - 17;
                        int y2 = j * MainSize.CBGap + 20 - 17;
                        if (type==1)
                        {
                            g.FillEllipse(new SolidBrush(Color.Black), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
                        }
                        else
                        {
                            g.FillEllipse(new SolidBrush(Color.White), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
                        }
                    }
                }
        }
    }
}

【8】两panel的Paint事件(画画监听事件,当图像需要重新“画”的时候执行)——分别画棋盘和操作界面

        //画棋盘
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = panel1.CreateGraphics();
            ChessBoard.DrawCB(g);//重新加载(画)棋盘
            Chess.ReDrawC(panel1, ChessCheck);//重新加载(画)棋子。
        }

        //设置游戏控制界面的大小
        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            panel2.Size = new Size(MainSize.Wid - MainSize.CBWid-20, MainSize.Hei);
        }

【9】三个按钮监听事件的实现(开始游戏,重置游戏,退出游戏)

        //按开始键后的结果
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "游戏开始";
            start = true;
            button1.Enabled = false;
            button2.Enabled = true;
        }

        //按重置后的结果
        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要重新开始?", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                label1.Text = "游戏未开始";
                start = false;
                button1.Enabled = true;
                button2.Enabled = false;
                InitializeThis();
            }
        }

        //退出程序
        private void button3_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

【10】当鼠标点击的时候加载棋子(根据鼠标点击位置——画单个棋子)

        //根据鼠标点击的位置画棋子
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //把棋盘分为【15,15】的数组

            if (start)
            {
                int x1 = (e.X) / MainSize.CBGap;

                int y1 = (e.Y) / MainSize.CBGap;

                try
                {
                    //判断此位置是否为空
                    if (ChessCheck[x1, y1] != 0)
                    {
                        return;//已经有棋子占领这个位置了
                    }

                //下黑子还是白子

                if (type)
                {
                    ChessCheck[x1, y1] = 1;
                }
                else
                {
                    ChessCheck[x1, y1] = 2;
                }

                //画棋子

                Chess.DrawC(panel1, type, e);

                //换颜色

                type = !type;

                }
                catch (Exception)
                {
                    //防止因鼠标点击边界,而导致数组越界,进而运行中断。
                }

                //判断是否胜利

                if (IsFull(ChessCheck) && !BlackVictory(ChessCheck) && !WhiteVictory(ChessCheck))
                {
                    MessageBox.Show("平局");
                    InitializeThis();
                    label1.Text = "游戏尚未开始!";
                }
                if (BlackVictory(ChessCheck))
                {
                    MessageBox.Show("黑方胜利(Black Win)");
                    InitializeThis();
                    label1.Text = "游戏尚未开始!";
                }
                if (WhiteVictory(ChessCheck))
                {
                    MessageBox.Show("白方胜利(White Win)");
                    InitializeThis();
                    label1.Text = "游戏尚未开始!";
                }
            }
            else
            {
                MessageBox.Show("请先开始游戏!", "提示信息!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

【11】判断棋盘是否下满的函数

        //是否满格
        public bool IsFull(int[,] ChessCheck)
        {
            bool full = true;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (ChessCheck[i, j] == 0)
                        return full = false;
                }
            }
            return full;
        }

【12】判断黑棋是否胜利的函数

        //是否黑子胜利
        public bool BlackVictory( int[,] ChessBack)
        {
            bool Win = false;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (ChessCheck[i,j]!=0)
                    {
                        //纵向判断
                        if (j<11)
                        {
                            if (ChessCheck[i, j] == 1 && ChessCheck[i, j + 1] == 1 && ChessCheck[i, j + 2] == 1 && ChessCheck[i, j + 3] == 1 && ChessCheck[i, j + 4] == 1)
                            {
                                return Win = true;
                            }
                        }
                        //横向判断
                        if (i<11)
                        {
                            if (ChessCheck[i, j] == 1 && ChessCheck[i + 4, j] == 1 && ChessCheck[i + 1, j] == 1 && ChessCheck[i + 2, j] == 1 && ChessCheck[i + 3, j] == 1)
                            {
                                return Win = true;
                            }
                        }
                        //斜向右下判断
                        if (i<11&&j<11)
                        {
                            if (ChessCheck[i, j] == 1 && ChessCheck[i + 1, j + 1] == 1 && ChessCheck[i + 2, j + 2] == 1 && ChessCheck[i + 3, j + 3] == 1 && ChessCheck[i + 4, j + 4] == 1)
                            {
                                return Win = true;
                            }
                        }
                        //斜向左下判断
                        if (i>=4&&j<11)
                        {
                            if (ChessCheck[i, j] == 1 && ChessCheck[i - 1, j + 1] == 1 && ChessCheck[i - 2, j + 2] == 1 && ChessCheck[i - 3, j + 3] == 1 && ChessCheck[i - 4, j + 4] == 1)
                            {
                                return Win = true;
                            }
                        }
                    }
                }
            }
            return Win;
        }

【13】判断白棋是否胜利的函数——代码类似上面

        //是否白子赢,代码同上
        public bool WhiteVictory( int[,] ChessBack)
        {
            bool Win = false;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (ChessCheck[i, j] != 0)
                    {
                        if (j <11)
                        {
                            if (ChessCheck[i, j] == 2 && ChessCheck[i, j + 1] == 2 && ChessCheck[i, j + 2] == 2 && ChessCheck[i, j + 3] == 2 && ChessCheck[i, j + 4] == 2)
                            {
                                return Win = true;
                            }
                        }
                        if(i<11)
                        {
                            if (ChessCheck[i, j] == 2 && ChessCheck[i + 4, j] == 2 && ChessCheck[i + 1, j] == 2 && ChessCheck[i + 2, j] == 2 && ChessCheck[i + 3, j] == 2)
                            {
                                return Win = true;
                            }
                        }
                        if (i<11&&j<11)
                        {
                            if (ChessCheck[i, j] == 2 && ChessCheck[i + 1, j + 1] == 2 && ChessCheck[i + 2, j + 2] == 2 && ChessCheck[i + 3, j + 3] == 2 && ChessCheck[i + 4, j + 4] == 2)
                            {
                                return Win = true;
                            }
                           
                           
                        }
                        if(i>=4&&j<11)
                        if (ChessCheck[i, j] == 2 && ChessCheck[i - 1, j + 1] == 2 && ChessCheck[i - 2, j + 2] == 2 && ChessCheck[i - 3, j +3] == 2 && ChessCheck[i - 4, j +4] == 2)
                        {
                            return Win = true;
                        }
                    }
                }
            }
            return Win;
        }

三、实验环境

四、运行效果

五、源码——代码只是辅助学习,望不要照贴代码运行交作业喔!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace 五子棋Forms
{
    //以下是MainSize类

    class MainSize
    {
        /*
        //主框体大小
        public static int Wid { get { return 550; } }
        public static int Hei { get { return 500; } }
        //棋盘大小
        public static int CBWid { get { return 440; } }
        public static int CBHei { get { return 440; } }
        //棋盘宽度
        public static int CBGap { get { return 40; } }
        //棋子直径
        public static int ChessRadious
        {
            get { return 34; }
        }
        */
        //主框体大小
        public static int Wid { get { return 710; } }
        public static int Hei { get { return 640; } }
        //棋盘大小
        public static int CBWid { get { return 600; } }
        public static int CBHei { get { return 600; } }
        //棋盘宽度
        public static int CBGap { get { return 40; } }
        //棋子直径
        public static int ChessRadious
        {
            get { return 34; }
        }
    }
}

namespace 五子棋Forms
{
    //以下是ChessBoard类
    class ChessBoard
    {
        public static void DrawCB(Graphics g)
        {
            int num = MainSize.CBWid / MainSize.CBGap - 1;
            int gap = MainSize.CBGap;
            g.Clear(Color.Gold);
            for (int i = 0; i < num + 1; i++)
            {
                g.DrawLine(new Pen(Color.Black), 20, 20 + i * gap, 20 + num * gap, 20 + i * gap);
                g.DrawLine(new Pen(Color.Black), 20 + gap * i, 20, 20 + i * gap, 20 + gap * num);
            }
        }
    }
}

namespace 五子棋Forms
{
    //以下是Chess类
    class Chess
    {
        public static void DrawC(Panel p, bool type, MouseEventArgs e)
        {
            Graphics g = p.CreateGraphics();
            //确定棋子的中心位置
            int x1 = (e.X) / MainSize.CBGap;
            int x2 = x1 * MainSize.CBGap + 20 - 17;
            int y1 = (e.Y) / MainSize.CBGap;
            int y2 = y1 * MainSize.CBGap + 20 - 17;
            if (type)
            {
                g.FillEllipse(new SolidBrush(Color.Black), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
            }
            else
            {
                g.FillEllipse(new SolidBrush(Color.White), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
            }
        }

        //当界面被重新聚焦的时候,把棋盘上的棋子重新加载(画)出来
        public static void ReDrawC(Panel p, int[,] ChessCheck)
        {
            Graphics g = p.CreateGraphics();
            for (int i = 0; i < ChessCheck.GetLength(0); i++)
                for (int j = 0; j < ChessCheck.GetLength(1); j++)
                {
                    //MessageBox.Show("ReDrawC", "信息提示!", MessageBoxButtons.OK);
                    int type=ChessCheck[i, j] ;
                    if (type!= 0)
                    {
                        //确定棋子的中心位置
                        int x2 = i * MainSize.CBGap + 20 - 17;
                        int y2 = j * MainSize.CBGap + 20 - 17;
                        if (type==1)
                        {
                            g.FillEllipse(new SolidBrush(Color.Black), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
                        }
                        else
                        {
                            g.FillEllipse(new SolidBrush(Color.White), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
                        }
                    }
                }
        }
    }
}

namespace 五子棋Forms
{
    public partial class Form1 : Form
    {
        //游戏是否开始
        private bool start;
        //下的是黑子还是白子
        private bool type = true;
        //棋盘大小
        private const int size = 15;
        //是否为空,空为0,不为空为1、2
        //private int[,] ChessCheck = new int[11, 11];
        private int[,] ChessCheck = new int[size, size];

        public Form1()
        {
            InitializeComponent();
        }

        //设置游戏窗体大小
        private void Form1_Load(object sender, EventArgs e)
        {
            start = false;
            button1.Enabled = true;
            button2.Enabled = false;
            this.Width = MainSize.Wid;
            this.Height = MainSize.Hei;
            this.Location = new Point(260, 75);
        }


        //初始化
        private void InitializeThis()
        {
            for (int i = 0; i < size; i++)
                for (int j = 0; j < size; j++)
                    ChessCheck[i, j] = 0;
            start = false;
            this.panel1.Invalidate();
            type = true;
        }

        //画棋盘
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = panel1.CreateGraphics();
            ChessBoard.DrawCB(g);//重新加载(画)棋盘
            Chess.ReDrawC(panel1, ChessCheck);//重新加载(画)棋子。
        }

        //设置游戏控制界面的大小
        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            panel2.Size = new Size(MainSize.Wid - MainSize.CBWid-20, MainSize.Hei);
        }
        
        //按开始键后的结果
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "游戏开始";
            start = true;
            button1.Enabled = false;
            button2.Enabled = true;
        }

        //按重置后的结果
        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要重新开始?", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                label1.Text = "游戏未开始";
                start = false;
                button1.Enabled = true;
                button2.Enabled = false;
                InitializeThis();
            }
        }

        //退出程序
        private void button3_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        //根据鼠标点击的位置画棋子
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //把棋盘分为【15,15】的数组

            if (start)
            {
                int x1 = (e.X) / MainSize.CBGap;

                int y1 = (e.Y) / MainSize.CBGap;

                try
                {
                    //判断此位置是否为空
                    if (ChessCheck[x1, y1] != 0)
                    {
                        return;//已经有棋子占领这个位置了
                    }

                //下黑子还是白子

                if (type)
                {
                    ChessCheck[x1, y1] = 1;
                }
                else
                {
                    ChessCheck[x1, y1] = 2;
                }

                //画棋子

                Chess.DrawC(panel1, type, e);

                //换颜色

                type = !type;

                }
                catch (Exception)
                {
                    //防止因鼠标点击边界,而导致数组越界,进而运行中断。
                }

                //判断是否胜利

                if (IsFull(ChessCheck) && !BlackVictory(ChessCheck) && !WhiteVictory(ChessCheck))
                {
                    MessageBox.Show("平局");
                    InitializeThis();
                    label1.Text = "游戏尚未开始!";
                }
                if (BlackVictory(ChessCheck))
                {
                    MessageBox.Show("黑方胜利(Black Win)");
                    InitializeThis();
                    label1.Text = "游戏尚未开始!";
                }
                if (WhiteVictory(ChessCheck))
                {
                    MessageBox.Show("白方胜利(White Win)");
                    InitializeThis();
                    label1.Text = "游戏尚未开始!";
                }
            }
            else
            {
                MessageBox.Show("请先开始游戏!", "提示信息!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //是否满格
        public bool IsFull(int[,] ChessCheck)
        {
            bool full = true;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (ChessCheck[i, j] == 0)
                        return full = false;
                }
            }
            return full;
        }

        //是否黑子胜利
        public bool BlackVictory( int[,] ChessBack)
        {
            bool Win = false;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (ChessCheck[i,j]!=0)
                    {
                        //纵向判断
                        if (j<11)
                        {
                            if (ChessCheck[i, j] == 1 && ChessCheck[i, j + 1] == 1 && ChessCheck[i, j + 2] == 1 && ChessCheck[i, j + 3] == 1 && ChessCheck[i, j + 4] == 1)
                            {
                                return Win = true;
                            }
                        }
                        //横向判断
                        if (i<11)
                        {
                            if (ChessCheck[i, j] == 1 && ChessCheck[i + 4, j] == 1 && ChessCheck[i + 1, j] == 1 && ChessCheck[i + 2, j] == 1 && ChessCheck[i + 3, j] == 1)
                            {
                                return Win = true;
                            }
                        }
                        //斜向右下判断
                        if (i<11&&j<11)
                        {
                            if (ChessCheck[i, j] == 1 && ChessCheck[i + 1, j + 1] == 1 && ChessCheck[i + 2, j + 2] == 1 && ChessCheck[i + 3, j + 3] == 1 && ChessCheck[i + 4, j + 4] == 1)
                            {
                                return Win = true;
                            }
                        }
                        //斜向左下判断
                        if (i>=4&&j<11)
                        {
                            if (ChessCheck[i, j] == 1 && ChessCheck[i - 1, j + 1] == 1 && ChessCheck[i - 2, j + 2] == 1 && ChessCheck[i - 3, j + 3] == 1 && ChessCheck[i - 4, j + 4] == 1)
                            {
                                return Win = true;
                            }
                        }
                    }
                }
            }
            return Win;
        }

        //是否白子赢,代码同上
        public bool WhiteVictory( int[,] ChessBack)
        {
            bool Win = false;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (ChessCheck[i, j] != 0)
                    {
                        if (j <11)
                        {
                            if (ChessCheck[i, j] == 2 && ChessCheck[i, j + 1] == 2 && ChessCheck[i, j + 2] == 2 && ChessCheck[i, j + 3] == 2 && ChessCheck[i, j + 4] == 2)
                            {
                                return Win = true;
                            }
                        }
                        if(i<11)
                        {
                            if (ChessCheck[i, j] == 2 && ChessCheck[i + 4, j] == 2 && ChessCheck[i + 1, j] == 2 && ChessCheck[i + 2, j] == 2 && ChessCheck[i + 3, j] == 2)
                            {
                                return Win = true;
                            }
                        }
                        if (i<11&&j<11)
                        {
                            if (ChessCheck[i, j] == 2 && ChessCheck[i + 1, j + 1] == 2 && ChessCheck[i + 2, j + 2] == 2 && ChessCheck[i + 3, j + 3] == 2 && ChessCheck[i + 4, j + 4] == 2)
                            {
                                return Win = true;
                            }
                           
                           
                        }
                        if(i>=4&&j<11)
                        if (ChessCheck[i, j] == 2 && ChessCheck[i - 1, j + 1] == 2 && ChessCheck[i - 2, j + 2] == 2 && ChessCheck[i - 3, j +3] == 2 && ChessCheck[i - 4, j +4] == 2)
                        {
                            return Win = true;
                        }
                    }
                }
            }
            return Win;
        }

    }
}

六、说明——代码改自:https://blog.csdn.net/qq_41007523/article/details/80813180

qq_41007523大神的博客

此博主代码有些小问题,所以我改进了下。呸,瞎改了下。

七、实验感想

我好菜啊!!!

猜你喜欢

转载自blog.csdn.net/lfy905805357/article/details/81123656
今日推荐