C# winform 简单五子棋 200行代码实现人机对战

本文介绍了一个简单的五子棋人机对战程序设计过程,包括功能需求、界面设计及核心算法实现。通过枚举棋盘上的每一个空位并评估其价值来决定最佳落子点,实现了基本的人工智能对战功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、功能需求

接上篇博文,本文描述简单人机对战实现过程,只是简单实现考虑走一步策略,如果要想实现走多步策略,可以在本文估值算法的基础上用极大极小值配合剪枝算法,实现考虑多步策略,这样ai会显得更加聪明,后期如果有时间完善,会更新代码。

2、界面设计

参考上一篇博文的界面。

3、算法描述

其实算法非常简单,毕竟ai部分只有200行代码,所以应该只能算是实现估值函数,即当前局面走一步时最好的位置,不考虑走多步。我的思路如下:

(1)枚举当前局面棋子可能的落点,并给不同的局面赋值,越重要的局面分数会越高

(2)遍历每个可落子点

(3)在可落子点落子时,分四个方向去获得落子前后五个位置并转换成序列,然后检测该序列满足(1)中的情况,并获得一定分数累加存到字典中

(4)对字典进行排序

(5)取得分数最高的点即为最优落子点

当然,这只是考虑一步,但也有不错的棋力,如果要考虑多步,请参考前面提到的极大极小值配合剪枝算法,其实本博文的算法可以当作极大极小值配合剪枝算法的估值函数,然后只做迭代就好了。

4、代码实现

对上一博文的代码有一点小的更新,因此全部贴出代码,可能枚举时有考虑不到的情况,可能有些小问题,不过思想是没有问题的,项目代码如下:

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;


