利用Java知识制作象棋小游戏
package com.chess;
public class Main {
public static void main(String[] args) {
// write your code here
new ChessMainframe("象棋对弈系统");
}
}
package com.chess;
import java.awt.event.MouseEvent;
import java.util.List;
public class ChessRule {
public void cannonRule(ChessManBehavior play, ChessManBehavior playQ[], MouseEvent me, List<ChessRegret> chessRegretList, int man) {
int count = 0;
int cx = me.getX();
int cy = me.getY() + 30;
//上下移动 保证在炮这个棋子横坐标+宽度的57距离内
if (cx - play.getX() >= 0 && cx - play.getX() < 57) {
for (int i = 56; i <= 569; i += 57) {
if (cy - i >= 0 && cy - i < 57) {
for (int j = 0; j < 32; j++) {
if (playQ[j].getX() == play.getX() && play.getName() != playQ[j].getName() && playQ[j].died==false) {
for (int k = i; k < play.getY(); k += 57) {
if (playQ[j].getY() == k) {
count++;
}
}
for (int k = play.getY(); k < i; k += 57) {
if (playQ[j].getY() == k) {
count++;
}
}
}
}
if (count == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX(), i, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
}
}
//左右运动 保证在炮这个棋子的纵坐标+高度57的范围内
if (me.getY() - play.getY()+30 >= 0 && me.getY() - play.getY()+30 < 57) {
for (int i = 24; i <= 480; i += 57) {
if (me.getX() - i >= 0 && me.getX() - i < 57) {
for (int j = 0; j < 32; j++) {
if (playQ[j].getY() == play.getY() && play.getName() != playQ[j].getName() && playQ[j].died==false) {
for (int k = i; k < play.getX(); k += 57) {
if (playQ[j].getX() == k) {
count++;
}
}
for (int k = play.getX(); k < i; k += 57) {
if (playQ[j].getX() == k) {
count++;
}
}
}
}
if (count == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(i, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
}
}
}
public void carRule(ChessManBehavior play, ChessManBehavior playQ[], MouseEvent me, List<ChessRegret> chessRegretList, int man) {
cannonRule(play, playQ, me, chessRegretList, man);
}
public void horseRule(ChessManBehavior play, ChessManBehavior playQ[], MouseEvent me, List<ChessRegret> chessRegretList, int man) {
int ex = 0, ey = 0, move = 0;
//棋子的坐标从工具栏下开始算,鼠标点击的坐标从工具栏上开始算,所以需要将坐标点+30
ex = me.getX(); ey = me.getY() + 30;
//左上坐标点
if (play.getX() - ex > 0 && play.getX() - ex <= 57 && play.getY() - ey >= 59 && play.getY() - ey <= 114) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getX() == play.getX() && playQ[i].getY() - play.getY() == -57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY() - 114, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//右上坐标点
if ( ex-play.getX() > 0 && play.getX() - ex <= 57 && play.getY() - ey >= 59 && play.getY() - ey <= 114) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getX() == play.getX() && playQ[i].getY() - play.getY() == -57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY() - 114, 55, 55);
//播放声音
play.playSounds(man);
}
return;
}
//左下坐标点
if (play.getX() - ex > 0 && play.getX() - ex <= 57 && ey-play.getY() >= 114 && ey-play.getY() <= 170) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getX() == play.getX() && playQ[i].getY() - play.getY() == 57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY() + 114, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//右下坐标点
if ( ex-play.getX() > 0 && play.getX() - ex <= 57 && ey-play.getY() >= 114 && ey-play.getY() <= 170) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getX() == play.getX() && playQ[i].getY() - play.getY() == 57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY() + 114, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//左左上坐标点
if (play.getX() - ex > 0 && play.getX() - ex <= 114 && play.getY() - ey >= 0 && play.getY() - ey <= 57) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getY() == play.getY() && playQ[i].getX() - play.getX() == -57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 114, play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//左左下坐标点
if (play.getX() - ex > 0 && play.getX() - ex <= 114 && ey-play.getY() >= 57 && ey-play.getY() <= 114) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getY() == play.getY() && playQ[i].getX() - play.getX() == -57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 114, play.getY() + 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//右右上
if ( ex-play.getX() >= 114 && ex-play.getX() <= 170 && play.getY() - ey > 0 && play.getY() - ey <= 57) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getY() == play.getY() && playQ[i].getX() - play.getX() == 57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 114, play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//右右下
if ( ex-play.getX() >= 114 && ex-play.getX() <= 170 && ey-play.getY() >= 57 && ey-play.getY() <= 114) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getY() == play.getY() && playQ[i].getX() - play.getX() == 57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 114, play.getY() + 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
}
public void elephantRule(ChessManBehavior play, ChessManBehavior playQ[], MouseEvent me, List<ChessRegret> chessRegretList, int man) {
int ex = me.getX();
int ey = me.getY()+30;
int move = 0;
//大象不能过界
if (play.getY() <= 284) {
if (ey > 341)//点击的点过了河,象不能移动
return;
}
if (play.getY() >= 341) {
if (ey < 341)
return;
}
//左上移动
if (play.getX() - ex <= 114 && play.getX() - ex >= 58 && play.getY() - ey <= 114 && play.getY() - ey >= 58) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getX() == play.getX() - 57 && playQ[i].getY() - play.getY() == -57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 114, play.getY() - 114, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//左下移动
if (play.getX() - ex <= 114 && play.getX() - ex >= 58 && ey-play.getY() >= 114 && ey-play.getY() <= 170) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getX() == play.getX() - 57 && playQ[i].getY() - play.getY() == 57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 114, play.getY() + 114, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//右上移动
if ( ex-play.getX() >= 114 && ex-play.getX() <= 170 && play.getY() - ey <= 114 && play.getY() - ey >= 58) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getX() == play.getX() + 57 && playQ[i].getY() - play.getY() == -57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 114, play.getY() - 114, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//右下移动
if ( ex-play.getX() >= 114 && ex-play.getX() <= 170 && ey-play.getY() >= 114 && ey-play.getY() <= 170) {
for (int i = 0; i < 32; i++) {
if (playQ[i].died == false && playQ[i].getX() == play.getX() + 57 && playQ[i].getY() - play.getY() == 57) {
move = 1;
break;
}
}
if (move == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 114, play.getY() + 114, 55, 55);
//播放声音
play.playSounds(man);
}
}
}
public void chapRule(ChessManBehavior play, ChessManBehavior playQ[], MouseEvent me, List<ChessRegret> chessRegretList, int man) {
int judge = play.getName().charAt(1) - '0';
int ex = me.getX(); int ey = me.getY() + 30;
//右上
if (ex - play.getX() >= 57 && ex - play.getX() <= 113 && play.getY() - ey > 0 && play.getY() - ey <= 57) {
if (judge == 1 && play.getX() + 57 >= 252 && play.getX() + 57 <= 309) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
}
if (judge == 2 && play.getY() - 57 <= 512 && play.getY() - 57 >= 455 && play.getX() + 57 >= 252 && play.getX() + 57 <= 309) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
}
}
//右下
if (ex - play.getX() >= 57 && ex - play.getX() <= 113 && ey-play.getY() > 0 && ey-play.getY() <= 113) {
if (judge == 1 && play.getX() + 57 <= 309 && play.getX() + 57 >= 252) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY() + 57, 55, 55);
//播放声音
play.playSounds(man);
}
if (judge == 2 && play.getY() + 57 <= 569 && play.getY() + 57 >= 512 && play.getX() + 57 >= 252 && play.getX() + 57 <= 309) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY() + 57, 55, 55);
//播放声音
play.playSounds(man);
}
}
//左上
if ( play.getX()-ex >= 0 && play.getX()-ex <= 57 && play.getY() - ey > 0 && play.getY() - ey <= 57) {
if (judge == 1 && play.getX() - 57 >= 195 && play.getX() - 57 <= 252) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
}
if (judge == 2 && play.getY() - 57 <= 512 && play.getY() - 57 >= 455) {
play.setBounds(play.getX() - 57, play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
}
}
//左下
if ( play.getX()-ex >= 0 && play.getX()-ex <= 57 && ey-play.getY() > 0 && ey-play.getY() <= 113) {
if (judge == 1 && play.getX() - 57 >= 195 && play.getX() - 57 <= 252) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY() + 57, 55, 55);
//播放声音
play.playSounds(man);
}
if (judge == 2 && play.getY() + 57 <= 569 && play.getY() + 57 >= 512 && (play.getX() + 57 >= 252 && play.getX() == 309)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY() + 57, 55, 55);//播放声音
play.playSounds(man);
}
}
}
public void masterRule(ChessManBehavior play, ChessManBehavior playQ[], MouseEvent me, List<ChessRegret> chessRegretList, int man) {
int ex = me.getX();
int ey = me.getY()+30;
int judge = play.getName().charAt(1) - '0';
//向上移动
if (me.getX() - play.getX() >= 0 && me.getX() - play.getX() < 57 && play.getY() - ey > 0 && play.getY() - ey <= 57) {
if (judge == 1 && (play.getX() == 195 || play.getX() == 309 || play.getX() == 252) && (play.getY() == 113 || play.getY() == 170)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX(), play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
if (judge == 2 && (play.getX() == 195 || play.getX() == 309 || play.getX() == 252) && (play.getY() == 512 || play.getY() == 569)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX(), play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//向下移动
if (me.getX() - play.getX() >= 0 && me.getX() - play.getX() < 57 && ey - play.getY() >= 57 && ey - play.getY() <= 113) {
if (judge == 1 && (play.getX() == 195 || play.getX() == 309 || play.getX() == 252) && (play.getY() == 56 || play.getY() == 113)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX(), play.getY() + 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
if (judge == 2 && (play.getX() == 195 || play.getX() == 309 || play.getX() == 252) && (play.getY() == 512 || play.getY() == 455)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX(), play.getY() + 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//向左移动
if (ey - play.getY() >= 0 && ey - play.getY() <= 56 && play.getX() - ex > 0 && play.getX() - ex <= 57) {
if (judge == 1 && (play.getX() == 252 || play.getX() == 309) && (play.getY() == 56 && play.getY() == 113 || play.getY() == 170)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
return;
}
if (judge == 2 && (play.getX() == 252 || play.getX() == 309) && (play.getY() == 569 && play.getY() == 512 || play.getY() == 455)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//向右移动
if (ey - play.getY() >= 0 && ey - play.getY() <= 56 && ex-play.getX() > 57 && ex-play.getX() <= 113) {
if (judge == 1 && (play.getX() == 252 || play.getX() == 195) && (play.getY() == 56 && play.getY() == 113 || play.getY() == 170)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
return;
}
if (judge == 2 && (play.getX() == 252 || play.getX() == 195) && (play.getX() == 569 && play.getY() == 512 || play.getY() == 455)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
}
}
}
public void soliderRule(ChessManBehavior play, ChessManBehavior playQ[], MouseEvent me, List<ChessRegret> chessRegretList, int man) {
int judge = play.getName().charAt(1) - '0';
int ex = me.getX();
int ey = me.getY()+30;
//黑色棋子
if (judge == 1) {
//向下运动
if (ex - play.getX() >= 0 && ex - play.getX() < 57 && ey - play.getY() >= 57 && ey - play.getY() <= 113) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX(), play.getY() + 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
//向右运动 通过棋子的Y坐标判断是否已经过河
if (play.getY() >= 341 && ey - play.getY() >= 0 && ey - play.getY() <= 56 && ex - play.getX() >= 57 && ex - play.getX() <= 113) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
return;
}
//向左运动 通过棋子的Y坐标判断是否已经过河
if (play.getY() >= 341 && ey - play.getY() >= 0 && ey - play.getY() <= 56 && play.getX() - ex > 0 && play.getX() - ex <= 57) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
//红色棋子
if (judge == 2) {
//向上运动
if (me.getX() - play.getX() >= 0 && me.getX() - play.getX() < 57 && play.getY() - ey > 0 && play.getY() - ey <= 57) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX(), play.getY() - 57, 55, 55);
//播放声音
play.playSounds(man);
return;
}
//向右运动
if (play.getY() <= 284 && ey - play.getY() >= 0 && ey - play.getY() <= 56 && ex - play.getX() >= 57 && ex - play.getX() <= 113) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() + 57, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
return;
}
//向左运动
if (play.getY() <= 284 && ey - play.getY() >= 0 && ey - play.getY() <= 56 && play.getX() - ex > 0 && play.getX() - ex <= 57) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.man = man;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegretList.add(chessRegret);
play.setBounds(play.getX() - 57, play.getY(), 55, 55);
//播放声音
play.playSounds(man);
return;
}
}
}
public void carEat(ChessManBehavior play, ChessManBehavior playQ[], ChessManBehavior enemy, List<ChessRegret> chessRegretList, int man, int beEat) {
int count = 0;
if (play.getX() == enemy.getX()) {
for (int i = 0; i < 32; i++) {
//往下吃
if (play.getY() < enemy.getY()) {
for (int j = play.getY() + 57; j < enemy.getY(); j = j + 57) {
if (playQ[i].getX() == play.getX() && playQ[i].getY() == j && playQ[i].died == false) {
count++;
}
}
} else {//往上吃
for (int j = enemy.getY() + 57; j < play.getY(); j = j + 57) {
if (playQ[i].getX() == play.getX() && playQ[i].getY() == j && playQ[i].died == false) {
count++;
}
}
}
}
if (count == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
//左右吃
if (play.getY() == enemy.getY()) {
for (int i = 0; i < 32; i++) {
//往左吃
if (play.getX() > enemy.getX()) {
for (int j = enemy.getX() + 57; j < play.getX() - 57; j = j + 57) {
if (playQ[i].getY() == play.getY() && playQ[i].getX() == j && playQ[i].died == false)
count++;
}
}else{//往右吃
for (int j=play.getX()+57;j<enemy.getX();j+=57){
if (playQ[i].getY()==play.getY() && playQ[i].getX()==j && playQ[i].died==false)
count++;
}
}
}
if (count == 0) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
public void cannonEat(ChessManBehavior play, ChessManBehavior playQ[], ChessManBehavior enemy, List<ChessRegret> chessRegretList, int man, int beEat) {
int count = 0;
//上下吃
if (play.getX() == enemy.getX()) {
for (int i = 0; i < 32; i++) {
if (play.getY() < enemy.getY()) {
//往下吃
for (int j = play.getY() + 57; j < enemy.getY(); j = j + 57) {
if (playQ[i].getX() == play.getX() && playQ[i].getY() == j && playQ[i].died == false) {
count++;
}
}
} else {//往上吃
for (int j = enemy.getY() + 57; j < play.getY(); j = j + 57) {
if (playQ[i].getX() == play.getX() && playQ[i].getY() == j && playQ[i].died == false) {
count++;
}
}
}
}
if (count == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
//左右吃
if (play.getY() == enemy.getY()) {
for (int i = 0; i < 32; i++) {
//往左吃
if (play.getX() > enemy.getX()) {
for (int j = enemy.getX() + 57; j < play.getX(); j = j + 57) {
if (playQ[i].getY() == play.getY() && playQ[i].getX() == j && playQ[i].died == false)
count++;
}
}else{//往右吃
for(int j=play.getX()+57;j<enemy.getX();j+=57){
if (playQ[i].getY()==play.getY() && playQ[i].getX()==j && playQ[i].died==false)
count++;
}
}
}
if (count == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
public void horseEat(ChessManBehavior play, ChessManBehavior playQ[], ChessManBehavior enemy, List<ChessRegret> chessRegretList, int man, int beEat) {
int canGo = 1;
//左上和右上坐标
if ((play.getX() - 57 == enemy.getX() || play.getX() + 57 == enemy.getX()) && (play.getY() - 114 == enemy.getY())) {
//判断马眼是否有棋子
for (int i = 0; i < 32; i++) {
if (playQ[i].getX() == play.getX() && playQ[i].getY() == play.getY() - 57 && playQ[i].died == false) {
canGo = 0;
}
}
if (canGo == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
//左下和右下
if ((play.getX() - 57 == enemy.getX() || play.getX() + 57 == enemy.getX()) && (play.getY() + 114 == enemy.getY())) {
//判断马眼是否有棋子
for (int i = 0; i < 32; i++) {
if (playQ[i].getX() == play.getX() && playQ[i].getY() == play.getY() + 57 && playQ[i].died == false) {
canGo = 0;
}
}
if (canGo == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
//左左上和左左下
if (play.getX() - 114 == enemy.getX() && (play.getX() - 57 == enemy.getX() || play.getY() + 57 == enemy.getY())) {
for (int i = 0; i < 32; i++) {
if (playQ[i].getX() - 57 == play.getX() && playQ[i].getY() == play.getY() && playQ[i].died == false) {
canGo = 0;
}
}
if (canGo == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
//右右上和右右下
if (play.getX() + 114 == enemy.getX() && (play.getX() - 57 == enemy.getX() || play.getY() +57 == enemy.getY())) {
for (int i = 0; i < 32; i++) {
if (playQ[i].getX() + 57 == play.getX() && playQ[i].getY() == play.getY() && playQ[i].died == false) {
canGo = 0;
}
}
if (canGo == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
public void elephantEat(ChessManBehavior play, ChessManBehavior playQ[], ChessManBehavior enemy, List<ChessRegret> chessRegretList, int man, int beEat) {
int canGo = 1;
int judge = play.getName().charAt('1') - '0';
int enemyYpos = enemy.getY();
//左上
if (play.getX() - 114 == enemy.getX() && play.getY() - 114 == enemy.getY()) {
if ((judge == 1 && enemy.getY() <= 284) || (judge == 2 && enemy.getY() >= 341)) {//判断棋子颜色,控制器移动范围
for (int i = 0; i < 32; i++) {
if (playQ[i].getX() == play.getX() - 57 && playQ[i].getY() == play.getY() - 57 && playQ[i].died == false)
canGo = 0;
}
if (canGo == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//右上
if (play.getX() + 114 == enemy.getX() && play.getY() - 114 == enemy.getY()) {
if ((judge == 1 && enemy.getY() <= 284) || (judge == 2 && enemy.getY() >= 341)) {//判断棋子颜色,控制器移动范围
for (int i = 0; i < 32; i++) {
if (playQ[i].getX() == play.getX() + 57 && playQ[i].getY() == play.getY() - 57 && playQ[i].died == false)
canGo = 0;
}
if (canGo == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//左下
if (play.getX() - 114 == enemy.getX() && play.getY() + 114 == enemy.getY()) {
if ((judge == 1 && enemy.getY() <= 284) || (judge == 2 && enemy.getY() >= 341)) {//判断棋子颜色,控制器移动范围
for (int i = 0; i < 32; i++) {
if (playQ[i].getX() == play.getX() - 57 && playQ[i].getY() == play.getY() + 57 && playQ[i].died == false)
canGo = 0;
}
if (canGo == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//右下
if (play.getX() + 114 == enemy.getX() && play.getY() + 114 == enemy.getY()) {
if ((judge == 1 && enemy.getY() <= 284) || (judge == 2 && enemy.getY() >= 341)) {//判断棋子颜色,控制器移动范围
for (int i = 0; i < 32; i++) {
if (playQ[i].getX() == play.getX() + 57 && playQ[i].getY() == play.getY() + 57 && playQ[i].died == false)
canGo = 0;
}
if (canGo == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
}
public void chapEat(ChessManBehavior play, ChessManBehavior playQ[], ChessManBehavior enemy, List<ChessRegret> chessRegretList, int man, int beEat) {
int canGo = 1;//1可以移动,0不能移动
int judge = play.getName().charAt(1) - '0';
//左上
if (enemy.getX() == play.getX() - 57 && enemy.getY() == play.getY() - 57) {
if (judge == 1) {
if ((enemy.getY() == 56 && enemy.getX() == 195) || (enemy.getY() == 113 && enemy.getX() == 252)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if ((enemy.getY() == 455 && enemy.getX() == 195) || (enemy.getY() == 512 && enemy.getX() == 252)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//右上
if (enemy.getX() == play.getX() + 57 && enemy.getY() == play.getY() - 57) {
if (judge == 1) {
if ((enemy.getY() == 56 && enemy.getX() == 309) || (enemy.getY() == 113 && enemy.getX() == 252)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if ((enemy.getY() == 455 && enemy.getX() == 309) || (enemy.getY() == 512 && enemy.getX() == 252)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//左下
if (enemy.getX() == play.getX() - 57 && enemy.getY() == play.getY() + 57) {
if (judge == 1) {
if ((enemy.getY() == 170 && enemy.getX() == 195) || (enemy.getY() == 113 && enemy.getX() == 252)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if ((enemy.getY() == 569 && enemy.getX() == 195) || (enemy.getY() == 512 && enemy.getX() == 252)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//右下
if (enemy.getX() == play.getX() + 57 && enemy.getY() == play.getY() + 57) {
if (judge == 1) {
if ((enemy.getY() == 170 && enemy.getX() == 309) || (enemy.getY() == 113 && enemy.getX() == 252)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if ((enemy.getY() == 569 && enemy.getX() == 309) || (enemy.getY() == 512 && enemy.getX() == 252)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
}
public void masterEat(ChessManBehavior play, ChessManBehavior playQ[], ChessManBehavior enemy, List<ChessRegret> chessRegretList, int man, int beEat) {
int judge = play.getName().charAt(1) - '0';
//向上
if (enemy.getX() == play.getX() && enemy.getY() == play.getY() - 57) {
if (judge == 1) {
if ((enemy.getX() == 252 && enemy.getY() == 56) || (enemy.getX() == 252 && enemy.getY() == 113) || (enemy.getX() == 195 && enemy.getY() == 56) ||
(enemy.getX() == 195 && enemy.getY() == 113) || (enemy.getX() == 309 && enemy.getY() == 56) || (enemy.getX() == 309 && enemy.getY() == 113)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if ((enemy.getX() == 252 && enemy.getY() == 455) || (enemy.getX() == 252 && enemy.getY() == 512) || (enemy.getX() == 195 && enemy.getY() == 455) ||
(enemy.getX() == 195 && enemy.getY() == 512) || (enemy.getX() == 309 && enemy.getY() == 455) || (enemy.getX() == 309 && enemy.getY() == 512)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//向下
if (enemy.getX() == play.getX() && enemy.getY() == play.getY() + 57) {
if (judge == 1) {
if ((enemy.getX() == 252 && enemy.getY() == 170) || (enemy.getX() == 252 && enemy.getY() == 113) || (enemy.getX() == 109 && enemy.getY() == 170) ||
(enemy.getX() == 109 && enemy.getY() == 113) || (enemy.getX() == 309 && enemy.getY() == 170) || (enemy.getX() == 309 && enemy.getY() == 113)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if ((enemy.getX() == 252 && enemy.getY() == 569) || (enemy.getX() == 252 && enemy.getY() == 512) || (enemy.getX() == 109 && enemy.getY() == 569) ||
(enemy.getX() == 109 && enemy.getY() == 512) || (enemy.getX() == 309 && enemy.getY() == 569) || (enemy.getX() == 309 && enemy.getY() == 512)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//向左
if (enemy.getX() == play.getX() - 57 && enemy.getY() == play.getY()) {
if (judge == 1) {
if ((enemy.getX() == 252 && enemy.getY() == 56) || (enemy.getX() == 195 && enemy.getY() == 56) || (enemy.getX() == 252 && enemy.getY() == 113) ||
(enemy.getX() == 195 && enemy.getY() == 113) || (enemy.getX() == 252 && enemy.getY() == 170) || (enemy.getX() == 195 && enemy.getY() == 170)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if ((enemy.getX() == 252 && enemy.getY() == 455) || (enemy.getX() == 195 && enemy.getY() == 455) || (enemy.getX() == 252 && enemy.getY() == 512) ||
(enemy.getX() == 195 && enemy.getY() == 512) || (enemy.getX() == 252 && enemy.getY() == 569) || (enemy.getX() == 195 && enemy.getY() == 569)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//向右
if (enemy.getX() == play.getX() + 57 && enemy.getY() == play.getY()) {
if (judge == 1) {
if ((enemy.getX() == 252 && enemy.getY() == 56) || (enemy.getX() == 309 && enemy.getY() == 56) || (enemy.getX() == 252 && enemy.getY() == 113) ||
(enemy.getX() == 309 && enemy.getY() == 113) || (enemy.getX() == 252 && enemy.getY() == 170) || (enemy.getX() == 309 && enemy.getY() == 170)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if ((enemy.getX() == 252 && enemy.getY() == 455) || (enemy.getX() == 309 && enemy.getY() == 455) || (enemy.getX() == 252 && enemy.getY() == 512) ||
(enemy.getX() == 309 && enemy.getY() == 512) || (enemy.getX() == 252 && enemy.getY() == 569) || (enemy.getX() == 309 && enemy.getY() == 569)) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
}
public void soliderEat(ChessManBehavior play, ChessManBehavior playQ[], ChessManBehavior enemy, List<ChessRegret> chessRegretList, int man, int beEat) {
int judge = play.getName().charAt(1) - '0';
if (enemy.getX() == play.getX() && enemy.getY() == play.getY() - 57) {
if (judge == 2) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
//向下
if (enemy.getX() == play.getX() && enemy.getY() == play.getY() + 57) {
if (judge == 1) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
//向左
if (enemy.getX() == play.getX() - 57 && enemy.getY() == play.getY()) {
if (judge == 1) {
if (enemy.getY() >= 341) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if (enemy.getY() < 341) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
//向右
if (enemy.getX() == play.getX() + 57 && enemy.getY() == play.getY()) {
if (judge == 1) {
if (enemy.getY() >= 341) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
if (judge == 2) {
if (enemy.getY() < 341) {
ChessRegret chessRegret = new ChessRegret();
chessRegret.eatMan = beEat;
chessRegret.cx = play.getX();
chessRegret.cy = play.getY();
chessRegret.man = man;
chessRegretList.add(chessRegret);
enemy.setVisible(false);
enemy.died = true;
play.setBounds(enemy.getX(), enemy.getY(), 55, 55);
return;
}
}
}
}
public int masterMeet(ChessManBehavior playQ[]){
if (playQ[30].getX()!=playQ[31].getX())
return 0;
int count=0;
int cx=playQ[30].getX();
for (int j=playQ[30].getY()+57;j<playQ[31].getY();j+=57){
for(int i=0;i<32;i++){
if (playQ[i].getX()==cx && playQ[i].getY()==j && playQ[i].died==false){
count++;
}
}
}
if (count>0)
return 0;
return 1;
}
}
package com.chess;
public class ChessRegret {
int man;//走棋的棋子
int cx;//走到的x坐标
int cy;//走到的y坐标
int eatMan;//吃掉的棋子
public ChessRegret(){
eatMan=-1;//默认没有吃棋子
}
}
package com.chess;
/*
定义棋子的行为动作及声音
*/
import javax.swing.*;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class ChessManBehavior extends JLabel {
boolean died=false;
ChessManBehavior(Icon image){
super(image);
}
public void music(String s){
try {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(s));
Clip clip = AudioSystem.getClip();
clip.open(inputStream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void playSounds(int man) {
if (man > 0 && man < 4) {
music("sounds/车.wav");
}//移动车的声音
else if (man >4 && man < 8) {
music("sounds/马.wav");
}//移动马的声音
else if (man > 8 && man < 12) {
music("sounds/象.wav");
}//移动象的声音
else if (man > 12 && man <= 15) {
music("sounds/士.wav");
}//移动士的声音
else if (man > 16 && man < 26) {
music("sounds/兵.wav");
}//移动兵的声音
else if (man > 25 && man < 30) {
music("sounds/炮.wav");
}//移动炮的声音
else{
music("sounds/将.wav");
}//移动帅的声音
}
/*public void Stop() {
clip.stop();
clip.close();
}*/
/*public void playSounds(int man){
if(man>25&&man<30){
try{
InputStream fileau=new FileInputStream("sounds/cannon.wav");
AudioStream as=new AudioStream(fileau);
AudioPlayer.player.start(as);
} catch (IOException e){
e.printStackTrace();
}
}
}*/
}
package com.chess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
//主框架类
public class ChessMainframe extends JFrame implements ActionListener, MouseListener, Runnable {
ChessManBehavior[] play = new ChessManBehavior[32];// 定义32个棋子
JLabel image;//棋盘
Container con;//窗格
JToolBar jmain;//工具栏
JButton anew, repent, exit;//重新开始,悔棋,退出按钮
JLabel text;//当前信息
/*true 游戏结束*/
boolean gameIsOver;
/*规则类,判断走棋和吃子*/
ChessRule chessRule;
/*单击棋子*****/
/*chessManClick=true 闪烁闪烁 并给线程响应*/
/*chessManClick=false 吃棋子 停止闪烁 并给线程响应*/
boolean chessManClick;
/*chessPlayClick=1 黑棋走棋*/
/*chessPlayClick=2 红棋走棋*/
/*chessPlayClick=3 双方都不能走棋*/
int chessPlayClick = 2;
Thread tmain;//控制棋子闪烁的线程
static int Man;//把第一次的单击棋子给线程响应
List<ChessRegret> regretList;
/*构造函数
* 初始化图形用户界面
* */
public ChessMainframe(String title) {
con = this.getContentPane();//获得窗口的内容面板(获得客格引用)
con.setLayout(null);//设置成绝对布局,组件位置和形状不会随着窗口变化而变化
chessRule =new ChessRule();
this.setTitle(title);
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();//需要先画棋子,先放的JLabel永远在最前
/*注册监听者*/
// 注册按钮监听
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() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("系统关闭");
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.setSize(558, 676);//标题栏默认高度为26px
this.setVisible(true);
gameIsOver = false;
regretList = new ArrayList<ChessRegret>();
chessRule = new ChessRule();
/* this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});*/
}
public void chessReset() {
// 重新开始
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);
for (i = 0; i < 32; i++) {
System.out.println(i + " " + play[i].getName());
}
gameIsOver = false;
for (i = 0; i < 32; i++) {
play[i].setVisible(true);
play[i].died = false;
}
text.setText(" 红棋走棋");
chessPlayClick=2;
Man = -1;
regretList.clear();//清空后悔棋的list
repent.setEnabled(true);
}
/*添加棋子方法******/
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 ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(in);
play[i].setBounds(k, 170, 55, 55);
play[i].setName("炮1" + i);
}
in = new ImageIcon("image/黑将.gif");
play[30] = new ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(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 ChessManBehavior(in);
play[i].setBounds(k, 455, 55, 55);
play[i].setName("炮2" + i);
}
in = new ImageIcon("image/红将.gif");
play[31] = new ChessManBehavior(in);
play[31].setBounds(252, 569, 55, 55);
play[31].setName("帅2");
for (i=0;i<32;i++){
System.out.println(i+" "+play[i].getName());
}
}
@Override
public void actionPerformed(ActionEvent e) {
// 重新开始按钮
if (e.getSource().equals(anew)) {
System.out.println("click renew");
chessReset();
}
// 重新悔棋按钮
if (e.getSource().equals(repent)) {
System.out.println("regret");
if (regretList.isEmpty() == false) {
ChessRegret chessRegret = regretList.get(regretList.size() - 1);
Man = chessRegret.man;
play[Man].setBounds(chessRegret.cx, chessRegret.cy, 55, 55);
if (chessRegret.eatMan != -1) {
play[chessRegret.eatMan].setVisible(true);
}
if (play[Man].getName().charAt(1) == '2') {
chessPlayClick = 2;
text.setText(" 红棋走棋");
} else {
chessPlayClick = 1;
text.setText(" 黑棋走棋");
}
regretList.remove(regretList.size() - 1);
}
}
// 退出
if (e.getSource().equals(exit)) {
int j = JOptionPane.showConfirmDialog(this, "真的要退出吗?", "退出", JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);
if (j == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
/*单击棋子方法*/
@Override
public void mouseClicked(MouseEvent e) {
// 当前坐标
int ex = 0;
int ey = 0;
// 启动线程
if (tmain == null) {
tmain = new Thread(this);
tmain.start();
}
if (gameIsOver)
return;
// 单击棋盘(移动棋子)
if (e.getSource().equals(image)) {
// 该红棋走棋的时候
if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2') {
ex = play[Man].getX();
ey = play[Man].getY();
if (Man >= 0 && Man < 4) {
System.out.println("移动红车");
chessRule.carRule(play[Man], play, e, regretList, Man);
}
if (Man >= 4 && Man < 8) {
System.out.println("移动红马");
chessRule.horseRule(play[Man], play, e, regretList, Man);
}
if (Man >= 8 && Man < 12) {
System.out.println("移动红象");
chessRule.elephantRule(play[Man], play, e, regretList, Man);
}
if (Man >= 12 && Man <= 15) {
System.out.println("移动红士");
chessRule.chapRule(play[Man], play, e, regretList, Man);
}
if (Man >= 16 && Man < 26) {
System.out.println("移动红兵");
chessRule.soliderRule(play[Man], play, e, regretList, Man);
}
if (Man >= 25 && Man < 30) {
System.out.println("移动红炮");
chessRule.cannonRule(play[Man], play, e, regretList, Man);
}
if (Man == 30 || Man == 31) {
System.out.println("移动红帅");
chessRule.masterRule(play[Man], play, e, regretList, Man);
}
if (ex == play[Man].getX() && ey == play[Man].getY()) {
text.setText(" 红旗走棋");
chessPlayClick = 2;
} else {
if (chessRule.masterMeet(play) == 1) {
JOptionPane.showMessageDialog(this, "游戏结束,黑棋赢了!!!");
gameIsOver = true;
repent.setEnabled(false);
return;
}
text.setText(" 黑棋走棋");
chessPlayClick = 1;
chessManClick = false;
}
}
// 该黑棋走棋的时候
else if (chessPlayClick==1 && play[Man].getName().charAt(1)=='1') {
ex=play[Man].getX();
ey=play[Man].getY();
if (Man >= 0 && Man < 4) {
System.out.println("移动黑车");
chessRule.carRule(play[Man], play, e, regretList, Man);
}
if (Man >= 4 && Man < 8) {
System.out.println("移动黑马");
chessRule.horseRule(play[Man], play, e, regretList, Man);
}
if (Man >= 8 && Man < 12) {
System.out.println("移动黑象");
chessRule.elephantRule(play[Man], play, e, regretList, Man);
}
if (Man >= 12 && Man <= 15) {
System.out.println("移动黑士");
chessRule.chapRule(play[Man], play, e, regretList, Man);
}
if (Man >= 16 && Man < 26) {
System.out.println("移动黑兵");
chessRule.soliderRule(play[Man], play, e, regretList, Man);
}
if (Man >= 25 && Man < 30) {
System.out.println("移动黑炮");
chessRule.cannonRule(play[Man], play, e, regretList, Man);
}
if (Man == 30 || Man == 31) {
System.out.println("移动黑将");
chessRule.masterRule(play[Man], play, e, regretList, Man);
}
// 是否走棋错误(是否待在原地没有动)
if (ex == play[Man].getX() && ey == play[Man].getY()) {
text.setText(" 黑旗走棋");
chessPlayClick = 1;
} else {
if (chessRule.masterMeet(play) == 1) {
JOptionPane.showMessageDialog(this, "游戏结束,黑棋赢了!!!");
text.setText("游戏结束,黑棋赢了!!!");
chessManClick=false;
gameIsOver = true;
return;
}
text.setText(" 红棋走棋");
chessPlayClick = 2;
chessManClick = false;
}
}
}
// 单击棋子
else {
// 第一次单击棋子(闪烁棋子)
int iClick = -1;
for (int i = 0; i < 32; ++i) {
if (e.getSource().equals(play[i])) {
iClick = i;
break;
}
}
if (chessPlayClick == 2) {
int preX=-1;
int preY=-1;
if (play[iClick].getName().charAt(1) == '2') {
Man = iClick;
preX=play[Man].getX();
preY=play[Man].getY();
chessManClick = true;
return;
}
int judge = play[Man].getName().charAt(1) - '0';
if (play[iClick].getName().charAt(1) == '1' && judge == 2) {
if (Man >= 0 && Man < 4) {
System.out.println("车吃子判断");
chessRule.carEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 4 && Man < 8) {
System.out.println("马吃子判断");
chessRule.horseEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 8 && Man < 12) {
System.out.println("象吃子判断");
chessRule.elephantEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 12 && Man < 16) {
System.out.println("士吃子判断");
chessRule.chapEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 16 && Man < 26) {
System.out.println("兵吃子判断");
chessRule.soliderEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 26 && Man < 30) {
System.out.println("炮吃子判断");
chessRule.cannonEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 30 && Man < 32) {
System.out.println("BOSS吃子判断");
chessRule.masterEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (play[iClick].getX() == play[Man].getX() && play[iClick].getY() == play[Man].getY()) {
if (chessRule.masterMeet(play) == 1) {
JOptionPane.showMessageDialog(this, "游戏结束,黑棋赢了");
text.setText("游戏结束,黑棋赢了!!!");
chessManClick = false;
gameIsOver = true;
return;
}
if (play[30].isVisible() == false) {
JOptionPane.showMessageDialog(this, "游戏结束,红棋赢了!!!");
text.setText("游戏结束,红棋赢了!!!");
chessManClick = false;
gameIsOver = true;
repent.setEnabled(false);
return;
}
chessPlayClick = 1;
text.setText(" 黑棋走棋");
chessManClick = false;
}
}
}
// 该黑棋吃棋的时候
else if (chessPlayClick == 1) {
if (play[iClick].getName().charAt(1) == '1') {
Man = iClick;
chessManClick = true;
}
int judge = play[Man].getName().charAt(1) - '0';
if (play[iClick].getName().charAt(1) == '2' && judge == 1) {
if (Man >= 0 && Man < 4) {
System.out.println("车吃子判断");
chessRule.carEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 4 && Man < 8) {
System.out.println("马吃子判断");
chessRule.horseEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 8 && Man < 12) {
System.out.println("象吃子判断");
chessRule.elephantEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 12 && Man < 16) {
System.out.println("士吃子判断");
chessRule.chapEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 16 && Man < 26) {
System.out.println("兵吃子判断");
chessRule.soliderEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 26 && Man < 30) {
System.out.println("炮吃子判断");
chessRule.cannonEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
if (Man >= 30 && Man < 32) {
System.out.println("BOSS吃子判断");
chessRule.masterEat(play[Man], play, play[iClick], regretList, Man,iClick);
}
// 是否胜利
if (play[iClick].getX() == play[Man].getX() && play[iClick].getY() == play[Man].getY()) {
if (chessRule.masterMeet(play) == 1) {
JOptionPane.showMessageDialog(this, "游戏结束,红棋赢了");
text.setText("游戏结束,红棋赢了!!!");
chessManClick = false;
gameIsOver = true;
return;
}
if (play[31].isVisible() == false) {
JOptionPane.showMessageDialog(this, "游戏结束,黑棋赢了!!!");
text.setText("游戏结束,黑棋赢了!!!");
chessManClick = false;
gameIsOver = true;
return;
}
chessPlayClick = 2;
text.setText(" 红棋走棋");
chessManClick = false;
}
}
}
}
}
@Override
public void mousePressed (MouseEvent e){
}
@Override
public void mouseReleased (MouseEvent e){
}
@Override
public void mouseEntered (MouseEvent e){
}
@Override
public void mouseExited (MouseEvent e){
}
/*线程方法控制棋子闪烁*/
@Override
public synchronized void run () {
while (true) {
// 单击棋子第一下开始闪烁
if (chessManClick) {
play[Man].setVisible(false);
// 时间控制
try {
tmain.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
play[Man].setVisible(true);
for (int i = 0; i < 32; i++) {
if (i != Man && play[i].died == false)
play[i].setVisible(true);
}
}
// 闪烁当前提示信息 以免用户看不见
else {
text.setVisible(false);
try {
tmain.sleep(250);
} catch (Exception e) {
e.printStackTrace();
}
text.setVisible(true);
}
try {
tmain.sleep(350);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
希望可以帮助到您。