在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 五子棋 双人对战 游戏源码下载

C# 五子棋 双人对战 游戏源码下载

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:5.33M
  • 下载次数:60
  • 浏览次数:948
  • 发布时间:2017-12-02
  • 实例类别:C#语言基础
  • 发 布 人:promise魅眸
  • 文件格式:.rar
  • 所需积分:5
 相关标签: 五子棋

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace frmGobang
{
    public partial class frmMian : Form
    {
        public frmMian()
        {
            InitializeComponent();
        }
        //定义模子的颜色,1是黑子,2为白子
        int qizi = 1;
        //设置控件是否可用状态
        bool flat = false;
        bool qiziFalt=true;

        //棋子名称
        string name;
        //定义二维数组存放棋子
        int[,] array = new int[17, 17];

        //记录黑,白棋子的坐标
        Point hei = Point.Empty;
        Point bai = Point.Empty;
        
        /// <summary>
        /// 绘制五子棋盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMian_Paint(object sender, PaintEventArgs e)
        {
            //绘制一个绘图画面
            Graphics tu = this.CreateGraphics();

            //定义线条的颜色和粗细
            Pen pen = new Pen(Color.Black, 2);
            for (int i = 0; i < 16; i  )
            {
                //定义线线之间的距离为30
                Point pt1 = new Point(30, 30 * i);      //表示要连接的第一个点
                Point pt2 = new Point(450, 30 * i);     //表示要连接的第二个点
                tu.DrawLine(pen, pt1, pt2);             //绘制连接线
            }
            for (int i = 0; i < 16; i  )
            {
                Point pt1=new Point (30 * i,30);
                Point pt2=new Point (30 * i,450);
                tu.DrawLine(pen,pt1,pt2);
            }
            
            //有棋子后,绘制棋子的图案
            for (int i = 0; i < 16; i  )
			{
			    for (int j = 0; j < 16; j  )
			    {
			        //定义单色画笔。 画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径
                    SolidBrush sb;
                    Rectangle rec=new Rectangle (i*30-10,j*30-10,20,20);
                    //当前棋子为黑子
                    if(array[i,j]==1)
                    {
                        sb=new SolidBrush (Color.Black);
                        tu.FillEllipse(sb,rec);
                    }
                    //当前棋子为白子
                    else if(array[i,j]==2)
                    {
                        sb = new SolidBrush(Color.White);
                        tu.FillEllipse(sb,rec);
                    }
			    }
			}
            //释放资源
           tu.Dispose();
           
        }

        /// <summary>
        /// 退出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiExit_Click(object sender, EventArgs e)
        {
            DialogResult reault = MessageBox.Show("确定要退出游戏吗?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (reault == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        [DllImport("user32.dll")]
        private static extern int GetSystemMetrics(int nIndex);
        private static int i, y;
        [DllImport("user32.dll")]
        private static extern int SetCursorPos(int x, int y);
        [DllImport("user32.dll")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        Random rnd = new Random();
        /// <summary>
        /// 此处慎入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiShiRu_Click(object sender, EventArgs e)
        {
            tTime.Enabled = true;
            pTime.Visible = true;
            lblDianboTishi.Text = "你现在只能点击骷髅了啦!!";
            lblzui.Visible = false;
            lblqizi.Visible = false;
            pand.Visible = true;
            flat = false;
            pirPian.Focus();
            msGobang.Enabled = false;
            NewChong();

           
        }

        int zouCount = 0;

        /// <summary>
        /// 鼠标释放时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMian_MouseDown(object sender, MouseEventArgs e)
        {
            
            //判断是否选择开始游戏
            if (!flat)
            {
                MessageBox.Show("请选择开始游戏","山治提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

           
            //提到坐标的位置
            int x1 = e.X % 30;
            int x2 = e.X / 30;

            //不绘制任何图形
            if (x1 > 10 && x1 < 20)
            {
                return;
            }
            else if (x1 >= 20)
            {
                x2  ;
                
            }
          
            //绘制另一个点
            int liX = x2 * 30;
            //计算图形Y的坐标
            int y1 = e.Y % 30;
            int y2 = e.Y / 30;
            if (y1 > 10 && y1 < 20)
            {
                return;
            }
            else if (y1 >= 20)
            {
                y2  ;
            }

            //获得坐标
            int liY = y2 * 30;
            //记录上一个位置是下棋子 的位置
            if (qizi == 1)
            {
                hei = new Point(x2, y2);
            }
            else
            {
                bai = new Point(x2, y2);
            }
            if (x2 >= 17 || y2 >= 17)
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //判断当前是否已经存在棋子了‘
            if (array[x2, y2] > 0)
            {
                MessageBox.Show("此处已有棋子,不可作弊哟!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                return;
            }
            else
            {
                //将棋子下
                array[x2, y2] = qizi;
                zouCount  ;
                lblzui.Visible = true;
                lblqizi.Visible = true;
                tTime.Stop();
                tTime.Enabled = false;
                pTime.Visible = false;
            }
            int r = 10;
            Rectangle gle = new Rectangle(liX - r, liY - r, r * 2, r * 2);
            SolidBrush sb = new SolidBrush(Color.Black);
            if (qizi == 2)
            {
                sb = new SolidBrush(Color.White);
            }
            Graphics gh = this.CreateGraphics();
            gh.FillEllipse(sb, gle);
            gh.Dispose();


            int count = 1;
            if (qizi == 1)
            {
                name = "黑棋";
                lblqizi.Text = "下棋方:白棋";
                lblzui.Text = "准备方:黑棋";
            }
            else if (qizi == 2)
            {
                name = "白棋";

                lblqizi.Text = "下棋方:黑棋";
                lblzui.Text = "准备方:白棋";
            }

            //水平判断
            int xcount=x2;
            int ycount=y2;
            while(array[--xcount,ycount]==qizi)
            {
                count  ;
            }
           
            xcount=x2;
            if (x2 >= 17 || y2 >= 17||xcount >=17)
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                while (array[  xcount, ycount] == qizi)
                {
                    count  ;
                }

                if (count == 5)
                {
                    MessageBox.Show(name   "获胜,游戏结束", "索隆提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    NewChong();

                }
            }
            catch
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            //垂直方向判断 
            xcount = x2;
            ycount = y2;
            count = 1;
            while (array[xcount, --ycount] == qizi) 
            {
                count  ;
            }
            ycount = y2;
            if (x2 >= 17 || y2 >= 17||ycount>=17)
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try 
            { 
                while(array[xcount,  ycount]==qizi)
                {
                    count  ;
                }
                if (count == 5)
                {
                    MessageBox.Show(name   "获胜,游戏结束", "罗宾提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    NewChong();
                }
            }
            catch
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //左上至右下
            xcount = x2;
            ycount = y2;
            count = 1;
            while (array[--xcount, --ycount] == qizi)
            {
                count  ;
            }
            xcount = x2;
            ycount = y2;
            if (x2 >= 17 || y2 >= 17||xcount>=17||ycount>=17)
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try 
            { 
                while (array[  xcount,   ycount] == qizi)
                {
                    count  ;
                }
                if (count == 5)
                {
                    MessageBox.Show(name   "获胜,游戏结束", "乔巴提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    NewChong();
                }
                }
            catch
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //右上至左下
            xcount = x2;
            ycount = y2;
            count = 1;
            try { 
            while (array[  xcount, --ycount] == qizi)
            {
                count  ;
            }
            }
            catch
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            xcount = x2;
            ycount = y2;
            if (x2 >= 17 || y2 >= 17)
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try 
            { 
                while (array[--xcount,   ycount] == qizi)
                {
                    count  ;
                }
                if (count == 5)
                {
                    MessageBox.Show(name   "获胜,游戏结束", "弗兰奇提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    NewChong();
                }
            }
            catch
            {
                MessageBox.Show("不可以乱点的哟!!!", "布鲁克提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
          
            //更换棋子
            if (qizi == 1)
                qizi = 2;
            else
                qizi = 1;
            if (zouCount >= 224)
            {
                MessageBox.Show("平局", "弗兰奇提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                NewChong();
            }
           
        }

        /// <summary>
        /// 定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tTime_Tick(object sender, EventArgs e)
        {
            lblDianboTishi.Left -= 1;
            if (lblDianboTishi.Right == 0)
            {
                lblDianboTishi.Location = new Point(281, 9);
            }
        }

        /// <summary>
        /// 开始游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiDouble_Click(object sender, EventArgs e)
        {
            
           
        }

        /// <summary>
        /// 重新开始游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiMan_Click(object sender, EventArgs e)
        {
            NewChong();
            //设置棋盘重新开始
            flat = true;

            tTime.Enabled = true;
            pTime.Visible = true;
        }

        private void NewChong()
        {
            //重新设置棋子数组
            array = new int[17, 17];
            Rectangle rec = new Rectangle(20, 20, 450, 450);
            //使控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向控件发送绘制消息
            this.Invalidate(rec);
            flat = false;
        }

        /// <summary>
        /// 悔棋
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiHuiQi_Click(object sender, EventArgs e)
        {
            int count = 0;
            if (count > 2)
            {
                MessageBox.Show("亲,这是在下棋,不是在后悔哟!!!", "乌索普提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (hei != Point.Empty && bai != Point.Empty&&count<2)
            {
                //判断黑子还是白子先走,再来清棋子,之后设置轮到谁下棋子了
                if (qizi == 1)
                {
                    array[bai.X, bai.Y] = 0;
                    qizi = 2;
                    count  ;
                }
                else if(qizi==2)
                {
                    array[hei.X, hei.Y] = 0;
                    qizi = 1;
                    count  ;
                }
                Rectangle rec = new Rectangle(20, 20, 450, 450);
                //使控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向控件发送绘制消息
                this.Invalidate(rec);
            }
           
        }

        /// <summary>
        /// 认输
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiShu_Click(object sender, EventArgs e)
        {
            if (flat == true)
            {
                DialogResult result = MessageBox.Show("对方即将赢得比赛,您确定要认输吗?", "路飞提醒您", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.OK)
                {
                    if (name == "黑棋")
                    {
                        name = "白棋";
                    }
                    else
                    {
                        name = "黑棋";
                    }

                    MessageBox.Show(name   "赢得比赛!", "娜美公告", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    NewChong();
                    flat = false;
                }
            }
        }

        /// <summary>
        /// 图片事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pirPian_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("您想成为海贼王的男人吗!!!", "海贼王全体成员提醒你", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {



                long tick = 1;
                i = GetSystemMetrics(0);
                y = GetSystemMetrics(1);
                while (true)
                {
                    tick  = rnd.Next(2);
                    if (tick % 643 == 0)
                    {
                        int x, z;
                        int k = 4;
                        while (k-- > 0)
                        {
                            x = rnd.Next(i);
                            z = rnd.Next(y);
                            switch (rnd.Next(3))
                            {
                                case 0:
                                    SetCursorPos(x, z);
                                    break;
                                case 1:
                                    mouse_event(0x2 | 0x4 | 0x8000, x, z, 0, 0);
                                    break;
                                case 2:
                                    mouse_event(0x8 | 0x10 | 0x8000, x, z, 0, 0);
                                    break;
                            }
                            Thread.Sleep(500);

                        }
                    }
                    if (tick % 313 == 0)
                    {
                        switch (rnd.Next(4))
                        {
                            case 0:
                                SystemSounds.Asterisk.Play();
                                break;
                            case 1:
                                SystemSounds.Beep.Play();
                                break;
                            case 2:
                                SystemSounds.Exclamation.Play();
                                break;
                            case 3:
                                SystemSounds.Hand.Play();
                                break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 开始游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiBeg_Click(object sender, EventArgs e)
        {
            if (flat == false)
            {
                flat = true;
                if (qiziFalt)
                    name = "白棋";
                else
                    name = "黑棋";
                tTime.Enabled = true;
                pTime.Visible = true;
                lblDianboTishi.Text = "现在可以开始游戏了!";
            }
        }
        
        /// <summary>
        /// 重新开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiChong_Click(object sender, EventArgs e)
        {
            if (flat == false)
            {
                MessageBox.Show("还没开始呢,请选择开始游戏吧!", "罗宾提醒您", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            NewChong();
            //设置棋盘重新开始
            flat = true;

            tTime.Enabled = true;
            pTime.Visible = true;
            lblDianboTishi.Text = "现在可以开始游戏了!";
        }

        private void pLuang_Click(object sender, EventArgs e)
        {
            MessageBox.Show("哎呀!不要乱点呀,请选择开始游戏吧!", "布鲁克提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

标签: 五子棋

实例下载地址

C# 五子棋 双人对战 游戏源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警