namespace WindowsFormsAppAnimalRegonize
{
    public partial class FormChess : Form
    {
        class Pos
        {
            public int X { get; set; }
            public int Y { get; set; }
            public bool Have_chess { get; set; }
            public int Type { get; set; }
            public Pos(int X, int Y, bool Have_chess, int Type)
            {
                this.X = X;
                this.Y = Y;
                this.Have_chess = Have_chess;
                this.Type = Type;
            }
        }
        private List<Pos> null_chess_pos_list;
        private Stack<Button> cur_chess_btn_list;
        private List<Button> buttons;
        private int down_chess;
        private string[] game_types = {"双人对战","人机对战","联网对战" };
        private int cur_game_type;
        private bool game_over;
        public FormChess()
        {
            InitializeComponent();
            comboBox1.Items.AddRange(game_types);
            comboBox1.SelectedIndex = 0;
            init_score_dict();
        }
        private void init_game()
        {
            switch(comboBox1.SelectedItem.ToString())
            {
                case "双人对战":
                    cur_game_type = 0;
                    break;
                case "人机对战":
                    cur_game_type = 1;
                    break;
                case "联网对战":
                    cur_game_type = 2;
                    break;
            }
            game_over = false;
            //白棋先走,1为白棋2为黑棋
            down_chess = 1;
            if (buttons == null) buttons = new List<Button>();
            if (null_chess_pos_list == null)
            {
                null_chess_pos_list = new List<Pos>();
                for (int x = 0; x < 13; x++)
                {
                    for (int y = 0; y < 13; y++)
                    {
                        new_chess_pos(30 + 50 * x, 30 + 50 * y);
                        var pos = new Pos(x, y, false, 0);
                        null_chess_pos_list.Add(pos);
                    }
                }
            }
            else
            {
                for (int i = 0; i < null_chess_pos_list.Count; i++)
                {
                    null_chess_pos_list[i].Have_chess = false;
                    null_chess_pos_list[i].Type = 0;
                }
            }
            if (cur_chess_btn_list == null) cur_chess_btn_list = new Stack<Button>();
            else
            {
                while (cur_chess_btn_list.Count > 0)
                {
                    var btn = cur_chess_btn_list.Peek();
                    btn.BackgroundImage = Properties.Resources.chess_bg1;
                    cur_chess_btn_list.Pop();
                }
            }
            set_down_chess_label();
        }
        //绘制棋盘
        private void draw_chess_grid()
        {
            var graphics = this.CreateGraphics();
            this.Show();//一定要加这句,不然无法显示
            var pen = new Pen(Brushes.Black, 2.0f);
            for (int x = 0; x < 13; x++)
            {
                var p1 = new Point(50 + x * 50, 50);
                var p2 = new Point(50 + x * 50, 50 + 50 * 12);
                graphics.DrawLine(pen, p1, p2);
            }
            for (int y = 0; y < 13; y++)
            {
                var p1 = new Point(50, 50 + y * 50);
                var p2 = new Point(50 + 50 * 12, 50 + y * 50);
                graphics.DrawLine(pen, p1, p2);
            }
        }
        //右上方向判断
        private int right_up_check(Pos pos,int count)
        {
            if (count == 5) {
                if (pos.Type == 2)MessageBox.Show("黑方赢棋", "系统提示");
                else MessageBox.Show("白方赢棋", "系统提示");
                game_over = true;
            }
            if (pos.X + 1 <13 && pos.Y - 1 >0 && pos.Type != 0)
            {
                var index = get_pos_index_from_null_chess_list(pos.X + 1, pos.Y - 1);
                if (null_chess_pos_list[index].Type == pos.Type) right_up_check(null_chess_pos_list[index],count + 1);
            }
            return count;
        }
        //左上方向判断
        private int left_up_check(Pos pos, int count)
        {
            if (count == 5)
            {
                if (pos.Type == 2) MessageBox.Show("黑方赢棋", "系统提示");
                else MessageBox.Show("白方赢棋", "系统提示");
                game_over = true;
            }
            if (pos.X - 1 > 0 && pos.Y - 1 > 0 && pos.Type != 0)
            {
                var index = get_pos_index_from_null_chess_list(pos.X - 1, pos.Y - 1);
                if (null_chess_pos_list[index].Type == pos.Type ) left_up_check(null_chess_pos_list[index], count + 1);
            }
            return count;
        }
        //横向方向判断
        private int horizontal_check(Pos pos, int count)
        {
            if (count == 5)
            {
                if (pos.Type == 2) MessageBox.Show("黑方赢棋", "系统提示");
                else MessageBox.Show("白方赢棋", "系统提示");
                game_over = true;
            }
            if (pos.X + 1 < 13 && pos.Type != 0)
            {
                var index = get_pos_index_from_null_chess_list(pos.X + 1, pos.Y );
                if (null_chess_pos_list[index].Type == pos.Type) horizontal_check(null_chess_pos_list[index], count + 1);
            }
            return count;
        }
        //竖向方向判断
        private int vertical_check(Pos pos, int count)
        {
            if (count == 5)
            {
                if (pos.Type == 2) MessageBox.Show("黑方赢棋", "系统提示");
                else MessageBox.Show("白方赢棋", "系统提示");
                game_over = true;
            }
            if (pos.Y + 1 < 13 && pos.Type != 0)
            {
                var index = get_pos_index_from_null_chess_list(pos.X , pos.Y + 1);
                if (null_chess_pos_list[index].Type == pos.Type) vertical_check(null_chess_pos_list[index], count + 1);
            }
            return count;
        }
        //判断赢局
        private void check_win()
        {
            for (int i = 0; i < null_chess_pos_list.Count; i++)
            {
                left_up_check(null_chess_pos_list[i], 1);
                right_up_check(null_chess_pos_list[i], 1);
                horizontal_check(null_chess_pos_list[i], 1);
                vertical_check(null_chess_pos_list[i], 1);
            }
        }
        //初始生成下棋位置
        private void new_chess_pos(int x,int y)
        {
            var button = new Button();
            button.Location = new Point(x, y);
            button.Size = new Size(40, 40);
            set_btn_style(button);
            this.Controls.Add(button);
            button.Click += new EventHandler(button1_Click);
            buttons.Add(button);
        }
        //窗口坐标转点坐标
        private Point location_to_point(Button button)
        {
            return new Point((button.Location.X - 30)/50, (button.Location.Y - 30)/50);
        }
        //点坐标转窗口坐标
        private Point point_to_location(Pos pos)
        {
            return new Point(pos.X * 50 +30, pos.Y * 50 +30);
        }
        //根据点坐标得到棋子位置索引
        private int get_pos_index_from_null_chess_list(int x,int y)
        {
            for (int i = 0; i < null_chess_pos_list.Count; i++)
                if (null_chess_pos_list[i].X == x && null_chess_pos_list[i].Y == y)return i;
            return -1;
        }
        //根据窗口坐标得到索引
        private int get_btn_index_from_button_list(int x, int y)
        {
            for (int i = 0; i < buttons.Count; i++)
                if (buttons[i].Location.X == x && buttons[i].Location.Y == y) return i;
            return -1;
        }
        //ai走棋
        private void ai_move_chess()
        {
            Pos pos = get_best_pos();
            var point = point_to_location(pos);
            var index = get_btn_index_from_button_list(point.X, point.Y);
            move_chess(buttons[index]);
        }
        //走棋
        private void move_chess(Button button)
        {
            var point = location_to_point(button);
            var index = get_pos_index_from_null_chess_list(point.X, point.Y);
            if (null_chess_pos_list[index].Have_chess) return;
            null_chess_pos_list[index].Have_chess = true;
            if (down_chess == 2)
            {
                button.BackgroundImage = Properties.Resources.black;
                null_chess_pos_list[index].Type = 2;
                down_chess = 1;
            }
            else
            {
                button.BackgroundImage = Properties.Resources.white;
                null_chess_pos_list[index].Type = 1;
                down_chess = 2;
            }
            set_down_chess_label();
            //添加到下棋栈列
            cur_chess_btn_list.Push(button);
            check_win();
        }
        // 设置透明按钮样式
        private void set_btn_style(Button btn)
        {
            btn.FlatStyle = FlatStyle.Flat;//样式
            btn.ForeColor = Color.Transparent;//前景
            btn.BackColor = Color.Transparent;//去背景
            btn.FlatAppearance.BorderSize = 0;//去边线
            btn.FlatAppearance.MouseOverBackColor = Color.Transparent;//鼠标经过
            btn.FlatAppearance.MouseDownBackColor = Color.Transparent;//鼠标按下
        }
        //提示当前由哪方下棋
        private void set_down_chess_label()
        {
            if (down_chess == 2) label_game_type.Text = "黑方下棋";
            else label_game_type.Text = "白方下棋";
        }
        //悔棋
        private void back_chess()
        {
            if (cur_chess_btn_list==null) return;
            if (cur_chess_btn_list.Count == 0) return;
            var btn = cur_chess_btn_list.Peek();
            var p = location_to_point(btn);
            var index = get_pos_index_from_null_chess_list(p.X,p.Y);
            null_chess_pos_list[index].Type = 0;
            null_chess_pos_list[index].Have_chess = false;
            btn.BackgroundImage = Properties.Resources.chess_bg1;
            cur_chess_btn_list.Pop();
            if (down_chess == 2) down_chess = 1;
            else down_chess = 2;
            set_down_chess_label();
        }
        //下棋点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            if (game_over) return;
            switch (cur_game_type)
            {
                case 0:
                    move_chess((Button)sender);
                    break;
                case 1:
                    move_chess((Button)sender);
                    ai_move_chess();
                    break;
                case 2:
                    break;
            }
        }
        //开始游戏
        private void button_start_Click(object sender, EventArgs e)
        {
            init_game();
        }
        //悔棋
        private void button_back_Click(object sender, EventArgs e)
        {
            if (game_over) return;
            switch (cur_game_type)
            {
                case 0:
                    back_chess();
                    break;
                case 1:
                    back_chess();
                    back_chess();
                    break;
                case 2:
                    break;
            }
        }
        //重置游戏
        private void button_reset_Click(object sender, EventArgs e)
        {
            init_game();
        }
        //ai部分代码
        private Dictionary<string, int> dict = new Dictionary<string, int>();//存储枚举分数的字典
        private void init_score_dict()
        {
            //5个连子
            dict.Add("22222",122222);
            //4个连子
            dict.Add("022220", 12222);
            dict.Add("122220", 5222);
            dict.Add("20222", 5222);
            dict.Add("22022", 5222);
            //3个连子
            dict.Add("02220", 2522);
            dict.Add("122200", 1222);
            dict.Add("120220", 1222);
            dict.Add("020221", 1222);
            dict.Add("122020", 1222);
            dict.Add("022021", 1222);
            //2个连子
            dict.Add("00220", 522);
            dict.Add("122000", 522);
            dict.Add("120200", 322);
            //1个子
            dict.Add("211112", 52222);
            dict.Add("11121", 52222);
            dict.Add("11211", 52222);
            dict.Add("21110", 4222);
            dict.Add("12110", 4222);
            dict.Add("11210", 4222);
            dict.Add("21100",822);
            dict.Add("12100", 822);
            dict.Add("11200", 822);
            dict.Add("21000", 222);
            dict.Add("12000", 222);
        }
        //水平方向,传入的pos均为当前要估值的pos
        private string get_horizontal_pos_str(Pos head_temp_pos)
        {
            int head_x = head_temp_pos.X,trail_x = head_temp_pos.X,head_count =0,trail_count =0;
            while (head_count<5 && head_x - 1>0)
            {
                head_count++;
                head_x--;
            }
            while (trail_count < 5 && trail_x + 1 < 13)
            {
                trail_count++;
                trail_x++;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = head_temp_pos.X - head_count; i <= head_temp_pos.X+trail_count; i++)
            {
                if (i == head_temp_pos.X) sb.Append(head_temp_pos.Type.ToString());
                else
                {
                    var index = get_pos_index_from_null_chess_list(i, head_temp_pos.Y);
                    sb.Append(null_chess_pos_list[index].Type.ToString());
                }
            }
            return sb.ToString();
        }
        //竖直方向
        private string get_vertical_pos_str(Pos head_temp_pos)
        {
            int head_y = head_temp_pos.Y, trail_y = head_temp_pos.Y, head_count = 0, trail_count = 0;
            while (head_count < 5 && head_y - 1 > 0)
            {
                head_count++;
                head_y--;
            }
            while (trail_count < 5 && trail_y + 1 < 13)
            {
                trail_count++;
                trail_y++;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = head_temp_pos.Y - head_count; i <= head_temp_pos.Y + trail_count; i++)
            {
                if (i == head_temp_pos.Y) sb.Append(head_temp_pos.Type.ToString());
                else
                {
                    var index = get_pos_index_from_null_chess_list(head_temp_pos.X,i);
                    sb.Append(null_chess_pos_list[index].Type.ToString());
                }
            }
            return sb.ToString();
        }
        //左上方向
        private string get_left_up_pos_str(Pos head_temp_pos)
        {
            int head_x=head_temp_pos.X,head_y = head_temp_pos.Y, trail_x = head_temp_pos.X, trail_y = head_temp_pos.Y, head_count = 0, trail_count = 0;
            while (head_count < 5 &&  head_y - 1 > 0 && head_x - 1>0)
            {
                head_count++;
                head_y--;
                head_x--;
            }
            while (trail_count < 5 && trail_y + 1 < 13 && trail_x + 1 <13)
            {
                trail_count++;
                trail_y++;
                trail_x++;
            }
            var step = head_temp_pos.X - head_count;
            StringBuilder sb = new StringBuilder();
            for (int i = head_temp_pos.Y - head_count; i <= head_temp_pos.Y + trail_count; i++)
            {
                if (i == head_temp_pos.Y) sb.Append(head_temp_pos.Type.ToString());
                else
                {
                    var index = get_pos_index_from_null_chess_list(step, i);
                    sb.Append(null_chess_pos_list[index].Type.ToString());
                }
                step++;
            }
            return sb.ToString();
        }
        //右上方向
        private string get_right_up_pos_str(Pos head_temp_pos)
        {
            int head_x = head_temp_pos.X, head_y = head_temp_pos.Y, trail_x = head_temp_pos.X, trail_y = head_temp_pos.Y, head_count = 0, trail_count = 0;
            while (head_count < 5 && head_y + 1 < 13 && head_x - 1 > 0)
            {
                head_count++;
                head_y++;
                head_x--;
            }
            while (trail_count < 5 && trail_y - 1 > 0 && trail_x + 1 < 13)
            {
                trail_count++;
                trail_y--;
                trail_x++;
            }
            var step = head_temp_pos.Y + head_count;
            StringBuilder sb = new StringBuilder();
            for (int i = head_temp_pos.X - head_count; i <= head_temp_pos.X + trail_count; i++)
            {
                if (i == head_temp_pos.X) sb.Append(head_temp_pos.Type.ToString());
                else
                {
                        var index = get_pos_index_from_null_chess_list(i, step);
                        sb.Append(null_chess_pos_list[index].Type.ToString());
                }
                step--;
            }
            return sb.ToString();
        }
        //得到在位置落子时的分数
        private int get_pos_score(Pos pos)
        {
            int value = 0;
            string left_up = get_left_up_pos_str(pos);
            string right_up = get_right_up_pos_str(pos);
            string horizontal = get_horizontal_pos_str(pos);
            string vertical = get_vertical_pos_str(pos);
            foreach (KeyValuePair<string,int> pair in dict)
            {
                if (left_up.Contains(pair.Key) || left_up.Contains(new string(pair.Key.ToCharArray().Reverse().ToArray()))) value += pair.Value;
                if (right_up.Contains(pair.Key) || right_up.Contains(new string(pair.Key.ToCharArray().Reverse().ToArray()))) value += pair.Value;
                if (horizontal.Contains(pair.Key) || horizontal.Contains(new string(pair.Key.ToCharArray().Reverse().ToArray()))) value += pair.Value;
                if (vertical.Contains(pair.Key) || vertical.Contains(new string(pair.Key.ToCharArray().Reverse().ToArray()))) value += pair.Value;
            }
            return value;
        }
        //得到最好的落子位置
        private Pos get_best_pos()
        {
            var dict_score = new Dictionary<Pos, int>();
            for (int i = 0; i < null_chess_pos_list.Count; i++)
            {
                if (null_chess_pos_list[i].Have_chess == false)
                {
                    Pos pos = new Pos(null_chess_pos_list[i].X, null_chess_pos_list[i].Y, null_chess_pos_list[i].Have_chess,2);
                    dict_score.Add(null_chess_pos_list[i], get_pos_score(pos));
                }
            }
            var temp_dict_score = dict_score.OrderByDescending(o => o.Value).ToDictionary(p =>p.Key,o => o.Value);
            return temp_dict_score.Keys.First();
        }
    }
}

