c#大作业——围棋(单机版)

博主以围棋作为大作业,挑战自我并重温童年围棋经历。程序大部分为原创,仅初期参考了中国象棋。文章展示程序的效果图及主要代码,包括主窗口和围棋类的代码实现。

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

之所以选择围棋作为大作业一方面是想挑战一下,另一方面是由于从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.youkuaiyun.com/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.youkuaiyun.com/Luoriliming

namespace AlphaGo
{
    //枚举类型:棋子
    public 
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值