广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#实现围棋游戏
  • 201
分享到

C#实现围棋游戏

2024-04-02 19:04:59 201人浏览 独家记忆
摘要

本文实例为大家分享了C#实现围棋游戏的具体代码,供大家参考,具体内容如下 之所以选择围棋作为大作业一方面是想挑战一下,另一方面是由于从6岁学围棋到11岁放下,再到今天已将近8年了,也

本文实例为大家分享了C#实现围棋游戏的具体代码,供大家参考,具体内容如下

之所以选择围棋作为大作业一方面是想挑战一下,另一方面是由于从6岁学围棋到11岁放下,再到今天已将近8年了,也算是回味一下童年吧,毕竟,曾梦想执子走天涯。

这是效果图:

这个程序除了一开始参考了中国象棋,其他的都是自己完成的。

不说了,上代码!!!

这个是主窗口代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.windows.FORMs;
using System.IO;
using System.Media;
 
//该产品归属****大学18级信息工程学院计算机系王**所有,如需转载,请注明原作者及出处:https://blog.csdn.net/Luoriliming
namespace AlphaGo
{
    public partial class FormMain : Form
    {
        //加载围棋类
        private PlayChess playchess = new PlayChess();
 
        public FormMain()
        {
            InitializeComponent();
            //根据屏幕分辨率调整大小
            playchess._rowHeight = Screen.PrimaryScreen.Bounds.Size.Height / 25;
            playchess._colWidth = playchess._rowHeight;
            playchess._lefttop.Y = 2* playchess._rowHeight;
            playchess._lefttop.X = playchess._lefttop.Y;
        }
        //绘制
        private void FormMain_Paint(object sender, PaintEventArgs e)
        {
            playchess.Drawboard(e.Graphics);
            playchess.DrawPiece(e.Graphics);
        }
        //开局
        private void toolStripMenuItemBegin_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = false;
            playchess. PlaySound("begin.wav");
            playchess.Begin(Player.白);
            Invalidate();
        }
        //计时器
        private void timer1_Tick(object sender, EventArgs e)
        {
 
            if (playchess._time1 <= 0)
            {
                if (playchess._curPlayer == Player.黑)
                    playchess._curPlayer = Player.白;
                else
                    playchess._curPlayer = Player.黑;
                if (playchess._pickChess == Piece.黑子)
                    playchess._pickChess = Piece.白子;
                else
                    playchess._pickChess = Piece.黑子;
                if (playchess._timeColor == Color.Yellow)
                    playchess._timeColor = Color.Red;
                else
                    playchess._timeColor = Color.Yellow;
                playchess._time2 = 60;
                playchess._time1 = 60;
                timer1.Enabled = !timer1.Enabled;
                timer2.Enabled = !timer2.Enabled;
            }
            else
            {
 
                playchess._time1 = playchess._time1 - 1;
                Invalidate();
            }
        }
        //鼠标移动
        private void FormMain_MouseMove(object sender, MouseEventArgs e)
        {
            playchess._curMousePoint = e.Location;
            Invalidate();
        }
 
        private void FormMain_MouseDown(object sender, MouseEventArgs e)
        {
            //若单击右键
            if (e.Button == MouseButtons.Left)
            {
                int row, col;
                //输出此时鼠标位置,并判断是否在范围内
                bool valid = playchess.ConvertPointToRowCol(new Point(e.X, e.Y), out row, out col);
                    playchess._dropRow = row;
                    playchess._dropCol = col;
                if (valid == true)
                {
                    if (playchess._chess[playchess._dropRow, playchess._dropCol] == Piece.无子)
                    {
                        playchess.PlaySound("drop.wav");
                        if (timer2.Enabled == false)
                            timer2.Enabled = true;
                        else
                            timer2.Enabled = false;
                        if (timer1.Enabled == false)
                            timer1.Enabled = true;
                        else
                            timer1.Enabled = false;
                        playchess.DropPiece(playchess._dropRow, playchess._dropCol);
                    }
                    Invalidate();
                }
            }
        }
        //计时器
        public void timer2_Tick(object sender, EventArgs e)
        {
            
            if (playchess._time2 <= 0)
            {
                if (playchess._curPlayer == Player.黑)
                    playchess._curPlayer = Player.白;
                else
                    playchess._curPlayer = Player.黑;
                if (playchess._pickChess == Piece.黑子)
                    playchess._pickChess = Piece.白子;
                else
                    playchess._pickChess = Piece.黑子;
                playchess._time2 = 60;
                playchess._time1 = 60;
                timer1.Enabled = !timer1.Enabled;
                timer2.Enabled = !timer2.Enabled;
            }
            else
            {
 
                playchess._time2 = playchess._time2 - 1;
                Invalidate();
            }
        }
        //判断胜负
        private void ToolStripMenuItemEnd_Click(object sender, EventArgs e)
        {
           
            if (playchess.IsOver() == Player.黑)
            {
                MessageBox.Show("黑棋获胜!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                playchess . _black++;
            }
            else if (playchess.IsOver() == Player.白)
            {
                MessageBox.Show("白棋获胜!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                playchess._white++;
            }
            else if (playchess.IsOver() == Player.无)
            {
                MessageBox.Show("和棋!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
            }
            timer1.Enabled = false;
            timer2.Enabled = false;
            playchess._pickChess = Piece.无子;
 
        }
 
        private void ToolStripMenuItemSave_Click(object sender, EventArgs e)
        {
            //显示保存残局对话框
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                playchess.WriteTo(bw);
                bw.Close();
                fs.Close();
            }
        }
 
        private void ToolStripMenuItemOpen_Click(object sender, EventArgs e)
        {
            //显示打开残局对话框
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                playchess.ReadFrom(br);
                br.Close();
                fs.Close();
                Invalidate();
            }
        }
    }
}

