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
    {
        无子 =
  • 13
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
原型模式是一种创建型设计模式,它允许你通过复制一个已经存在的对象来创建新的对象,而不是通过实例化来创建。这种方式可以大大减少对象的创建时间和内存消耗。 在C#中,原型模式的实现需要满足两个条件: 1. 实现ICloneable接口,该接口只有一个方法Clone(),用于复制对象。 2. 对象必须是可复制的,即必须是浅复制或深复制。 浅复制是指复制一个对象,但是不复制对象中的引用类型成员变量,这意味着复制的对象和原始对象共享相同的引用类型成员变量。深复制则是复制一个对象及其引用类型成员变量,这意味着复制的对象和原始对象不共享相同的引用类型成员变量。 下面是一个使用原型模式的示例代码: ```csharp using System; namespace PrototypePattern { // 实现ICloneable接口 public class Person : ICloneable { public string Name { get; set; } public int Age { get; set; } public Address Address { get; set; } public object Clone() { // 深复制 return new Person { Name = this.Name, Age = this.Age, Address = new Address { Street = this.Address.Street, City = this.Address.City, State = this.Address.State } }; } public override string ToString() { return $"Name: {Name}, Age: {Age}, Address: {Address}"; } } public class Address { public string Street { get; set; } public string City { get; set; } public string State { get; set; } public override string ToString() { return $"{Street}, {City}, {State}"; } } class Program { static void Main(string[] args) { var person1 = new Person { Name = "Tom", Age = 20, Address = new Address { Street = "123 Main St", City = "New York", State = "NY" } }; // 使用Clone方法复制对象 var person2 = (Person)person1.Clone(); person2.Name = "Jerry"; person2.Address.Street = "456 Elm St"; Console.WriteLine(person1); Console.WriteLine(person2); } } } ``` 输出结果: ``` Name: Tom, Age: 20, Address: 123 Main St, New York, NY Name: Jerry, Age: 20, Address: 456 Elm St, New York, NY ``` 可以看到,使用原型模式可以方便地创建新的对象,而且不需要关心对象的创建过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值