分类: Java
2007-07-25 16:24:22
// Java core packages
import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
public class Chess extends JApplet implements ActionListener
{
//构成用户图形界面
JLabel Gamename,Hint,askYou;
JTextField inputField;
JTextArea outputArea;
JLabel timer;
//创建一个字符数组来描绘出棋盘
String board[][]=new String[11][10];
//创建棋盘装饰性的常量
String titleup="黑方";
String titledown="红方";
String han="汉界";
String chu="楚河";
//创建字符数组存储棋子
String chess[][]=new String[2][7];
//初始化状态栏中的注释信息
String comment="";
//建立静态变量,一次性画出棋盘
String outputB=outputBoard();
//开始构建GUI成分
public void init()
{
//构建container容器并更改其显示样式为FlowLayout
Container container=getContentPane();
container.setLayout(new FlowLayout());
//创建操作提示及用户操作框
Hint=new JLabel("请输入棋子操作:x,y-x,y");
container.add(Hint);
askYou=new JLabel("轮到红色");
container.add(askYou);
inputField=new JTextField(10);
inputField.addActionListener(this);
container.add(inputField);
//创建一个用来显示和储存回合数的Label
timer=new JLabel("1");
container.add(timer);
//绘制输出区域来响应每一步动作
outputArea=new JTextArea();
outputArea.setText(outputB);//显示初始化棋盘
outputArea.setEditable(false);//输出的内容不能被改写,很重要
outputArea.setFont(new java.awt.Font("Dialog", 0, 22));
container.add(outputArea);
Gamename=new JLabel("中国象棋文字简易版 Ver0.3 Powered By 05141 曹硕");
container.add(Gamename);
}
//设置操作事件
public void actionPerformed(ActionEvent actionEvent)
{
//获得输入的指令
String input=inputField.getText();
//初始化棋盘变量
String output="";
//获得当前的棋盘
String screen=outputArea.getText();
char a[]=input.toCharArray();//将字符串拆分为字符
int range=outofRange(a);//解析指令是否有效,并符合游戏规则
int time=Integer.parseInt(timer.getText());//获得现在的回合数
if (range==0)//out of range
{
comment="您的操作不符合规则,请重新操作.";
showStatus(comment);
}
else//in the board
{
int s1=10-(a[2]-48);
int s2=10-(a[0]-48);
int s3=10-(a[6]-48);
int s4=10-(a[4]-48);
int t=0;
int judge;
if (time%2!=0)//轮到红色玩家
{
//检测是否吃掉自己
judge=cannotEat(s3,s4,time);
if (judge==1)
{
for (t=0;t<=6;t++)
{
//扫描用户选择的棋子
if (board[s1][s2]==chess[0][t])
{
break;
}
}
switch (t)//决定如何移动当前的棋子
{
case 0:
{
output=chariot(s1,s2,s3,s4);
break;
}
case 1:
{
output=knight(s1,s2,s3,s4);
break;
}
case 2:
{
output=minister(s1,s2,s3,s4);
break;
}
case 3:
{
output=server(s1,s2,s3,s4);
break;
}
case 4:
{
output=king(s1,s2,s3,s4);
break;
}
case 5:
{
output=canon(s1,s2,s3,s4);
break;
}
case 6:
{
output=soldier(s1,s2,s3,s4,time);
break;
}
}
}
}
if (time%2==0)//轮到黑色玩家
{
judge=cannotEat(s3,s4,time);
if (judge==1)
{
for (t=0;t<=6;t++)
{
if (board[s1][s2]==chess[1][t])
{
break;
}
}
switch (t)
{
case 0:
{
output=chariot(s1,s2,s3,s4);
break;
}
case 1:
{
output=knight(s1,s2,s3,s4);
break;
}
case 2:
{
output=minister(s1,s2,s3,s4);
break;
}
case 3:
{
output=server(s1,s2,s3,s4);
break;
}
case 4:
{
output=king(s1,s2,s3,s4);
break;
}
case 5:
{
output=canon(s1,s2,s3,s4);
break;
}
case 6:
{
output=soldier(s1,s2,s3,s4,time);
break;
}
}
}
}
if (output=="")//输入错误
{
comment="您的操作不符合规则,请重新操作.";
showStatus(comment);
outputArea.setText(screen);
}
if (output!="")
{
int life=win();//检测游戏是否已经结束
if (life==2)//继续游戏
{
time++;
timer.setText(Integer.toString(time));
outputArea.setText(output);
inputField.setText("");
if (time%2==0)
askYou.setText("轮到黑色");
if (time%2!=0)
askYou.setText("轮到红色");
}
if (life==1)//其中一方胜利
{
outputArea.setText(output);
inputField.setText("");
inputField.setEditable(false);
if (time%2==1)
askYou.setText("红色玩家胜利!");
else
askYou.setText("黑色玩家胜利!");
}
}
}
}
//创建棋盘棋子
String outputBoard()
{
//在数组[0][0]-[1][6]放置双方棋子
chess[0][0]="?";
chess[0][1]="?";
chess[0][2]="?";
chess[0][3]="?";//红方“士”
chess[0][4]="?";//红方“帅”
chess[0][5]="?";//红方“炮”
chess[0][6]="?";//红方“兵”
chess[1][0]="車";
chess[1][1]="馬";
chess[1][2]="象";
chess[1][3]="仕";
chess[1][4]="将";
chess[1][5]="砲";
chess[1][6]="卒";
//将初始化棋子放入11*10数组中
board[0][0]=" ";
board[0][1]="9";
board[0][2]="8";
board[0][3]="7";
board[0][4]="6";
board[0][5]="5";
board[0][6]="4";
board[0][7]="3";
board[0][8]="2";
board[0][9]="1";
board[1][0]=" 9 ";
board[2][0]=" 8 ";
board[3][0]=" 7 ";
board[4][0]=" 6 ";
board[5][0]=" 5 ";
board[6][0]=" 4 ";
board[7][0]=" 3 ";
board[8][0]=" 2 ";
board[9][0]=" 1 ";
board[10][0]=" 0 ";
board[1][1]="車";
board[1][2]="馬";
board[1][3]="象";
board[1][4]="仕";
board[1][5]="将";
board[1][6]="仕";
board[1][7]="象";
board[1][8]="馬";
board[1][9]="車";
board[10][1]="?";//红方“车”
board[10][2]="?";//红方“马”
board[10][3]="?";//红方“相”
board[10][4]="?";//红方“士”
board[10][5]="?";//红方“帅”
board[10][6]="?";//红方“士”
board[10][7]="?";//红方“相”
board[10][8]="?";//红方“马”
board[10][9]="?";//红方“车”
board[2][1]="┣";
board[2][2]="╋";
board[2][3]="╋";
board[2][4]="╋";
board[2][5]="╋";
board[2][6]="╋";
board[2][7]="╋";
board[2][8]="╋";
board[2][9]="┫";
board[3][1]="┣";
board[3][2]="砲";
board[3][3]="╋";
board[3][4]="╋";
board[3][5]="╋";
board[3][6]="╋";
board[3][7]="╋";
board[3][8]="砲";
board[3][9]="┫";
board[4][1]="卒";
board[4][2]="╋";
board[4][3]="卒";
board[4][4]="╋";
board[4][5]="卒";
board[4][6]="╋";
board[4][7]="卒";
board[4][8]="╋";
board[4][9]="卒";
board[5][1]="┗";
board[5][2]="┻";
board[5][3]="┻";
board[5][4]="┻";
board[5][5]="┻";
board[5][6]="┻";
board[5][7]="┻";
board[5][8]="┻";
board[5][9]="┛";
board[6][1]="┏";
board[6][2]="┳";
board[6][3]="┳";
board[6][4]="┳";
board[6][5]="┳";
board[6][6]="┳";
board[6][7]="┳";
board[6][8]="┳";
board[6][9]="┓";
board[7][1]="?";
board[7][2]="╋";
board[7][3]="?";
board[7][4]="╋";
board[7][5]="?";
board[7][6]="╋";
board[7][7]="?";
board[7][8]="╋";
board[7][9]="?";
board[8][1]="┣";
board[8][2]="?";//红方“炮”
board[8][3]="╋";
board[8][4]="╋";
board[8][5]="╋";
board[8][6]="╋";
board[8][7]="╋";
board[8][8]="?";//红方“炮”
board[8][9]="┫";
board[9][1]="┣";
board[9][2]="╋";
board[9][3]="╋";
board[9][4]="╋";
board[9][5]="╋";
board[9][6]="╋";
board[9][7]="╋";
board[9][8]="╋";
board[9][9]="┫";
//开始绘制棋盘及棋子
String output="";
output+=" ";
for (int i=0;i<=5;i++)
{
for (int j=0;j<10;j++)
{
output+=board[i][j];
}
if (i==0)
output+=" ";
if (i==1)
output+=" "+titleup;
output+=" ";
}
output+=" ┃ "+han+" "+chu+" ┃ ";
for (int i=6;i<=10;i++)
{
for (int j=0;j<10;j++)
{
output+=board[i][j];
}
if (i==10)
output+=" "+titledown;
output+=" ";
}
return output;
}
//移动棋子
String move(int s1, int s2, int s3, int s4)
{
//将棋子移动到目标数组位置进行缓存
board[s3][s4]=board[s1][s2];
//辨别不同的起始位置并重绘棋盘
if (s1==10|s1==5)
{
if (s2==9)
board[s1][9]="┛";
if (s2==1)
board[s1][1]="┗";
if (s2>=2&s2<=8)
board[s1][s2]="┻";
}
if (s1==1|s1==6)
{
if (s2==9)
board[s1][9]="┓";
if (s2==1)
board[s1][1]="┏";
if (s2>=2&s2<=8)
board[s1][s2]="┳";
}
if (s2==1)
{
if (s1!=10&s1!=6&s1!=5&s1!=1)
board[s1][1]="┣";
}
if (s2==9)
{
if (s1!=10&s1!=6&s1!=5&s1!=1)
board[s1][9]="┫";
}
if (s2>=2&&s2<=8)
{
if (s1!=10&s1!=6&s1!=5&s1!=1)
board[s1][s2]="╋";
}
//输出被移动后的棋盘
String output="";
for (int i=0;i<=5;i++)
{
for (int j=0;j<10;j++)
{
output+=board[i][j];
}
if (i==0)
output+=" ";
if (i==1)
output+=" "+titleup;
output+=" ";
}
output+=" ┃ "+han+" "+chu+" ┃ ";
for (int i=6;i<=10;i++)
{
for (int j=0;j<10;j++)
{
output+=board[i][j];
}
if (i==10)
output+=" "+titledown;
output+=" ";
}
return output;
}
//车
String chariot(int s1, int s2, int s3, int s4)
{
String outputChariot="";
int scanner=scan(s1,s2,s3,s4);
if (scanner==0)
outputChariot=move(s1,s2,s3,s4);
return outputChariot;
}
//马
String knight(int s1, int s2, int s3, int s4)
{
String outputKnight="";
if (Math.abs(s1-s3)+Math.abs(s2-s4)==3)
{
if (s1!=s3&s2!=s4)
{
if (s3-s1==2)
{
boolean south=dot(s1+1,s2);
if (south==false)
outputKnight=move(s1,s2,s3,s4);
}
if (s1-s3==2)
{
boolean north=dot(s1-1,s2);
if (north==false)
outputKnight=move(s1,s2,s3,s4);
}
if (s2-s4==2)
{
boolean west=dot(s1,s2-1);
if (west==false)
outputKnight=move(s1,s2,s3,s4);
}
if (s4-s2==2)
{
boolean east=dot(s1,s2+1);
if (east==false)
outputKnight=move(s1,s2,s3,s4);
}
}
}
return outputKnight;
}
//相
String minister(int s1, int s2, int s3, int s4)
{
String outputMinister="";
if (s1>=1&&s1<=5)
{
if (s3>=1&&s3<=5)
{
if (s3-s1==2&&s4-s2==2)
{
boolean southeast=dot(s1+1,s2+1);
if (southeast==false)
outputMinister=move(s1,s2,s3,s4);
}
if (s1-s3==2&&s4-s2==2)
{
boolean northeast=dot(s1-1,s2+1);
if (northeast==false)
outputMinister=move(s1,s2,s3,s4);
}
if (s3-s1==2&&s2-s4==2)
{
boolean southwest=dot(s1+1,s2-1);
if (southwest==false)
outputMinister=move(s1,s2,s3,s4);
}
if (s1-s3==2&&s2-s4==2)
{
boolean nouthwest=dot(s1-1,s2-1);
if (nouthwest==false)
outputMinister=move(s1,s2,s3,s4);
}
}
}
if (s1>=6&&s1<=10)
{
if (s3>=6&&s3<=10)
{
if (s3-s1==2&&s4-s2==2)
{
boolean southeast=dot(s1+1,s2+1);
if (southeast==false)
outputMinister=move(s1,s2,s3,s4);
}
if (s1-s3==2&&s4-s2==2)
{
boolean northeast=dot(s1-1,s2+1);
if (northeast==false)
outputMinister=move(s1,s2,s3,s4);
}
if (s3-s1==2&&s2-s4==2)
{
boolean southwest=dot(s1+1,s2-1);
if (southwest==false)
outputMinister=move(s1,s2,s3,s4);
}
if (s1-s3==2&&s2-s4==2)
{
boolean nouthwest=dot(s1-1,s2-1);
if (nouthwest==false)
outputMinister=move(s1,s2,s3,s4);
}
}
}
return outputMinister;
}
//士
String server(int s1, int s2, int s3, int s4)
{
String outputServer="";
if (s4>=4&&s4<=6)
{
if (Math.abs(s1-s3)==1&&Math.abs(s2-s4)==1)
{
if (s1>=1&&s1<=3)
{
if (s3>=1&&s3<=3)
{
outputServer=move(s1,s2,s3,s4);
}
}
if (s1>=8&&s1<=10)
{
if (s3>=8&&s3<=10)
{
outputServer=move(s1,s2,s3,s4);
}
}
}
}
return outputServer;
}
//将
String king(int s1, int s2, int s3, int s4)
{
String outputKing="";
int scanner=scan(s1,s2,s3,s4);
if (s4>=4&&s4<=6)
{
if (Math.abs(s1-s3)+Math.abs(s2-s4)==1)
{
if (s1>=1&&s1<=3)
{
if (s3>=1&&s3<=3)
{
outputKing=move(s1,s2,s3,s4);
}
}
if (s1>=8&&s1<=10)
{
if (s3>=8&&s3<=10)
{
outputKing=move(s1,s2,s3,s4);
}
}
}
}
return outputKing;
}
//兵
String soldier(int s1, int s2, int s3, int s4, int time)
{
String outputSoldier="";
if (time%2!=0)//红方兵
{
if (s1>=6)
{
if (s2==s4&&s3-s1==-1)
{
outputSoldier=move(s1,s2,s3,s4);
}
}
if (s1<=5)
{
if (Math.abs(s2-s4)+s1-s3==1)
{
outputSoldier=move(s1,s2,s3,s4);
}
}
}
if (time%2==0)//黑方兵
{
if (s1<=5)
{
if (s2==s4&&s3-s1==1)
{
outputSoldier=move(s1,s2,s3,s4);
}
}
if (s1>=6)
{
if (Math.abs(s2-s4)+s3-s1==1)
{
outputSoldier=move(s1,s2,s3,s4);
}
}
}
return outputSoldier;
}
//炮
String canon(int s1, int s2, int s3, int s4)
{
String outputCanon="";
int scanner=scan(s1,s2,s3,s4);
boolean destiny=dot(s3,s4);
//炮可以隔子吃子
if (destiny==true&scanner==1)
outputCanon=move(s1,s2,s3,s4);
//炮可以大范围直线移动
if (destiny==false&scanner==0)
outputCanon=move(s1,s2,s3,s4);
return outputCanon;
}
//判断是否同阵营吃子
int cannotEat(int s3, int s4, int time)
{
int judge=1;//1代表没有吃自己的子
for (int i=0;i<=6;i++)
{
if (time%2!=0)
{
if (board[s3][s4]==chess[0][i])
judge=0;//红吃红子
}
else
{
if (board[s3][s4]==chess[1][i])
judge=0;//黑吃黑子
}
}
return judge;
}
// 判断是否将棋子走出了棋盘
int outofRange(char a[])
{
int range=1;
if (a.length!=7)
range=0;
return range;
}
int scan(int s1, int s2, int s3, int s4)
{
int scan=0;
if (Math.abs(s2-s4)>1&s1==s3)
{
if (s2>s4)
{
for (int k=0;k<=1;k++)
{
for (int l=0;l<=6;l++)
{
for (int j=s4+1;j<=s2-1;j++)
{
if (board[s1][j]==chess[k][l])
scan++;
}
}
}
}
if (s2
for (int k=0;k<=1;k++)
{
for (int l=0;l<=6;l++)
{
for (int j=s2+1;j<=s4-1;j++)
{
if (board[s1][j]==chess[k][l])
scan++;
}
}
}
}
}
if (Math.abs(s1-s3)>1&s2==s4)
{
if (s1>s3)
{
for (int k=0;k<=1;k++)
{
for (int l=0;l<=6;l++)
{
for (int j=s3+1;j<=s1-1;j++)
{
if (board[j][s2]==chess[k][l])
scan++;
}
}
}
}
if (s1
for (int k=0;k<=1;k++)
{
for (int l=0;l<=6;l++)
{
for (int j=s1+1;j<=s3-1;j++)
{
if (board[j][s2]==chess[k][l])
scan++;
}
}
}
}
}
if (Math.abs(s1-s3)==1|Math.abs(s2-s4)==1)
scan=0;
return scan;
}
int win()
{
int life=0;
for (int i=1;i<=3;i++)
{
for (int j=4;j<=6;j++)
{
if (board[i][j]==chess[1][4])
life++;
}
}
for (int i=8;i<=10;i++)
{
for (int j=4;j<=6;j++)
{
if (board[i][j]==chess[0][4])
life++;
}
}
return life;
}
boolean dot(int d1,int d2)
{
boolean dot=false;
for (int k=0;k<=1;k++)
{
for (int l=0;l<=6;l++)
{
if (board[d1][d2]==chess[k][l])
dot=true;
}
}
return dot;
}
}