棋谱:
棋谱 红方 黑方
1. 炮二平五 炮2进3
2. 炮八退一 象7进5
3. 炮五平八 象5进3
4. 前炮进七 前象退5
5. 后炮进一 象5进3
6. 前炮退一 后象进5
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace 我的网络象棋CSharp版
{
public partial class Fu_pan : Form
{
public const short REDPLAYER = 20;
public const short BLACKPLAYER = 0;
//map是布局,r是棋子半径,CurSelect表示当前选中的棋子,LocalPlayer记录自己是红是黑
int[,] Map = new int[10, 11];
int CurSelect;
int r;
public int LocalPlayer;
public PictureBox[] picChess = new PictureBox[37]; //象棋棋子图片控件
private StreamReader Myread;
private string s; //每步走棋信息
private int Step_Num = 0; //步数
private ArrayList step_info = new ArrayList();
string pathfile; //复盘打开的文件名
public Fu_pan()
{
InitializeComponent();
}
public Graphics GetGraphicsObject(ref PictureBox pic)
{
System.Drawing.Graphics g;
Bitmap bmp = new Bitmap(pic.Width, pic.Height);
pic.Image = bmp;
g = Graphics.FromImage(bmp);
return g;
}
//绘制棋盘。如果愿意,可以用漂亮的棋盘图片代替
private void DrawBoard()
{
int i;
//获取对将用于绘图的图形对象的引用创建图形图像。
//Dim g As Graphics = picBoard.CreateGraphics
//获取对将用于绘图的图形对象的,使用这个GetGraphicsObject函数
Graphics g = GetGraphicsObject(ref picBoard);
Pen myPen = new Pen(Color.Red);
myPen.Width = 1;
//r = picBoard.ClientRectangle.Width / 18
r = 25;
picBoard.Height = r * 20;
for (i = 0; i <= 8; i++)
{
//竖线
if (i == 0 | i == 8)
{
myPen.Width = 2;
}
else
{
myPen.Width = 1;
}
g.DrawLine(myPen, r + i * 2 * r, r, r + i * 2 * r, r * 2 * 10 - r + 1);
}
for (i = 0; i <= 9; i++)
{
//横线
if (i == 0 | i == 9)
{
myPen.Width = 2;
}
else
{
myPen.Width = 1;
}
g.DrawLine(myPen, r, r + i * 2 * r, r * 2 * 9 - r, r + i * 2 * r);
}
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(r + 1, r + r * 8 + 1, r * 9 * 2 - 2 * r - 2, 2 * r - 2);
System.Drawing.SolidBrush brush1 = new System.Drawing.SolidBrush(Color.Brown);
g.DrawEllipse(System.Drawing.Pens.Black, rectangle);
g.DrawRectangle(System.Drawing.Pens.Blue, rectangle);
g.FillRectangle(brush1, rectangle);
Font font1 = new System.Drawing.Font("Arial", 20);
System.Drawing.SolidBrush brush2 = new System.Drawing.SolidBrush(Color.Yellow);
g.DrawString(" 汉界 楚河", font1, brush2, (r + 1), (r + r * 8 + 1));
//g.DrawLine(myPen, r + 1, r + r * 8 + 1, r * 9 * 2 - r - 1, r + r * 8 + r * 2 - 1)
//画九宫斜线
g.DrawLine(myPen, r + r * 6 + 1, r + 1, r + r * 6 + r * 4 - 1, r + r * 4 - 1);
g.DrawLine(myPen, r + r * 6 + 1, r + r * 4 - 1, r + r * 6 + r * 4 - 1, r + 1);
g.DrawLine(myPen, r + r * 6 + 1, r * 14 + r + 1, r + r * 6 + r * 4 - 1, r * 14 + r + r * 4 - 1);
g.DrawLine(myPen, r + r * 6 + 1, r * 14 + r + r * 4 - 1, r + r * 6 + r * 4 - 1, r * 14 + r + 1);
}
private void LoadChess()
{
string str;
string path;
path = System.Windows.Forms.Application.StartupPath;
// bin路径
int i;
for (i = 1; i <= 36; i++)
{
//黑方对应的是 1至16,红方对应的是21至36
picChess[i] = new PictureBox();
this.Controls.Add(picChess[i]);
picChess[i].SetBounds(0, 0, (r - 1) * 2, (r - 1) * 2);
picChess[i].BackColor = System.Drawing.SystemColors.GrayText;
picChess[i].Name = "MyPic" + i.ToString();
picChess[i].SizeMode = PictureBoxSizeMode.StretchImage;
picChess[i].Width = 48;
//35
picChess[i].Height = 48;
//35
str = path + "//image//q" + i.ToString() + ".jpg";
if (i < 13 & i > 0)
picChess[i].Image = Image.FromFile(str);
if (i < 33 & i > 20)
picChess[i].Image = Image.FromFile(str);
picChess[i].Text = i.ToString();
picChess[i].Visible = false;
picChess[i].BringToFront();
//AddHandler CType(picChess[i], PictureBox).Click, AddressOf pic_click
//AddHandler picChess[i].Click, AddressOf pic_click
}
}
private void Draw_qizi()
{
//显示棋盘上的棋子
string str;
string path;
path = System.Windows.Forms.Application.StartupPath;
// bin路径
int i;
//默认红方棋子在下方,黑方棋子在上方
picChess[1].Tag = "将"; picChess[21].Tag = "帅";
MoveChess(1, 5, 1); MoveChess(21, 5, 10);
picChess[2].Tag = "士"; picChess[22].Tag = "仕";
MoveChess(2, 4, 1); MoveChess(22, 4, 10);
picChess[3].Tag = "士"; picChess[23].Tag = "仕";
MoveChess(3, 6, 1); MoveChess(23, 6, 10);
picChess[4].Tag = "象"; picChess[24].Tag = "相";
MoveChess(4, 3, 1); MoveChess(24, 3, 10);
picChess[5].Tag = "象"; picChess[25].Tag = "相";
MoveChess(5, 7, 1); MoveChess(25, 7, 10);
picChess[6].Tag = "马"; picChess[26].Tag = "马";
MoveChess(6, 2, 1); MoveChess(26, 2, 10);
picChess[7].Tag = "马"; picChess[27].Tag = "马";
MoveChess(7, 8, 1); MoveChess(27, 8, 10);
picChess[8].Tag = "车"; picChess[28].Tag = "车";
MoveChess(8, 1, 1); MoveChess(28, 1, 10);
picChess[9].Tag = "车"; picChess[29].Tag = "车";
MoveChess(9, 9, 1); MoveChess(29, 9, 10);
picChess[10].Tag = "炮"; picChess[30].Tag = "炮";
MoveChess(10, 2, 3); MoveChess(30, 2, 8);
picChess[11].Tag = "炮"; picChess[31].Tag = "炮";
MoveChess(11, 8, 3); MoveChess(31, 8, 8);
for (i = 12; i <= 16; i++)
{
picChess[i].Tag = "卒";
MoveChess(i, (i - 12) * 2 + 1, 4);
str = path + "//image//q12.jpg";
picChess[i].Image = Image.FromFile(str);
picChess[20 + i].Tag = "兵";
MoveChess(20 + i, (i - 12) * 2 + 1, 7);
str = path + "//image//q32.jpg";
picChess[20 + i].Image = Image.FromFile(str);
}
for (i = 1; i <= 16; i++)
{
picChess[i].Visible = true;
picChess[20 + i].Visible = true;
}
int j;
int c;
//当游戏者是黑方BLACKPLAYER时,需要将棋子对调一下
if (LocalPlayer == BLACKPLAYER)
{
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= 5; j++)
{
if (Map[i, j] != 0)
{
c = Map[i, 11 - j];
MoveChess(Map[i, j], i, 11 - j);
MoveChess(c, i, j);
}
}
}
}
}
private int GetPointX(int x)
{
return (x) / (2 * r) + 1;
//屏幕像素坐标转换成在棋盘中坐标值
}
private int GetPointY(int y)
{
return (y) / (2 * r) + 1;
}
private int GetChessX(int idx)
{
//棋子在棋盘中坐标值
return (picChess[idx].Left) / (2 * r) + 1;
}
private int GetChessY(int idx)
{
return (picChess[idx].Top) / (2 * r) + 1;
}
//动画效果移动棋子
private void MoveChessTo(int idx, int x, int y)
{
x = x * 2 * r - 2 * r;
y = y * 2 * r - 2 * r;
int step1;
step1 = Convert.ToInt16(Math.Sqrt(Math.Pow((x - picChess[idx].Left), 2) + Math.Pow((y - picChess[idx].Top), 2))) / 50;
if (step1 == 0)
step1 = 1;
while (Math.Abs(picChess[idx].Left - x) > step1 || Math.Abs(picChess[idx].Top - y) > step1)
{
if (x != picChess[idx].Left)
{
picChess[idx].Left = picChess[idx].Left + step1 * Math.Abs(picChess[idx].Left - x) / (x - picChess[idx].Left);
}
if (y != picChess[idx].Top)
{
picChess[idx].Top = picChess[idx].Top + step1 * Math.Abs(picChess[idx].Top - y) / (y - picChess[idx].Top);
}
System.Windows.Forms.Application.DoEvents();
}
}
private void cls_map()
{
int i;
int j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= 10; j++)
{
Map[i, j] = 0;
}
}
}
private void MoveChess(int idx, int x, int y)
{
//MoveChessTo(idx, x, y)
//Map[x, y] = idx
picChess[idx].Parent = picBoard;
picChess[idx].Left = (x - 1) * 2 * r;
picChess[idx].Top = (y - 1) * 2 * r;
picChess[idx].BackColor = System.Drawing.SystemColors.GrayText;
Map[x, y] = idx;
}
private void Map_modify(int idx, int x, int y)
{
Map[GetChessX(idx), GetChessY(idx)] = 0;
//原处显示一个无棋子的图片框
No_chessPic.Left = picChess[idx].Left + r ;
No_chessPic.Top = picChess[idx].Top + r -r/2-2;
if (Map[x, y] != 0)
{
picChess[Map[x, y]].Visible = false;
}
//动画效果移动棋子
MoveChessTo(idx, x, y);
Map[x, y] = idx;
}
private void Fu_pan_Load(object sender, EventArgs e)//Load事件
{
cls_map();
LocalPlayer = REDPLAYER;
}
//删除窗体中所有控件名是APattern格式的控件
void DeleteControl(Control APanent, string APattern)
{
for (int i = APanent.Controls.Count - 1; i >= 0; i--)
{
if (System.Text.RegularExpressions.Regex.IsMatch(
APanent.Controls[i].Name, APattern))
APanent.Controls.RemoveAt(i);
else DeleteControl(APanent.Controls[i], APattern);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
DeleteControl(this, @"^MyPic/w*");//删除已有的MyPic开头的控件
listBox1.Items.Clear();
Step_Num = 0;
step_info.Clear();
No_chessPic.Visible = false;
cls_map();
LocalPlayer = REDPLAYER;
OpenFileDialog DlgOpenFile = new OpenFileDialog();
//创建一个打开文件对话框实例
DlgOpenFile.Filter = "txt files(*.txt)|*.txt";
DlgOpenFile.RestoreDirectory = true;
DlgOpenFile.ShowDialog();
pathfile = DlgOpenFile.FileName;
//"棋谱1.txt"
Myread = new StreamReader(pathfile, System.Text.Encoding.Default);
s = Myread.ReadLine(); //读取首行---棋谱 红方 黑方
s = Myread.ReadLine(); //读取第一步
//棋谱格式("前三位数字,第四位点号,第5位空格,第6--9位红方走棋信息,")
//棋谱格式("第10,11位空格,第12位黑方走棋信息,")
while ((s != null))
{
s = s.Substring(5, s.Length - 5);
step_info.Add(s.Substring(0, 4)); //红方走棋信息
if (s.Length - 6 < 4) break; //仅有红棋,无黑棋信息
s = s.Substring(6, s.Length - 6);
step_info.Add(s.Substring(0, 4)); //黑方走棋信息
s = Myread.ReadLine();
}
Myread.Close();
LoadChess();
DrawBoard();
Draw_qizi();
button2.Enabled = true;
button3.Enabled = true;
}
private bool IsMyChess(int idx)
{
bool functionReturnValue = false;
if (0 < idx - LocalPlayer & idx - LocalPlayer < 20)
{
functionReturnValue = true;
}
else
{
functionReturnValue = false;
}
return functionReturnValue;
}
private void button2_Click(object sender, System.EventArgs e)
{
//单步复盘
string s,chess_p,GoDirect,c;
int a, b, x1=0, y1=0, x2, y2, dx=0, dy=0, idx=0;
c = "";
if (Step_Num < step_info.Count)
{
No_chessPic.Visible = true;
if (CurSelect != 0)
{
//上次移动棋子的ID
picChess[CurSelect].BackColor = System.Drawing.SystemColors.GrayText;
picChess[CurSelect].BorderStyle = BorderStyle.None;
}
if (LocalPlayer == REDPLAYER)
{
//红方
s = step_info[Step_Num].ToString() ;
chess_p = s.Substring(0, 1);
if (chess_p == "前" | chess_p == "后")
{
//在垂直方向有相同子
c = chess_p;
//c存放"前" Or "后"
chess_p = s.Substring(1, 1);
//得到前车的车
Findxy(c, chess_p, ref idx, ref x1, ref y1);
//处理"前" Or "后"时x1,y1
a = 10 - x1;
}
else
{
a = ConvertNum(s.Substring(1, 1));
//得到车二的2
}
b = ConvertNum(s.Substring(3, 1));
GoDirect = s.Substring(2, 1);
listBox1.Items.Add("红" + Convert.ToString(Step_Num / 2 + 1) + ". " + s.Substring(0, 4));
switch (GoDirect)
{
case "平":
//如果是平,x坐标变化
x1 = 10 - a;
x2 = 10 - b;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1;
}
else
{
y2 = Findy(x1, chess_p, ref idx);
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
break;
case "进":
//红方进
if (chess_p == "兵" | chess_p == "炮" | chess_p == "车" | chess_p == "帅")
{
//如果是直线进,y坐标变化 炮八进二
x1 = 10 - a;
x2 = x1;
dy = b;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1 - dy;
}
else
{
y2 = Findy(x1, chess_p, ref idx) - dy;
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
}
if (chess_p == "马" | chess_p == "相" | chess_p == "仕")
{
//如果是斜线进,x,y坐标同时变化 马二进三
x1 = 10 - a;
x2 = 10 - b;
dx = Math.Abs(b - a);
if (chess_p == "马")
{
if (dx == 1) dy = 2;
if (dx == 2) dy = 1;
}
if (chess_p == "相") dy = dx;
if (chess_p == "仕") dy = dx;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1 - dy;
}
else
{
y2 = Findy(x1, chess_p, ref idx) - dy;
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
}
break;
case "退":
//红方退
if (chess_p == "兵" | chess_p == "炮" | chess_p == "车" | chess_p == "帅")
{
//如果是直线进,y坐标变化 炮八退二
x1 = 10 - a;
x2 = x1;
dy = b;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1 + dy;
}
else
{
y2 = Findy(x1, chess_p, ref idx) + dy;
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
}
if (chess_p == "马" | chess_p == "相" | chess_p == "仕")
{
//如果是斜线进,x,y坐标同时变化 马三退一
x1 = 10 - a;
x2 = 10 - b;
dx = Math.Abs(b - a);
if (chess_p == "马")
{
if (dx == 1)
dy = 2;
if (dx == 2)
dy = 1;
}
if (chess_p == "相") dy = dx;
if (chess_p == "仕") dy = dx;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1 + dy;
}
else
{
y2 = Findy(x1, chess_p, ref idx) + dy;
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
}
break;
}
}
else
{
//************************黑方**************************************
//黑方
s = step_info[Step_Num].ToString();
chess_p = s.Substring(0, 1);
c = "";
if (chess_p == "前" | chess_p == "后")
{
//在垂直方向有相同子
c = (chess_p == "前" ? "后" : "前");
//c存放"前" Or "后"
chess_p = s.Substring(1, 1);
//得到前车的车
Findxy(c, chess_p, ref idx, ref x1, ref y1);
//处理"前" Or "后"时x1,y1
a = 10 - x1;
}
else
{
a = ConvertNum(s.Substring(1, 1));
//得到车二的2
}
b = ConvertNum(s.Substring(3, 1));
GoDirect = s.Substring(2, 1);
listBox1.Items.Add("黑" + Convert.ToString(Step_Num / 2 + 1) + ". " + s.Substring(0, 4));
switch (GoDirect)
{
case "平":
//如果是平,x坐标变化
x1 = a;
x2 = b;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1;
}
else
{
y2 = Findy(x1, chess_p, ref idx);
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
break;
case "进":
//**********黑方进
if (chess_p == "卒" | chess_p == "炮" | chess_p == "车" | chess_p == "将")
{
//如果是直线进,y坐标变化 炮8进2
x1 = a;
x2 = x1;
dy = b;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1 + dy;
}
else
{
y2 = Findy(x1, chess_p, ref idx) + dy;
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
}
if (chess_p == "马" | chess_p == "象" | chess_p == "士")
{
//如果是斜线进,x,y坐标同时变化 马2进3
x1 = a;
x2 = b;
dx = Math.Abs(b - a);
if (chess_p == "马")
{
if (dx == 1)
dy = 2;
if (dx == 2)
dy = 1;
}
if (chess_p == "象")
dy = dx;
if (chess_p == "士")
dy = dx;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1 + dy;
}
else
{
y2 = Findy(x1, chess_p, ref idx) + dy;
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
}
break;
case "退":
//**********黑方退
if (chess_p == "卒" | chess_p == "炮" | chess_p == "车" | chess_p == "将")
{
//如果是直线进,y坐标变化 炮8退2
x1 = a;
x2 = x1;
dy = b;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1 - dy;
}
else
{
y2 = Findy(x1, chess_p, ref idx) - dy;
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
}
if (chess_p == "马" | chess_p == "象" | chess_p == "士")
{
//如果是斜线进,x,y坐标同时变化 马3退1
x1 = a;
x2 = b;
dx = Math.Abs(b - a);
if (chess_p == "马")
{
if (dx == 1)
dy = 2;
if (dx == 2)
dy = 1;
}
if (chess_p == "象")
dy = dx;
if (chess_p == "士")
dy = dx;
if (c == "前" | c == "后")
{
//在垂直方向有相同子
y2 = y1 - dy;
}
else
{
y2 = Findy(x1, chess_p, ref idx) - dy;
}
Map_modify(idx, x2, y2);
MoveChess(idx, x2, y2);
}
break;
}
}
Step_Num = Step_Num + 1;
//My.Computer.Audio.Play("move.wav");
//换成对方
if (LocalPlayer == REDPLAYER)
{
LocalPlayer = BLACKPLAYER;
}
else
{
LocalPlayer = REDPLAYER;
}
//*******************************************************
CurSelect = idx;
//当前移动棋子的ID
picChess[CurSelect].BackColor = System.Drawing.SystemColors.Desktop;
picChess[CurSelect].BorderStyle = BorderStyle.FixedSingle;
}
else
{
MessageBox.Show("结束了!!!!!!", "提示");
//Myread.Close();
}
}
private int ConvertNum(string s)
{
switch (s)
{
case "一":
case "1": return 1;
case "二":
case "2": return 2;
case "三":
case "3": return 3;
case "四":
case "4": return 4;
case "五":
case "5": return 5;
case "六":
case "6": return 6;
case "七":
case "7": return 7;
case "八":
case "8": return 8;
case "九":
case "9": return 9;
default:
return 0;
}
}
private void Findxy(string c, string Qi_zi, ref int idx, ref int x1, ref int y1)
{
int x,y;
if (c == "前")
{
for (x = 1; x <= 9; x++)
{
for (y = 1; y <= 10; y++)
{
idx = Map[x, y];
//象棋索引号,1--16黑方 21--36红
if (idx == 0 | !IsMyChess(idx))
{
}
else
{
if (picChess[idx].Tag.ToString() == Qi_zi)
{
x1 = x;
y1 = y;
return;
//Exit For错误
}
}
}
}
}
if (c == "后")
{
for (x = 1; x <= 9; x++)
{
for (y = 10; y >= 1; y += -1)
{
idx = Map[x, y];
//象棋索引号,1--16黑方 21--36红
if (idx == 0 | !IsMyChess(idx))
{
}
else
{
if (picChess[idx].Tag.ToString() == Qi_zi)
{
x1 = x;
y1 = y;
return;
}
}
}
}
}
}
private int Findy(int x1, string Qi_zi, ref int idx)
{
for (int y = 1; y <= 10; y++)
{
idx = Map[x1, y];
//象棋索引号,1--16黑方 21--36红
if (idx == 0 | !IsMyChess(idx))
{
}
else
{
if (picChess[idx].Tag.ToString() == Qi_zi)
{
return y;
}
}
}
idx = 0;
return 0;//没找到!
}
private void button3_Click(object sender, EventArgs e)
{
int n=0;
button3.Enabled = false;
while (n <=step_info.Count)
{
button2_Click(sender, e);//调用单步复盘
//System.Threading.Thread.Sleep(1000) '延时1秒等1000ms
WaitingSeconds(1); //延时1秒
n++;
}
button3.Enabled = true ;
}
protected void WaitingSeconds(int seconds)
{
System.DateTime tmpNow = DateTime.Now;
while (DateTime.Now.Subtract(tmpNow).Seconds < seconds)
{
Application.DoEvents();
}
}
private void button4_Click(object sender, System.EventArgs e)
{
Bitmap Newbmp2;
Newbmp2 = new System.Drawing.Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
//Newbmp2.MakeTransparent()
pictureBox1.Image = Newbmp2;
No_chessPic.Image = Newbmp2;
Newbmp2.Save("22.bmp");
}
private void MakeTransparent_Example2(PaintEventArgs e)
{
// Create a Bitmap object from an image file.
Bitmap myBitmap = new Bitmap("Grapes.gif");
// Draw myBitmap to the screen.
e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height);
// Get the color of a background pixel.
Color backColor = myBitmap.GetPixel(1, 1);
myBitmap.MakeTransparent(backColor);
e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height);
// Draw the transparent bitmap to the screen.
}
}
}