中国象棋源代码Java程序
中国象棋(代码)
中国象棋(web版源代码)程序:using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;namespace WebApplication1{public partial class WebForm1 : System.Web.UI.Page{int tru = 20;int fals = 40;public ImageButton[,] _Image=new ImageButton[11,10];//将上一次点击点的坐标保存到数据库中的lastx和lastypublic void SaveToLast(){if (Session["user"].ToString() == "red" &&_GetUserState(Session["user"].ToString()) == 20){int x, y, lastx, lasty;x = Getpointx();y = Getpointy();lastx = x;lasty = y;Updatalastx(lastx);Updatalasty(lasty);}if (Session["user"].ToString() == "black" &&_GetUserState(Session["user"].ToString()) == 20){int x, y, lastx, lasty;x = Getpointx();y = Getpointy();lastx = x;lasty = y;Updatalastx(lastx);Updatalasty(lasty);}}//将棋盘上所有棋子图片显示到棋盘上private void _Drawqizi(){//_Init();int i,j,k;if (_GetUserState("red") != 0 && _GetUserState("black") != 0){if (Session["user"].ToString() == "red"){for (i = 1; i <= 10; i++)for (j = 1; j <= 9; j++){k = _GetDataQipan(i, j);_Image[i, j].ImageUrl = _GetImageAdd(k);}}if (Session["user"].ToString() == "black"){for (i = 1; i <= 10; i++)for (j = 1; j <= 9; j++){k = _GetDataQipan(i, j);_Image[11 - i, 10 - j].ImageUrl =_GetImageAdd(k);}}}//初始化:对_Image[,]赋值,对ImageButton进行编号private void _Init(){_Image[1, 1] = ImageButton1; _Image[1, 2] = ImageButton2; _Image[1, 3] = ImageButton3; _Image[1, 4] = ImageButton4; _Image[1, 5] = ImageButton5; _Image[1, 6] = ImageButton6; _Image[1, 7] = ImageButton7; _Image[1, 8] = ImageButton8; _Image[1, 9] = ImageButton9;_Image[2, 1] = ImageButton11; _Image[2, 2] = ImageButton12; _Image[2, 3] = ImageButton13; _Image[2, 4] = ImageButton14; _Image[2, 5] = ImageButton15; _Image[2, 6] = ImageButton16; _Image[2, 7] = ImageButton17; _Image[2, 8] = ImageButton18; _Image[2, 9] = ImageButton19;_Image[3, 1] = ImageButton21; _Image[3, 2] = ImageButton22; _Image[3, 3] = ImageButton23; _Image[3, 4] = ImageButton24; _Image[3, 5] = ImageButton25; _Image[3, 6] = ImageButton26; _Image[3, 7] = ImageButton27; _Image[3, 8] = ImageButton28; _Image[3, 9] = ImageButton29;_Image[4, 1] = ImageButton31; _Image[4, 2] = ImageButton32; _Image[4, 3] = ImageButton33; _Image[4, 4] = ImageButton34; _Image[4, 5] = ImageButton35; _Image[4, 6] = ImageButton36; _Image[4, 7] = ImageButton37; _Image[4, 8] = ImageButton38; _Image[4, 9] = ImageButton39;_Image[5, 1] = ImageButton41; _Image[5, 2] = ImageButton42; _Image[5, 3] = ImageButton43; _Image[5, 4] = ImageButton44; _Image[5, 5] = ImageButton45; _Image[5, 6] = ImageButton46; _Image[5, 7] = ImageButton47; _Image[5, 8] = ImageButton48; _Image[5, 9] = ImageButton49;_Image[6, 1] = ImageButton51; _Image[6, 2] = ImageButton52; _Image[6, 3] = ImageButton53; _Image[6, 4] = ImageButton54; _Image[6, 5] = ImageButton55; _Image[6, 6] = ImageButton56; _Image[6, 7] = ImageButton57; _Image[6, 8] = ImageButton58; _Image[6, 9] = ImageButton59;_Image[7, 1] = ImageButton61; _Image[7, 2] = ImageButton62; _Image[7, 3] = ImageButton63; _Image[7, 4] = ImageButton64; _Image[7, 5] = ImageButton65; _Image[7, 6] = ImageButton66; _Image[7, 7] = ImageButton67; _Image[7, 8] = ImageButton68; _Image[7, 9] = ImageButton69;_Image[8, 1] = ImageButton71; _Image[8, 2] = ImageButton72; _Image[8, 3] = ImageButton73; _Image[8, 4] = ImageButton74; _Image[8, 5] = ImageButton75; _Image[8, 6] = ImageButton76; _Image[8, 7] = ImageButton77; _Image[8, 8] = ImageButton78; _Image[8, 9] = ImageButton79;_Image[9, 1] = ImageButton81; _Image[9, 2] = ImageButton82; _Image[9, 3] = ImageButton83; _Image[9, 4] = ImageButton84; _Image[9, 5] = ImageButton85; _Image[9, 6] = ImageButton86; _Image[9, 7] = ImageButton87; _Image[9, 8] = ImageButton88; _Image[9, 9] = ImageButton89;_Image[10, 1] = ImageButton91; _Image[10, 2] = ImageButton92; _Image[10, 3] = ImageButton93; _Image[10, 4] = ImageButton94; _Image[10, 5] = ImageButton95; _Image[10, 6] = ImageButton96; _Image[10, 7] = ImageButton97; _Image[10, 8] = ImageButton98; _Image[10, 9] = ImageButton99;int i, j;for (i = 1; i <= 10; i++)for (j = 1; j <= 9; j++){_Image[i, j].ImageUrl = "~/image/back.gif";}}//初始化棋盘,将两方棋子放好位private void _InitQizi(){int i = 0, j = 0, k = 0;int[] initqipandata = new int[37];for (i = 1; i <= 10; i++)for (j = 1; j <= 9; j++){_UpdataQipan(i, j, k);}for (i = 0; i <= 36; i++)initqipandata[i] = i;_UpdataQipan(1, 1, initqipandata[8]); _UpdataQipan(1, 2, initqipandata[6]); _UpdataQipan(1, 3, initqipandata[4]); _UpdataQipan(1, 4, initqipandata[2]); _UpdataQipan(1, 5, initqipandata[1]); _UpdataQipan(1,6, initqipandata[3]); _UpdataQipan(1, 7, initqipandata[5]); _UpdataQipan(1, 8, initqipandata[7]); _UpdataQipan(1, 9, initqipandata[9]); _UpdataQipan(3, 2, initqipandata[10]); _UpdataQipan(3, 8, initqipandata[11]); _UpdataQipan(4, 1, initqipandata[12]); _UpdataQipan(4, 3, initqipandata[13]);_UpdataQipan(4, 5, initqipandata[14]); _UpdataQipan(4, 7,initqipandata[15]); _UpdataQipan(4, 9, initqipandata[16]);_UpdataQipan(10, 1, initqipandata[28]); _UpdataQipan(10, 2, initqipandata[26]); _UpdataQipan(10, 3, initqipandata[24]);_UpdataQipan(10, 4, initqipandata[22]); _UpdataQipan(10, 5,initqipandata[21]); _UpdataQipan(10, 6, initqipandata[23]);_UpdataQipan(10, 7, initqipandata[25]); _UpdataQipan(10, 8,initqipandata[27]); _UpdataQipan(10, 9, initqipandata[29]); _UpdataQipan(8, 2, initqipandata[30]); _UpdataQipan(8, 8, initqipandata[31]);_UpdataQipan(7, 1, initqipandata[32]); _UpdataQipan(7, 3,initqipandata[33]); _UpdataQipan(7, 5, initqipandata[34]); _UpdataQipan(7, 7, initqipandata[35]); _UpdataQipan(7, 9, initqipandata[36]);}//获取棋子图片地址private string _GetImageAdd(int xxx){string x="" ;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select add_image from qizi where(no_qizi='"+xxx+"');", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())x = sr["add_image"].ToString();//Session["add_image"] = x;myconn.Close();return x;}//获取点击后的图片地址private string _GetImageDownAdd(int xxx){string x ="";SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select add_image_down from qizi where(no_qizi='" + xxx + "')", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())x = sr["add_image_down"].ToString();myconn.Close();return x;}//读取鼠标点击点的坐标private int Getpointx(){int x = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select x from zuobiao", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read()){x = int.Parse(sr["x"].ToString());}myconn.Close();return x;}private int Getpointy( ){int x = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select y from zuobiao", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read()){x = int.Parse(sr["y"].ToString());}myconn.Close();return x;}private int Getpointlastx( ){int x = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select lastx from zuobiao", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read()){x = int.Parse(sr["lastx"].ToString());}myconn.Close();return x;}private int Getpointlasty( ){int x = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select lasty from zuobiao", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read()){x = int.Parse(sr["lasty"].ToString());}myconn.Close();return x;}//写入鼠标点击点的坐标private void Updatax(int xxx){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update zuobiao set x='" + xxx + "'", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}private void Updatay(int yyy){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update zuobiao set y='" + yyy + "'", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}private void Updatalastx(int lastxxx){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update zuobiao set lastx='"+lastxxx +"'" , myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}private void Updatalasty(int lastyyy){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update zuobiao set lasty='"+ lastyyy +"'", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}//以上四个函数的集合,达到一次写入四个坐标值的目的private void _UpdataaZuobiao(int xxx, int yyy, int lastxxx, int lastyyy){Updatax(xxx);Updatay(yyy);Updatalastx(lastxxx);Updatalasty(lastyyy);}//保存当前坐标值到x和yprivate void _UpdatZuobiaoXY(int xxx, int yyy){if (Session["user"].ToString() == "red" &&_GetUserState(Session["user"].ToString()) == 20){Updatax(xxx);Updatay(yyy);}if (Session["user"].ToString() == "black" &&_GetUserState(Session["user"].ToString()) == 20){Updatax(xxx);Updatay(yyy);}}//读取棋盘上的棋子信息private int _GetDataQipan(int xxx, int yyy){int i,data=0;i = (xxx - 1) * 9 + yyy;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select data_qipan from qipan where(position='"+i+"')", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())data = int.Parse(sr["data_qipan"].ToString());myconn.Close();return data;}//将棋子信息写入棋盘private void _UpdataQipan(int xxx, int yyy,int data){int i;i = (xxx - 1) * 9 + yyy;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update qipan set data_qipan=" + data + "where (position='"+i+"')", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}//读出用户此时状态private int _GetUserState(string id){int data = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select state from yonghu where(id='" + id + "')", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())data = int.Parse(sr["state"].ToString());myconn.Close();return data;}//读出用户输赢状态 0表示在进行游戏 20表示赢 40表示输private int _GetUserWin(string id){int data = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select winner from yonghu where(id='" + id + "')", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())data = int.Parse(sr["winner"].ToString());myconn.Close();return data;}//写入用户状态private void _UpdataUserState(string id,int sta){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update yonghu set state="+ sta + "where (id='" + id + "')", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}//写入用户输赢状态 0表示在进行游戏 20表示赢 40表示输private void _UpdataUserWin(string id, int sta){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update yonghu set winner=" + sta + "where (id='" + id + "')", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}protected void Page_Load(object sender, EventArgs e){_Init();// _Test();}//测试程序函数private void _Test(){_UpdataUserState("red",tru);_UpdataUserState("black", tru);}private bool_IsAbleToPut()//********************************************************需要传递x,y,laastx,lasty{if (Session["user"].ToString() == "red" &&_GetUserState(Session["user"].ToString()) == 40)return false;if (Session["user"].ToString() == "black" &&_GetUserState(Session["user"].ToString()) == 40)return false;int x, y, lastx, lasty;int qipandata,lastqipandata;x = Getpointx();y = Getpointy();lastx = Getpointlastx();lasty = Getpointlasty();if (Session["user"].ToString() == "black"){x = 11 - x;y = 10 - y;lastx = 11 - lastx;lasty = 10 - lasty;}qipandata = _GetDataQipan(x, y);lastqipandata = _GetDataQipan(lastx, lasty);// if (lastqipandata==0)// return false;if(Session["user"].ToString() == "red"&&(lastqipandata <= 20 || qipandata >=20))//****************************************************************现以红方为对象return false;if (Session["user"].ToString() == "black" && ((lastqipandata == 0 || lastqipandata >= 20) || (qipandata > 0 && qipandata <= 20)))return false;int i, j, c;//将|帅if (lastqipandata == 1 || lastqipandata == 21){if ((x - lastx) * (y - lasty) != 0) return false;if(Math.Abs(x - lastx) > 1 || Math.Abs(y - lasty) > 1) return false;if (y < 4 || y > 6 || (x > 3 && x < 8)) return false;return true;}//士|仕if (lastqipandata == 2 || lastqipandata == 3 || lastqipandata == 22 || lastqipandata == 23){if ((x - lastx) * (y - lasty) == 0) return false;if(Math.Abs(x - lastx) > 1 || Math.Abs(y - lasty) > 1) return false;if (y < 4 || y > 6 || (x > 3 && x < 8)) return false;return true;}//象|相if (lastqipandata == 4 || lastqipandata == 5 || lastqipandata == 24 || lastqipandata == 25){if ((x - lastx) * (y - lasty) == 0) return false;if (Math.Abs(x - lastx) != 2 || Math.Abs(y - lasty) != 2) return false;if(Session["user"].ToString() == "red"&& x < 6) return false;if (Session["user"].ToString() == "black" && x > 5) return false;i = 0; j = 0;//i,j必须有初始值if (x - lastx == 2){i = x - 1;}if (x - lastx == -2){i = x + 1;}if (y - lasty == 2){j = y - 1;}if (y - lasty == -2){j = y + 1;}if (_GetDataQipan(i, j) != 0) return false;return true;}//马if (lastqipandata == 6 || lastqipandata == 7 || lastqipandata == 26 || lastqipandata == 27){if (Math.Abs(x - lastx) * Math.Abs(y - lasty) != 2)return false;if (x - lastx == 2){if (_GetDataQipan(x - 1, lasty) != 0)return false;}if (x - lastx == -2){if (_GetDataQipan(x + 1, lasty) != 0)return false;}if (y - lasty == 2){if (_GetDataQipan(lastx, y - 1) != 0)return false;}if (y - lasty == -2){if (_GetDataQipan(lastx, y + 1) != 0)return false;}return true;}//车if (lastqipandata == 8 || lastqipandata == 9 || lastqipandata == 28 || lastqipandata == 29){//判断是否直线if ((x - lastx) * (y - lasty) != 0) return false;//判断是否隔有棋子if (x != lastx){if (lastx > x) { int t = x; x = lastx; lastx = t; }for (i = lastx; i <= x; i += 1){if (i != x && i != lastx){if (_GetDataQipan(i, y) != 0)return false;}}}if (y != lasty){if (lasty > y) { int t = y; y = lasty; lasty = t; }for (j = lasty; j <= y; j += 1){if (j != y && j != lasty){if (_GetDataQipan(x, j) != 0)return false;}}}return true;}//炮if (lastqipandata == 10 || lastqipandata == 11 || lastqipandata == 30 || lastqipandata == 31){bool swapflagx = false;bool swapflagy = false;if ((x - lastx) * (y - lasty) != 0) return false;c = 0;if (x != lastx){if (lastx > x) { int t = x; x = lastx; lastx = t; swapflagx = true; }for (i = lastx; i <= x; i += 1){if (i != x && i != lastx){if (_GetDataQipan(i, y) != 0)c = c + 1;//IsAbleToPut = False: Exit Function}}}if (y != lasty){if (lasty > y) { int t = y; y = lasty; lasty = t; swapflagy = true; }for (j = lasty; j <= y; j += 1){if (j != y && j != lasty){if (_GetDataQipan(x, j) != 0)c = c + 1;//IsAbleToPut = False: Exit Function}}}if (c > 1) return false; //与目标处间隔1个以上棋子if (c == 0) //与目标处无间隔棋子{if(swapflagx == true) { int t = x; x = lastx; lastx = t; }if(swapflagy == true) { int t = y; y = lasty; lasty = t; }if (_GetDataQipan(x, y) != 0) return false;}if (c == 1)//与目标处间隔1个棋子{if(swapflagx == true) { int t = x; x = lastx; lastx = t; }if(swapflagy == true) { int t = y; y = lasty; lasty = t; }// if ((IsMyChess(qipan[x, y]) || qipan[x, y] == 0))//return false;//***********ismychess************************************************** ***************************if (qipandata == 0)return false;}return true;}//卒|兵if (lastqipandata == 12 || lastqipandata == 13 || lastqipandata == 14 || lastqipandata == 15 || lastqipandata == 16 || lastqipandata == 32 || lastqipandata == 33 || lastqipandata == 34 || lastqipandata == 35 || lastqipandata == 36){if ((x - lastx) * (y - lasty) != 0)return false;if(Math.Abs(x - lastx) > 1 || Math.Abs(y - lasty) > 1)return false;if (Session["user"].ToString() == "red" && (x >= 6 && (y - lasty) != 0)) return false;if(Session["user"].ToString() == "black"&& (x <= 5 && (y - lasty) != 0)) return false;if(Session["user"].ToString() == "red"&& (x - lastx > 0)) return false;if(Session["user"].ToString() == "black"&& (x - lastx < 0)) return false;return true;}return false;}//移动棋子private void _MoveChess()//********************************************************需要传递x,y,laastx,lasty{//如果能移动则移动棋子if (_IsAbleToPut()){int x, y, lastx, lasty,qipandata, lastqipandata, tr;tr = 0;x = Getpointx();y = Getpointy();lastx = Getpointlastx();lasty = Getpointlasty();if (Session["user"].ToString() == "black"){x = 11 - x;y = 10 - y;lastx = 11 - lastx;lasty = 10 - lasty;}qipandata = _GetDataQipan(x, y);lastqipandata = _GetDataQipan(lastx, lasty);_UpdataQipan(x, y, lastqipandata);_UpdataQipan(lastx, lasty, tr);if (Session["user"].ToString() == "red"){_UpdataUserState("red", fals);_UpdataUserState("black", tru);}if (Session["user"].ToString() == "black"){_UpdataUserState("red", tru);_UpdataUserState("black", fals);}if (qipandata == 1){Response.Write("<script language=javascript>alert('恭喜您,您赢啦');</script>");_UpdataUserState("red", fals);_UpdataUserState("black", fals);Button6.Enabled = true;_UpdataUserWin("red",20);_UpdataUserWin("black", 40);}if (qipandata == 21){Response.Write("<script language=javascript>alert('恭喜您,您赢啦');</script>");_UpdataUserState("red", fals);_UpdataUserState("black", fals);Button6.Enabled = true;_UpdataUserWin("red", 40);_UpdataUserWin("black", 20);}_UpdataaZuobiao(0, 0, 0, 0);}//如果不能移动则更换已方被点击棋子的背景图片}protected void ImageButton1_Click1(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 1; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton2_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 2; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton3_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 3; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton4_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 4; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton5_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 5; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton6_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 6; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton7_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 7; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton8_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 8; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton9_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 9; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton11_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 1; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton12_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 2; _UpdatZuobiaoXY(x, y);_MoveChess(); _Drawqizi();}protected void ImageButton13_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 3; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton14_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 4; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton15_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 5; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton16_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 6; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton17_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 7; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton18_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 8; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton19_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 9; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton21_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 1; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton22_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 2; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton23_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 3; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton24_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 4; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton25_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 5; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton26_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 6; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton27_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 7; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton28_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 8; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton29_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 9; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton31_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 1; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton32_Click(object sender,ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 2; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton33_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 3; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton34_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 4; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton35_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 5; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton36_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 6; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton37_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 7; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}。
中国象棋java版
这是我自己新编的中国象棋,发上来和大家共享我先说一下这个一共我创建了3个类需要把下面的代码分开copy,图片我不知道怎么弄了,只能你们自己弄了,你们可以看最后一个类,这个类也比较的小很好看懂的,图片的的路径弄好就可以了要是也想jar文件的,可以联系我,我的邮箱是suqin909@。
我希望能互相学习交流,因为我也是刚试着开始做的。
我把制作的好的文件也发上去只要解压就可以用了(电脑一定要装jdk,不要说你没有虚拟机,就想运行的。
)不过百度文库好像不能传压缩文件,我就不知道怎么办了。
要是用人告诉我好方法我也上传一个。
第一个类:package chess.test;import java.awt.Color;import java.awt.Container;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.KeyListener;import java.awt.image.BufferedImage;import java.io.File;//import java.io.IOException;import javax.imageio.ImageIO;//import javax.imageio.stream.ImageInputStream;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JToolBar;public class Chess extends JFrame implements MouseListener ,Runnable,KeyListener{ /****/private static final long serialVersionUID = 1L;//获取屏幕尺寸private int with=Toolkit.getDefaultToolkit().getScreenSize().width;private int height=Toolkit.getDefaultToolkit().getScreenSize().height;private BufferedImage bgImage =null;// private String isred = "红方先行";//标志是否有子//如果要时时变化就就要创建线程对像Thread t= new Thread();//记录棋子所在位置private boolean isXia = false;public static ChessMan chessBoard[][];//鼠标的位置int x;int y;//拿起棋子时的位置int Ox;int Oy;//初始状态下,棋盘上就有每个棋子//蓝方棋子private ChessMan LanChe1;private ChessMan LanChe2;private ChessMan LanMa1;private ChessMan LanMa2;private ChessMan LanXiang1;private ChessMan LanXiang2;private ChessMan LanShi1;private ChessMan LanShi2;private ChessMan LanJiang;private ChessMan LanPao1;private ChessMan LanPao2;private ChessMan LanBing1;private ChessMan LanBing2;private ChessMan LanBing3;private ChessMan LanBing4;private ChessMan LanBing5;//红方棋子private ChessMan HongChe1;private ChessMan HongChe2;private ChessMan HongMa1;private ChessMan HongMa2;private ChessMan HongXiang1;private ChessMan HongXiang2;private ChessMan HongShi1;private ChessMan HongShi2;private ChessMan HongJiang;private ChessMan HongPao1;private ChessMan HongPao2;private ChessMan HongBing1;private ChessMan HongBing2;private ChessMan HongBing3;private ChessMan HongBing4;private ChessMan HongBing5;// private boolean canEat = false;// //工具栏// JToolBar jmain;// //重新开始// JButton anew;// //悔棋// JButton repent;// //打开// JButton showOpen;// //保存// JButton showSave;// //退出// JButton exit;//// //当前信息// JLabel text;//临时棋子private ChessMan temChessMan = null;public Chess(){this.setTitle("中国象棋"+101/50);this.setSize(530,630);this.setLocation((with-530)/2, (height-630)/2);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*//窗格Container con = null;//获行客格引用con = this.getContentPane();con.setLayout(null);//创建工具栏jmain = new JToolBar();text = new JLabel(" 热烈欢迎");text.setToolTipText("提示信息");anew = new JButton(" 新游戏" );anew.setToolTipText("重新开始新的一局");exit = new JButton(" 退出");exit.setToolTipText("退出本程序");repent = new JButton(" 悔棋");repent.setToolTipText("返回到上次走棋的位置");showOpen = new JButton("打开");showOpen.setToolTipText("打开以前棋局");showSave = new JButton("保存");showSave.setToolTipText("保存当前棋局");//把组件添加到工具栏jmain.setLayout(new GridLayout(0,6));jmain.add(anew);jmain.add(repent);jmain.add(showOpen);jmain.add(showSave);jmain.add(exit);jmain.add(text);jmain.setBounds(0,500,450,30);con.add(jmain);*/StaticValue.init();t.start();//添加KeyListenerthis.addKeyListener(this);this.addMouseListener(this);// chess=new int [9][10];//调整棋子的两个变量;setChessMan();resetChessMan();this.setVisible(true);this.repaint();}//初始化每个棋子public void setChessMan(){LanChe1 = new ChessMan(55,75,nChe1,"LanChe1");LanChe2 = new ChessMan(455,75,nChe2,"LanChe2");LanMa1 = new ChessMan(105,75,nMa1,"LanMa1");LanMa2 = new ChessMan(405,75,nMa2,"LanMa2");LanXiang1 = new ChessMan(155,75,nXiang1,"LanXiang1");LanXiang2 = new ChessMan(355,75,nXiang2,"LanXiang2");LanShi1 = new ChessMan(205,75,nShi1,"LanShi1");LanShi2 = new ChessMan(305,75,nShi1,"LanShi2");LanJiang = new ChessMan(255,75,nJiang,"LanJiang");LanPao1 = new ChessMan(105,175,nPao1,"LanPao1");LanPao2 = new ChessMan(405,175,nPao1,"LanPao2");LanBing1 = new ChessMan(55,225,nBing1,"LanBing1");LanBing2 = new ChessMan(155,225,nBing1,"LanBing2");LanBing3 = new ChessMan(255,225,nBing1,"LanBing3");LanBing4 = new ChessMan(355,225,nBing1,"LanBing4");LanBing5 = new ChessMan(455,225,nBing1,"LanBing5");HongChe1 = new ChessMan(55,525,StaticValue.HongChe1,"HongChe1");HongChe2 = new ChessMan(455,525,StaticValue.HongChe2,"HongChe2");HongMa1 = new ChessMan(105,525,StaticValue.HongMa1,"HongMa1");HongMa2 = new ChessMan(405,525,StaticValue.HongMa2,"HongMa2");HongXiang1 = new ChessMan(155,525,StaticValue.HongXiang1,"HongXiang1");HongXiang2 = new ChessMan(355,525,StaticValue.HongXiang2,"HongXiang2");HongShi1 = new ChessMan(205,525,StaticValue.HongShi1,"HongShi1");HongShi2 = new ChessMan(305,525,StaticValue.HongShi2,"HongShi2");HongJiang = new ChessMan(255,525,StaticValue.HongJiang,"HongJiang");HongPao1 = new ChessMan(105,425,StaticValue.HongPao1,"HongPao1");HongPao2 = new ChessMan(405,425,StaticValue.HongPao2,"HongPao2");HongBing1 = new ChessMan(55,375,StaticValue.HongBing1,"HongBing1");HongBing2 = new ChessMan(155,375,StaticValue.HongBing2,"HongBing2");HongBing3 = new ChessMan(255,375,StaticValue.HongBing3,"HongBing3");HongBing4 = new ChessMan(355,375,StaticValue.HongBing1,"HongBing4");HongBing5 = new ChessMan(455,375,StaticValue.HongBing1,"HongBing5");}public void paint(Graphics g){BufferedImage image =new BufferedImage(530,595,BufferedImage.TYPE_3BYTE_BGR) ;Graphics g2 = image.createGraphics();try{bgImage = ImageIO.read(new File(System.getProperty("user.dir")+"/bin/chessImage2.jpg"));}catch(Exception e){e.printStackTrace();}// System.out.println(System.getProperty("user.dir")+"/bin/"+"1.jpg");//一般情况下应该先绘制背景,因为是先绘制的先显示,//如果背景最后绘制,那么背景会覆盖其他已绘制的。
中国象棋游戏代码
中国象棋游戏代码#include<stdio.h>#include<stdlib.h>#include<string.h>char x, y, z;int i, i1, j, j1, k, k1, ii, jj;int f(char X){if (X == '0')printf(" ");else{if (X >= 'a' || X <= 'g'){switch (X - 'a'){case 6:printf("兵");break;case 5:printf("包");break;case 4:printf("帅");break;case 3:printf("士");break;case 2:printf("相");break;case 1:printf("马");break;case 0:printf("车");}}if (X <= 'A' || X <= 'G'){switch (X - 'A'){case 6:printf("卒");break;case 5:printf("炮");break;case 4:printf("将");break;case 3:printf("仕");break;case 2:printf("象");break;case 1:printf("馬");break;case 0:printf("車");}}}}char f2(char **str2, int q){int m, n, o, p;char x1, x2, y2, y1, z1;if (q == 1)printf("\n轮到红子动!\n");elseprintf("\n轮到黑子动!\n");for (;;){if (q == 1)printf("\n攻(红)方:车(a) 马(b)\n");elseprintf("\n黑(守)方:将(E)\n");lm1:printf("请选择棋子代号:");scanf("%c", &x);scanf("%c", &x);if (q == 1 && x != 'a' && x != 'b'){printf("请按要求输入!\n");goto lm1;}if (q == 0 && x != 'E'){printf("请按要求输入!\n");goto lm1;}printf("(欲换棋子输入'0 0')请选择该子所在位置(行与列):");scanf("%d%d", &i, &j);if (i == 0 && j == 0)goto lm1;for (;;){if (i < 1 || i > 3 || j < 1 || j > 3){printf("请认真输入,位置应在棋盘内!\n再次输入:");scanf("%d%d", &i, &j);}elsebreak;}i--;j--;if (*(*(str2 + j) + i) == x)break;elseprintf("该位置没有该棋子!请认真输入!\n");}printf("请输入要下的位置(移动后位置):");label1:scanf("%d%d", &i1, &j1);for (;;){if (i1 < 1 || i1 > 3 || j1 < 1 || j1 > 3){printf("请认真输入,位置应在棋盘内!\n再次输入:");scanf("%d%d", &i1, &j1);}elsebreak;}i1--;j1--;y = *(*(str2 + j1) + i1);if (q == 1 && (y == 'a' || y == 'b')){printf("移动后位置有己方棋子\n重新输入下的位置:");goto label1;}else{if (i1 == i && j1 == j){printf("原地未动\n重新输入下的位置:");goto label1;}else{if (x == 'a'){if (i1 != i && j1 != j){printf("'车'应直走!\n重新输入下的位置:");goto label1;}else{if (i1 == i){if (j1 > j)for (k = j + 1; k < j1; k++){if (*(*(str2 + k) + i) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label1;}}else{for (k = j1 + 1; k < j; k++){if (*(*(str2 + k) + i) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label1;}}}}else{if (i1 > i)for (k = i + 1; k < i1; k++){if (*(*(str2 + j) + k) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label1;}}else{for (k = i1 + 1; k < i; k++){if (*(*(str2 + j) + k) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label1;}}}}}if (i1 != ii && j1 != jj){if (ii == 0){if ((jj == 0 || jj == 2)&& (*(*(str2 + 1) + 2) != 'b'|| ((*(*str2 + 1) + 2) == 'b' && *(*(str2 + 1) + 1) != '0'))) {printf("未将军!\n");goto lm1;}if (jj == 1&& (*(*(str2 + 0) + 2) != 'b'|| (*(*(str2 + 0) + 2) == 'b' && *(*(str2) + 1) != '0'))&& (*(*(str2 + 2) + 2) != 'b'|| (*(*(str2 + 2) + 2) == 'b' && *(*(str2 + 2) + 1) != '0'))) {printf("未将军!\n");goto lm1;}}if (ii == 1 && jj == 0&& (*(*(str2 + 2)) != 'b'|| (*(*(str2 + 2)) == 'b' && *(*(str2 + 1)) != '0'))&& (*(*(str2 + 2) + 2) != 'b'|| (*(*(str2 + 2) + 2) == 'b' && *(*(str2 + 1) + 2) != '0'))) {printf("未将军!\n");goto lm1;}if (ii == 1 && jj == 2&& (**str2 != 'b' || (**str2 == 'b' && *(*(str2 + 1)) != '0'))&& (*(*(str2) + 2) != 'b'|| (*(*(str2) + 2) == 'b' && *(*(str2 + 1) + 2) != '0'))) {printf("未将军!\n");goto lm1;}if (ii == 2){if ((jj == 0 || jj == 2)&& (*(*(str2 + 1)) != 'b'|| ((*(*str2 + 1)) == 'b' && *(*(str2 + 1) + 1) != '0'))) {printf("未将军!\n");goto lm1;}if (jj == 1&& (*(*(str2 + 0)) != 'b'|| (*(*(str2 + 0)) == 'b' && *(*(str2) + 1) != '0'))&& (*(*(str2 + 2)) != 'b'|| (*(*(str2 + 2)) == 'b' && *(*(str2 + 2) + 1) != '0'))) {printf("未将军!\n");goto lm1;}}}}if (x == 'b'){if (i1 == i + 1 || i1 == i - 1){if (j1 == j + 2){if (*(*(str2 + j + 1) + i) != '0'){printf("'马'撇腿!\n重新输入下的位置:");goto label1;}else{if (((i1 != ii + 1 || i1 != ii - 1)&& (j1 != jj + 2 || j1 != jj - 2)) && ((j1 != jj + 1|| j1 != jj - 1)&& (i1 != ii + 2|| i1 !=ii - 2))&& y != 'E'){printf("未将军\n");goto lm1;}goto la1;}}else{if (j1 == j - 2){if (*(*(str2 + j - 1) + i) != '0'){printf("'马'撇腿!\n重新输入下的位置:");goto label1;}else{if (((i1 != ii + 1 || i1 != ii - 1)&& (j1 != jj + 2 || j1 != jj - 2))&& ((j1 != jj + 1 || j1 != jj - 1)&& (i1 != ii + 2 || i1 != ii - 2)) && y != 'E'){printf("未将军\n");goto lm1;}goto la1;}}else{printf("'马'应走'日'\n重新输入下的位置:");goto label1;}}}else{if (i1 == i + 2 || i1 == i - 2){if (j1 == j + 1 || j1 == j - 1){if (*(*(str2 + j) + (i1 + i) / 2) != '0'){printf("'马'撇腿!\n重新输入下的位置:");goto label1;}else{if (((i1 != ii + 1 || i1 != ii - 1)&& (j1 != jj + 2 || j1 != jj - 2))&& ((j1 != jj + 1 || j1 != jj - 1)&& (i1 != ii + 2 || i1 != ii - 2)) && y != 'E'){printf("未将军\n");goto lm1;}goto la1;}}else{printf("'马'应走'日'\n重新输入下的位置:");goto label1;}}else{printf("'马'应走'日'\n重新输入下的位置:");goto label1;}}}if (x == 'E'){ii = i1;jj = j1;if ((i1 == i + 1 || i1 == i - 1) && j1 == j)goto la1;else{if (i1 == i && (j1 == j + 1 || j1 == j - 1))goto la1;else{printf("'帅'一次只(直)走一格\n重新输入下的位置:");goto label1;}}}}la1:*(*(str2 + j1) + i1) = x;*(*(str2 + j) + i) = '0';printf("\n");for (m = 0; m < 3; m++){for (n = 0; n < 3; n++){o = *(*(str2 + n) + m);f(o);}printf("\n\n");}}}char f1(char **str, int q){int m, n, o, p, o3;char x1, x2, y2, y1, z1;if (q == 1){x1 = 'a';x2 = 'A';y1 = 'g';y2 = 'G';o3 = 7;}else{x1 = 'A';x2 = 'a';y1 = 'G';y2 = 'g';o3 = 2;}if (q == 1)printf("\n轮到红子动!\n");elseprintf("\n轮到黑子动!\n");for (;;){if (q == 1)printf("\n红方:车(a) 马(b) 相(c) 士(d) 帅(e) 包(f) 兵(g)\n");elseprintf("\n黑方:車(A) 馬(B) 象(C) 仕(D) 将(E) 炮(F) 卒(G)\n"); lm:printf("请选择棋子代号:");scanf("%c", &x);scanf("%c", &x);if (x >= x2 && x <= y2){printf("请不要动对方棋子\n");goto lm;}else if (x > y1 || x < x1){printf("输入不合要求!\n");goto lm;}printf("(欲换棋子输入'0 0')请选择该子所在位置(行与列):");scanf("%d%d", &i, &j);if (i == 0 && j == 0)goto lm;for (;;){if (i < 1 || i > 10 || j < 1 || j > 9){printf("请认真输入,位置应在棋盘内!\n再次输入:");scanf("%d%d", &i, &j);}elsebreak;}i--;j--;if (*(*(str + j) + i) == x)break;elseprintf("该位置没有该棋子!请认真输入!\n");}printf("请输入要下的位置(移动后位置):");label:scanf("%d%d", &i1, &j1);for (;;){if (i1 < 1 || i1 > 10 || j1 < 1 || j1 > 9){printf("请认真输入,位置应在棋盘内!\n再次输入:");scanf("%d%d", &i1, &j1);}elsebreak;}i1--;j1--;y = *(*(str + j1) + i1);if (y >= x1 && y <= y1){printf("移动后位置有己方棋子\n重新输入下的位置:");goto label;}else{if (i1 == i && j1 == j){printf("原地未动\n重新输入下的位置:");goto label;}else{/*车的情况*/if (x == 'a' || x == 'A'){if (i1 != i && j1 != j){if (q == 1)printf("'车'应直走!\n");elseprintf("'車'应直走!\n重新输入下的位置:");goto label;}else{if (i1 == i){if (j1 > j)for (k = j + 1; k < j1; k++){if (*(*(str + k) + i) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label;}}else{for (k = j1 + 1; k < j; k++){if (*(*(str + k) + i) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label;}}}}else{if (i1 > i)for (k = i + 1; k < i1; k++){if (*(*(str + j) + k) != '0')printf("中间有子挡\n重新输入下的位置:");goto label;}}else{for (k = i1 + 1; k < i; k++){if (*(*(str + j) + k) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label;}}}}}}/*车的情况*/ /*马的情况*/ if (x == 'b' || x == 'B'){if (i1 == i + 1 || i1 == i - 1){if (j1 == j + 2){if (*(*(str + j + 1) + i) != '0'){if (q == 1)printf("'马'撇腿!\n");elseprintf("'馬'撇腿!\n重新输入下的位置:");goto label;}elsegoto la;}else{if (j1 == j - 2){if (*(*(str + j - 1) + i) != '0'){if (q == 1)printf("'马'撇腿!\n");printf("'馬'撇腿!\n重新输入下的位置:");goto label;}elsegoto la;}else{if (q == 1)printf("'马'应走'日'\n");elseprintf("'馬'应走'日'\n重新输入下的位置:");goto label;}}}else{if (i1 == i + 2 || i1 == i - 2){if (j1 == j + 1 || j1 == j - 1){if (*(*(str + j) + (i1 + i) / 2) != '0'){if (q == 1)printf("'马'撇腿!\n");elseprintf("'馬'撇腿!\n重新输入下的位置:");goto label;}elsegoto la;}else{if (q == 1)printf("'马'应走'日'\n");elseprintf("'馬'应走'日'\n重新输入下的位置:");goto label;}}else{if (q == 1)printf("'马'应走'日'\n");elseprintf("'馬'应走'日'\n重新输入下的位置:");goto label;}}}/*马的情况*/ /*相的情况*/ if (x == 'c' || x == 'C'){if ((i1 == i + 2 || i1 == i - 2) && (j1 == j + 2 || j1 == j - 2)){if (*(*(str + (j + j1) / 2) + (i + i1) / 2) != '0'){if (q == 1)printf("'相'压田!\n");elseprintf("'象'压田!\n重新输入下的位置:");goto label;}elsegoto la;}else{if (q == 1)printf("'相'应飞'田'\n");elseprintf("'象'应飞'田'\n重新输入下的位置:");goto label;}}/*相的情况*/ /*士的情况*/ if (x == 'd' || x == 'D'){if (q == 1 && (j1 < 3 || j1 > 5 || i1 < o3)){printf("'士'不可出九宫格\n重新输入下的位置:");goto label;}else{if (q == 0 && (j1 < 3 || j1 > 5 || i1 > o3)){printf("'仕'不可出九宫格\n重新输入下的位置:");goto label;}else{if ((i1 == i + 1 || i1 == i - 1) && (j1 == j + 1 || j1 == j - 1))goto la;else{if (q == 1)printf("'士'应斜着走\n");elseprintf("'仕'应斜着走\n重新输入下的位置:");goto label;}}}}/*士的情况*/ /*帅的情况*/if (x == 'e' || x == 'E'){for (k = i1 - 1; k >= 0; k--){if (*(*(str + j1) + k) != '\0'){if (q == 1)z1 = 'E';elsez1 = 'e';if (*(*(str + j1) + k) == z1){printf("'将''帅' 照面\n重新输入下的位置:");goto label;}break;}}if (q == 1 && (j1 < 3 || j1 > 5 || i1 < o3)){printf("'帅'不可出九宫格\n重新输入下的位置:");goto label;}else{if (q == 0 && (j1 < 3 || j1 > 5 || i1 > o3)){printf("'将'不可出九宫格\n重新输入下的位置:");goto label;}else{if ((i1 == i + 1 || i1 == i - 1) && j1 == j)goto la;else{if (i1 == i && (j1 == j + 1 || j1 == j - 1))goto la;else{if (q == 1)printf("'帅'一次只(直)走一格\n");elseprintf("'将'一次只(直)走一格\n重新输入下的位置:");goto label;}}}}}/*帅的情况*/ /*包的情况*/ if (x == 'f' || x == 'F'){if (i1 != i && j1 != j){if (q == 1)printf("'包'应直走!\n");elseprintf("'炮'应直走!\n重新输入下的位置:");goto label;}else{if (i1 == i){if (j1 > j){for (p = 0, k = j + 1; k < j1; k++)if (*(*(str + k) + i) != '0')p++;if ((p == 0 && *(*(str + j1) + i1) != '0')|| (p == 1 && *(*(str + j1) + i1) == '0')){printf("有子挡\n重新输入下的位置:");goto label;}else{if (p > 1){printf("隔子太多\n重新输入下的位置:");goto label;}elsegoto la;}}else{for (p = 0, k = j1 + 1; k < j; k++)if (*(*(str + k) + i) != '0')p++;if ((p == 0 && *(*(str + j1) + i1) != '0')|| (p == 1 && *(*(str + j1) + i1) == '0')){printf("有子挡\n重新输入下的位置:");goto label;}else{if (p > 1){printf("隔子太多\n重新输入下的位置:");goto label;}elsegoto la;}}}else{if (i1 > i){for (p = 0, k = i + 1; k < i1; k++)if (*(*(str + k) + i) != '0')p++;if ((p == 0 && *(*(str + j1) + i1) != '0')|| (p == 1 && *(*(str + j1) + i1) == '0')){printf("有子挡\n重新输入下的位置:");goto label;}else{if (p > 1){printf("隔子太多\n重新输入下的位置:");goto label;}elsegoto la;}}else{for (p = 0, k = i1 + 1; k < i; k++)if (*(*(str + k) + i) != '0')p++;if ((p == 0 && *(*(str + j1) + i1) != '0')|| (p == 1 && *(*(str + j1) + i1) == '0')){printf("有子挡\n重新输入下的位置:");goto label;}else{if (p > 1){printf("隔子太多\n重新输入下的位置:");goto label;}elsegoto la;}}}}}/*包的情况*/ /*兵的情况*/if (x == 'g'){if (i1 > i){printf("'兵'不能后退\n重新输入下的位置:");goto label;}if (i1 == i && i1 > 5){printf("'兵'没过河不能横着走\n重新输入下的位置:");goto label;}else{if (i1 == i && (j1 == j + 1 || j1 == j - 1))goto la;else{if (j1 == j && i1 == i - 1)goto la;else{printf("'兵'一次只走一格\n重新输入下的位置:");goto label;}}}}/*兵的情况*/ /*卒的情况*/ if (x == 'G'){if (i1 < i){printf("'卒'不能后退\n重新输入下的位置:");goto label;}if (i1 == i && i1 < 5){printf("'卒'没过河不能横着走\n重新输入下的位置:");goto label;}else{if (i1 == i && (j1 == j + 1 || j1 == j - 1))goto la;else{if (j1 == j && i1 == i + 1)goto la;else{printf("'卒'一次只走一格\n重新输入下的位置:");goto label;}}}}/*卒的情况*/ la:*(*(str + j1) + i1) = x;*(*(str + j) + i) = '0';printf("\n");for (m = 0; m < 10; m++){for (n = 0; n < 9; n++){o = *(*(str + n) + m);f(o);}printf("\n\n");}}}}int main(){int i0, j0, k0, m, n, m1, n1, p1, p3, ww;char **str, **str2;char x0, str1[10];str = (char **)malloc(1600);for (i0 = 0; i0 < 11; i0++)*(str + i0) = (char *)malloc(40);str2 = (char **)malloc(144);for (i0 = 0; i0 < 4; i0++)*(str2 + i0) = (char *)malloc(12);ww1:printf("\n欢迎游戏!\n 中国象棋\n\n");printf("请选择游戏模式:\n1.双人大战!\n2.九宫象棋!\n请输入编号:");scanf("%d", &ww);if (ww != 1 && ww != 2){printf("请按要求输入!\n");goto ww1;}if (ww == 2){printf("\n游戏规则:\n攻(红)方先动棋子不可出九宫格,在步步将军的情况下最终吃将获胜!\n""守(黑)方一直逃,直到对方无法将死便获胜!\n""攻(红)方只有車,馬二子,走法同象棋\n\n");lma1:printf("\n");for (i0 = 0; i0 < 3; i0++){for (j0 = 0; j0 < 3; j0++)*(*(str2 + j0) + i0) = '0';}**str2 = 'a';**(str2 + 2) = 'b';*(*(str2 + 1) + 1) = 'E';*(*(str2) + 2) = 'b';*(*(str2 + 2) + 2) = 'a';for (i0 = 0; i0 < 3; i0++){for (j0 = 0; j0 < 3; j0++){x0 = *(*(str2 + j0) + i0);f(x0);}printf("\n\n");}ii = 1, jj = 1;}else{printf("\n游戏简介:本游戏与现实中象棋规则相同!为方便游戏运行,棋子将用字母替代\n\n""红方:车(a) 马(b) 相(c) 士(d) 帅(e) 包(f) 兵(g)\n""黑方:車(A) 馬(B) 象(C) 仕(D) 将(E) 炮(F) 卒(G)\n\n");lma:printf("\n");for (i0 = 0; i0 < 10; i0++)for (j0 = 0; j0 < 9; j0++)*(*(str + j0) + i0) = '0';**str = 'A';**(str + 1) = 'B';**(str + 2) = 'C';**(str + 3) = 'D';**(str + 4) = 'E';*(*(str + 1) + 2) = 'F';*(*(str) + 3) = 'G';*(*(str + 2) + 3) = 'G';*(*(str + 4) + 3) = 'G';*(*(str) + 6) = 'g';*(*(str + 2) + 6) = 'g';*(*(str + 4) + 6) = 'g';*(*(str + 1) + 7) = 'f';*(*(str) + 9) = 'a';*(*(str + 1) + 9) = 'b';*(*(str + 2) + 9) = 'c';*(*(str + 3) + 9) = 'd';*(*(str + 4) + 9) = 'e';for (i0 = 0; i0 < 10; i0++){for (j0 = 5; j0 < 9; j0++)*(*(str + j0) + i0) = *(*(str + 8 - j0) + i0);}for (i0 = 0; i0 < 10; i0++){for (j0 = 0; j0 < 9; j0++){x0 = *(*(str + j0) + i0);f(x0);}printf("\n\n");}}printf("Ready Go?(输入“start”开始):");scanf("%s", str1);for (;;){if (str1[0] == 's' && str1[1] == 't' && str1[2] == 'a' && str1[3] == 'r' && str1[4] == 't'&& str1[5] == '\0')break;elseprintf("请正确输入!{Not ready?)\n再次输入:");scanf("%s", str1);}printf("\n红子先动!\n\n是否开启悔棋功能\n""征求双方意见,同意输入1,反对输入0(将关闭悔棋功能)\n");l11:printf("红方意见:");scanf("%d", &m1);if (m1 != 0 && m1 != 1){printf("请按要求输入!\n");goto l11;}l12:printf("黑方意见:");scanf("%d", &n1);if (n1 != 0 && n1 != 1){printf("请按要求输入!\n");goto l12;}p3 = m1 * n1;for (m = 1;; m++){l3:m = m % 2;if (ww == 1)f1(str, m);elsef2(str2, m);if (p3 != 0){printf("是否悔棋\n征求双方意见,同意输入1,反对输入0,关闭悔棋功能输入2\n");l1:printf("红方意见:");scanf("%d", &m1);if (m1 != 0 && m1 != 1 && m1 != 2){printf("请按要求输入!\n");goto l1;}l2:printf("黑方意见:");scanf("%d", &n1);if (n1 != 0 && n1 != 1 && n1 != 2){printf("请按要求输入!\n");goto l2;}p1 = m1 * n1;if (p1 == 1 && m1 != 2 && n1 != 2){if (ww == 1){*(*(str + j) + i) = x;*(*(str + j1) + i1) = y;}else{*(*(str2 + j) + i) = x;*(*(str2 + j1) + i1) = y;}printf("\n");if (ww == 1){for (i0 = 0; i0 < 10; i0++){for (j0 = 0; j0 < 9; j0++){x0 = *(*(str + j0) + i0);f(x0);}printf("\n\n");}}else{for (i0 = 0; i0 < 3; i0++){for (j0 = 0; j0 < 3; j0++){x0 = *(*(str2 + j0) + i0);f(x0);}printf("\n\n");}}goto l3;}if (m1 == 2 || n1 == 2)p3 = 0;}if (ww == 1){if (y == 'E'){printf("红方胜\n");break;}if (y == 'e'){printf("黑方胜\n");break;}}else{if (y == 'E'){printf("攻(红)方胜\n");break;}}}ljl:printf("是否再来一局?是(Yes),否(No),返回主菜单(Return):");scanf("%s", str1);if (str1[0] == 'Y' && str1[1] == 'e' && str1[2] == 's' && str1[3] == '\0') {if (ww == 1)goto lma;if (ww == 2)goto lma1;}else{if (str1[0] == 'N' && str1[1] == 'o' && str1[2] == '\0')printf("Game Over!\n");else{if (str1[0] == 'R' && str1[1] == 'e' && str1[2] == 't' && str1[3] == 'u' && str1[4] == 'r' && str1[5] == 'n' && str1[6] == '\0')goto ww1;else{printf("请按要求输入!\n");goto ljl;}}}for (i0 = 0; i0 < 10; i0++)free(*(str + i0));free(str);for (i0 = 0; i0 < 3; i0++)free(*(str2 + i0));free(str2);system("PAUSE");return 0;}。
中国象棋3D
中国象棋3D(一)配置Java3D环境本次实验我们组打算使用的编程语言是Java,但是Eclipse自带的函数库里不含Java3D 所需的配置信息,所以需要我们自己配置3D环境,下面是我们组的添加方法。
首先下载配置文件,包括j3dcore.jar,j3dutils.jar,vecmath.jar以及j3dcore-ogl-cg.dll,j3dcore-ogl-chk.dll,j3dcore-ogl.dll,j3dcore-d3d.dll然后将配置文件复制到Eclipse安装目录下的JDK/JRE/bin和DK/JRE/lib里当运行Java程序的时候按下面步骤添加jars文件1.点击工程鼠标右键->build path->configure build path2.add external jars3.选择相应的jar点击OK 然后运行程序(二)代码编写在编写代码部分,我们组遇到了许多问题,因为是从未接触过的知识,所以在编程过程中经常报错,为了在规定时间内完成任务,我们组就现学现用,在网上下载相关的代码,结合Java_API手册一遍又一遍的反复读,手册上没有的类就上网查,遇到英文就用翻译软件翻译了来看,最后再一步又一步的解决编程过程中的一个个error首先介绍一点必备的Java3D相关知识Java3D的场景图结构:Java3D实际上是Java语言在三维图形领域的扩展,与Java一样,Java3D有纯粹的面向对象结构。
Java3D的数据结构采用的是Scene Graphs Structure(场景图),就是一些具有方向性的不对称图形组成的树状结构(图1)。
我们在一个Java3D应用程序看到的逼真三维场景从程序的角度看来,实际就是由Java3D定义的一系列的对象,这些对象不是杂乱无序,对象之间也不是毫无关系。
如果想让三维图像正常显示,必须在这两点上遵循Java3D场景图的规定。
观察图1,Java3D场景图的树结构由各种各样的对象组成:在图中出现的这些对象都实现了Java3D中有重要的意义的类,从逻辑上我们将它们分为三类:•根节点(Root):Virtual Universe Object•节点(Node):Local Object、Branch Group Nodes、Behavior Node、Shape3D Node…•叶子节点(Leaf):Appearance、Geomery..图1:在应用中的Java3D场景图场景图中线和线的交汇点称为节点(Node),这些节点都是Java3D类的实例(Instance of Class),节点之间的线表示各个实例之间的关系。
极品象棋2012Java_java版中国象棋-其它资源游戏-源码中国
极品象棋2012Java_java版中国象棋-其它资源游戏-源码中国压缩包 : version_4.0_final.zip 列表SimpleChineseChess/SimpleChineseChess/.classpathSimpleChineseChess/.fatjarSimpleChineseChess/.projectSimpleChineseChess/.settings/SimpleChineseChess/.settings/org.eclipse.core.resources.prefsSimpleChineseChess/.settings/org.eclipse.jdt.core.prefsSimpleChineseChess/bin/SimpleChineseChess/bin/com/SimpleChineseChess/bin/com/chess/SimpleChineseChess/bin/com/chess/Audios/SimpleChineseChess/bin/com/chess/Audios/CAPTURE.WAVSimpleChineseChess/bin/com/chess/Audios/CHECK.WAVSimpleChineseChess/bin/com/chess/Audios/CLICK.WAVSimpleChineseChess/bin/com/chess/Audios/DRAW.WAVSimpleChineseChess/bin/com/chess/Audios/ILLEGAL.WAVSimpleChineseChess/bin/com/chess/Audios/LOSS.WAVSimpleChineseChess/bin/com/chess/Audios/MOVE.WAVSimpleChineseChess/bin/com/chess/app/SimpleChineseChess/bin/com/chess/app/ChessFrame$1.classSimpleChineseChess/bin/com/chess/app/ChessFrame.classSimpleChineseChess/bin/com/chess/app/Constants.classSimpleChineseChess/bin/com/chess/app/Functions.classSimpleChineseChess/bin/com/chess/app/MyAudioPlayer.classSimpleChineseChess/bin/com/chess/app/Rule$1.classSimpleChineseChess/bin/com/chess/app/Rule$Search.classSimpleChineseChess/bin/com/chess/app/Rule.classSimpleChineseChess/bin/com/chess/app/SimpleChess.classSimpleChineseChess/bin/com/chess/app/images/SimpleChineseChess/bin/com/chess/app/images/ba.gifSimpleChineseChess/bin/com/chess/app/images/bb.gifSimpleChineseChess/bin/com/chess/app/images/bc.gifSimpleChineseChess/bin/com/chess/app/images/bk.gif SimpleChineseChess/bin/com/chess/app/images/bn.gif SimpleChineseChess/bin/com/chess/app/images/board.gif SimpleChineseChess/bin/com/chess/app/images/bp.gif SimpleChineseChess/bin/com/chess/app/images/br.gif SimpleChineseChess/bin/com/chess/app/images/ra.gif SimpleChineseChess/bin/com/chess/app/images/rb.gif SimpleChineseChess/bin/com/chess/app/images/rc.gif SimpleChineseChess/bin/com/chess/app/images/rk.gif SimpleChineseChess/bin/com/chess/app/images/rn.gif SimpleChineseChess/bin/com/chess/app/images/rp.gif SimpleChineseChess/bin/com/chess/app/images/rr.gif SimpleChineseChess/bin/com/chess/app/images/selected.gif SimpleChineseChess/bin/com/chess/test/SimpleChineseChess/bin/com/chess/test/AudioPlayerTest.class SimpleChineseChess/bin/com/chess/test/TimerTest.class SimpleChineseChess/build.xmlSimpleChineseChess/res/SimpleChineseChess/res/Audios/SimpleChineseChess/res/Audios/CAPTURE.WAV SimpleChineseChess/res/Audios/CHECK.WAV SimpleChineseChess/res/Audios/CLICK.WAV SimpleChineseChess/res/Audios/DRAW.WAV SimpleChineseChess/res/Audios/ILLEGAL.WAV SimpleChineseChess/res/Audios/LOSS.WAV SimpleChineseChess/res/Audios/MOVE.WAV SimpleChineseChess/res/images/SimpleChineseChess/res/images/ba.gifSimpleChineseChess/res/images/bb.gifSimpleChineseChess/res/images/bc.gifSimpleChineseChess/res/images/bk.gifSimpleChineseChess/res/images/bn.gifSimpleChineseChess/res/images/board.gifSimpleChineseChess/res/images/bp.gifSimpleChineseChess/res/images/br.gifSimpleChineseChess/res/images/ra.gifSimpleChineseChess/res/images/rb.gifSimpleChineseChess/res/images/rc.gifSimpleChineseChess/res/images/rk.gifSimpleChineseChess/res/images/rn.gifSimpleChineseChess/res/images/rp.gifSimpleChineseChess/res/images/rr.gifSimpleChineseChess/res/images/selected.gif SimpleChineseChess/src/SimpleChineseChess/src/com/SimpleChineseChess/src/com/chess/SimpleChineseChess/src/com/chess/Audios/ SimpleChineseChess/src/com/chess/Audios/CAPTURE.WAV SimpleChineseChess/src/com/chess/Audios/CHECK.WAV SimpleChineseChess/src/com/chess/Audios/CLICK.WAV SimpleChineseChess/src/com/chess/Audios/DRAW.WAV SimpleChineseChess/src/com/chess/Audios/ILLEGAL.WAV SimpleChineseChess/src/com/chess/Audios/LOSS.WAV SimpleChineseChess/src/com/chess/Audios/MOVE.WAV SimpleChineseChess/src/com/chess/app/ SimpleChineseChess/src/com/chess/app/Constants.java SimpleChineseChess/src/com/chess/app/Functions.java SimpleChineseChess/src/com/chess/app/MyAudioPlayer.java SimpleChineseChess/src/com/chess/app/Rule.java SimpleChineseChess/src/com/chess/app/SimpleChess.java SimpleChineseChess/src/com/chess/app/images/ SimpleChineseChess/src/com/chess/app/images/ba.gif SimpleChineseChess/src/com/chess/app/images/bb.gif SimpleChineseChess/src/com/chess/app/images/bc.gif SimpleChineseChess/src/com/chess/app/images/bk.gif SimpleChineseChess/src/com/chess/app/images/bn.gif SimpleChineseChess/src/com/chess/app/images/board.gif SimpleChineseChess/src/com/chess/app/images/bp.gifSimpleChineseChess/src/com/chess/app/images/br.gif SimpleChineseChess/src/com/chess/app/images/ra.gif SimpleChineseChess/src/com/chess/app/images/rb.gif SimpleChineseChess/src/com/chess/app/images/rc.gif SimpleChineseChess/src/com/chess/app/images/rk.gif SimpleChineseChess/src/com/chess/app/images/rn.gif SimpleChineseChess/src/com/chess/app/images/rp.gif SimpleChineseChess/src/com/chess/app/images/rr.gif SimpleChineseChess/src/com/chess/app/images/selected.gif SimpleChineseChess/src/com/chess/test/ SimpleChineseChess/src/com/chess/test/AudioPlayerTest.java SimpleChineseChess/src/com/chess/test/TimerTest.java。
中国象棋打谱系统-Java语言毕业设计(内附详细的Java程序)
目录1.绪论 (1)1.1引言 (1)1.2主要设计内容 (1)2.开发工具简介 (2)2.1 java语言概述 (2)2.2 java语言的特点 (2)2.3 关于ECLIPSE (4)3.程序设计需求分析 (5)3.1任务概述 (5)3.2综合要求 (5)3.3 设计基本要求 (5)4.程序的总体设计 (6)4.1线程的设计 (6)4.2线程的生命周期 (7)5.程序的详细设计 (9)5.1程序流程图 (9)5.2数据字典 (10)5.3运行结果及界面 (14)6.实验总结 (16)参考文献 (16)附录(部分源代码) (17)JA V A语言课程设计1.绪论1.1引言象棋水平的发展是需要靠信息技术来推动的,国际象棋有两个很好的范例,一个是象棋棋谱编辑和对弈程序的公共平台——WinBoard平台,另一个是商业的国际象棋数据库和对弈软件——ChessBase,他们为国际象棋爱好者和研究者提供了极大的便利。
国际象棋软件有着成功的商业运作,已发展成一种产业。
然而,电脑在中国象棋上的运用还刚刚起步,尽管国内涌现出一大批中国象棋的专业网站和专业软件,但是由于缺乏必要的基础工作,电脑技术在中国象棋上的应用优势还无法体现出来。
在设计中国象棋软件过程中,国际象棋软件有很多值得借鉴的成功经验和优秀的思想。
例如 B. Moreland,微软(Microsoft)的程序设计师,业余从事国际象棋引擎Ferret的开发,他的一系列关于国际象棋程序设计的文章非常值得其他棋类程序设计人员借鉴。
然而,中国象棋与国际象棋存在着很大的差异,因此国际象棋的某些成熟技术,无法直接应用于中国象棋,需要对其加以改进和创新。
1.2主要设计内容本课题采用Java语言编写这个中国象棋对弈系统程序。
主要工作内容:搜集相关资料,准备参考资料,学习掌握开发方法、开发工具,需求分析,确定游戏程序实施方案,根据要求设计具体的流程图,编写程序,修改、完善程序,系统调试、测试,优化处理。
中国象棋游戏源代码
using System;using System.Collections.Generic; using ponentModel; using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace象棋{enum player{blank,red,blue,};enum chesstype{blank,jiang,che,ma,pao,xiang,zu,shi};struct chess{public player side;public chesstype type;};//下载于struct block{public PictureBox container;public chess item;};public partial class Form1 : Form{public Form1(){InitializeComponent();pictureboxlist = new List<PictureBox>(81); Matrix=new block[10][];int i,j;for (i = 0; i < 10;i++ ){Matrix[i] = new block[9];}for(i=0;i<10;i++){for(j=0;j<9;j++){Control[] col =this.Controls.Find("pictureBox" + (i*9+j+1), false); Matrix[i][j].container=col[0] as PictureBox;Matrix[i][j].container.Location = new Point(60 * j, 60 * i);}}redcoll = new collecter();bluecool = new collecter();for (i = 91; i < 107;i++ ){Control[] col =this.Controls.Find("pictureBox" + i, false);bluecool.add(col[0] as PictureBox);}for (i = 107; i < 123;i++ ){Control[] col =this.Controls.Find("pictureBox" + i, false);redcoll.add(col[0] as PictureBox);}resetground();}List<PictureBox> pictureboxlist;block[][] Matrix;collecter redcoll;collecter bluecool;int chozenX;int chozenY;player currentside;bool beenchozen;bool clickswitch;private void click1(object sender, EventArgs e) {if(!clickswitch){resetground();return;}string name = (sender as PictureBox).Name;string number = name.Substring(10);int index = Convert.ToInt32(number);int i,j;bool flag = false;i=(index-1)/9;j=(index-1)%9;//下载于if (beenchozen){Matrix[chozenX][chozenY].container.BorderStyle = BorderStyle.None;Matrix[chozenX][chozenY].container.BackColor =Color.Transparent;beenchozen = false;if(Matrix[chozenX][chozenY].item.side==Matrix[i][j].it em.side){return;}if(Matrix[chozenX][chozenY].item.side != player.blank) {if(Matrix[i][j].item.type== chesstype.jiang){flag=true;}if(!movechess(i, j)){return;}if(flag){if (currentside == player.red) {MessageBox.Show("红方胜利!点击任意处重新开局");}else{MessageBox.Show("蓝方胜利!点击任意处重新开局");}clickswitch = false;}}if (currentside == player.red){currentside = player.blue;label1.Text = "蓝方";label1.ForeColor = Color.Blue;}else{currentside = player.red;label1.Text = "红方";label1.ForeColor = Color.Red;}}else if(Matrix[i][j].item.side== currentside){Matrix[i][j].container.BorderStyle = BorderStyle.FixedSingle;Matrix[i][j].container.BackColor = Color.Brown;chozenX = i;chozenY = j;beenchozen = true;}}private void resetground(){int i, j;for (i = 0; i < 10;i++ ){for(j=0;j<9;j++){Matrix[i][j].container.Image = null;Matrix[i][j].item.side = player.blank;Matrix[i][j].item.type = chesstype.blank;}}beenchozen = false;clickswitch = true;currentside = player.red;label1.Text = "红方";label1.ForeColor = Color.Red;redcoll.clear();bluecool.clear();Matrix[0][0].container.Image = global::象棋.Properties.Resources.蓝车;Matrix[0][1].container.Image = global::象棋.Properties.Resources.蓝马;Matrix[0][2].container.Image = global::象棋.Properties.Resources.蓝象;Matrix[0][3].container.Image = global::象棋.Properties.Resources.蓝士;Matrix[0][4].container.Image = global::象棋.Properties.Resources.蓝将;Matrix[0][5].container.Image = global::象棋.Properties.Resources.蓝士;Matrix[0][6].container.Image = global::象棋.Properties.Resources.蓝象;Matrix[0][7].container.Image = global::象棋.Properties.Resources.蓝马;Matrix[0][8].container.Image = global::象棋.Properties.Resources.蓝车;Matrix[2][1].container.Image = global::象棋.Properties.Resources.蓝炮;Matrix[2][7].container.Image = global::象棋.Properties.Resources.蓝炮;Matrix[3][0].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][2].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][4].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][6].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][8].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[6][0].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][2].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][4].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][6].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][8].container.Image = global::象棋.Properties.Resources.红卒;Matrix[7][1].container.Image = global::象棋.Properties.Resources.红炮;Matrix[7][7].container.Image = global::象棋.Properties.Resources.红炮;Matrix[9][0].container.Image = global::象棋.Properties.Resources.红车;Matrix[9][1].container.Image = global::象棋.Properties.Resources.红马;Matrix[9][2].container.Image = global::象棋.Properties.Resources.红象;Matrix[9][3].container.Image = global::象棋.Properties.Resources.红士;Matrix[9][4].container.Image = global::象棋.Properties.Resources.红将;Matrix[9][5].container.Image = global::象棋.Properties.Resources.红士;Matrix[9][6].container.Image = global::象棋.Properties.Resources.红象;Matrix[9][7].container.Image = global::象棋.Properties.Resources.红马;Matrix[9][8].container.Image = global::象棋.Properties.Resources.红车;//下载于Matrix[0][0].item.side = player.blue;Matrix[0][1].item.side = player.blue;Matrix[0][2].item.side = player.blue;Matrix[0][3].item.side = player.blue;Matrix[0][4].item.side = player.blue;Matrix[0][5].item.side = player.blue;Matrix[0][6].item.side = player.blue;Matrix[0][7].item.side = player.blue;Matrix[0][8].item.side = player.blue;Matrix[2][1].item.side = player.blue;Matrix[2][7].item.side = player.blue;Matrix[3][0].item.side = player.blue;Matrix[3][2].item.side = player.blue;Matrix[3][4].item.side = player.blue;Matrix[3][6].item.side = player.blue;Matrix[3][8].item.side = player.blue;Matrix[6][0].item.side = player.red;Matrix[6][2].item.side = player.red;Matrix[6][4].item.side = player.red;Matrix[6][6].item.side = player.red;Matrix[6][8].item.side = player.red;Matrix[7][1].item.side = player.red;Matrix[7][7].item.side = player.red;Matrix[9][0].item.side = player.red;Matrix[9][1].item.side = player.red;Matrix[9][2].item.side = player.red;Matrix[9][3].item.side = player.red;Matrix[9][4].item.side = player.red;Matrix[9][5].item.side = player.red;Matrix[9][6].item.side = player.red;Matrix[9][7].item.side = player.red;Matrix[9][8].item.side = player.red;Matrix[0][0].item.type = chesstype.che; Matrix[0][1].item.type = chesstype.ma;Matrix[0][2].item.type = chesstype.xiang; Matrix[0][3].item.type = chesstype.shi; Matrix[0][4].item.type = chesstype.jiang; Matrix[0][5].item.type = chesstype.shi; Matrix[0][6].item.type = chesstype.xiang; Matrix[0][7].item.type = chesstype.ma;Matrix[0][8].item.type = chesstype.che; Matrix[2][1].item.type = chesstype.pao; Matrix[2][7].item.type = chesstype.pao; Matrix[3][0].item.type = chesstype.zu;Matrix[3][2].item.type = chesstype.zu;Matrix[3][4].item.type = chesstype.zu;Matrix[3][6].item.type = chesstype.zu;Matrix[3][8].item.type = chesstype.zu;Matrix[6][0].item.type = chesstype.zu;Matrix[6][2].item.type = chesstype.zu;Matrix[6][4].item.type = chesstype.zu;Matrix[6][6].item.type = chesstype.zu;Matrix[6][8].item.type = chesstype.zu;Matrix[7][1].item.type = chesstype.pao;Matrix[7][7].item.type = chesstype.pao;Matrix[9][0].item.type = chesstype.che;Matrix[9][1].item.type = chesstype.ma;Matrix[9][2].item.type = chesstype.xiang; Matrix[9][3].item.type = chesstype.shi;Matrix[9][4].item.type = chesstype.jiang; Matrix[9][5].item.type = chesstype.shi;Matrix[9][6].item.type = chesstype.xiang; Matrix[9][7].item.type = chesstype.ma;Matrix[9][8].item.type = chesstype.che;}private bool movechess(int X,int Y){int i, j, k, n=0;switch(Matrix[chozenX][chozenY].item.type) {case chesstype.che:if(chozenX==X){i = chozenY < Y ? chozenY : Y; j = chozenY > Y ? chozenY : Y;for(k=i+1;k<j;k++){if(Matrix[X][k].item.side!= player.blank){return false;}}}if (chozenY == Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){return false;}}}setmove(X, Y);return true;case chesstype.jiang:if(Matrix[X][Y].item.type== chesstype.jiang&&chozenY==Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){return false;}}setmove(X, Y);return true;}if(Matrix[chozenX][chozenY].item.side== player.blue){if(Y<3||Y>5||X>2){return false;}}else{if(Y<3||Y>5||X<7){return false;}}if((chozenX-X)*(chozenX-X)+(chozenY-Y)*(chozenY-Y)!=1) {return false;}setmove(X, Y);return true;case chesstype.ma:if (Math.Abs(chozenX - X) == 1 && Math.Abs(chozenY - Y) == 2){if (Matrix[chozenX][chozenY +(Y - chozenY) / Math.Abs(Y - chozenY)].item.side!= player.blank){return false;}}else if (Math.Abs(chozenX - X) == 2 && Math.Abs(chozenY - Y) == 1){if (Matrix[chozenX + (X - chozenX) / Math.Abs(X - chozenX)][chozenY].item.side != player.blank){return false;}}else{return false;}setmove(X, Y);return true;case chesstype.pao: n = 0;if(chozenX==X){i = chozenY < Y ? chozenY : Y; j = chozenY > Y ? chozenY : Y; n = 0;for(k=i+1;k<j;k++){if(Matrix[X][k].item.side!= player.blank){n++;}}if(n>1){return false;}}else if (chozenY == Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X; n = 0;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){n++;}}if (n > 1){return false;}}else{return false;}if(n==0&&Matrix[X][Y].item.side!= player.blank){return false;}if(n==1&&Matrix[X][Y].item.side== player.blank){return false;}setmove(X, Y);return true;case chesstype.shi:if(Matrix[chozenX][chozenY].item.side== player.blue){if(Y<3||Y>5||X>2){return false;}}else{if(Y<3||Y>5||X<7){return false;}}if(Math.Abs(X-chozenX)!=1||Math.Abs(chozenY-Y)!=1){return false;}setmove(X, Y);return true;case chesstype.xiang:if(Matrix[chozenX][chozenY].item.side == player.blue){if (X>4){return false;}}else{if (X<5){return false;}}if ((X - chozenX) * (X - chozenX) + (chozenY - Y) * (chozenY - Y) != 8){return false;}if(Matrix[(X+chozenX)/2][(Y+chozenY)/2].item.side!=player.blank){return false;}setmove(X, Y);return true;case chesstype.zu:if(X!=chozenX&&Y!=chozenY){return false;}if(Matrix[chozenX][chozenY].item.side == player.blue) {if (chozenX<5&&X-chozenX!=1) {return false;}if(chozenX>4){if(X==chozenX&&Math.Abs(Y-chozenY)!=1){return false;}if(Y==chozenY&&X-chozenX!=1){return false;}}}else{if (chozenX>4&&chozenX-X!=1) {return false;}if (chozenX <5){if (X == chozenX && Math.Abs(Y - chozenY) != 1){return false;}if (Y == chozenY && chozenX-X != 1){return false;}}}setmove(X,Y);return true;}return false;}private void setmove(int X,int Y){if (Matrix[X][Y].item.side== player.red) {redcoll.push(Matrix[X][Y].container.Image);}else if (Matrix[X][Y].item.side == player.blue){bluecool.push(Matrix[X][Y].container.Image);}Matrix[X][Y].container.Image =Matrix[chozenX][chozenY].container.Image;Matrix[X][Y].item =Matrix[chozenX][chozenY].item;Matrix[chozenX][chozenY].container.Image = null;Matrix[chozenX][chozenY].item.side = player.blank;Matrix[chozenX][chozenY].item.type= chesstype.blank;}}class collecter{public PictureBox[] container;public int number;public int chessnum;public collecter(){number = 0;container = new PictureBox[16];chessnum = 0;}public void add(PictureBox box).{container[number++] = box;}public void push(Image ima){container[chessnum++].BackgroundImage = ima;}public void clear(){for (int i = 0; i < chessnum; i++){container[i].BackgroundImage = null; }chessnum = 0;}};}.。
象棋游戏代码
#include<stdio.h>#include<math.h>int x[11][12];void main(){int i,j;int x1,y1,x2,y2,rg,gg;int rgo(int,int,int,int);int ggo(int,int,int,int);/*初始化棋子(开局)*/for(i=1;i<=5;i++){ x[i][1]=x[10-i][1]=i+10;x[i][10]=x[10-i][10]=i+20;if(i%2==1){ x[i][4]=x[10-i][4]=17;x[i][7]=x[10-i][7]=27;}}x[2][3]=x[8][3]=16;x[2][8]=x[8][8]=26;for(i=0;i<=9;i++)x[i][0]=i;for(i=1;i<=10;i++)x[0][i]=i;printf("S27\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n"); for(j=10;j>=0;j--){ for(i=0;i<=9;i++){if(i==0){printf("%2d |",x[i][j]);continue;}if(j==0){printf(" 0%d ",x[i][j]);continue;}switch(x[i][j]){case 0 : printf(" ");break;case 11 : printf(" 车");break;case 12 : printf(" 马");break;case 13 : printf(" 相");break;case 14 : printf(" 士");break;case 15 : printf(" 帅");break;case 16 : printf(" 炮");break;case 17 : printf(" 兵");break;case 21 : printf(" JU ");break;case 22 : printf(" MA ");break;case 23 : printf(" XN ");break;case 24 : printf(" SH ");break;case 25 : printf(" JN ");break;case 26 : printf(" PO ");break;case 27 : printf(" ZU ");break;}}if(j==1)printf("|\n---+-------------------------------------\n");else if(j==0)printf("\n");elseprintf("|\n | |\n");}/*走子*/for(;;){/*红子走子*/for(rg=0;rg==0;){for(x1=0,y1=0,x2=0,y2=0;x[x1][y1]==0||x[x1][y1]>=20||x2<1||y2<1||x2>9||y2>10||x[x2][y2] <20&&x[x2][y2]>10||x[x2][y2]==x[x1][y1];){printf("请输入红子坐标(x1,y1,x2,y2):");scanf("%d,%d,%d,%d",&x1,&y1,&x2,&y2);}if(x[x1][y1]>20){continue;}rg=rgo(x1,y1,x2,y2);if(rg==0)continue;else{if(x[x2][y2]==25){x[x2][y2]=x[x1][y1],x[x1][y1]=0;printf("lu qi shu le\n");}elsex[x2][y2]=x[x1][y1],x[x1][y1]=0;}}printf("S27 制作\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n"); for(j=10;j>=0;j--){for(i=0;i<=9;i++){if(i==0){printf("%2d |",x[i][j]);continue;}if(j==0){printf(" 0%d ",x[i][j]);continue;}switch(x[i][j]){case 0 : printf(" ");break;case 11 : printf(" 车");break;case 12 : printf(" 马");break;case 13 : printf(" 相");break;case 14 : printf(" 士");break;case 15 : printf(" 帅");break;case 16 : printf(" 炮");break;case 17 : printf(" 兵");break;case 21 : printf(" JU ");break;case 22 : printf(" MA ");break;case 23 : printf(" XN ");break;case 24 : printf(" SH ");break;case 25 : printf(" JN ");break;case 26 : printf(" PO ");break;case 27 : printf(" ZU ");break;}}if(j==1)printf("|\n---+-------------------------------------\n");else if(j==0)printf("\n");printf("|\n | |\n");}/*绿子走子*/for(gg=0;gg==0;){for(x1=0,y1=0,x2=0,y2=0;x[x1][y1]<10||x2<1||y2<1||x2>9||y2>10||x[x2][y2]>20||x[x2][y2]= =x[x1][y1];){printf("请输入绿子坐标(x1,y1,x2,y2):");scanf("%d,%d,%d,%d",&x1,&y1,&x2,&y2);}if(x[x1][y1]<20){continue;}gg=ggo(x1,y1,x2,y2);if(gg==0)continue;else{if(x[x2][y2]==25){x[x2][y2]=x[x1][y1],x[x1][y1]=0;printf("hong qi shu le\n");}elsex[x2][y2]=x[x1][y1],x[x1][y1]=0;}}printf("S27 制作\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n");for(j=10;j>=0;j--){for(i=0;i<=9;i++){if(i==0){printf("%2d |",x[i][j]);continue;}if(j==0){printf(" 0%d ",x[i][j]);continue;switch(x[i][j]){case 0 : printf(" ");break;case 11 : printf(" 车");break;case 12 : printf(" 马");break;case 13 : printf(" 相");break;case 14 : printf(" 士");break;case 15 : printf(" 帅");break;case 16 : printf(" 炮");break;case 17 : printf(" 兵");break;case 21 : printf(" JU ");break;case 22 : printf(" MA ");break;case 23 : printf(" XN ");break;case 24 : printf(" SH ");break;case 25 : printf(" JN ");break;case 26 : printf(" PO ");break;case 27 : printf(" ZU ");break;}}if(j==1)printf("|\n---+-------------------------------------\n");else if(j==0)printf("\n");elseprintf("|\n | |\n"); }}}/*判定红棋是否有错*/int rgo(int xa,int ya,int xb,int yb){ if(x[xa][ya]==11) /*车*/{int t;if(xb-xa==0){if(yb<ya)t=yb,yb=ya,ya=t;for(t=1+ya;t<yb;t++)if(x[xa][t]!=0&&x[xa][t]<20)return 0;}else if(yb-ya==0){ if(xb<xa)t=xb,xb=xa,xa=t;for(t=1+xa;t<xb;t++)if(x[t][ya]!=0&&x[xa][t]<20)return 0;}else return 0;return 1;}if(x[xa][ya]==12) /*马*/ { if(fabs(yb-ya)==2&&fabs(xb-xa)==1) {if(yb-ya>0&&x[xa][ya+1]!=0)return 0;if(yb-ya<0&&x[xa][ya-1]!=0)return 0;if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;}else if(fabs(xb-xa)==2&&fabs(yb-ya)==1) {if(xb-xa>0&&x[xa+1][ya]!=0)return 0;if(xb-xa<0&&x[xa-1][ya]!=0)return 0;if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;}else return 0;return 1;}if(x[xa][ya]==13) /*相*/ {if(yb>5)return 0;if(fabs(yb-ya)==2&&fabs(xb-xa)==2) {if(yb-ya>0){if(xb-xa>0&&x[xa+1][ya+1]!=0)return 0;else if(xb-xa<0&&x[xa-1][ya+1]!=0)return 0;}else{if(xb-xa>0&&x[xa+1][ya-1]!=0)return 0;else if(xb-xa<0&&x[xa-1][ya-1]!=0)return 0;}if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;}else return 0;return 1;}if(x[xa][ya]==14) /*士*/{if(xb<4||xb>6||yb>3)return 0;if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;if(fabs(yb-ya)==fabs(xb-xa)==1)return 1;else return 0;}if(x[xa][ya]==15) /*帅*/{if(xb<4||xb>6||yb>3)return 0;if((fabs(yb-ya)==1&&xb-xa==0)||(fabs(xb-xa)==1&&yb-ya==0)) return 1;else return 0;}if(x[xa][ya]==16) /*炮*/{int t,k;if(yb-ya==0){if(xb<xa)t=xb,xb=xa,xa=t;for(t=0,k=1;xb>xa+k;k++)if(x[xa+k][ya]!=0)t++;if(t>1)return 0;else if(t==0){if(x[xb][yb]==0||x[xa][ya]==0)return 1;else return 0;}else if(t==1){if(x[xb][yb]>20)return 1;else return 0;}}else if(xb-xa==0){if(yb<ya)t=yb,yb=ya,ya=t;for(t=0,k=1;yb>ya+k;k++)if(x[xa][ya+k]!=0)t++;if(t>1)return 0;else if(t==0){if(x[xb][yb]==0||x[xa][ya]==0)return 1;else return 0;}else if(t==1){if(x[xb][yb]>20)return 1;else return 0;}}else return 0;}if(x[xa][ya]==17) /*兵*/{if(yb==ya&&fabs(xb-xa)==1||yb-ya==1&&xb==xa) {if((ya==4||ya==5)&&xb!=xa)return 0;if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;}else return 0;}}/*判定绿棋是否有错*/int ggo(int xa,int ya,int xb,int yb){if(x[xa][ya]==21) /*车*/ {int t;if(xb-xa==0){if(yb<ya)t=yb,yb=ya,ya=t;for(t=1+ya;t<yb;t++)if(x[xa][t]!=0&&x[xa][t]>20)return 0;}else if(yb-ya==0){if(xb<xa)t=xb,xb=xa,xa=t;for(t=1+xa;t<xb;t++)if(x[t][ya]!=0&&x[t][ya]>20)return 0;}else return 0;return 1;}if(x[xa][ya]==22) /*马*/ {if(fabs(yb-ya)==2&&fabs(xb-xa)==1) {if(yb-ya>0&&x[xa][ya+1]!=0)return 0;if(yb-ya<0&&x[xa][ya-1]!=0)return 0;if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;}else if(fabs(xb-xa)==2&&fabs(yb-ya)==1) {if(xb-xa>0&&x[xa+1][ya]!=0)return 0;if(xb-xa<0&&x[xa-1][ya]!=0)if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;}else return 0;return 1;}if(x[xa][ya]==23) /*相*/ {if(yb<6)return 0;if(fabs(yb-ya)==2&&fabs(xb-xa)==2) {if(yb-ya>0){if(xb-xa>0&&x[xa+1][ya+1]!=0)return 0;else if(xb-xa<0&&x[xa-1][ya+1]!=0)return 0;}else{if(xb-xa>0&&x[xa+1][ya-1]!=0)return 0;else if(xb-xa<0&&x[xa-1][ya-1]!=0)return 0;}if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;}else return 0;return 1;}if(x[xa][ya]==24) /*士*/ {if(xb<4||xb>6||yb<8)return 0;if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;if(fabs(yb-ya)==fabs(xb-xa)==1) return 1;else return 0;}if(x[xa][ya]==25) /*帅*/ {return 0;if((fabs(yb-ya)==1&&xb-xa==0)||(fabs(xb-xa)==1&&yb-ya==0)) return 1;else return 0;}if(x[xa][ya]==26) /*炮*/{ int t,k;if(yb-ya==0){ if(xb<xa)t=xb,xb=xa,xa=t;for(t=0,k=1;xb>xa+k;k++)if(x[xa+k][ya]!=0)t++;if(t>1)return 0;else if(t==0){ if(x[xb][yb]==0||x[xa][ya]==0)return 1;else return 0;}else if(t==1){ if(x[xb][yb]<20)return 1;else return 0;}}else if(xb-xa==0){if(yb<ya)t=yb,yb=ya,ya=t;for(t=0,k=1;yb>ya+k;k++)if(x[xa][ya+k]!=0)t++;if(t>1)return 0;else if(t==0){if(x[xb][yb]==0||x[xa][ya]==0)return 1;else return 0;}else if(t==1){return 1;else return 0;}}else return 0;}if(x[xa][ya]==27) /*兵*/{if(yb==ya&&fabs(xb-xa)==1||ya-yb==1&&xb==xa) {if((ya==6||ya==7)&&xb!=xa)return 0;if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;}else return 0;return 1;}}。
中国象棋游戏源代码
using System;using System.Collections.Generic; using ponentModel; using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace象棋{enum player{blank,red,blue,};enum chesstype{blank,jiang,che,ma,pao,xiang,zu,shi};struct chess{public player side;public chesstype type;};//下载于struct block{public PictureBox container;public chess item;};public partial class Form1 : Form{public Form1(){InitializeComponent();pictureboxlist = new List<PictureBox>(81); Matrix=new block[10][];int i,j;for (i = 0; i < 10;i++ ){Matrix[i] = new block[9];}for(i=0;i<10;i++){for(j=0;j<9;j++){Control[] col =this.Controls.Find("pictureBox" + (i*9+j+1), false); Matrix[i][j].container=col[0] as PictureBox;Matrix[i][j].container.Location = new Point(60 * j, 60 * i);}}redcoll = new collecter();bluecool = new collecter();for (i = 91; i < 107;i++ ){Control[] col =this.Controls.Find("pictureBox" + i, false);bluecool.add(col[0] as PictureBox);}for (i = 107; i < 123;i++ ){Control[] col =this.Controls.Find("pictureBox" + i, false);redcoll.add(col[0] as PictureBox);}resetground();}List<PictureBox> pictureboxlist;block[][] Matrix;collecter redcoll;collecter bluecool;int chozenX;int chozenY;player currentside;bool beenchozen;bool clickswitch;private void click1(object sender, EventArgs e) {if(!clickswitch){resetground();return;}string name = (sender as PictureBox).Name;string number = name.Substring(10);int index = Convert.ToInt32(number);int i,j;bool flag = false;i=(index-1)/9;j=(index-1)%9;//下载于if (beenchozen){Matrix[chozenX][chozenY].container.BorderStyle = BorderStyle.None;Matrix[chozenX][chozenY].container.BackColor =Color.Transparent;beenchozen = false;if(Matrix[chozenX][chozenY].item.side==Matrix[i][j].it em.side){return;}if(Matrix[chozenX][chozenY].item.side != player.blank) {if(Matrix[i][j].item.type== chesstype.jiang){flag=true;}if(!movechess(i, j)){return;}if(flag){if (currentside == player.red) {MessageBox.Show("红方胜利!点击任意处重新开局");}else{MessageBox.Show("蓝方胜利!点击任意处重新开局");}clickswitch = false;}}if (currentside == player.red){currentside = player.blue;label1.Text = "蓝方";label1.ForeColor = Color.Blue;}else{currentside = player.red;label1.Text = "红方";label1.ForeColor = Color.Red;}}else if(Matrix[i][j].item.side== currentside){Matrix[i][j].container.BorderStyle = BorderStyle.FixedSingle;Matrix[i][j].container.BackColor = Color.Brown;chozenX = i;chozenY = j;beenchozen = true;}}private void resetground(){int i, j;for (i = 0; i < 10;i++ ){for(j=0;j<9;j++){Matrix[i][j].container.Image = null;Matrix[i][j].item.side = player.blank;Matrix[i][j].item.type = chesstype.blank;}}beenchozen = false;clickswitch = true;currentside = player.red;label1.Text = "红方";label1.ForeColor = Color.Red;redcoll.clear();bluecool.clear();Matrix[0][0].container.Image = global::象棋.Properties.Resources.蓝车;Matrix[0][1].container.Image = global::象棋.Properties.Resources.蓝马;Matrix[0][2].container.Image = global::象棋.Properties.Resources.蓝象;Matrix[0][3].container.Image = global::象棋.Properties.Resources.蓝士;Matrix[0][4].container.Image = global::象棋.Properties.Resources.蓝将;Matrix[0][5].container.Image = global::象棋.Properties.Resources.蓝士;Matrix[0][6].container.Image = global::象棋.Properties.Resources.蓝象;Matrix[0][7].container.Image = global::象棋.Properties.Resources.蓝马;Matrix[0][8].container.Image = global::象棋.Properties.Resources.蓝车;Matrix[2][1].container.Image = global::象棋.Properties.Resources.蓝炮;Matrix[2][7].container.Image = global::象棋.Properties.Resources.蓝炮;Matrix[3][0].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][2].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][4].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][6].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][8].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[6][0].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][2].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][4].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][6].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][8].container.Image = global::象棋.Properties.Resources.红卒;Matrix[7][1].container.Image = global::象棋.Properties.Resources.红炮;Matrix[7][7].container.Image = global::象棋.Properties.Resources.红炮;Matrix[9][0].container.Image = global::象棋.Properties.Resources.红车;Matrix[9][1].container.Image = global::象棋.Properties.Resources.红马;Matrix[9][2].container.Image = global::象棋.Properties.Resources.红象;Matrix[9][3].container.Image = global::象棋.Properties.Resources.红士;Matrix[9][4].container.Image = global::象棋.Properties.Resources.红将;Matrix[9][5].container.Image = global::象棋.Properties.Resources.红士;Matrix[9][6].container.Image = global::象棋.Properties.Resources.红象;Matrix[9][7].container.Image = global::象棋.Properties.Resources.红马;Matrix[9][8].container.Image = global::象棋.Properties.Resources.红车;//下载于Matrix[0][0].item.side = player.blue;Matrix[0][1].item.side = player.blue;Matrix[0][2].item.side = player.blue;Matrix[0][3].item.side = player.blue;Matrix[0][4].item.side = player.blue;Matrix[0][5].item.side = player.blue;Matrix[0][6].item.side = player.blue;Matrix[0][7].item.side = player.blue;Matrix[0][8].item.side = player.blue;Matrix[2][1].item.side = player.blue;Matrix[2][7].item.side = player.blue;Matrix[3][0].item.side = player.blue;Matrix[3][2].item.side = player.blue;Matrix[3][4].item.side = player.blue;Matrix[3][6].item.side = player.blue;Matrix[3][8].item.side = player.blue;Matrix[6][0].item.side = player.red;Matrix[6][2].item.side = player.red;Matrix[6][4].item.side = player.red;Matrix[6][6].item.side = player.red;Matrix[6][8].item.side = player.red;Matrix[7][1].item.side = player.red;Matrix[7][7].item.side = player.red;Matrix[9][0].item.side = player.red;Matrix[9][1].item.side = player.red;Matrix[9][2].item.side = player.red;Matrix[9][3].item.side = player.red;Matrix[9][4].item.side = player.red;Matrix[9][5].item.side = player.red;Matrix[9][6].item.side = player.red;Matrix[9][7].item.side = player.red;Matrix[9][8].item.side = player.red;Matrix[0][0].item.type = chesstype.che; Matrix[0][1].item.type = chesstype.ma;Matrix[0][2].item.type = chesstype.xiang; Matrix[0][3].item.type = chesstype.shi; Matrix[0][4].item.type = chesstype.jiang; Matrix[0][5].item.type = chesstype.shi; Matrix[0][6].item.type = chesstype.xiang; Matrix[0][7].item.type = chesstype.ma;Matrix[0][8].item.type = chesstype.che; Matrix[2][1].item.type = chesstype.pao; Matrix[2][7].item.type = chesstype.pao; Matrix[3][0].item.type = chesstype.zu;Matrix[3][2].item.type = chesstype.zu;Matrix[3][4].item.type = chesstype.zu;Matrix[3][6].item.type = chesstype.zu;Matrix[3][8].item.type = chesstype.zu;Matrix[6][0].item.type = chesstype.zu;Matrix[6][2].item.type = chesstype.zu;Matrix[6][4].item.type = chesstype.zu;Matrix[6][6].item.type = chesstype.zu;Matrix[6][8].item.type = chesstype.zu;Matrix[7][1].item.type = chesstype.pao;Matrix[7][7].item.type = chesstype.pao;Matrix[9][0].item.type = chesstype.che;Matrix[9][1].item.type = chesstype.ma;Matrix[9][2].item.type = chesstype.xiang; Matrix[9][3].item.type = chesstype.shi;Matrix[9][4].item.type = chesstype.jiang; Matrix[9][5].item.type = chesstype.shi;Matrix[9][6].item.type = chesstype.xiang; Matrix[9][7].item.type = chesstype.ma;Matrix[9][8].item.type = chesstype.che;}private bool movechess(int X,int Y){int i, j, k, n=0;switch(Matrix[chozenX][chozenY].item.type) {case chesstype.che:if(chozenX==X){i = chozenY < Y ? chozenY : Y; j = chozenY > Y ? chozenY : Y;for(k=i+1;k<j;k++){if(Matrix[X][k].item.side!= player.blank){return false;}}}if (chozenY == Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){return false;}}}setmove(X, Y);return true;case chesstype.jiang:if(Matrix[X][Y].item.type== chesstype.jiang&&chozenY==Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){return false;}}setmove(X, Y);return true;}if(Matrix[chozenX][chozenY].item.side== player.blue){if(Y<3||Y>5||X>2){return false;}}else{if(Y<3||Y>5||X<7){return false;}}if((chozenX-X)*(chozenX-X)+(chozenY-Y)*(chozenY-Y)!=1) {return false;}setmove(X, Y);return true;case chesstype.ma:if (Math.Abs(chozenX - X) == 1 && Math.Abs(chozenY - Y) == 2){if (Matrix[chozenX][chozenY +(Y - chozenY) / Math.Abs(Y - chozenY)].item.side!= player.blank){return false;}}else if (Math.Abs(chozenX - X) == 2 && Math.Abs(chozenY - Y) == 1){if (Matrix[chozenX + (X - chozenX) / Math.Abs(X - chozenX)][chozenY].item.side != player.blank){return false;}}else{return false;}setmove(X, Y);return true;case chesstype.pao: n = 0;if(chozenX==X){i = chozenY < Y ? chozenY : Y; j = chozenY > Y ? chozenY : Y; n = 0;for(k=i+1;k<j;k++){if(Matrix[X][k].item.side!= player.blank){n++;}}if(n>1){return false;}}else if (chozenY == Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X; n = 0;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){n++;}}if (n > 1){return false;}}else{return false;}if(n==0&&Matrix[X][Y].item.side!= player.blank){return false;}if(n==1&&Matrix[X][Y].item.side== player.blank){return false;}setmove(X, Y);return true;case chesstype.shi:if(Matrix[chozenX][chozenY].item.side== player.blue){if(Y<3||Y>5||X>2){return false;}}else{if(Y<3||Y>5||X<7){return false;}}if(Math.Abs(X-chozenX)!=1||Math.Abs(chozenY-Y)!=1){return false;}setmove(X, Y);return true;case chesstype.xiang:if(Matrix[chozenX][chozenY].item.side == player.blue){if (X>4){return false;}}else{if (X<5){return false;}}if ((X - chozenX) * (X - chozenX) + (chozenY - Y) * (chozenY - Y) != 8){return false;}if(Matrix[(X+chozenX)/2][(Y+chozenY)/2].item.side!=player.blank){return false;}setmove(X, Y);return true;case chesstype.zu:if(X!=chozenX&&Y!=chozenY){return false;}if(Matrix[chozenX][chozenY].item.side == player.blue) {if (chozenX<5&&X-chozenX!=1) {return false;}if(chozenX>4){if(X==chozenX&&Math.Abs(Y-chozenY)!=1){return false;}if(Y==chozenY&&X-chozenX!=1){return false;}}}else{if (chozenX>4&&chozenX-X!=1) {return false;}if (chozenX <5){if (X == chozenX && Math.Abs(Y - chozenY) != 1){return false;}if (Y == chozenY && chozenX-X != 1){return false;}}}setmove(X,Y);return true;}return false;}private void setmove(int X,int Y){if (Matrix[X][Y].item.side== player.red) {redcoll.push(Matrix[X][Y].container.Image);}else if (Matrix[X][Y].item.side == player.blue){bluecool.push(Matrix[X][Y].container.Image);}Matrix[X][Y].container.Image =Matrix[chozenX][chozenY].container.Image;Matrix[X][Y].item =Matrix[chozenX][chozenY].item;Matrix[chozenX][chozenY].container.Image = null;Matrix[chozenX][chozenY].item.side = player.blank;Matrix[chozenX][chozenY].item.type= chesstype.blank;}}class collecter{public PictureBox[] container;public int number;public int chessnum;public collecter(){number = 0;container = new PictureBox[16];chessnum = 0;}public void add(PictureBox box).{container[number++] = box;}public void push(Image ima){container[chessnum++].BackgroundImage = ima;}public void clear(){for (int i = 0; i < chessnum; i++){container[i].BackgroundImage = null; }chessnum = 0;}};}.。
中国象棋java源码
东北大学秦皇岛分校信息与计算科学系Java程序设计实验报告专业名称信息与计算科学班级学号*******学生姓名彭军指导教师王薇完成时间2011.04.15一、设计题目中国象棋。
二、设计说明2.1 引言象棋水平的发展是需要靠信息技术来推动的,国际象棋有两个很好的范例,一个是象棋棋谱编辑和对弈程序的公共平台——WinBoard平台,另一个是商业的国际象棋数据库和对弈软件——ChessBase,他们为国际象棋爱好者和研究者提供了极大的便利。
国际象棋软件有着成功的商业运作,已发展成一种产业。
然而,电脑在中国象棋上的运用还刚刚起步,尽管国内涌现出一大批中国象棋的专业网站和专业软件,但是由于缺乏必要的基础工作,电脑技术在中国象棋上的应用优势还无法体现出来。
在设计中国象棋软件过程中,国际象棋软件有很多值得借鉴的成功经验和优秀的思想。
例如B. Moreland,微软(Microsoft)的程序设计师,业余从事国际象棋引擎Ferret的开发,他的一系列关于国际象棋程序设计的文章非常值得其他棋类程序设计人员借鉴。
然而,中国象棋与国际象棋存在着很大的差异,因此国际象棋的某些成熟技术,无法直接应用于中国象棋,需要对其加以改进和创新。
本文针对中国象棋程序设计的一系列问题,总结出一些搜索引擎的设计方法,并给出java 语言的实现2.2程序的设计及实现本系统主要有以下4个模块,每个模块对应一个程序包:1、engine:搜索引擎包,系统的核心部分。
2、message:网络对战过程中各种消息及其传递机制的类实现包。
3、main:主界面实现包。
4、pieces:棋子及其相关类实现包。
现就各个包中的要点给与说明。
2.2.1 搜索引擎的实现(engine包)(1) BitBoard.java:位棋盘的实现,见2.4节。
(2) CCEvalue.java:评价函数知识类。
本程序使用开源软件“梦入神蛋”的快速评价函数。
该函数包含子力价值和棋子所在位置的奖励值。
纯Java写的中国象棋
play[i].setBounds(k,398,55,55);
play[i].setName("卒2" + i);
}
//炮
in = new ImageIcon("image\\红炮.GIF");
for (i=28,k=81;i<30;i++,k+=342){
new WindowAdapter() {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
);
//窗体居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
}
catch(Exception e){
}
play[Man].setVisible(true);
}
//闪烁当前提示信息以免用户看不见
else {
text.setVisible(false);
//时间控制
try{
tmain.sleep(250);
}
catch(Exception e){
}
text.setVisible(true);
play[i] = new JLabel(in);
play[i].setBounds(k,569,55,55);
play[i].setName("象2");
}
//士
in = new ImageIcon("image\\红士.GIF");
中国象棋打谱系统-Java语言毕业设计(内附详细Java程序)
JAVA语言课程设计设计题目:中国象棋打谱系统目录JA V A语言课程设计目录1 设计 (1)1.1 设计内容 (1)1.2 设计要求 (1)1.3 总体设计 (2)2 具体设计 (3)2.1运行效果和程序设计 (3)2.2 主类Chess (5)2.3对弈棋盘ChessBoard (9)2.4 棋子ChesePiece (19)2.5棋点ChessPoint (21)2.6 走棋规则Rule (23)2.7步骤MoveStep………………………………………….23.2.8记录棋谱MakeChessManual…………………………..24.2.9棋谱演示Demon (24)3课程总结 (25)1.1 设计内容中国象棋打谱系统使用户能够将对弈过程保存成文件,并能够重新演示对弈过程,即复盘。
1.2 设计要求1.中国象棋是双方在由9条和10条横线分割而成的棋盘对弈。
竖线与横线的交叉点称做棋点,每个棋子都放置在棋点上,而不是方格中。
河界将棋盘分为两个等分=份,没一边都有一个由9个点组成的区域“将”,“帅”,“士”只能在“九宫”内移动,并且“将”,“帅”,的每一步只可以水平或垂直移动一个棋点。
士:“士”只能在“九宫”内移动,并且每一步只可以沿对角线方向移动一个棋点。
象;一步必须沿对角线方向走两个棋点。
但它既不能过河,也不能跳过或穿越障碍,即象征走的过程不能被“别眼”。
马:每一步只可以水平或垂直移动一个棋点。
但它必须沿对角线方向,向左或向右移动,中国象棋的马不能跳过障碍,即在走的过程不能被“别腿”。
车:可以沿水平或垂直方向移动任意个无障碍的点。
炮:行走规则和车很类似,但它必须跳过一个棋子来吃掉对方的一个棋子。
被跳过的棋子被称为桥或者屏风。
兵:过河前每步只能向前移动一个棋点。
过河后,它便增加了向左右移动的能力。
并不允许向后移动。
2.单击“中国象棋”菜单可以选择“制作棋谱”,“保存棋谱”和“演示棋谱”菜单项。
3.选择“制作棋谱”,将出现相应的棋盘,该棋盘可进行实战对弈,可以随时选择“保存棋谱”,将对弈过程保存成一个棋谱文件。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.io.*;public class Chess{public static void main(String args[]){new ChessMainFrame("中国象棋:观棋不语真君子,棋死无悔大丈夫");}}class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{//玩家JLabel play[] = new JLabel[32];//棋盘JLabel image;//窗格Container con;//工具栏JToolBar jmain;//重新开始JButton anew;//悔棋JButton repent;//退出JButton exit;//当前信息JLabel text;//保存当前操作Vector Var;//规则类对象(使于调用方法)ChessRule rule;/**** 单击棋子** chessManClick = true 闪烁棋子并给线程响应** chessManClick = false 吃棋子停止闪烁并给线程响应*/boolean chessManClick;/**** 控制玩家走棋** chessPlayClick=1 黑棋走棋** chessPlayClick=2 红棋走棋默认红棋** chessPlayClick=3 双方都不能走棋*/int chessPlayClick=2;//控制棋子闪烁的线程Thread tmain;//把第一次的单击棋子给线程响应static int Man,i;ChessMainFrame(){new ChessMainFrame("中国象棋");}/**** 构造函数** 初始化图形用户界面*/ChessMainFrame(String Title){//获行客格引用con = this.getContentPane();con.setLayout(null);//实例化规则类rule = new ChessRule();Var = new Vector();//创建工具栏jmain = new JToolBar();text = new JLabel("欢迎使用象棋对弈系统");//当鼠标放上显示信息text.setToolTipText("信息提示");anew = new JButton(" 新游戏 ");anew.setToolTipText("重新开始新的一局");exit = new JButton(" 退出 ");exit.setToolTipText("退出象棋程序程序");repent = new JButton(" 悔棋 ");repent.setToolTipText("返回到上次走棋的位置");//把组件添加到工具栏jmain.setLayout(new GridLayout(0,4));jmain.add(anew);jmain.add(repent);jmain.add(exit);jmain.add(text);jmain.setBounds(0,0,558,30);con.add(jmain);//添加棋子标签drawChessMan();//注册按扭监听anew.addActionListener(this);repent.addActionListener(this);exit.addActionListener(this);//注册棋子移动监听for (int i=0;i<32;i++){con.add(play[i]);play[i].addMouseListener(this);}//添加棋盘标签con.add(image = new JLabel(new ImageIcon("image\\Main.GIF")));image.setBounds(0,30,558,620);image.addMouseListener(this);//注册窗体关闭监听this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent we){System.exit(0);}});//窗体居中Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();Dimension frameSize = this.getSize();if (frameSize.height > screenSize.height){frameSize.height = screenSize.height;}if (frameSize.width > screenSize.width){frameSize.width = screenSize.width;}this.setLocation((screenSize.width - frameSize.width) / 2 - 280 ,(screenSize.height - frameSize.height ) / 2 - 350);//设置this.setIconImage(new ImageIcon("image\\红将.GIF").getImage());this.setResizable(false);this.setTitle(Title);this.setSize(558,670);this.show();}/**** 添加棋子方法*/public void drawChessMan(){//流程控制int i,k;//图标Icon in;//黑色棋子//车in = new ImageIcon("image\\黑车.GIF");for (i=0,k=24;i<2;i++,k+=456){play[i] = new JLabel(in);play[i].setBounds(k,56,55,55);play[i].setName("车1");}//马in = new ImageIcon("image\\黑马.GIF");for (i=4,k=81;i<6;i++,k+=342){play[i] = new JLabel(in);play[i].setBounds(k,56,55,55);play[i].setName("马1");}//相in = new ImageIcon("image\\黑象.GIF");for (i=8,k=138;i<10;i++,k+=228){play[i] = new JLabel(in);play[i].setBounds(k,56,55,55);play[i].setName("象1");}//士in = new ImageIcon("image\\黑士.GIF"); for (i=12,k=195;i<14;i++,k+=114){ play[i] = new JLabel(in);play[i].setBounds(k,56,55,55);play[i].setName("士1");}//卒in = new ImageIcon("image\\黑卒.GIF"); for (i=16,k=24;i<21;i++,k+=114){ play[i] = new JLabel(in);play[i].setBounds(k,227,55,55);play[i].setName("卒1" + i);}//炮in = new ImageIcon("image\\黑炮.GIF"); for (i=26,k=81;i<28;i++,k+=342){ play[i] = new JLabel(in);play[i].setBounds(k,170,55,55);play[i].setName("炮1" + i);}//将in = new ImageIcon("image\\黑将.GIF"); play[30] = new JLabel(in);play[30].setBounds(252,56,55,55);play[30].setName("将1");//红色棋子//车in = new ImageIcon("image\\红车.GIF"); for (i=2,k=24;i<4;i++,k+=456){play[i] = new JLabel(in);play[i].setBounds(k,569,55,55);play[i].setName("车2");}//马in = new ImageIcon("image\\红马.GIF"); for (i=6,k=81;i<8;i++,k+=342){play[i] = new JLabel(in);play[i].setBounds(k,569,55,55);play[i].setName("马2");}//相in = new ImageIcon("image\\红象.GIF"); for (i=10,k=138;i<12;i++,k+=228){ play[i] = new JLabel(in);play[i].setBounds(k,569,55,55);play[i].setName("象2");}//士in = new ImageIcon("image\\红士.GIF"); for (i=14,k=195;i<16;i++,k+=114){ play[i] = new JLabel(in);play[i].setBounds(k,569,55,55);play[i].setName("士2");}//兵in = new ImageIcon("image\\红卒.GIF"); for (i=21,k=24;i<26;i++,k+=114){ play[i] = new JLabel(in);play[i].setBounds(k,398,55,55);play[i].setName("卒2" + i);}//炮in = new ImageIcon("image\\红炮.GIF"); for (i=28,k=81;i<30;i++,k+=342){ play[i] = new JLabel(in);play[i].setBounds(k,455,55,55);play[i].setName("炮2" + i);}//帅in = new ImageIcon("image\\红将.GIF"); play[31] = new JLabel(in);play[31].setBounds(252,569,55,55);play[31].setName("帅2");}/**** 线程方法控制棋子闪烁*/public void run(){while (true){//单击棋子第一下开始闪烁if (chessManClick){play[Man].setVisible(false);//时间控制try{tmain.sleep(200);}catch(Exception e){}play[Man].setVisible(true);}//闪烁当前提示信息以免用户看不见else {text.setVisible(false);//时间控制try{tmain.sleep(250);}catch(Exception e){}text.setVisible(true);}try{tmain.sleep(350);}catch (Exception e){}}}/**** 单击棋子方法*/public void mouseClicked(MouseEvent me){System.out.println("Mouse");//当前坐标int Ex=0,Ey=0;//启动线程if (tmain == null){tmain = new Thread(this);tmain.start();}//单击棋盘(移动棋子)if (me.getSource().equals(image)){//该红棋走棋的时候if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2'){Ex = play[Man].getX();Ey = play[Man].getY();//移动卒、兵if (Man > 15 && Man < 26){rule.armsRule(Man,play[Man],me);}//移动炮else if (Man > 25 && Man < 30){rule.cannonRule(play[Man],play,me);}//移动车else if (Man >=0 && Man < 4){rule.cannonRule(play[Man],play,me);}//移动马else if (Man > 3 && Man < 8){rule.horseRule(play[Man],play,me);}//移动相、象else if (Man > 7 && Man < 12){rule.elephantRule(Man,play[Man],play,me);}//移动仕、士else if (Man > 11 && Man < 16){rule.chapRule(Man,play[Man],play,me);}//移动将、帅else if (Man == 30 || Man == 31){rule.willRule(Man,play[Man],play,me);}//是否走棋错误(是否在原地没有动)if (Ex == play[Man].getX() && Ey == play[Man].getY()){text.setText(" 红棋走棋");chessPlayClick=2;}else {text.setText(" 黑棋走棋");chessPlayClick=1;}}//if//该黑棋走棋的时候else if (chessPlayClick == 1 && play[Man].getName().charAt(1) == '1'){Ex = play[Man].getX();Ey = play[Man].getY();//移动卒、兵if (Man > 15 && Man < 26){rule.armsRule(Man,play[Man],me);}//移动炮else if (Man > 25 && Man < 30){rule.cannonRule(play[Man],play,me);}//移动车else if (Man >=0 && Man < 4){rule.cannonRule(play[Man],play,me);}//移动马else if (Man > 3 && Man < 8){rule.horseRule(play[Man],play,me);}//移动相、象else if (Man > 7 && Man < 12){rule.elephantRule(Man,play[Man],play,me);}//移动仕、士else if (Man > 11 && Man < 16){rule.chapRule(Man,play[Man],play,me);}//移动将、帅else if (Man == 30 || Man == 31){rule.willRule(Man,play[Man],play,me);}//是否走棋错误(是否在原地没有动)if (Ex == play[Man].getX() && Ey == play[Man].getY()){ text.setText(" 黑棋走棋");chessPlayClick=1;}else {text.setText(" 红棋走棋");chessPlayClick=2;}}//else if//当前没有操作(停止闪烁)chessManClick=false;}//if//单击棋子else{//第一次单击棋子(闪烁棋子)if (!chessManClick){for (int i=0;i<32;i++){//被单击的棋子if (me.getSource().equals(play[i])){//告诉线程让该棋子闪烁Man=i;//开始闪烁chessManClick=true;break;}}//for}//if//第二次单击棋子(吃棋子)else if (chessManClick){//当前没有操作(停止闪烁)chessManClick=false;for (i=0;i<32;i++){//找到被吃的棋子if (me.getSource().equals(play[i])){//该红棋吃棋的时候if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2'){Ex = play[Man].getX();Ey = play[Man].getY();//卒、兵吃规则if (Man > 15 && Man < 26){rule.armsRule(play[Man],play[i]);}//炮吃规则else if (Man > 25 && Man < 30){rule.cannonRule(0,play[Man],play[i],play,me);}//车吃规则else if (Man >=0 && Man < 4){rule.cannonRule(1,play[Man],play[i],play,me);}//马吃规则else if (Man > 3 && Man < 8){rule.horseRule(play[Man],play[i],play,me);}//相、象吃规则else if (Man > 7 && Man < 12){rule.elephantRule(play[Man],play[i],play);}//士、仕吃棋规则else if (Man > 11 && Man < 16){rule.chapRule(Man,play[Man],play[i],play);}//将、帅吃棋规则else if (Man == 30 || Man == 31){rule.willRule(Man,play[Man],play[i],play);play[Man].setVisible(true);}//是否走棋错误(是否在原地没有动)if (Ex == play[Man].getX() && Ey == play[Man].getY()){text.setText(" 红棋走棋");chessPlayClick=2;break;}else{text.setText(" 黑棋走棋");chessPlayClick=1;break;}}//if//该黑棋吃棋的时候else if (chessPlayClick == 1 &&play[Man].getName().charAt(1) == '1'){Ex = play[Man].getX();Ey = play[Man].getY();//卒吃规则if (Man > 15 && Man < 26){rule.armsRule(play[Man],play[i]);}//炮吃规则else if (Man > 25 && Man < 30){rule.cannonRule(0,play[Man],play[i],play,me);}//车吃规则else if (Man >=0 && Man < 4){rule.cannonRule(1,play[Man],play[i],play,me);}//马吃规则else if (Man > 3 && Man < 8){rule.horseRule(play[Man],play[i],play,me);}//相、象吃规则else if (Man > 7 && Man < 12){rule.elephantRule(play[Man],play[i],play);}//士、仕吃棋规则else if (Man > 11 && Man < 16){rule.chapRule(Man,play[Man],play[i],play);}//将、帅吃棋规则else if (Man == 30 || Man == 31){rule.willRule(Man,play[Man],play[i],play);play[Man].setVisible(true);}//是否走棋错误(是否在原地没有动)if (Ex == play[Man].getX() && Ey == play[Man].getY()){text.setText(" 黑棋走棋");chessPlayClick=1;break;}else {text.setText(" 红棋走棋");chessPlayClick=2;break;}}//else if}//if}//for//是否胜利if (!play[31].isVisible()){JOptionPane.showConfirmDialog(this,"黑棋胜利","玩家一胜利",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);//双方都不可以在走棋了chessPlayClick=3;text.setText(" 黑棋胜利");}//ifelse if (!play[30].isVisible()){JOptionPane.showConfirmDialog(this,"红棋胜利","玩家二胜利",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);chessPlayClick=3;text.setText(" 红棋胜利");}//else if}//else}//else}public void mousePressed(MouseEvent me){}public void mouseReleased(MouseEvent me){}public void mouseEntered(MouseEvent me){}public void mouseExited(MouseEvent me){}/**** 定义按钮的事件响应*/public void actionPerformed(ActionEvent ae) { //重新开始按钮if (ae.getSource().equals(anew)){int i,k;//重新排列每个棋子的位置//黑色棋子//车for (i=0,k=24;i<2;i++,k+=456){play[i].setBounds(k,56,55,55);}//马for (i=4,k=81;i<6;i++,k+=342){play[i].setBounds(k,56,55,55);}//相for (i=8,k=138;i<10;i++,k+=228){play[i].setBounds(k,56,55,55);}//士for (i=12,k=195;i<14;i++,k+=114){play[i].setBounds(k,56,55,55);}//卒for (i=16,k=24;i<21;i++,k+=114){ play[i].setBounds(k,227,55,55); }//炮for (i=26,k=81;i<28;i++,k+=342){ play[i].setBounds(k,170,55,55); }//将play[30].setBounds(252,56,55,55);//红色棋子//车for (i=2,k=24;i<4;i++,k+=456){ play[i].setBounds(k,569,55,55); }//马for (i=6,k=81;i<8;i++,k+=342){ play[i].setBounds(k,569,55,55); }//相for (i=10,k=138;i<12;i++,k+=228){ play[i].setBounds(k,569,55,55); }//士for (i=14,k=195;i<16;i++,k+=114){ play[i].setBounds(k,569,55,55); }//兵for (i=21,k=24;i<26;i++,k+=114){ play[i].setBounds(k,398,55,55); }//炮for (i=28,k=81;i<30;i++,k+=342){ play[i].setBounds(k,455,55,55); }//帅play[31].setBounds(252,569,55,55);chessPlayClick = 2;text.setText(" 红棋走棋");for (i=0;i<32;i++){play[i].setVisible(true);}//清除Vector中的容Var.clear();}//悔棋按钮else if (ae.getSource().equals(repent)){try{//获得setVisible属性值String S = (String)Var.get(Var.size()-4);//获得X坐标int x = Integer.parseInt((String)Var.get(Var.size()-3));//获得Y坐标int y = Integer.parseInt((String)Var.get(Var.size()-2));//获得索引int M = Integer.parseInt((String)Var.get(Var.size()-1));//赋给棋子play[M].setVisible(true);play[M].setBounds(x,y,55,55);if (play[M].getName().charAt(1) == '1'){text.setText(" 黑棋走棋");chessPlayClick = 1;}else{text.setText(" 红棋走棋");chessPlayClick = 2;}//删除用过的坐标Var.remove(Var.size()-4);Var.remove(Var.size()-3);Var.remove(Var.size()-2);Var.remove(Var.size()-1);//停止旗子闪烁chessManClick=false;}catch(Exception e){}}//退出else if (ae.getSource().equals(exit)){int j=JOptionPane.showConfirmDialog(this,"真的要退出吗?","退出",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);if (j == JOptionPane.YES_OPTION){System.exit(0);}}}/*定义中国象棋规则的类*/class ChessRule {/**卒子的移动规则*/public void armsRule(int Man,JLabel play,MouseEvent me){ //黑卒向下if (Man < 21){//向下移动、得到终点的坐标模糊成合法的坐标if ((me.getY()-play.getY()) > 27 && (me.getY()-play.getY()) < 86 && (me.getX()-play.getX()) < 55 && (me.getX()-play.getX()) > 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(play.getX(),play.getY()+57,55,55);}//向右移动、得到终点的坐标模糊成合法的坐标、必须过河else if (play.getY() > 284 && (me.getX() - play.getX()) >= 57 && (me.getX() - play.getX()) <= 112){play.setBounds(play.getX()+57,play.getY(),55,55);}//向左移动、得到终点的坐标模糊成合法的坐标、必须过河else if (play.getY() > 284 && (play.getX() - me.getX()) >= 2 && (play.getX() - me.getX()) <=58){//模糊坐标play.setBounds(play.getX()-57,play.getY(),55,55);}}//红卒向上else{//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));//向上移动、得到终点的坐标模糊成合法的坐标if ((me.getX()-play.getX()) >= 0 && (me.getX()-play.getX()) <= 55 && (play.getY()-me.getY()) >27 && play.getY()-me.getY() < 86){play.setBounds(play.getX(),play.getY()-57,55,55);}//向右移动、得到终点的坐标模糊成合法的坐标、必须过河else if (play.getY() <= 341 && (me.getX() - play.getX()) >= 57 && (me.getX() - play.getX()) <= 112){play.setBounds(play.getX()+57,play.getY(),55,55);}//向左移动、得到终点的坐标模糊成合法的坐标、必须过河else if (play.getY() <= 341 && (play.getX() - me.getX()) >= 3 && (play.getX() - me.getX()) <=58){play.setBounds(play.getX()-57,play.getY(),55,55);}}}//卒移动结束/**卒吃棋规则*/public void armsRule(JLabel play1,JLabel play2){//向右走if ((play2.getX() - play1.getX()) <= 112 && (play2.getX() - play1.getX()) >= 57 && (play1.getY() - play2.getY()) < 22 && (play1.getY() - play2.getY()) > -22 && play2.isVisible() && play1.getName().charAt(1)!=play2.getName().charAt(1)){//黑棋要过河才能右吃棋if (play1.getName().charAt(1) == '1' && play1.getY() > 284 && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}//红棋要过河才左能吃棋else if (play1.getName().charAt(1) == '2' && play1.getY() < 341 && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}}//向左走else if ((play1.getX() - play2.getX()) <= 112 && (play1.getX() - play2.getX()) >= 57 && (play1.getY() - play2.getY()) < 22 && (play1.getY() - play2.getY()) > -22 && play2.isVisible() && play1.getName().charAt(1)!=play2.getName().charAt(1)){//黑棋要过河才能左吃棋if (play1.getName().charAt(1) == '1' && play1.getY() > 284 && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}//红棋要过河才能右吃棋else if (play1.getName().charAt(1) == '2' && play1.getY() < 341 && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}}//向上走else if (play1.getX() - play2.getX() >= -22 && play1.getX() - play2.getX() <= 22 && play1.getY() - play2.getY() >= -112 && play1.getY() - play2.getY() <= 112){//黑棋不能向上吃棋if (play1.getName().charAt(1) == '1' && play1.getY() < play2.getY() && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}//红棋不能向下吃棋else if (play1.getName().charAt(1) == '2' && play1.getY() > play2.getY() && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}}//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play1.isVisible()));Var.add(String.valueOf(play1.getX()));Var.add(String.valueOf(play1.getY()));Var.add(String.valueOf(Man));//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play2.isVisible()));Var.add(String.valueOf(play2.getX()));Var.add(String.valueOf(play2.getY()));Var.add(String.valueOf(i));}//卒吃结束/**炮、车移动规则*/public void cannonRule(JLabel play,JLabel playQ[],MouseEventme){//起点和终点之间是否有棋子int Count = 0;//上、下移动if (play.getX() - me.getX() <= 0 && play.getX() - me.getX() >= -55){//指定所有模糊Y坐标for (int i=56;i<=571;i+=57){//移动的Y坐标是否有指定坐标相近的if (i - me.getY() >= -27 && i - me.getY() <= 27){//所有的棋子for (int j=0;j<32;j++){//找出在同一条竖线的所有棋子、并不包括自己if (playQ[j].getX() - play.getX() >= -27 && playQ[j].getX() - play.getX() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){//从起点到终点(从左到右)for (int k=play.getY()+57;k<i;k+=57){//大于起点、小于终点的坐标就可以知道中间是否有棋子if (playQ[j].getY() < i && playQ[j].getY() > play.getY()){//中间有一个棋子就不可以从这条竖线过去Count++;break;}}//for//从起点到终点(从右到左)for (int k=i+57;k<play.getY();k+=57){//找起点和终点的棋子if (playQ[j].getY() < play.getY() && playQ[j].getY() > i){Count++;break;}}//for}//if}//for//起点和终点没有棋子就可以移动了if (Count == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(play.getX(),i,55,55);break;}}//if}//for}//if//左、右移动else if (play.getY() - me.getY() >=-27 && play.getY() - me.getY() <= 27){//指定所有模糊X坐标for (int i=24;i<=480;i+=57){//移动的X坐标是否有指定坐标相近的if (i - me.getX() >= -55 && i-me.getX() <= 0){//所有的棋子for (int j=0;j<32;j++){//找出在同一条横线的所有棋子、并不包括自己if (playQ[j].getY() - play.getY() >= -27 && playQ[j].getY() - play.getY() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){//从起点到终点(从上到下)for (int k=play.getX()+57;k<i;k+=57){//大于起点、小于终点的坐标就可以知道中间是否有棋子if (playQ[j].getX() < i && playQ[j].getX() > play.getX()){//中间有一个棋子就不可以从这条横线过去Count++;break;}}//for//从起点到终点(从下到上)for (int k=i+57;k<play.getX();k+=57){//找起点和终点的棋子if (playQ[j].getX() < play.getX() && playQ[j].getX() > i){Count++;break;}}//for}//if}//for//起点和终点没有棋子if (Count == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(i,play.getY(),55,55);break;}}//if}//for}//else}//炮、车移动方法结束/**炮、车吃棋规则*/public void cannonRule(int Chess,JLabel play,JLabel playTake,JLabel playQ[],MouseEvent me){//起点和终点之间是否有棋子int Count = 0;//所有的棋子for (int j=0;j<32;j++){//找出在同一条竖线的所有棋子、并不包括自己if (playQ[j].getX() - play.getX() >= -27 && playQ[j].getX() - play.getX() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){//自己是起点被吃的是终点(从上到下)for (int k=play.getY()+57;k<playTake.getY();k+=57){//大于起点、小于终点的坐标就可以知道中间是否有棋子if (playQ[j].getY() < playTake.getY() && playQ[j].getY() > play.getY()){//计算起点和终点的棋子个数Count++;break;}}//for//自己是起点被吃的是终点(从下到上)for (int k=playTake.getY();k<play.getY();k+=57){//找起点和终点的棋子if (playQ[j].getY() < play.getY() && playQ[j].getY() > playTake.getY()){Count++;break;}}//for}//if//找出在同一条竖线的所有棋子、并不包括自己else if (playQ[j].getY() - play.getY() >= -10 && playQ[j].getY() - play.getY() <= 10 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){//自己是起点被吃的是终点(从左到右)for (int k=play.getX()+50;k<playTake.getX();k+=57){//大于起点、小于终点的坐标就可以知道中间是否有棋子if (playQ[j].getX() < playTake.getX() && playQ[j].getX() > play.getX()){Count++;break;}}//for//自己是起点被吃的是终点(从右到左)for (int k=playTake.getX();k<play.getX();k+=57){//找起点和终点的棋子if (playQ[j].getX() < play.getX() && playQ[j].getX() > playTake.getX()){Count++;break;}}//for}//if}//for//起点和终点之间要一个棋子是炮的规则、并不能吃自己的棋子if (Count == 1 && Chess == 0 && playTake.getName().charAt(1) != play.getName().charAt(1)){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(playTake.isVisible()));Var.add(String.valueOf(playTake.getX()));Var.add(String.valueOf(playTake.getY()));Var.add(String.valueOf(i));playTake.setVisible(false);play.setBounds(playTake.getX(),playTake.getY(),55,55);}//起点和终点之间没有棋子是车的规则、并不能吃自己的棋子else if (Count ==0 && Chess == 1 && playTake.getName().charAt(1) != play.getName().charAt(1)){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(playTake.isVisible()));Var.add(String.valueOf(playTake.getX()));Var.add(String.valueOf(playTake.getY()));Var.add(String.valueOf(i));playTake.setVisible(false);play.setBounds(playTake.getX(),playTake.getY(),55,55);}}//炮、车吃棋方法结束/**马移动规则*/public void horseRule(JLabel play,JLabel playQ[],MouseEvent me){ //保存坐标和障碍int Ex=0,Ey=0,Move=0;//上移、左边if (play.getX() - me.getX() >= 2 && play.getX() - me.getX() <= 57 && play.getY() - me.getY() >= 87 && play.getY() - me.getY() <= 141){ //合法的Y坐标for (int i=56;i<=571;i+=57){//移动的Y坐标是否有指定坐标相近的if (i - me.getY() >= -27 && i - me.getY() <= 27){Ey = i;break;}}//合法的X坐标for (int i=24;i<=480;i+=57){//移动的X坐标是否有指定坐标相近的if (i - me.getX() >= -55 && i-me.getX() <= 0){Ex = i;break;}}//正前方是否有别的棋子for (int i=0;i<32;i++){if (playQ[i].isVisible() && play.getX() - playQ[i].getX() == 0 && play.getY() - playQ[i].getY() == 57 ){Move = 1;break;}}//可以移动该棋子if (Move == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(Ex,Ey,55,55);}}//if//左移、上边else if (play.getY() - me.getY() >= 27 && play.getY() - me.getY() <= 86 && play.getX() - me.getX() >= 70 && play.getX() - me.getX() <= 130){//Yfor (int i=56;i<=571;i+=57){if (i - me.getY() >= -27 && i - me.getY() <= 27){Ey = i;}}//Xfor (int i=24;i<=480;i+=57){if (i - me.getX() >= -55 && i-me.getX() <= 0){Ex = i;}}//正左方是否有别的棋子for (int i=0;i<32;i++){if (playQ[i].isVisible() && play.getY() - playQ[i].getY() == 0 && play.getX() - playQ[i].getX() == 57 ){Move = 1;break;}}if (Move == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(Ex,Ey,55,55);}}//else//下移、右边else if (me.getY() - play.getY() >= 87 && me.getY() - play.getY() <= 141 && me.getX() - play.getX() <= 87 && me.getX() - play.getX() >= 2 ){//Yfor (int i=56;i<=571;i+=57){if (i - me.getY() >= -27 && i - me.getY() <= 27){Ey = i;}}//Xfor (int i=24;i<=480;i+=57){if (i - me.getX() >= -55 && i-me.getX() <= 0){Ex = i;}}//正下方是否有别的棋子for (int i=0;i<32;i++){if (playQ[i].isVisible() && play.getX() - playQ[i].getX() == 0 && playQ[i].getY() - play.getY() == 57 ){Move = 1;break;}}if (Move == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(Ex,Ey,55,55);}}//else//上移、右边else if (play.getY() - me.getY() >= 87 && play.getY() - me.getY() <= 141 && me.getX() - play.getX() <= 87 && me.getX() - play.getX() >= 30 ){//合法的Y坐标for (int i=56;i<=571;i+=57){if (i - me.getY() >= -27 && i - me.getY() <= 27){。