这个是围棋类代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;
 
//该产品归属****大学18级信息工程学院计算机系王**所有,如需转载,请注明原作者及出处:Https://blog.csdn.net/Luoriliming
 
namespace AlphaGo
{
    //枚举类型:棋子
    public enum Piece
    {
        无子 = 0, 黑子 = 1, 白子 = 2
    }
    //枚举类型:双方棋手
    public enum Player
    {
        无 = 0, 黑 = 1, 白 = 2
    }
    //类:走棋步骤
    public class Step
    {
        public Player _player;//走棋方
        public int _dropRow;//落下棋子行号
        public int _dropCol;//落下棋子列号
        public Piece _dropPiece;//落下位置棋子
    }
    //类:围棋
    class PlayChess
    {
        //类字段:棋子
        public Piece[,] _chess = new Piece[25, 25];
        //初始化执子为无子
        public Piece _pickChess = Piece.无子;
        //落下棋子的位置
        public int _dropRow = 0;
        public int _dropCol = 0;
        //闪烁
        public Color _timeColor = Color.Yellow;
        //类字段:走棋方
        public Player _curPlayer = Player.无;
        //鼠标移动位置
        public Point _curMousePoint = new Point(0, 0);
        //类字段:设置走棋步骤表
        public List<Step> _listStep = new List<Step>();
        //保存棋盘左上角坐标
        public Point _lefttop = new Point(100, 100);
        //棋子半径
        public int _pieceR = 10;
        //保存行高和列宽
        public int _rowHeight = 30;
        public int _colWidth = 30;
        //加载图像
        //导入各种图片
        Bitmap deskbmp = new Bitmap("desktop.jpg");
        public Bitmap _redbmp = new Bitmap("黑方头像.jpg");
        public Bitmap _bluebmp = new Bitmap("白方头像.jpg");
        //设置是否遍历数组
        public bool[,] _bianli = new bool[25, 25];
        //设置气的数组
        public int[,] _qi = new int[20, 20];
        //设置白棋黑棋个数
        public int _Whitechess;
        public int _Blackchess;
        //计时
        public int _time1 = 60;
        public int _time2 = 60;
        //黑白子各胜局数
        public int _black = 0;
        public int _white = 0;
        //劫
        private bool[,] _jie = new bool[25, 25];
        public int _lastrow = 0;
        public int _lastcol = 0;
        public int _chessnumber = 0;
 
