五子棋c#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //储存落点列表
        List<Point> list1 = new List<Point>(250);
        //记录棋盘及上面棋子信息
        public int[,] chessboard = new int[15, 15];//0无子1黑色2白色
        public int[] x = new int[250];
        public int[] y = new int[250];
        //判断胜负方法
        public bool judgment(int[,] ChessBoard, int judgment)
        {
            bool win = false;
            //遍历棋盘
            for (int p = 0; p < 15; p++)
            {
                for (int q = 0; q < 15; q++)
                {
                    if (ChessBoard[p, q] == judgment)
                    {
                        //水平向右
                        if (p < 11)
                        {
                            if (ChessBoard[p + 1, q] == judgment &&
                                ChessBoard[p + 2, q] == judgment &&
                                ChessBoard[p + 3, q] == judgment &&
                                ChessBoard[p + 4, q] == judgment)
                            { win = true; }
                        }
                        //水平向左
                        if (p > 3)
                        {
                            if (ChessBoard[p - 1, q] == judgment &&
                                ChessBoard[p - 2, q] == judgment &&
                                ChessBoard[p - 3, q] == judgment &&
                                ChessBoard[p - 4, q] == judgment)
                            { win = true; }
                        }
                        //垂直向上
                        if (q > 3)
                        {
                            if (ChessBoard[p , q-1] == judgment &&
                                ChessBoard[p , q-2] == judgment &&
                                ChessBoard[p , q-3] == judgment &&
                                ChessBoard[p , q-4] == judgment)
                            { win = true; }
                        }
                        //垂直向下
                        if (q < 11)
                        {
                            if (ChessBoard[p, q + 1] == judgment &&
                                ChessBoard[p, q + 2] == judgment &&
                                ChessBoard[p, q + 3] == judgment &&
                                ChessBoard[p, q + 4] == judgment)
                            { win = true; }
                        }
                        //斜向右上
                        if (q < 11 && p < 11)
                        {
                            if (ChessBoard[p + 1, q + 1] == judgment &&
                                ChessBoard[p + 2, q + 2] == judgment &&
                                ChessBoard[p + 3, q + 3] == judgment &&
                                ChessBoard[p + 4, q + 4] == judgment)
                            { win = true; }
                        }
                        //斜向左下
                        if (p > 3 && q > 3)
                        {
                            if (ChessBoard[p - 1, q - 1] == judgment &&
                               ChessBoard[p - 2, q - 2] == judgment &&
                               ChessBoard[p - 3, q - 3] == judgment &&
                               ChessBoard[p - 4, q - 4] == judgment)
                            { win = true; }
                        }
                        //斜向右下
                        if (q > 3 && p < 11)
                        {
                            if (ChessBoard[p + 1,q - 1]==judgment&&
                                ChessBoard[p + 2,q - 2]==judgment&&
                                ChessBoard[p + 3,q - 3]==judgment&&
                                ChessBoard[p + 4, q - 4] == judgment)
                            { win = true; }
                        }
                        //斜向左上
                        if (p > 3 && q < 11)
                        {
                            if (ChessBoard[p - 1, q + 1] == judgment &&
                                ChessBoard[p - 2, q + 2] == judgment &&
                                ChessBoard[p - 3, q + 3] == judgment &&
                                ChessBoard[p - 4, q + 4] == judgment)
                            { win = true; }
                        }
                    }
                }

            }
            return win;

        }
        //绘制棋盘与落子
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {

            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Black, 1);
            SolidBrush sb = new SolidBrush(Color.Black);
            SolidBrush sw = new SolidBrush(Color.White);
            //棋盘
            for (int i = 0; i < 15; i++)
            {
                g.DrawLine(pen, 45 + i * 35, 45, 45 + i * 35, 45 + 35 * 14);
                g.DrawLine(pen, 45, 45 + i * 35, 45 + 14 * 35, 45 + i * 35);
            }
            //落子
            if (fupan == false)
            {
                for (int i = 0; i < list1.Count; i++)
                {
                    if (i % 2 == 0)
                    {
                        label1.Text = "请落白子";
                        g.FillEllipse(sb, list1[i].X - 10, list1[i].Y - 10, 20, 20);
                    }
                    else
                    {
                        label1.Text = "请落黑子";
                        g.FillEllipse(sw, list1[i].X - 10, list1[i].Y - 10, 20, 20);
                    }
                }
            }
            else
            {
                for (int i = 0; i < list2.Count; i++)
                {
                    if (i % 2 == 0)
                    {
                        label1.Text = "黑子";
                        g.FillEllipse(sb, list2[i].X - 10, list1[i].Y - 10, 20, 20);
                    }
                    else
                    {
                        label1.Text = "白子";
                        g.FillEllipse(sw, list2[i].X - 10, list1[i].Y - 10, 20, 20);
                    }
                }
            }

        }
        //落子调整
        public Point adjust(Point p)
        {
            Point adjustpoint = new Point(45 + (int)((p.X - 45) / 35.0 + 0.5) * 35,
                45 + (int)((p.Y - 45) / 35.0 + 0.5) * 35);
            return adjustpoint;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
        //鼠标落子判断与储存
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            //判断是否超过边界
            if (45 <= e.X && e.X <= 45 + 35 * 14 && 45 <= e.Y && 45 + 35 * 14 >= e.Y)
            {
                Point p = new Point(e.X, e.Y);
                //重复落子警告
                if (list1.Contains(adjust(p)))
                {
                    MessageBox.Show("不能重复落子!", "警告", MessageBoxButtons.OK);
                }
                else
                {
                    Point p2 = adjust(p);
                    timer2.Start();
                    a = 100;
                    list1.Add(p2);
                    x[list1.Count - 1] = (p2.X - 45) / 35;
                    y[list1.Count - 1] = (p2.Y - 45) / 35;
                    if (list1.Count % 2 == 1)
                        chessboard[x[list1.Count - 1], y[list1.Count - 1]] = 1;
                    else
                        chessboard[x[list1.Count - 1], y[list1.Count - 1]] = 2;
                    //刷新paint
                    pictureBox1.Invalidate();
                    //胜负检测
                    if (list1.Count > 0 && judgment(chessboard,
                        chessboard[x[list1.Count - 1], y[list1.Count - 1]]))
                    {
                        timer2.Stop();
                        if (chessboard[x[list1.Count - 1], y[list1.Count - 1]] == 1)
                        {
                            MessageBox.Show("黑子获胜");
                        }
                        else if (chessboard[x[list1.Count - 1], y[list1.Count - 1]] == 2)
                        {
                            MessageBox.Show("白子获胜");
                        }
                    }
                }

            }
        }

        private void textBox游戏状态_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        //悔棋
        private void button悔棋_Click(object sender, EventArgs e)
        {
            if (list1.Count > 0)
            {
                chessboard[x[list1.Count - 1], y[list1.Count - 1]] = 0;
                list1.RemoveAt(list1.Count - 1);
                a = 100;
                pictureBox1.Invalidate();

            }
            else
            {
                MessageBox.Show("无棋可悔", "提示");
            }
        }
        //复盘
        //初始化
        bool fupan = false;
        List<Point> list2 = new List<Point>(250);
        //点击开始复盘
        private void button复盘_Click(object sender, EventArgs e)
        {
            fupan = true;
            timer1.Enabled = true;
            timer2.Stop();
        }
        //利用计时器ticker开始复盘
        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (list2.Count < list1.Count)
            {
                list2.Add(list1[i]);
                i++;
                pictureBox1.Invalidate();
            }
            else
            {
                timer1.Enabled = false;
                list2.Clear();
                i = 0;
                fupan = false;
                MessageBox.Show("复盘结束", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
        }
        //倒计时
        int a=100;
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (a > 0)
            {
                a--;
                textBox时间.Text = (a / 60).ToString().PadLeft(2, '0') + 
                    ":" + (a % 60).ToString().PadLeft(2, '0');
            }
            else
            {
                timer2.Stop();
                if (chessboard[x[list1.Count - 1], y[list1.Count - 1]] == 1)
                {
                    MessageBox.Show("白子超时,黑子获胜");
                }
                else if (chessboard[x[list1.Count - 1], y[list1.Count - 1]] == 2)
                {
                    MessageBox.Show("黑子超时,白子获胜");
                }
            }
        }

        private void textBox思考时间_TextChanged(object sender, EventArgs e)
        {

        }
        //改变背景色
        private void 改变棋盘颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            pictureBox1.BackColor = colorDialog1.Color;
        }
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的 C# 五子棋游戏示例,你可以参考它来开始编写你自己的五子棋游戏。这个示例使用了 Windows Forms 应用程序。 ``` using System; using System.Drawing; using System.Windows.Forms; namespace FiveInARow { public partial class Form1 : Form { private int[,] board = new int[15, 15]; // 棋盘 private int player = 1; // 玩家,1 表示黑色,2 表示白色 private bool gameOver = false; // 游戏结束标志 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.BackColor = Color.Orange; this.ClientSize = new Size(600, 600); this.Paint += new PaintEventHandler(DrawBoard); this.MouseClick += new MouseEventHandler(PlayChess); } private void DrawBoard(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.Clear(Color.Orange); for (int i = 0; i < 15; i++) { g.DrawLine(Pens.Black, 50, 50 + i * 40, 570, 50 + i * 40); g.DrawLine(Pens.Black, 50 + i * 40, 50, 50 + i * 40, 570); } } private void PlayChess(object sender, MouseEventArgs e) { if (gameOver) { return; } int x = e.X / 40; int y = e.Y / 40; if (board[x, y] != 0) { return; } board[x, y] = player; Graphics g = this.CreateGraphics(); g.FillEllipse(player == 1 ? Brushes.Black : Brushes.White, x * 40 - 16, y * 40 - 16, 32, 32); if (CheckWin(x, y)) { gameOver = true; MessageBox.Show("Player " + player + " Wins!"); } else { player = player == 1 ? 2 : 1; } } private bool CheckWin(int x, int y) { int count = 1; int i, j; // 水平方向 for (i = x - 1; i >= 0 && board[i, y] == player; i--) { count++; } for (i = x + 1; i < 15 && board[i, y] == player; i++) { count++; } if (count >= 5) return true; // 垂直方向 count = 1; for (j = y - 1; j >= 0 && board[x, j] == player; j--) { count++; } for (j = y + 1; j < 15 && board[x, j] == player; j++) { count++; } if (count >= 5) return true; // 左上到右下方向 count = 1; for (i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i, j] == player; i--, j--) { count++; } for (i = x + 1, j = y + 1; i < 15 && j < 15 && board[i, j] == player; i++, j++) { count++; } if (count >= 5) return true; // 右上到左下方向 count = 1; for (i = x + 1, j = y - 1; i < 15 && j >= 0 && board[i, j] == player; i++, j--) { count++; } for (i = x - 1, j = y + 1; i >= 0 && j < 15 && board[i, j] == player; i--, j++) { count++; } if (count >= 5) return true; return false; } } } ``` 这个示例实现了基本的五子棋游戏逻辑,包括绘制棋盘、落子、判断胜负等功能。当然,还有很多可以改进和扩展的地方,比如增加 AI 对手、添加音效、美化界面等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值