基本信息
源码名称:C# 五子棋 双人对战 游戏源码下载
源码大小:5.33M
文件格式:.rar
开发语言:C#
更新时间:2017-12-02
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 5 元 
   源码介绍

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);
        }
    }
}