        //类 最后一次走棋步骤
        public Step _LastStep
        {
            get
            {
                int stepCount = _listStep.Count;
                if (stepCount > 0)
                    return _listStep[stepCount - 1];
                else
                    return null;
 
            }
        }
        //类构造方法
        public PlayChess()
        {
            //初始化围棋
            Initialize();
 
        }
        //类:初始化围棋
        public void Initialize()
        {
            for (int i = 1; i <= 19; i++)
            {
                for (int j = 1; j <= 19; j++)
                {
                    _chess[j, i] = Piece.无子;
                }
            }
 
            _listStep.Clear();
        }
        //类:围棋开局
        public void Begin(Player FirstPlayer)
        {
            //初始化围棋
            Initialize();
            //初始化开局棋子布局
            for (int i = 1; i <= 19; i++)
            {
                for (int j = 1; j <= 19; j++)
                {
                    _chess[j, i] = Piece.无子;
                }
            }
            //初始化时间
            _time1 = 60;
            _time2 = 60;
            //初始化当前走棋方
            _curPlayer = Player.白;
            _pickChess = Piece.白子;
            //初始化遍历条件
            for (int i = 1; i <= 20; i++)
            {
                for (int j = 1; j <= 20; j++)
                {
                    _bianli[i, j] = false;
                }
            }
            //初始化劫
            for (int i = 1; i <= 20; i++)
            {
                for (int j = 1; j <= 20; j++)
                {
                    _jie[i, j] = false;
                }
            }
            for (int i = 1; i <= 20; i++)
            {
                for (int j = 1; j <= 20; j++)
                {
                    _jie[i, j] = false;
                }
            }
            //初始化落子属性
            _dropCol = 0;
            _dropRow = 0;
            _chessnumber = 0;
        }
        //类:落下棋子
        public bool DropPiece(int dropRow, int dropCol)
        {
            if (_curPlayer != Player.无 && MatchRules(_curPlayer, _dropRow, _dropCol) == true&&_jie[dropRow,dropCol]==false)
            {
                //保存走棋步骤到_listStep中
                Step step = new Step();
                step._player = _curPlayer;
                step._dropRow = dropRow;
                step._dropCol = dropCol;
                step._dropPiece = _chess[dropRow, dropCol];
                _listStep.Add(step);
                _chess[dropRow, dropCol] = _pickChess;
                //播放落子声音
                PlaySound("drop.wav");
                //从左上角开始判断所有棋子,为了避免像虚竹一样自杀的存在
                EatChess(1, 1);
                //重置遍历
                for (int i = 1; i <= 19; i++)
                {
                    for (int j = 1; j <= 19; j++)
                    {
                        _bianli[i, j] = false;
                    }
                }
                //判断劫
                for (int i = 1; i <= 20; i++)
                {
                    for (int j = 1; j <= 20; j++)
                    {
 
                        _jie[i, j] = false;
                    }
                }
                _jie[_lastrow, _lastcol] = true;
                //交换走棋方
                if (_pickChess == Piece.黑子)
                    _pickChess = Piece.白子;
                else if (_pickChess == Piece.白子)
                    _pickChess = Piece.黑子;
                else
                    return false;
                if (_curPlayer == Player.黑)
                    _curPlayer = Player.白;
                else
                    _curPlayer = Player.黑;
                //重置时间
                _time2 = 60;
                _time1 = 60;
                //步数加一
                _chessnumber = _chessnumber + 1;
                //添加上一步的存入
                _dropCol = dropCol;
                _dropRow = dropRow;
                _lastcol = _dropCol;
                _lastrow = _dropRow;
                return true;
            }
            else
                return false;
 
        }
        //类:判断胜负
        public Player IsOver()
        {
            //初始化黑白棋子所围的子
            _Blackchess = 0;
            _Whitechess = 0;
            for (int i = 1; i <= 20; i++)
            {
                for (int j = 1; j <= 20; j++)
                {
                    for (int l = 1; l <= 20; l++)
                    {
                        for (int k = 1; k <= 20; k++)
                        {
                            _bianli[l, k] = false;
                        }
                    }
                    //查找该子属于黑子还是白子
                    int x = SearchChess(i, j);
                    if (x == 2)
                        _Whitechess++;
                    else if (x == 1)
                        _Blackchess++;
                }
            }
            //如果黑子多于白子,则黑方获胜;如果白子多于黑子,则白方获胜;如果双方棋子数相同,则平局
            if (_Blackchess > _Whitechess)
            {
                return Player.黑;
            }
            else if (_Whitechess > _Blackchess)
            {
                return Player.白;
            }
            else
                return Player.无;
 
        }
        //类:围棋规则
        public bool MatchRules(Player player, int dropRow, int dropCol)
        {
            bool matchflag = false;
            {
                //只能下在无子的位置
                if (_chess[dropRow, dropCol] == Piece.无子 )
                {
                    matchflag = true;
                }
                return matchflag;
            }
        }
        //保存残局
        public void WriteTo(BinaryWriter binaryWriter)
        {
            binaryWriter.Write(_curPlayer.ToString());
            for (int j = 1; j <= 10; j++)
            {
                for (int i = 1; i <= 10; i++)
                {
                    binaryWriter.Write(_chess[i, j].ToString());
                }
            }
            binaryWriter.Write(_listStep.Count);
            for (int i = 0; i <= _listStep.Count - 1; i++)
            {
                binaryWriter.Write(_listStep[i]._player.ToString());
                binaryWriter.Write(_listStep[i]._dropRow);
                binaryWriter.Write(_listStep[i]._dropPiece.ToString());
                binaryWriter.Write(_listStep[i]._dropCol);
            }
 
        }
        //读取残局
        public void ReadFrom(BinaryReader binaryReader)
        {
            Initialize();
            _curPlayer = (Player)Enum.Parse(typeof(Player), binaryReader.ReadString());
            for (int j = 1; j <= 10; j++)
            {
                for (int i = 1; i <= 10; i++)
                {
                    _chess[i, j] = (Piece)Enum.Parse(typeof(Piece), binaryReader.ReadString());
                }
            }
            int stepCount = binaryReader.ReadInt32();
            for (int i = 0; i <= _listStep.Count - 1; i++)
            {
                Step step = new Step();
                step._player = (Player)binaryReader.ReadInt32();
                step._dropRow = binaryReader.ReadInt32();
                step._dropPiece = (Piece)binaryReader.ReadInt32();
                step._dropCol = binaryReader.ReadInt32();
                _listStep.Add(step);
            }
            _pickChess = Piece.无子;
 
        }
        public void Drawboard(Graphics g)
        {
            //绘制桌面
            g.DrawImage(deskbmp, new Point(0, 0));
            //创建粗笔和细笔
            Pen thickPen = new Pen(Color.Black, 6);
            Pen thinPen = new Pen(Color.Black, 2);
            //绘制粗外边框
            int gap = (int)(_rowHeight * 0.25);
            g.DrawRectangle(thickPen, new Rectangle(_lefttop.X - gap, _lefttop.Y - gap, _colWidth * 18 + 2 * gap, _rowHeight * 18 + 2 * gap));
            //绘制横轴竖轴
            for (int i = 1; i <= 19; i++)
            {
                g.DrawLine(thinPen, new Point(_lefttop.X, _lefttop.Y + (i - 1) * _rowHeight),
                                   new Point(_lefttop.X + 18 * _colWidth, _lefttop.Y + (i - 1) * _rowHeight));
            }
            for (int i = 1; i <= 19; i++)
            {
                g.DrawLine(thinPen, new Point(_lefttop.X + _colWidth * (i - 1), _lefttop.Y),
                                   new Point(_lefttop.X + (i - 1) * _colWidth, _lefttop.Y + 18 * _rowHeight));
            }
 
            SolidBrush Brush = new SolidBrush(Color.Black);
            SolidBrush white = new SolidBrush(Color.White);
            SolidBrush num = new SolidBrush(Color.Blue);
            //书写坐标
            Font font2 = new Font("黑体", (float)(_rowHeight * 0.6), FontStyle.Regular, GraphicsUnit.Pixel);
            for (int i = 1; i <= 19; i++)
            {
                g.DrawString(i.ToString(), font2, Brush, new Point((int)(_lefttop.X - _colWidth * 1.1),
                                                                 (int)(_lefttop.Y - _rowHeight * 0.4 + _rowHeight * (i - 1))));
            }
            string[] colNumber = new string[19] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S" };
            Font font3 = new Font("华文行楷", (float)(_rowHeight * 1.5), FontStyle.Regular, GraphicsUnit.Pixel);
            for (int i = 1; i <= 19; i++)
            {
                g.DrawString(colNumber[i - 1], font2, Brush, new Point((int)(_lefttop.X - _colWidth * 0.3 + _colWidth * (i - 1)),
                                                                 (int)(_lefttop.Y - _rowHeight * 1.1)));
            }
            //绘制黑白双方
            g.DrawString("黑方", font3, Brush, new Point((int)(_lefttop.X + _colWidth * 19),
                                                                 (int)(_lefttop.Y + _rowHeight * 2.2)));
            g.DrawString("白方", font3, white, new Point((int)(_lefttop.X + _colWidth * 19),
                                                                 (int)(_lefttop.Y + _rowHeight * 9.4)));
            SolidBrush fontBrush = new SolidBrush(Color.Black);
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 3 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 3 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 15 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 3 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 9 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 3 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 3 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 9 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 15 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 9 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 9 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 9 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 3 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 15 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 15 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 15 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
            g.FillEllipse(fontBrush, _lefttop.X + _colWidth * 9 - (int)(_pieceR * 0.7), _lefttop.Y + _rowHeight * 15 - (int)(_pieceR * 0.7), (int)(_pieceR * 1), (int)(_pieceR * 1));
 
            Font font4 = new Font("仿宋", (float)(_rowHeight * 1.5), FontStyle.Regular, GraphicsUnit.Pixel);
            //步数
            g.DrawString("步数:", font3, Brush, new Point((int)((int)(_lefttop.X + _colWidth * 21)),
                                                                 (int)(_lefttop.Y - _rowHeight)));
            g.DrawString(_chessnumber.ToString(), font4, num, new Point((int)(_lefttop.X + _colWidth * 25),
                                                                 (int)(_lefttop.Y - _rowHeight * 1.1)));
            //黑白各胜局数
            g.DrawString("黑:白   :", font2, Brush, new Point((int)((int)(_lefttop.X + _colWidth * 5)),
                                                                 (int)(_lefttop.Y + _rowHeight * 20)));
            g.DrawString(_black.ToString(), font2, num, new Point((int)(_lefttop.X + _colWidth * 7),
                                                                             (int)(_lefttop.Y + _rowHeight * 20)));
            g.DrawString(_white.ToString(), font2, num, new Point((int)(_lefttop.X + _colWidth * 8),
                                                                 (int)(_lefttop.Y + _rowHeight * 20)));
 
            //g.DrawRectangle(thickPen, (int)(_lefttop.X + _colWidth * 25), (int)(_lefttop.Y - _rowHeight), 130, 60);
            g.DrawString("白方计时器:", font3, white, new Point((int)((int)(_lefttop.X + _colWidth * 19)),
                                                                  (int)(_lefttop.Y + _rowHeight * 16)));
            g.DrawString("黑方计时器:", font3, Brush, new Point((int)((int)(_lefttop.X + _colWidth * 19)),
                                                                  (int)(_lefttop.Y + _rowHeight * 6)));
            g.DrawString(_time2.ToString() + "s", font4, num, new Point((int)(_lefttop.X + _colWidth * 27),
                                                                 (int)(_lefttop.Y + _rowHeight * 6)));
            g.DrawString(_time1.ToString() + "s", font4, num, new Point((int)(_lefttop.X + _colWidth * 27),
                                                                 (int)(_lefttop.Y + _rowHeight * 16)));
            //加载黑白方图片
            if (_curPlayer == Player.白)
            {
                g.DrawImage(_bluebmp, new Point(_lefttop.X + 8 * _colWidth + 625, _lefttop.Y + 9 * _rowHeight + 10));
            }
            else if (_curPlayer == Player.黑)
            {
                g.DrawImage(_redbmp, new Point(_lefttop.X + 8 * _colWidth + 625, _lefttop.Y + 2 * _rowHeight - 10));
            }
            DrawPickDropMark(g, _dropRow, _dropCol);
        }
 
