象棋1.0
1.初始化棋盘
棋子由32个PictureBox控件组成,这些控件被列入数组。
PictureBox[] qizi=new PictureBox[32]
。根据不同角色(红方或黑方)初始化棋盘。棋盘表示就是使用一种数据结构来描述棋盘及棋盘上的棋子,我们使用一个二维数组。一个典型的中国象棋棋盘是使用 9 ×10 的二维数组表示。每一个元素代表棋盘上的一个交点。一个没有棋子的交点所对应的元素是 -1,红方对应的元素是 0至15,黑方则用 16至31。
int
[,] arry=new int[,]方便下棋时判断是否能移动或吃棋。
private
void Initchessboard()//
初始化旗子坐标
{
}
2.走棋 (规则没实现)
对于象棋来说,有马走日,象走田等一系列复杂的规则。走法产生是博弈程序中一个相当复杂而且耗费运算时间的方面。不过,通过良好的数据结构,可以显著地提高生成的速度。
算法如下:
(1) 检查起始位置的棋子是否为当前方
(2) 检查终点位置棋子是否与起始位置不同
(3) 检查终点位置棋子是否为当前方,是则返回假
(4) 根据棋子名称的不同,按相应规则判断
A, 如果为“车”,检查是否走直线,及中间是否有子。
B, 如果为“马”,检查是否走“日”字,是否蹩脚。
C, 如果为“炮”,检查是否走直线,判断是否吃子,如果是吃子,则检查中间是否只有一个棋子,如果不吃则检查中间是否有棋子。
D, 如果为“兵”,检查是否走直线,走一步及向前走,根据是否过河,检查是否横走。
E, 如果为“将”,检查是否走直线,走一步及是否超过范围。
F, 如果为“士”,检查是否走斜线,走一步及是否超出范围。
G, 如果为“象”,检查是否走“田”字,是否蹩脚,及是否超出范围。
如何分辨棋子?程序中采用了控件自带属性tag,将棋子名字付值给tag,移动棋子时,通过tag值判断走法。
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace mychess
... {
public partial class NewChessForm : Form
...{
private int idx1; //选中图片框的id
int x1, y1;
private bool first_click = true;
PictureBox[] qizi = new PictureBox[32];
private enum Chess ...{ Black, Red };
private Chess mplayer;
int[,] arry;
public NewChessForm()
...{
InitializeComponent();
}
private void NewChessForm_Load(object sender, EventArgs e)//窗体加载
...{
Initchessboard();
mplayer = Chess.Red;
}
private void picchessboard_MouseDown(object sender, MouseEventArgs e)
...{
int x2, y2;
x2 = (e.X - 5) / 60;
y2 = (e.Y - 5) / 60;
//此处实现不吃子移动判断 ************
//idx1,x1,y,x2,y2判断是否符合走棋规则
//.......
//符合走棋则则移动到x2,y2;
//MessageBox.Show("坐标x2:" + e.X + "坐标y2:" + e.Y);
qizi[idx1].Top = y2 * 60 + 8;
qizi[idx1].Left = x2 * 60 + 8;
arry[x2, y2] = idx1;
first_click = true;
}
private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
...{
//这里图片MouseDown处理公共事件过程
//写图片处理
PictureBox P = (PictureBox)sender;
string a = P.Tag.ToString();//知道棋子名
//MessageBox.Show(a + "坐标x1:" + e.X + "坐标y1:" + e.Y, a + "x:" + P.Left + "y:" + P.Top);
if (first_click == true)
...{
x1 = (P.Left - 8) / 60;
y1 = (P.Top - 8) / 60;
idx1 = arry[x1, y1];//获取棋子id,即在qizi[32]数组中的下标
first_click = false;
}
else if (first_click == false) //吃子
...{
int x2, y2, idx2;
x2 = (P.Left - 8) / 60;
y2 = (P.Top - 8) / 60;
idx2 = arry[x2, y2];//获取被吃棋子,即在qizi[32]数组中的下标
first_click = true;
//idx1,x1,y,x2,y2判断是否符合走棋规则
//.....
//.....规则
/**///// 此处实现吃子移动判断*******************.......
//符合走棋则则移动到x2,y2;
//MessageBox.Show("坐标x2:" + e.X + "坐标y2:" + e.Y);
qizi[idx1].Top = y2 * 60 + 8;
qizi[idx1].Left = x2 * 60 + 8;
arry[x2, y2] = idx1;
qizi[idx2].Visible = false;
}
}
private void Initchessboard()//初始化旗子坐标
...{
/**//////红方 /////黑方
qizi[0] = pictureBox1; qizi[16] = pictureBox17;
qizi[1] = pictureBox2; qizi[17] = pictureBox18;
qizi[2] = pictureBox3; qizi[18] = pictureBox19;
qizi[3] = pictureBox4; qizi[19] = pictureBox20;
qizi[4] = pictureBox5; qizi[20] = pictureBox21;
qizi[5] = pictureBox6; qizi[21] = pictureBox22;
qizi[6] = pictureBox7; qizi[22] = pictureBox23;
qizi[7] = pictureBox8; qizi[23] = pictureBox24;
qizi[8] = pictureBox9; qizi[24] = pictureBox25;
qizi[9] = pictureBox10; qizi[25] = pictureBox26;
qizi[10] = pictureBox11; qizi[26] = pictureBox27;
qizi[11] = pictureBox12; qizi[27] = pictureBox28;
qizi[12] = pictureBox13; qizi[28] = pictureBox29;
qizi[13] = pictureBox14; qizi[29] = pictureBox30;
qizi[14] = pictureBox15; qizi[30] = pictureBox31;
qizi[15] = pictureBox16; qizi[31] = pictureBox32;
for (int i = 0; i < 32; i++)
...{
//黑方对应的是 0至15
qizi[i].MouseDown += pic_MouseDown;
qizi[i].Parent = picchessboard;
qizi[i].Visible = true;
qizi[i].BringToFront();
}
/**//////
if (mplayer == Chess.Black)
...{
for (int i = 0; i <= 8; i++)//红方
qizi[i].Location = new Point(60 * i + 8, 8);
qizi[9].Location = new Point(8 + 60 * 1, 8 + 60 * 2);
qizi[10].Location = new Point(8 + 60 * 7, 8 + 60 * 2);
for (int i = 11; i <= 15; i++)
qizi[i].Location = new Point(8 + 60 * 2 * (i - 11), 8 + 60 * 3);
for (int i = 16; i <= 24; i++)
qizi[i].Location = new Point(8 + 60 * (i - 16), 8 + 9 * 60);
qizi[25].Location = new Point(8 + 60 * 1, 8 + 60 * 7);
qizi[26].Location = new Point(8 + 60 * 7, 8 + 60 * 7);
for (int i = 27; i <= 31; i++)
qizi[i].Location = new Point(8 + 60 * 2 * (i - 27), 8 + 60 * 6);
for (int i = 0; i <= 31; i++)
qizi[i].Visible = true;
for (int i = 16; i <= 31; i++)
...{
qizi[i].Cursor = Cursors.Hand;
}
arry = new int[,]...{
...{0,-1,-1,11,-1,-1,27,-1,-1,16}, ...{1,-1,9,-1,-1,-1,-1,25,-1,17},
...{2,-1,-1,12,-1,-1,28,-1,-1,18}, ...{3,-1,-1,-1,-1,-1,-1,-1,-1,19},
...{4,-1,-1,13,-1,-1,29,-1,-1,20}, ...{5,-1,-1,-1,-1,-1,-1,-1,-1,21},
...{6,-1,-1,14,-1,-1,30,-1,-1,22}, ...{7,-1,10,-1,-1,-1,-1,26,-1,23},
...{8,-1,-1,15,-1,-1,31,-1,-1,24}};
}
if (mplayer == Chess.Red)
...{
for (int i = 0; i <= 8; i++)//黑方
qizi[i].Location = new Point(8 + 60 * i, 8 + 9 * 60);
qizi[9].Location = new Point(8 + 60 * 1, 8 + 60 * 7);
qizi[10].Location = new Point(8 + 60 * 7, 8 + 60 * 7);
for (int i = 11; i <= 15; i++)
qizi[i].Location = new Point(8 + 60 * 2 * (i - 11), 8 + 60 * 6);
for (int i = 16; i <= 24; i++)
qizi[i].Location = new Point(8 + 60 * (i - 16), 8);
qizi[25].Location = new Point(8 + 60 * 1, 8 + 60 * 2);
qizi[26].Location = new Point(8 + 60 * 7, 8 + 60 * 2);
for (int i = 27; i <= 31; i++)
qizi[i].Location = new Point(8 + 60 * 2 * (i - 27), 8 + 60 * 3);
arry = new int[,]...{...{16,-1,-1,27,-1,-1,11,-1,-1,0},...{17,-1,25,-1,-1,-1,-1,9,-1,1},
...{18,-1,-1,28,-1,-1,12,-1,-1,2},...{19,-1,-1,-1,-1,-1,-1,-1,-1,3},
...{20,-1,-1,29,-1,-1,13,-1,-1,4},...{21,-1,-1,-1,-1,-1,-1,-1,-1,5},
...{22,-1,-1,30,-1,-1,14,-1,-1,6},...{23,-1,26,-1,-1,-1,-1,10,-1,7},
...{24,-1,-1,31,-1,-1,15,-1,-1,8}};
for (int i = 0; i <= 31; i++)
qizi[i].Visible = true;
for (int i = 0; i <= 15; i++)
...{
qizi[i].Cursor = Cursors.Hand;
}
for (int i = 16; i <= 31; i++)
...{
qizi[i].Cursor = Cursors.Default;
}
}
}
private void picchessboard_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
...{
label1.Text = "x:" + e.X + "y:" + e.Y;
}
private void button1_Click(object sender, EventArgs e)
...{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace mychess
... {
public partial class NewChessForm : Form
...{
private int idx1; //选中图片框的id
int x1, y1;
private bool first_click = true;
PictureBox[] qizi = new PictureBox[32];
private enum Chess ...{ Black, Red };
private Chess mplayer;
int[,] arry;
public NewChessForm()
...{
InitializeComponent();
}
private void NewChessForm_Load(object sender, EventArgs e)//窗体加载
...{
Initchessboard();
mplayer = Chess.Red;
}
private void picchessboard_MouseDown(object sender, MouseEventArgs e)
...{
int x2, y2;
x2 = (e.X - 5) / 60;
y2 = (e.Y - 5) / 60;
//此处实现不吃子移动判断 ************
//idx1,x1,y,x2,y2判断是否符合走棋规则
//.......
//符合走棋则则移动到x2,y2;
//MessageBox.Show("坐标x2:" + e.X + "坐标y2:" + e.Y);
qizi[idx1].Top = y2 * 60 + 8;
qizi[idx1].Left = x2 * 60 + 8;
arry[x2, y2] = idx1;
first_click = true;
}
private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
...{
//这里图片MouseDown处理公共事件过程
//写图片处理
PictureBox P = (PictureBox)sender;
string a = P.Tag.ToString();//知道棋子名
//MessageBox.Show(a + "坐标x1:" + e.X + "坐标y1:" + e.Y, a + "x:" + P.Left + "y:" + P.Top);
if (first_click == true)
...{
x1 = (P.Left - 8) / 60;
y1 = (P.Top - 8) / 60;
idx1 = arry[x1, y1];//获取棋子id,即在qizi[32]数组中的下标
first_click = false;
}
else if (first_click == false) //吃子
...{
int x2, y2, idx2;
x2 = (P.Left - 8) / 60;
y2 = (P.Top - 8) / 60;
idx2 = arry[x2, y2];//获取被吃棋子,即在qizi[32]数组中的下标
first_click = true;
//idx1,x1,y,x2,y2判断是否符合走棋规则
//.....
//.....规则
/**///// 此处实现吃子移动判断*******************.......
//符合走棋则则移动到x2,y2;
//MessageBox.Show("坐标x2:" + e.X + "坐标y2:" + e.Y);
qizi[idx1].Top = y2 * 60 + 8;
qizi[idx1].Left = x2 * 60 + 8;
arry[x2, y2] = idx1;
qizi[idx2].Visible = false;
}
}
private void Initchessboard()//初始化旗子坐标
...{
/**//////红方 /////黑方
qizi[0] = pictureBox1; qizi[16] = pictureBox17;
qizi[1] = pictureBox2; qizi[17] = pictureBox18;
qizi[2] = pictureBox3; qizi[18] = pictureBox19;
qizi[3] = pictureBox4; qizi[19] = pictureBox20;
qizi[4] = pictureBox5; qizi[20] = pictureBox21;
qizi[5] = pictureBox6; qizi[21] = pictureBox22;
qizi[6] = pictureBox7; qizi[22] = pictureBox23;
qizi[7] = pictureBox8; qizi[23] = pictureBox24;
qizi[8] = pictureBox9; qizi[24] = pictureBox25;
qizi[9] = pictureBox10; qizi[25] = pictureBox26;
qizi[10] = pictureBox11; qizi[26] = pictureBox27;
qizi[11] = pictureBox12; qizi[27] = pictureBox28;
qizi[12] = pictureBox13; qizi[28] = pictureBox29;
qizi[13] = pictureBox14; qizi[29] = pictureBox30;
qizi[14] = pictureBox15; qizi[30] = pictureBox31;
qizi[15] = pictureBox16; qizi[31] = pictureBox32;
for (int i = 0; i < 32; i++)
...{
//黑方对应的是 0至15
qizi[i].MouseDown += pic_MouseDown;
qizi[i].Parent = picchessboard;
qizi[i].Visible = true;
qizi[i].BringToFront();
}
/**//////
if (mplayer == Chess.Black)
...{
for (int i = 0; i <= 8; i++)//红方
qizi[i].Location = new Point(60 * i + 8, 8);
qizi[9].Location = new Point(8 + 60 * 1, 8 + 60 * 2);
qizi[10].Location = new Point(8 + 60 * 7, 8 + 60 * 2);
for (int i = 11; i <= 15; i++)
qizi[i].Location = new Point(8 + 60 * 2 * (i - 11), 8 + 60 * 3);
for (int i = 16; i <= 24; i++)
qizi[i].Location = new Point(8 + 60 * (i - 16), 8 + 9 * 60);
qizi[25].Location = new Point(8 + 60 * 1, 8 + 60 * 7);
qizi[26].Location = new Point(8 + 60 * 7, 8 + 60 * 7);
for (int i = 27; i <= 31; i++)
qizi[i].Location = new Point(8 + 60 * 2 * (i - 27), 8 + 60 * 6);
for (int i = 0; i <= 31; i++)
qizi[i].Visible = true;
for (int i = 16; i <= 31; i++)
...{
qizi[i].Cursor = Cursors.Hand;
}
arry = new int[,]...{
...{0,-1,-1,11,-1,-1,27,-1,-1,16}, ...{1,-1,9,-1,-1,-1,-1,25,-1,17},
...{2,-1,-1,12,-1,-1,28,-1,-1,18}, ...{3,-1,-1,-1,-1,-1,-1,-1,-1,19},
...{4,-1,-1,13,-1,-1,29,-1,-1,20}, ...{5,-1,-1,-1,-1,-1,-1,-1,-1,21},
...{6,-1,-1,14,-1,-1,30,-1,-1,22}, ...{7,-1,10,-1,-1,-1,-1,26,-1,23},
...{8,-1,-1,15,-1,-1,31,-1,-1,24}};
}
if (mplayer == Chess.Red)
...{
for (int i = 0; i <= 8; i++)//黑方
qizi[i].Location = new Point(8 + 60 * i, 8 + 9 * 60);
qizi[9].Location = new Point(8 + 60 * 1, 8 + 60 * 7);
qizi[10].Location = new Point(8 + 60 * 7, 8 + 60 * 7);
for (int i = 11; i <= 15; i++)
qizi[i].Location = new Point(8 + 60 * 2 * (i - 11), 8 + 60 * 6);
for (int i = 16; i <= 24; i++)
qizi[i].Location = new Point(8 + 60 * (i - 16), 8);
qizi[25].Location = new Point(8 + 60 * 1, 8 + 60 * 2);
qizi[26].Location = new Point(8 + 60 * 7, 8 + 60 * 2);
for (int i = 27; i <= 31; i++)
qizi[i].Location = new Point(8 + 60 * 2 * (i - 27), 8 + 60 * 3);
arry = new int[,]...{...{16,-1,-1,27,-1,-1,11,-1,-1,0},...{17,-1,25,-1,-1,-1,-1,9,-1,1},
...{18,-1,-1,28,-1,-1,12,-1,-1,2},...{19,-1,-1,-1,-1,-1,-1,-1,-1,3},
...{20,-1,-1,29,-1,-1,13,-1,-1,4},...{21,-1,-1,-1,-1,-1,-1,-1,-1,5},
...{22,-1,-1,30,-1,-1,14,-1,-1,6},...{23,-1,26,-1,-1,-1,-1,10,-1,7},
...{24,-1,-1,31,-1,-1,15,-1,-1,8}};
for (int i = 0; i <= 31; i++)
qizi[i].Visible = true;
for (int i = 0; i <= 15; i++)
...{
qizi[i].Cursor = Cursors.Hand;
}
for (int i = 16; i <= 31; i++)
...{
qizi[i].Cursor = Cursors.Default;
}
}
}
private void picchessboard_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
...{
label1.Text = "x:" + e.X + "y:" + e.Y;
}
private void button1_Click(object sender, EventArgs e)
...{
}
}
}