之所以选择围棋作为大作业一方面是想挑战一下,另一方面是由于从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
{
无子 =