        public void DrawPiece(Graphics g)
        {
            for (int j = 1; j <= 19; j++)
            {
                for (int i = 1; i <= 19; i++)
                {
                    if (_chess[i, j] != Piece.无子)
                    {
                        //绘制棋子
                        DrawPieceByCode(g, i, j, _chess[i, j]);
                    }
                }
            }
            //绘制跟随鼠标移动的棋子
            DrawMousePiece(g, _curMousePoint.X, _curMousePoint.Y, _pickChess);
        }
        //鼠标位置
        public bool ConvertPointToRowCol(Point p, out int row, out int col)
        {
            row = (p.Y - _lefttop.Y) / _rowHeight + 1;
            if ((p.Y - _lefttop.Y) % _rowHeight >= _rowHeight / 2)
            {
                row++;
            }
            col = (p.X - _lefttop.X) / _colWidth + 1;
            if ((p.X - _lefttop.X) % _colWidth >= _colWidth / 2)
            {
                col++;
            }
            Point chessP = new Point();
            chessP.X = _lefttop.X + _colWidth * (col - 1);
            chessP.Y = _lefttop.Y + _rowHeight * (row - 1);
            double dist = Math.Sqrt(Math.Pow(p.X - chessP.X, 2) + Math.Pow(p.Y - chessP.Y, 2));
            if (dist <= _pieceR && (row <= 19) && (row >= 1) && (col <= 19) && (col >= 1))
            {
                return true;
            }
            else
            {
                row = 0;
                col = 0;
                return false;
            }
        }
        //绘制棋子
        public void DrawMousePiece(Graphics g, int x, int y, Piece chess)
        {
            SolidBrush fontBrush;
            _pieceR = (int)(_rowHeight * 0.3);
            if (chess != Piece.无子)
            {
                if (chess == Piece.黑子)
                {
                    fontBrush = new SolidBrush(Color.Black);
                }
                else
                {
                    fontBrush = new SolidBrush(Color.White);
                }
                g.FillEllipse(fontBrush, x - (int)(_pieceR * 1.0), y - (int)(_pieceR * 1), _pieceR * 2, _pieceR * 2);
            }
        }
        //绘制手中棋子
        public void DrawPieceByCode(Graphics g, int row, int col, Piece chess)
        {
            SolidBrush fontBrush;
            _pieceR = (int)(_rowHeight * 0.3);
            if (chess != Piece.无子)
            {
                if (chess == Piece.黑子)
                {
                    fontBrush = new SolidBrush(Color.Black);
                }
                else
                {
                    fontBrush = new SolidBrush(Color.White);
                }
                g.FillEllipse(fontBrush, _lefttop.X + _colWidth * (col - 1) - (int)(_pieceR), _lefttop.Y + _rowHeight * (row - 1) - (int)(_pieceR), _pieceR * 2, _pieceR * 2);
            }
        }
        public void DrawPickDropMark(Graphics g, int row, int col)
        {
            //在棋盘范围内绘制闪烁标记
            if (row > 0)
            {
                Pen pen = new Pen(_timeColor, 4);
                Point p = new Point(_lefttop.X + _colWidth * (col - 1), _lefttop.Y + _rowHeight * (row - 1));
                //绘制闪烁标记
                g.DrawLine(pen, p.X - _pieceR, p.Y - _pieceR, p.X - _pieceR / 2, p.Y - _pieceR);
                g.DrawLine(pen, p.X - _pieceR, p.Y - _pieceR, p.X - _pieceR, p.Y - _pieceR / 2);
                g.DrawLine(pen, p.X + _pieceR, p.Y - _pieceR, p.X + _pieceR / 2, p.Y - _pieceR);
                g.DrawLine(pen, p.X + _pieceR, p.Y - _pieceR, p.X + _pieceR, p.Y - _pieceR / 2);
                g.DrawLine(pen, p.X - _pieceR, p.Y + _pieceR, p.X - _pieceR / 2, p.Y + _pieceR);
                g.DrawLine(pen, p.X - _pieceR, p.Y + _pieceR, p.X - _pieceR, p.Y + _pieceR / 2);
                g.DrawLine(pen, p.X + _pieceR, p.Y + _pieceR, p.X + _pieceR / 2, p.Y + _pieceR);
                g.DrawLine(pen, p.X + _pieceR, p.Y + _pieceR, p.X + _pieceR, p.Y + _pieceR / 2);
 
            }
 
        }
        //吃子判定(因为不用考虑时间和空间复杂度,用暴力跑的,主要这段时间被卡超时卡太烦了)
        public void EatChess(int row, int col)
        {
            for (int i = 1; i <= 19; i++)
            {
                for (int j = 1; j <= 19; j++)
                {
                    if (_chess[i, j] != Piece.无子)
                    {
                        _qi[i, j] = Judge(_chess[i, j], i, j);
                    }
                }
            }
            for (int i = 1; i <= 19; i++)
            {
                for (int j = 1; j <= 19; j++)
                {
                    if (_chess[i, j] == _chess[i + 1, j] && i <= 18)
                    {
                        _qi[i + 1, j] = _qi[i, j] + _qi[i + 1, j];
                    }
                    if (_chess[i, j] == _chess[i, j + 1] && j <= 18)
                    {
                        _qi[i, j + 1] = _qi[i, j] + _qi[i, j + 1];
                    }
 
                }
            }
 
            for (int i = 1; i <= 19; i++)
            {
                for (int j = 1; j <= 19; j++)
                {
                    if (_chess[i, j] == _chess[i + 1, j] && i <= 18)
                    {
                        if (_qi[i, j] < _qi[i + 1, j])
                            _qi[i, j] = _qi[i + 1, j];
                        else
                            _qi[i + 1, j] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i, j + 1] && j <= 18)
                    {
                        if (_qi[i, j] < _qi[i, j + 1])
                            _qi[i, j] = _qi[i, j + 1];
                        else
                            _qi[i, j + 1] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i - 1, j] && i >= 2)
                    {
                        if (_qi[i, j] < _qi[i - 1, j])
                            _qi[i, j] = _qi[i - 1, j];
                        else
                            _qi[i - 1, j] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i, j - 1] && j >= 2)
                    {
                        if (_qi[i, j] < _qi[i, j - 1])
                            _qi[i, j] = _qi[i, j - 1];
                        else
                            _qi[i, j - 1] = _qi[i, j];
                    }
                }
            }
            for (int i = 19; i >= 1; i--)
            {
                for (int j = 19; j >= 1; j--)
                {
                    if (_chess[i, j] == _chess[i + 1, j] && i <= 18)
                    {
                        if (_qi[i, j] < _qi[i + 1, j])
                            _qi[i, j] = _qi[i + 1, j];
                        else
                            _qi[i + 1, j] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i, j + 1] && j <= 18)
                    {
                        if (_qi[i, j] < _qi[i, j + 1])
                            _qi[i, j] = _qi[i, j + 1];
                        else
                            _qi[i, j + 1] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i - 1, j] && i >= 2)
                    {
                        if (_qi[i, j] < _qi[i - 1, j])
                            _qi[i, j] = _qi[i - 1, j];
                        else
                            _qi[i - 1, j] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i, j - 1] && j >= 2)
                    {
                        if (_qi[i, j] < _qi[i, j - 1])
                            _qi[i, j] = _qi[i, j - 1];
                        else
                            _qi[i, j - 1] = _qi[i, j];
                    }
                }
            }
            for (int i = 1; i <= 19; i++)
            {
                for (int j = 19; j >= 1; j--)
                {
                    if (_chess[i, j] == _chess[i + 1, j] && i <= 18)
                    {
                        if (_qi[i, j] < _qi[i + 1, j])
                            _qi[i, j] = _qi[i + 1, j];
                        else
                            _qi[i + 1, j] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i, j + 1] && j <= 18)
                    {
                        if (_qi[i, j] < _qi[i, j + 1])
                            _qi[i, j] = _qi[i, j + 1];
                        else
                            _qi[i, j + 1] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i - 1, j] && i >= 2)
                    {
                        if (_qi[i, j] < _qi[i - 1, j])
                            _qi[i, j] = _qi[i - 1, j];
                        else
                            _qi[i - 1, j] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i, j - 1] && j >= 2)
                    {
                        if (_qi[i, j] < _qi[i, j - 1])
                            _qi[i, j] = _qi[i, j - 1];
                        else
                            _qi[i, j - 1] = _qi[i, j];
                    }
                }
            }
            for (int i = 19; i >= 1; i--)
            {
                for (int j = 1; j <= 19; j++)
                {
                    if (_chess[i, j] == _chess[i + 1, j] && i <= 18)
                    {
                        if (_qi[i, j] < _qi[i + 1, j])
                            _qi[i, j] = _qi[i + 1, j];
                        else
                            _qi[i + 1, j] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i, j + 1] && j <= 18)
                    {
                        if (_qi[i, j] < _qi[i, j + 1])
                            _qi[i, j] = _qi[i, j + 1];
                        else
                            _qi[i, j + 1] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i - 1, j] && i >= 2)
                    {
                        if (_qi[i, j] < _qi[i - 1, j])
                            _qi[i, j] = _qi[i - 1, j];
                        else
                            _qi[i - 1, j] = _qi[i, j];
                    }
                    if (_chess[i, j] == _chess[i, j - 1] && j >= 2)
                    {
                        if (_qi[i, j] < _qi[i, j - 1])
                            _qi[i, j] = _qi[i, j - 1];
                        else
                            _qi[i, j - 1] = _qi[i, j];
                    }
                }
            }
            for (int i = 1; i <= 19; i++)
            {
                for (int j = 1; j <= 19; j++)
                {
                    if (_qi[i, j] == 0 && _chess[i, j] != _pickChess)
                        _chess[i, j] = Piece.无子;
                }
            }
        }
        //判断气
        public int Judge(Piece nowchess, int row, int col)
        {
            int qi = 0;
            _bianli[row, col] = true;
            if (_chess[row, col + 1] == Piece.无子 && col <= 18)
            {
                qi++;
            }
            if (_chess[row, col - 1] == Piece.无子 && col >= 2)
            {
                qi++;
            }
            if (_chess[row + 1, col] == Piece.无子 && row <= 18)
            {
                qi++;
            }
            if (_chess[row - 1, col] == Piece.无子 && row >= 2)
            {
                qi++;
            }
            return qi;
 
 
        }
        //搜索
        public int SearchChess(int i, int j)
        {
            _bianli[i, j] = true;
            if (_chess[i, j] == Piece.黑子)
                return 1;
            else if (_chess[i, j] == Piece.白子)
                return 2;
            else
            {
                if (i + 1 <= 19 && _bianli[i + 1, j] == false)
                    return SearchChess(i + 1, j);
                if (j + 1 <= 19 && _bianli[i, j + 1] == false)
                    return SearchChess(i, j + 1);
                if (i - 1 >= 1 && _bianli[i - 1, j] == false)
                    return SearchChess(i - 1, j);
                if (j - 1 >= 1 && _bianli[i, j - 1] == false)
                    return SearchChess(i, j - 1);
                if (j - 1 >= 1 && _bianli[i, j - 1] == false)
                    return SearchChess(i, j - 1);
                if (i - 1 >= 1 && _bianli[i - 1, j] == false)
                    return SearchChess(i - 1, j);
                if (j + 1 <= 19 && _bianli[i, j + 1] == false)
                    return SearchChess(i, j + 1);
                if (i + 1 <= 19 && _bianli[i + 1, j] == false)
                    return SearchChess(i + 1, j);
                return 0;
            }
        }
        public void PlaySound(string wavFile)
        {
            //装载声音
            SoundPlayer soundplay = new SoundPlayer(wavFile);
            //播放声音
            soundplay.Play();
        }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: C#实现围棋游戏

