前言
该中国象棋为单机版程序,实现了全部的象棋走棋,并且实现了悔棋,落子标记等功能。唯一不足的是,该程序没有实现联机功能,后续有时间持续跟进的。
界面效果图
图1
部分代码
//棋子的行走规则
public bool RulesForChess(int row, int col)
{
bool tempCanDrop = false;
//判断是否落在原处
if (row == _pickRow && col == _pickCol)
tempCanDrop = false;
else
{
//象的走棋规则
if (_pickChess == Piece.蓝象)
{
//如果走田子,不绊象脚
if (Math.Abs(_pickRow - row) == 2 && Math.Abs(_pickCol - col) == 2
&& _chess[(_pickRow + row) / 2, (_pickCol + col) / 2] == Piece.无子 && row <= 5)
tempCanDrop = true;
}
//象的走棋规则
else if (_pickChess == Piece.红相)
{
//如果走田子,不绊象脚
if (Math.Abs(_pickRow - row) == 2 && Math.Abs(_pickCol - col) == 2
&& _chess[(_pickRow + row) / 2, (_pickCol + col) / 2] == Piece.无子 && row >= 6)
tempCanDrop = true;
}
//马的走棋规则
else if (_pickChess == Piece.红马 || _pickChess == Piece.蓝马)
{
//如果横着走日字,且不绊马脚
if (Math.Abs(_pickRow - row) == 1 && Math.Abs(_pickCol - col) == 2 &&
_chess[_pickRow, (_pickCol + col) / 2] == Piece.无子)
tempCanDrop = true;
//如果竖着走日字,且不绊马脚
else if (Math.Abs(_pickRow - row) == 2 && Math.Abs(_pickCol - col) == 1 &&
_chess[(_pickRow + row) / 2, _pickCol] == Piece.无子)
tempCanDrop = true;
}
//车的行走规则
else if (_pickChess == Piece.红车 || _pickChess == Piece.蓝车)
{
//如果车横着走
if (_pickRow == row)
{
//比较起点列和落点列的大小
int max = col > _pickCol ? col : _pickCol;
int min = col > _pickCol ? _pickCol : col;
//统计移动路径上棋子的数量
int chessNum = 0;
for (int i = min + 1; i <= max - 1; i++)
if (_chess[row, i] != Piece.无子)
chessNum++;
//当移动路径上棋子数量为0时,才允许落子
if (chessNum == 0)
{
//落子点为无子或对方棋子
if (_chess[row, col] == Piece.无子 || _chess[row, col].ToString().IndexOf(_curPlayer.ToString()) == -1)
tempCanDrop = true;
}
}
//如果车竖着走
else if (_pickCol == col)
{
//比较起点行和落点行的大小
int max = row > _pickRow ? row : _pickRow;
int min = row > _pickRow ? _pickRow : row;
//统计移动路径上棋子的数量
int chessNum = 0;
for (int i = min + 1; i <= max - 1; i++)
if (_chess[i, col] != Piece.无子)
chessNum++;
//当移动路径上棋子数量为0时,才允许落子
if (chessNum == 0)
{
//落子点为无子或对方棋子
if (_chess[row, col] == Piece.无子 || _chess[row, col].ToString().IndexOf(_curPlayer.ToString()) == -1)
tempCanDrop = true;
}
}
}
//士的行走规则
else if (_pickChess == Piece.红士)
{
//斜着走
if(row >= 8 && col <= 6 && col >= 4 && Math.Abs(row - _pickRow) == 1 && Math.Abs(col - _pickCol) == 1)
tempCanDrop = true;
}
//士的行走规则
else if (_pickChess == Piece.蓝士)
{
//斜着走
if (row <= 3 && col <= 6 && col >= 4 && Math.Abs(row - _pickRow) == 1 && Math.Abs(col - _pickCol) == 1)
tempCanDrop = true;
}
//帅或将的行走规则
else if (_pickChess == Piece.红帅)
{
if(row >= 8 && col >= 4 && col <= 6 && (Math.Abs(row - _pickRow) == 1 && Math.Abs(col - _pickCol) == 0 ||
Math.Abs(row - _pickRow) == 0 && Math.Abs(col - _pickCol) == 1))
tempCanDrop = true;
}
//帅或将的行走规则
else if (_pickChess == Piece.蓝将)
{
if (row <= 3 && col >= 4 && col <= 6 && (Math.Abs(row - _pickRow) == 1 && Math.Abs(col - _pickCol) == 0 ||
Math.Abs(row - _pickRow) == 0 && Math.Abs(col - _pickCol) == 1))
tempCanDrop = true;
}
//炮的行走规则
else if (_pickChess == Piece.红炮 || _pickChess == Piece.蓝炮)
{
//如果是竖着走
if (_pickCol == col)
{
//比较起点行和落点行的大小
int max = row > _pickRow ? row : _pickRow;
int min = row > _pickRow ? _pickRow : row;
//统计移动路径上棋子的数量
int chessNum = 0;
for (int i = min + 1; i <= max - 1; i++)
if (_chess[i, col] != Piece.无子)
chessNum++;
//当移动路径上棋子数量为0时,落子点为无子
if (chessNum == 0 && _chess[row, col] == Piece.无子)
tempCanDrop = true;
//当移动路径上棋子数量为1时,落子点为对方棋子
else if(chessNum == 1 && _chess[row, col] != Piece.无子 && _chess[row, col].ToString().IndexOf(_curPlayer.ToString()) == -1)
tempCanDrop = true;
}
//如果是横着走
else if (_pickRow == row)
{
//比较起点行和落点行的大小
int max = col > _pickCol ? col : _pickCol;
int min = col > _pickCol ? _pickCol : col;
//统计移动路径上棋子的数量
int chessNum = 0;
for (int i = min + 1; i <= max - 1; i++)
if (_chess[row, i] != Piece.无子)
chessNum++;
//当移动路径上棋子数量为0时,落子点为无子
if (chessNum == 0 && _chess[row, col] == Piece.无子)
tempCanDrop = true;
//当移动路径上棋子数量为1时,落子点为对方棋子
else if (chessNum == 1 && _chess[row, col] != Piece.无子 && _chess[row, col].ToString().IndexOf(_curPlayer.ToString()) == -1)
tempCanDrop = true;
}
}
//兵或卒的行走规则
else if (_pickChess == Piece.红卒)
{
//如果过了河界
if (_pickRow <= 5 && row <= _pickRow && (Math.Abs(row - _pickRow) == 1 && Math.Abs(col - _pickCol) == 0 || Math.Abs(row - _pickRow) == 0 && Math.Abs(col - _pickCol) == 1))
tempCanDrop = true;
//没有过河界
else if(row < _pickRow && (Math.Abs(row - _pickRow) == 1 && Math.Abs(col - _pickCol) == 0))
tempCanDrop = true;
}
//兵或卒的行走规则
else if (_pickChess == Piece.蓝兵)
{
//如果过了河界
if (_pickRow >= 6 && row >= _pickRow && (Math.Abs(row - _pickRow) == 1 && Math.Abs(col - _pickCol) == 0 || Math.Abs(row - _pickRow) == 0 && Math.Abs(col - _pickCol) == 1))
tempCanDrop = true;
//没有过河界
else if (row > _pickRow && (Math.Abs(row - _pickRow) == 1 && Math.Abs(col - _pickCol) == 0))
tempCanDrop = true;
}
}
return tempCanDrop;
}
完整文件
中国象棋程序