5、测试

由于只考虑一步,想要很强的棋力是不可能的,但是也不算特别白痴,给它赢它还是很“乐意”的,好奇的童鞋可以拷贝代码运行试试看看,本博文的思想思想仅供参考。

c#编写的五子棋人机 核心算法 核心算法就是计算计算机应该在哪里落子。    思路的伪代码如下。    PC_Stone    For i = 1 to 15     For j = 1 to 15     If ( board[i][j] != -1)     Qz[i][j] = -     Esle     FindQz(Qz[i][j])    getTheMaxQz()    而在这个过程中最主要的算法是计算每个点的权重,由此判断电脑应该将棋子落在哪个地方。    计算确定点的权重的函数是FindQz();,函数里面有对于多种不同情况下,函数所赋给那个点的权重值,这些值是累加的。函数主要通过对四个函数X1(),X2(),X3(),X4()的调用来确定每个点所处地位。    FindQz()函数可以分为两部分。    第一部分是假设人在此点落下一子后,此点给人所带来的好处是多少。通过对函数X1()计算如果在点board[i][j]落下,所在有多少连续的相同的点数。与X1(),类似的是X2()是计算board[i][j]所在列的点数。X3()是计算左高右低的斜排,X4()是左低右高的情况。经过计算后,将这四种情况所带来的改变加到一起,就是将棋子落在这里对假设方带来的好处。    第二部分是假设电脑在此落一点之后,此点给电脑带来的好处是多少。调用过程与第一部分基本一样,没什么不同。    经过这样的调用,将两部分计算出的结果加到一起,算出来的就是电脑下在这一点会带来的所有影响。选取影响最大的一点,落子,这样就能在一定程度上达到某种智能。    对于X1()函数,他的作用实现是这样的。运用两个计数器count与flag,count用于计算有多少相连的相同的子,并且是一board[i][j]为中心,向两边发散(说他是中心并不意味着他是相连的点的中心位置)。Flag是计算相连的子的两端是否有阻挡。阻挡分为两部分,一部分是到了表格的尽头,另一种是被另一种颜色的棋子挡住。这样综合count与flag两个参数,给出board[i][j]点对于的贡献值。    对于X2(),X3(),X4()他们的原理是与X1()一样的,只不过是坐标不同罢了。这样,计算完之后,再进比较,就能得到最好的点了。        对于特殊落点的判断问题:   设以围棋棋盘左下角为坐标原点建立直角坐标系xOy,   若(9,10)(9,11)(10,10)(11,9)上有黑子,(8,12)(10,9)(11,8)上有白子,现在到白棋走子,   若走(10,12)(11,11)就属于斜向走子,但是通常直向的(紧贴着棋子走的)走子要比斜向的走子对防守的贡献大,   若走(8,11)(8,10)(8,9)(9,9)其中一个的均属于单侧走子,而另一侧则空虚,所以也不好,   若走(11,10)也不好,因为在(11,8)的子已经对防守有了一定的贡献了,同理走(10,11)的也不好,因为(10,9)的子也对防守有了一定的贡献.所以说此时最佳的走子方法就是走(9,12)   若(8,10)有白子,(10,10)(11,10)(12,10)有黑子,到白子走,由于(8,10)对防守贡献了一部分,所以应该走(13,10)而不走(9,10)。   若为黑走,就应该走(13,10)而不走(9,10),因为白子的(8,10)会削弱它对攻的贡献
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值