本文链接: https://www.lsjlt.com/news/148427.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#实现围棋游戏
    本文实例为大家分享了C#实现围棋游戏的具体代码,供大家参考,具体内容如下 之所以选择围棋作为大作业一方面是想挑战一下,另一方面是由于从6岁学围棋到11岁放下,再到今天已将近8年了,也...
    99+
    2022-11-13
  • python实现一个围棋小游戏
    一道Python课作业题,大致如下: 编写一个类: 该类Building应具有以下方法: ●一个构造函数,它根本不接受任何参数(除了通常的`self`) ●setHeightRand...
    99+
    2022-11-13
    python围棋程序 python围棋算法
  • 基于Python如何实现围棋游戏
    本篇内容主要讲解“基于Python如何实现围棋游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基于Python如何实现围棋游戏”吧!1.导入模块tkinter:ttk覆盖tkinter部分对象...
    99+
    2023-06-30
  • C#实现飞行棋游戏
    飞行棋主要是讲的方法怎么应用,充分的去理解方法和方法的调用,整体收获还是很大的。 我想的是说一下整体的思路。在编程的时间里,逻辑是最重要的,先干嘛后干嘛,对吧。 直接上个飞行棋的图,...
    99+
    2022-11-12
  • C++实现五子棋小游戏
    本文实例为大家分享了C++实现五子棋小游戏的具体代码,供大家参考,具体内容如下 思路:先用用system(“color 70”)改变控制台的背景色为灰白色,前...
    99+
    2022-11-13
  • 基于Python实现围棋游戏的示例代码
    目录1.导入模块2.初始化棋盘3. 开始游戏4.放弃当前回合落子5.悔棋判断6.重新开始7.右侧太极图的设置8.落子设置9.吃子规则判定设置10.其他11.程序入口12.效果图文件自...
    99+
    2022-11-11
  • C语言实现三子棋游戏
    本文实例为大家分享了C语言实现三子棋游戏的具体代码,供大家参考,具体内容如下 game.h 设置头文件 #define ROW 3 #define COL 3 #include&...
    99+
    2022-11-12
  • C语言实现井字棋游戏
    本文实例为大家分享了C语言实现井字棋游戏的具体代码,供大家参考,具体内容如下 首先,我们需要一个大体的思路,先进行宏观规划,再对细节进行实现。 比如: 1、首先需要一个菜单面板作以修...
    99+
    2022-11-12
  • C语言实现三子棋游戏(棋盘可变)
    本文实例为大家分享了C语言实现三子棋游戏的具体代码,供大家参考,具体内容如下 思路: main函数结构部署 game函数功能的实现 ①创建存储空间 ②初始化存储空间为空格 ③打印棋盘...
    99+
    2022-11-12
  • 基于C++实现五子棋小游戏
    本文实例为大家分享了C++实现五子棋小游戏的具体代码,供大家参考,具体内容如下  (这是一个颜色会变化的呦) #include <iostream> usi...
    99+
    2022-11-13
  • C语言实现井字棋小游戏
    C语言实现简单的“井字棋游戏”,供大家参考,具体内容如下 总体构造: 1.游戏菜单的逻辑实现 2.游戏本体的代码实现 part 1:游戏菜单的整体逻辑 ①简单的通过一个输入0和1的s...
    99+
    2022-11-12
  • C#实现简单的飞行棋游戏
    本文实例为大家分享了C#实现简单飞行棋游戏的具体代码,供大家参考,具体内容如下 下面展示 完整代码: namespace 飞行棋 { class Program ...
    99+
    2022-11-12
  • C#实现简单飞行棋小游戏
    本文实例为大家分享了C#实现简单飞行棋小游戏的具体代码,供大家参考,具体内容如下 目标:实现飞行棋游戏基础功能 玩家在地图触发道具: 1、获得道具,可以进行一次选择 1–交换位置 2...
    99+
    2022-11-11
  • 用C语言实现五子棋游戏
    C语言写五子棋,使用多文件形式,使用代码看起来更好看;在这里我实现的功能是双人博弈,如果要实现人机对战,那么代码就会很复杂; 一.main.c 在主调函数中首先要提供一个给用户选择的...
    99+
    2022-11-12
  • C语言版实现三子棋游戏
    如何用C语言来实现三子棋(井字棋),供大家参考,具体内容如下 大致思路如下 1.先打印出菜单供用户进行游戏还是退出游戏 2.编写打印棋盘的函数 3.编写用户和电脑下棋的函数 4.最后...
    99+
    2022-11-12
  • C++实现简易的五子棋游戏
    本文实例为大家分享了C++实现简易五子棋游戏的具体代码,供大家参考,具体内容如下 //用c++实现五子棋 #include <iostream> #include <...
    99+
    2022-11-13
  • C++代码实现五子棋小游戏
    简单C++代码实现五子棋任务,供大家参考,具体内容如下 首先先展示一下运行的图片 话也不多说,直接分不同代码板块来介绍程序不同功能以及是如何实现的 首先,对于一个五子棋程序,我们要...
    99+
    2022-11-13
  • C++实现五子棋游戏(注释版)
    本文实例为大家分享了C++实现五子棋游戏的具体代码,供大家参考,具体内容如下 本程序是在vs2015环境下编译运行vs左上角:文件–新建–项目–W...
    99+
    2022-11-13
  • 用C语言实现三子棋游戏
    本文实例为大家分享了C语言实现三子棋游戏的具体代码,供大家参考,具体内容如下 初始工作,通过#define 定义一个标识符来表示一个常量(棋盘的横纵坐标) (因为在代码的编写中,会有...
    99+
    2022-11-12
  • 如何用C++实现三子棋游戏
    这篇文章主要介绍了如何用C++实现三子棋游戏的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何用C++实现三子棋游戏文章都会有所收获,下面我们一起来看看吧。游戏描述:先来点真实的!三子棋其实就是我们小时候经常玩...
    99+
    2023-06-26
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作