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