📄 chessboard.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
/**
* Title: 棋盘
* author 赵睿
* version:5子棋抽象设计版本3.0
* time 2007-11-17
*/
namespace CheongSam
{
/// <summary>
/// 棋盘结构(GridCount:网格数)(GridSpace:间隔)(Flage:标记)
/// </summary>
struct BoardMode
{
public int GridCount; //棋盘规格15*15
public int GridSpace; //棋盘网格间距
public int Flage; //棋盘标记对应ASCI码
public void init() //初始化棋盘规格
{
this.GridCount = 15;
this.GridSpace = 25;
this.Flage = 64;
}
}
class ChessBoard
{
public static Font BoardFont = new Font("宋体", 10, FontStyle.Bold); //字体
Rectangle rectBoard = new Rectangle(20,40,350,350);//棋盘尺寸
BoardMode boardMode = new BoardMode(); //棋盘的结构
MainUI UI;
Chess chess;
Graphics grap;
Computer computer;
Stack<string> noteStack = new Stack<string>();//棋子记录;
int[,] arrchessboard = new int[15, 15]; //存放棋子;2-空,1-黑,2-白
bool isComPer = false; //true电脑先,false玩家先,先手
bool isBlackWhite = true; //true黑,false白 , 棋子颜色
/// <summary>
/// 属性:棋盘结构(网格数GridCount=15,间隔GridSpace=25,标记Flage=64)
/// </summary>
public Stack<string> NoteStack{get{return this.noteStack;}}
/// <summary>
/// 构造棋盘,完成集合,棋子,等初始化
/// </summary>
/// <param name="obj"></param>
public ChessBoard(MainUI UI)
{
this.UI = UI;
boardMode.init();
intiChessBoard();
}
/// <summary>
/// 方法:初始化棋盘,完成默认设置
/// </summary>
private void intiChessBoard()
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
arrchessboard[i, j] = 2; //初始化为空
}
}
isBlackWhite = true; //黑子为默认棋子
this.noteStack.Clear(); //棋盘记录清空
this.chess = new Chess(this.UI); //构造棋子
computer = new Computer(isComPer);//构造电脑
this.UI.Invalidate();
}
/// <summary>
/// 方法:绘制棋盘,及棋子
/// </summary>
public void DrawChessBoard()
{
using (this.grap = this.UI.CreateGraphics())
{
grap.DrawRectangle(Pens.Black, rectBoard);//画框架
//
//精确画出网格
//
for (int i = 1; i <= boardMode.GridCount; i++)
{
if (i % boardMode.GridCount != 0)
{
//横向标记数值
grap.DrawString(((char)(i + boardMode.Flage)).ToString(), BoardFont,
Brushes.Black, new PointF((int)((i - 1) * boardMode.GridSpace) +
rectBoard.Left - 5, rectBoard.Top + rectBoard.Height));
//纵向线条
grap.DrawLine(Pens.Black, new Point((int)(i * boardMode.GridSpace + rectBoard.Left), rectBoard.Top),
new Point((int)(i * boardMode.GridSpace + rectBoard.Left), rectBoard.Top + rectBoard.Height));
//纵向标记数值
grap.DrawString("" + i, BoardFont, Brushes.Black,
new PointF(0, (int)((i - 1) * boardMode.GridSpace) +
rectBoard.Top - 5));
//横向线条
grap.DrawLine(Pens.Black, new Point(rectBoard.Left, (int)(i * boardMode.GridSpace + rectBoard.Top)),
new Point(rectBoard.Left + rectBoard.Width, (int)(i * boardMode.GridSpace + rectBoard.Top)));
if (i == 3 || i == 11)
{ //画交叉点
grap.FillRectangle(Brushes.Black, new Rectangle(rectBoard.Left +
i * boardMode.GridSpace - 5,
(int)(i * boardMode.GridSpace + rectBoard.Top) - 5, 10, 10));
grap.FillRectangle(Brushes.Black, new Rectangle(rectBoard.Left +
11 * boardMode.GridSpace - 5,
(int)(i * boardMode.GridSpace + rectBoard.Top) - 5, 10, 10));
grap.FillRectangle(Brushes.Black, new Rectangle(rectBoard.Left +
i * boardMode.GridSpace - 5,
(int)(11 * boardMode.GridSpace + rectBoard.Top) - 5, 10, 10));
}
}
else
{
//纵向标记数值
grap.DrawString(((char)(i + 64)).ToString(), BoardFont, Brushes.Black,
new PointF((int)((i - 1) * boardMode.GridSpace) + rectBoard.Left - 5,
rectBoard.Top + rectBoard.Height));
//横向标记数值
grap.DrawString("" + i, BoardFont, Brushes.Black,
new PointF(0, (int)((i - 1) * boardMode.GridSpace) +
rectBoard.Top - 5));
}
}
}
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (arrchessboard[i, j] == 0)
{
this.chess.DrawChess(i, j, true); //绘制黑子
}
if (arrchessboard[i, j] == 1)
{
this.chess.DrawChess(i, j, false);//绘制白子
}
}
}
}
/// <summary>
/// 方法:主要完成落棋的一系列检查
/// </summary>
/// <param name="m"></param>
/// <param name="n"></param>
public void DownChess(int m, int n)
{
chess.DrawChess(m, n, isBlackWhite);
arrchessboard[m, n] = isBlackWhite ? 0 : 1; //保存下子情况0是黑,1是白
if (isBlackWhite)//压入集合,记录下棋步骤
this.noteStack.Push("黑:" + m.ToString() + "," + n.ToString());
else
this.noteStack.Push("白:" + m.ToString() + "," + n.ToString());
//判断禁手
if (Rule.Result(m, n, this.arrchessboard) < 6)
{
switch (Rule.Result(m, n, this.arrchessboard))
{
case 1:
MessageBox.Show("黑棋双三禁手失败!");
break;
case 2:
MessageBox.Show("黑棋双四禁手失败!");
break;
case 3:
MessageBox.Show("黑棋长连禁手失败!");
break;
case 4:
if (isBlackWhite)
{
MessageBox.Show("黑棋胜利!");
Start();
}
else
{
MessageBox.Show("白棋胜利!");
Start();
}
break;
case 5:
MessageBox.Show("平局!");
Start();
break;
}
return;
}
else
{
//交换当前棋子颜色
isBlackWhite = !isBlackWhite;
}
}
public void PersonDownStone(int x, int y)
{
if (x < 20 || x > 370 || y < 40 || y > 400)
{
return;
}
//取下棋点
int m = (int)x / 25;
int n = (int)y / 25;
if (!Rule.IsExist(m, n, arrchessboard))
{
DownChess(m, n);
ComputerDownStone();
}
}
/// <summary>
/// 电脑下棋
/// </summary>
private void ComputerDownStone()
{
int m, n;
do
{
computer.Down(this.arrchessboard);
m = computer.X;
n = computer.Y;
}
while (Rule.IsExist(m, n, arrchessboard));
DownChess(m, n);
}
public void Start(bool isComPer)
{
this.isComPer = isComPer;
Start();
}
/// <summary>
/// 开始
/// </summary>
private void Start()
{
intiChessBoard();
this.DrawChessBoard();
if (isComPer)
{
ComputerDownStone();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -