C#开发的五子棋小游戏源码

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

namespace QiLeiGame
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            myImage = new Bitmap(panel1.Width, panel1.Height);//画图
            //luoZhiThread = new Thread(luoZhi);
            //luoZhiThread.Start();
        }


        private Image myImage;
        Thread luoZhiThread;

        private bool gameStart=false;

        /// <summary>
        /// 初始化背景数组
        /// int[x,y] x为行 y为列
        /// </summary>
        private int[,] bgGround = new int[11, 11]; 


        private int CurrentX;//当前bgGround的x行

        private int CurrentY;//当前bgGround的y列

        private bool IsWhite = false;//判断白棋还是黑棋先

        private bool IsOver = false;//记录游戏是否结束

        protected override void OnPaint(PaintEventArgs e)
        {
            Draw();
            base.OnPaint(e);
        }

        /// <summary>
        /// 画棋盘
        /// </summary>
        private void Draw()
        {
            Graphics g = Graphics.FromImage(myImage);
            g.Clear(this.BackColor);
            g.FillRectangle(Brushes.White, new Rectangle(new Point(10, 10), new Size(400, 400)));
            //循环次数应比背景bgGround行、列少1
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    g.DrawRectangle(new Pen(Brushes.Black), i * 40 + 10, j * 40 + 10, 40, 40);
                }
            }

            Graphics gg = panel1.CreateGraphics();
            gg.DrawImage(myImage, 0, 0);
        }

        /// <summary>
        /// 判断输赢
        /// </summary>
        /// <param name="flag">1为白棋 2为黑棋</param>
        private void JudgeResult(int flag)
        {
            int x = CurrentX;
            int y = CurrentY;
            int MinXNum = 0;
            int MaxXNum = 0;
            int count = 0;
            if (x > 4)
            {
                MinXNum = x - 4;
                if (x + 4 > 10)
                {
                    MaxXNum = 10;
                }
                else
                {
                    MaxXNum = x + 4;
                }
            }
            else
            {
                MaxXNum = x + 4;
            }
            int MinYNum = 0;
            int MaxYNum = 0;
            if (y > 4)
            {
                MinYNum = y - 4;
                if (y + 4 > 10)
                {
                    MaxYNum = 10;
                }
                else
                {
                    MaxYNum = y + 4;
                }
            }
            else
            {
                MaxYNum = y + 4;
            }
            #region //横向
            for (int i = MinXNum; i < MaxXNum + 1; i++)
            {
                if (bgGround[i, y] == flag)
                {
                    count++;
                    if (count > 4)
                        goto Label;
                }
                else
                {
                    count = 0;
                    if (i > MaxXNum - 4)
                        break;
                }
            }
            #endregion
            #region //竖向
            for (int i = MinYNum; i < MaxYNum + 1; i++)
            {
                if (bgGround[x, i] == flag)
                    count++;
                else
                {
                    count = 0;
                    if (i > MaxYNum - 4)
                        break;
                }
                if (count > 4)
                    goto Label;
            }
            #endregion
            //左斜
            for (int i = MinXNum; i < MaxXNum + 1; i++)
            {
                if (CurrentX + CurrentY - i < 0)
                    break;
                if (CurrentX + CurrentY - i <= 10)
                {
                    if (bgGround[i, CurrentX + CurrentY - i] == flag)
                    {
                        count++;
                    }
                    else
                    {
                        count = 0;
                        if (i > MaxYNum - 4)
                            break;
                    }
                }
                if (count > 4)
                    goto Label;
            }
            //右斜
            for (int i = MinXNum; i < MaxXNum + 1; i++)
            {
                if (i < CurrentX - CurrentY)
                    break;
                if (i + CurrentY - CurrentX > 10)
                    break;
                if (bgGround[i, i + CurrentY - CurrentX] == flag)
                {
                    count++;
                }
                else
                {
                    count = 0;
                    if (i > MaxYNum - 4)
                        break;
                }
                if (count > 4)
                    goto Label;
            }


            Label:
            if (flag == 1 && count > 4)
            {
                IsOver = true;
                MessageBox.Show("白棋赢,游戏结束");
                return;
            }
            else if (flag == 2 && count > 4)
            {
                IsOver = true;
                MessageBox.Show("黑棋赢,游戏结束");
                return;
            }
            else
            {
                IsOver = false;
            }
        }

        /// <summary>
        /// 画棋子
        /// </summary>
        /// <param name="g"></param>
        /// <param name="x">bgGround中x位置</param>
        /// <param name="y">bgGround中y位置</param>
        /// <param name="p">画笔</param>
        /// <param name="brush">棋子颜色</param>
        /// <param name="flag">1为白棋 2为黑棋</param>
        private void DrawChess(Graphics g, int x, int y, Pen p, Brush brush, int flag)
        {
            CurrentX = x; CurrentY = y;
            bgGround[x, y] = flag;
            g.DrawEllipse(p, x * 40, y * 40, 20, 20);
            g.FillEllipse(brush, x * 40, y * 40, 20, 20);

        }
        private void button_black_first_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string tag = btn.Text.ToString();
            if (tag.Contains("黑"))
            {
                IsWhite = false;
            }
            else//黑棋先 
            {
                IsWhite = true;
            }

        }

        private void button_reset_Click(object sender, EventArgs e)
        {
            IsOver = false;
            Draw();
            bgGround = new int[11, 11];
        }

        private void button_white_first_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string tag = btn.Text.ToString();
            if (tag.Contains("白"))//白棋先
            {
                IsWhite = true;
            }
            else//黑棋先 tag=2
            {
                IsWhite = false;
            }
        }

        bool luozhiBool=false;


        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            int x = (e.X - 10) % 40;
            int y = (e.Y - 10) % 40;
            x = e.X;
            y = e.Y;

            if (x > 30)
            {
                x = (e.X - 10) / 40 + 1;
            }
            else
            {
                x = (e.X - 10) / 40;
            }
            if (y > 30)
            {
                y = (e.Y - 10) / 40 + 1;
            }
            else
            {
                y = (e.Y - 10) / 40;
            }

            textBox1.Text = x.ToString();
            textBox2.Text = y.ToString();
            Graphics g = panel1.CreateGraphics();
            if(gameStart)
            {
                if (luozhiBool)
                {
                    if (IsWhite)
                    {
                        DrawChess(g, x, y, new Pen(Brushes.White), Brushes.White, 1);
                        IsWhite = false;
                        JudgeResult(1);
                    }
                    else
                    {
                        DrawChess(g, x, y, new Pen(Brushes.Black), Brushes.Black, 2);
                        IsWhite = true;
                        JudgeResult(2);
                    }
                    luozhiBool = false;
                }
            }   
            

        }

  
 

        private void button_start_Click(object sender, EventArgs e)
        {
            gameStart = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if(luoZhiThread.IsAlive)
            //{
            //    luoZhiThread.Abort();
            //}
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            luozhiBool = true;
        }
    }
}
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的 C# 实现五子棋游戏的示例代码。 首先,在窗体上添加一个 PictureBox 控件,用于绘制游戏棋盘。 ```csharp public partial class Form1 : Form { private const int ROWS = 15; // 棋盘行数 private const int COLS = 15; // 棋盘列数 private const int CELL_SIZE = 30; // 每个格子的大小 private int[,] board; // 棋盘数组,0表示空格,1表示黑棋,2表示白棋 private bool isBlackTurn; // 当前轮到黑棋或白棋下 public Form1() { InitializeComponent(); board = new int[ROWS, COLS]; isBlackTurn = true; // 初始化棋盘数组 for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i, j] = 0; } } // 设置PictureBox控件的大小和背景色 pictureBox1.Width = ROWS * CELL_SIZE; pictureBox1.Height = COLS * CELL_SIZE; pictureBox1.BackColor = Color.FromArgb(255, 225, 158); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { // 绘制棋盘 Pen pen = new Pen(Color.Black, 2); for (int i = 0; i <= ROWS; i++) { e.Graphics.DrawLine(pen, i * CELL_SIZE, 0, i * CELL_SIZE, ROWS * CELL_SIZE); } for (int j = 0; j <= COLS; j++) { e.Graphics.DrawLine(pen, 0, j * CELL_SIZE, COLS * CELL_SIZE, j * CELL_SIZE); } // 绘制棋子 for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (board[i, j] == 1) { e.Graphics.FillEllipse(Brushes.Black, i * CELL_SIZE - CELL_SIZE / 2, j * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE); } else if (board[i, j] == 2) { e.Graphics.FillEllipse(Brushes.White, i * CELL_SIZE - CELL_SIZE / 2, j * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE); } } } } private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { int x = e.X / CELL_SIZE; int y = e.Y / CELL_SIZE; if (x >= 0 && x < ROWS && y >= 0 && y < COLS && board[x, y] == 0) { if (isBlackTurn) { board[x, y] = 1; } else { board[x, y] = 2; } isBlackTurn = !isBlackTurn; pictureBox1.Invalidate(); // 重新绘制PictureBox控件 } } } ``` 在窗体设计器中,将 PictureBox 控件的 Paint 事件和 MouseClick 事件分别绑定到 pictureBox1_Paint 和 pictureBox1_MouseClick 方法。 这样就完成了一个简单的五子棋游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值