在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → java 象棋源代码

java 象棋源代码

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:3.04M
  • 下载次数:49
  • 浏览次数:433
  • 发布时间:2016-06-25
  • 实例类别:Android平台开发
  • 发 布 人:mzsts_HOST3VCOM
  • 文件格式:.rar
  • 所需积分:4
 相关标签: 象棋 源代码 游戏

实例介绍

【实例简介】
【实例截图】
【核心代码】 package chessui;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.lang.reflect.Field;
import java.util.List;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ChessUI extends JPanel {

private static final long serialVersionUID = 1L;
public static Frame frame = new Frame();
    int wzc=0;
// 各种图片的加载,这里的加载是为了更好让其他的类进行使用的使用
public static BufferedImage b_c;
public static BufferedImage b_m;
public static BufferedImage b_p;
public static BufferedImage b_j;
public static BufferedImage b_s;
public static BufferedImage b_x;
public static BufferedImage b_z;
public static BufferedImage r_c;
public static BufferedImage r_m;
public static BufferedImage r_p;
public static BufferedImage r_j;
public static BufferedImage r_s;
public static BufferedImage r_x;
public static BufferedImage r_z;
public static BufferedImage box;

static List<Chessman> redlist = new ArrayList<Chessman>();
static List<Chessman> blacklist = new ArrayList<Chessman>();
static List<Chessman> recover1 = new ArrayList<Chessman>();
static List<Chessman> recover2 = new ArrayList<Chessman>();
// 总集合 , 用来传递进行遍历
static List<Chessman> list = new ArrayList<Chessman>();
private final static String COMMON = "COMMON";// 无状体
private final static String PUT = "PUT"; // 落子
private final static String RED = "RED";// 红旗走
private final static String BLACK = "BLACK";// 黑棋走
private final static String NO = "NO";// 未赢
private final static String WIN = "WIN";// 赢了
private static String WINSTATE = NO;// 初始状态为没赢
     static Point Click_Start = null;// 第一次进行提子
static Point Click_End = null;// 第二次进行提子
static String ChessState = COMMON;// 棋子状体
static String StateOfRB = RED;// 先后手状态
static boolean OK = false;
static int flagrecover = 0;
Graphics g;
    static {
try {
b_c = ImageIO.read(ChessUI.class.getResource("../stype_1/b_c.png"));
b_m = ImageIO.read(ChessUI.class.getResource("../stype_1/b_m.png"));
b_p = ImageIO.read(ChessUI.class.getResource("../stype_1/b_p.png"));
b_j = ImageIO.read(ChessUI.class.getResource("../stype_1/b_j.png"));
b_s = ImageIO.read(ChessUI.class.getResource("../stype_1/b_s.png"));
b_x = ImageIO.read(ChessUI.class.getResource("../stype_1/b_x.png"));
b_z = ImageIO.read(ChessUI.class.getResource("../stype_1/b_z.png"));
r_c = ImageIO.read(ChessUI.class.getResource("../stype_1/r_c.png"));
r_m = ImageIO.read(ChessUI.class.getResource("../stype_1/r_m.png"));
r_p = ImageIO.read(ChessUI.class.getResource("../stype_1/r_p.png"));
r_j = ImageIO.read(ChessUI.class.getResource("../stype_1/r_j.png"));
r_s = ImageIO.read(ChessUI.class.getResource("../stype_1/r_s.png"));
r_x = ImageIO.read(ChessUI.class.getResource("../stype_1/r_x.png"));
r_z = ImageIO.read(ChessUI.class.getResource("../stype_1/r_z.png"));
box = ImageIO.read(ChessUI.class
.getResource("../stype_1/r_box.png"));
} catch (Exception e) {
throw new RuntimeException("图片加载异常");
}
}
public static void main(String[] args) {
ChessUI game = new ChessUI();// 为了可以不用到static我们要new 一个自己的对象
FirstFillData();// 1.创建界面    第一次填充数据
frame.add(game);
game.Action(); // 创建我们的图形化界面
game.CreateChessView(); // 2.画出原图
}
private static void FirstFillData() {
/*/ 进行第一次集合的填充使得我们获取开局的棋子
                     经过考虑 我认为存储格子的焦点更加的科学
   我们在规定的时候只要规定我们的比较点进入我们的焦点即可
 注入黑棋/*/

blacklist.clear();
redlist.clear();
list.clear();
StateOfRB = RED;
blacklist.add(new Rook(new Point(30, 30), "b"));
blacklist.add(new Rook(new Point(510, 30), "b"));
blacklist.add(new Horse(new Point(90, 30), "b"));
blacklist.add(new Horse(new Point(450, 30), "b"));
blacklist.add(new Chancellor(new Point(150, 30), "b"));
blacklist.add(new Chancellor(new Point(390, 30), "b"));
blacklist.add(new Bodyguard(new Point(210, 30), "b"));
blacklist.add(new Bodyguard(new Point(330, 30), "b"));
blacklist.add(new Cannon(new Point(90, 150), "b"));
blacklist.add(new Cannon(new Point(450, 150), "b"));
blacklist.add(new Soldier(new Point(30, 210), "b"));
blacklist.add(new Soldier(new Point(150, 210), "b"));
blacklist.add(new Soldier(new Point(270, 210), "b"));
blacklist.add(new Soldier(new Point(390, 210), "b"));
blacklist.add(new Soldier(new Point(510, 210), "b"));
blacklist.add(new General(new Point(270, 30), "b"));
// 注入红棋
redlist.add(new Rook(new Point(30, 570), "r"));
redlist.add(new Rook(new Point(510, 570), "r"));
redlist.add(new Horse(new Point(90, 570), "r"));
redlist.add(new Horse(new Point(450, 570), "r"));
redlist.add(new Chancellor(new Point(150, 570), "r"));
redlist.add(new Chancellor(new Point(390, 570), "r"));
redlist.add(new Bodyguard(new Point(210, 570), "r"));
redlist.add(new Bodyguard(new Point(330, 570), "r"));
redlist.add(new Cannon(new Point(90, 450), "r"));
redlist.add(new Cannon(new Point(450, 450), "r"));
redlist.add(new Soldier(new Point(30, 390), "r"));
redlist.add(new Soldier(new Point(150, 390), "r"));
redlist.add(new Soldier(new Point(270, 390), "r"));
redlist.add(new Soldier(new Point(390, 390), "r"));
redlist.add(new Soldier(new Point(510, 390), "r"));
redlist.add(new General(new Point(270, 570), "r"));
CreateAllList();
}
private static void CreateAllList() {
list.clear();
list.addAll(redlist);
list.addAll(blacklist);
}
private Timer timer;
private void Action() {
ListenerClick();
timer = new Timer(); timer.schedule(new TimerTask() {
public void run() {
}
}, new Date(), 1000); 
}
public void repaint() { frame.repaint(); }

 private void ListenerClick() {/*/ 点击监听/*/
 MouseListener mouseListener = new MouseAdapter() {
  public void mouseClicked(MouseEvent e) {
if (WIN.equals(WINSTATE))  return;
int x = e.getX(); int y = e.getY();
int really_x = 0; int really_y = 0;
// 在框内
if (x > 0 && x < 550 && y > 0 && y < 630) {
if ((x - 30) % 60 > 30)  really_x = ((x - 30) / 60 1) * 60 30;
else   really_x = (x - 30) / 60 * 60 30;
if ((y - 30) % 60 > 30)  really_y = ((y - 30) / 60 1) * 60 30;
   else  really_y = (y - 30) / 60 * 60 30;

switch (ChessState) { // 初始状态 ->我们要进入取子状态
case COMMON: /*/ 先判断我们选取的位置有没有棋子       进来就证明有棋子
                          下面我们要证明我们的棋子的状态并依据这个来更改状态/*/
if (findOFList(StateOfRB, really_x, really_y)) {
Click_Start = new Point(really_x, really_y);
ChessState = PUT;
}
OK = true;
break;
case PUT:/*/ 落子/*/ 
 /*/  问题现在在于如何落点  1.判断是不是自己,如果是自己的话就取消落子回到初始状态/*/ 
boolean myselfflag = isMyself(Click_Start, new Point(
really_x, really_y));
/*/  2.判断我们的落点是不是我们自己的棋子 如果是的话我们就无法移动/*/ 
boolean flag = WeCanPut(really_x, really_y); 
/*/ 在这里我们已经可以进行落子了,在这个之后我们来遍历数组/*/ 
if (StateOfRB.equals(RED)) {
for (Chessman c : blacklist) {
if (c.getName()) {
OK = true;
break;
}
}
} else {
for (Chessman c : redlist) {
if (c.getName()) {
OK = true;
break;
}
}
}
if (flag || myselfflag) {
if (!myselfflag) {
if (StateOfRB.equals(RED)) {
/*/ 要判断是不是要结束了/*/ 
if (OK) { StateOfRB = BLACK;
}
} else {
if (OK) { StateOfRB = RED;
}
}
}
Click_End = null;
Click_Start = null;
ChessState = COMMON;
}
break;
}
if (OK) { repaint(); OK = false;
} else { Frame f = new Frame();
f.setSize(200, 200); f.setLocation(500, 300);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
repaint();
if (RED.equals(StateOfRB)) f.add(new Button("Red  had  WIN"));
else  f.add(new Button("BLACK  had  WIN")); 
f.setVisible(true);
WINSTATE = WIN;
}

}
};
this.addMouseListener(mouseListener);
}
/*/  判断两个点是不是相等/*/ 
private boolean isMyself(Point p1, Point p2) {
if (p1.x == p2.x && p1.y == p2.y) {
return true;
}
return false;
}
private boolean WeCanPut(int really_x, int really_y) {

boolean flag_we_chess = false;
Point point = new Point(really_x, really_y);
if (RED.equals(StateOfRB)) {
for (Chessman c : redlist) {
Point chessP = c.getPoint();
if (point.x == chessP.x && point.y == chessP.y) {
flag_we_chess = true;
}
}
} else {
for (Chessman c : blacklist) {
Point chessP = c.getPoint();
if (point.x == chessP.x && point.y == chessP.y) {
flag_we_chess = true;
}}}
/*/ 落点不相同才会执行这一步/*/ 
if (!flag_we_chess) {

Click_End = new Point(really_x, really_y);
/*/  2.基于第一点我们的判断我们将我们是不是可以移动了/*/ 
for (Chessman c : list) {
Point chessP = c.getPoint();
if (chessP.x == Click_Start.x && chessP.y == Click_Start.y) {
if (c.Move(Click_End, list)) {
/*/  System.out.println("我可以动了");  
 当证明我可以动的时候就证明我可以将棋子重新放在另一个地方了/*/ 
List<Chessman> l = ChickList();
Chessman cflag = null;
for (Chessman chess : l) {
Point p = chess.getPoint();
// 这里可以设定一个方法
if (p.x == Click_Start.x && p.y == Click_Start.y) {
cflag = chess;
}
}
if (cflag != null) {
int flag = l.indexOf(cflag);
cflag.setPoint(Click_End);
l.set(flag, cflag); 
List<Chessman> otherlist = ChickOtherList();
removeElement(otherlist);
CreateAllList();
return true;
}
}
}
}

}
return false;
}

private void removeElement(List<Chessman> otherlist) {
Chessman c = null;
for (Chessman ch : otherlist) {
Point point = ch.getPoint();
if (point.x == Click_End.x && point.y == Click_End.y) {
c = ch;
}
}
boolean b = otherlist.remove(c); 
}
// 返回对面的数组
private List<Chessman> ChickOtherList() {
if ("RED".equals(StateOfRB)) {
return blacklist;
}
return redlist;
}

// 找到当前集合
private List<Chessman> ChickList() {
if ("RED".equals(StateOfRB)) {
return redlist;
}
return blacklist;
}
/*/ 查看一下我们选取的点是不是我们的棋子/*/
private boolean findOFList(String color, int x, int y) {
if ("RED".equals(color)) {
for (Chessman c : redlist) {
Point point = c.getPoint();
if (point.x == x && point.y == y) {
return true;
}
}
} else {
for (Chessman c : blacklist) {
Point point = c.getPoint();
if (point.x == x && point.y == y) {
return true;
}
}
}
return false;
}
private void CreateChessView() {
Point p1 = new Point(300, 20);
frame.setLocation(p1);
Dimension d = new Dimension(700, 700);
frame.setFocusable(true); 
frame.setResizable(false);
frame.setSize(d);
     //  frame.setBackground(Color.orange);/*加个颜色更有感觉*/
// Panel p = new Panel();
// p.add(new Button("1"));// 默认垂直居中,间距为5
// p.add(new Button("2"));
// p.add(new Button("3")); 
// frame.add(p);//两种方法
this.setResizable(false);
frame.setTitle("中国象棋 x1.0");
WindowListener windowlistener = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
MenuBar mb = new MenuBar();
Menu m = new Menu("choose");
MenuItem mi = new MenuItem("restart");
MenuItem mm = new MenuItem ("playrules");
MenuItem mn = new MenuItem("recover");
mb.add(m); 
m.add(mi);
m.add(mm);
m.add(mn);
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FirstFillData();
}
});
mm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame rule = new JFrame("rules");
Dimension d = new Dimension(300, 300);
rule.setFocusable(true); 
rule.setResizable(false);
rule.setSize(d);
JTextArea authorTextArea = new JTextArea("兵卒:过河前,仅可向前一步走," "\n" "   过河后可以左右走,有对方挡子,可以杀掉" "\n" "炮:移动走空白直线," "\n" "   有挡子,可以杀掉档子后面第一个对方棋子" "\n" "車:移动走空白直线, 可以杀掉对方挡子," "\n" "马 :走日字对角," "\n" "   若总是能以先长边再短边到达目的地,就可以走" "\n" "相:不过河,走田字对角,有档子不可以走" "\n" "仕: 每次仅走一步斜线,不可以出没有斜线区域" "\n" "将帅:每次仅可上下左右走一步," "\n" "  不可以出没有斜线区域,特别地,将帅不可面对面"
); 
authorTextArea.setPreferredSize(new Dimension(800, 1000));
rule.add(authorTextArea);
rule.setVisible(true);  
}
});
  mn.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
if (recover1 != null && recover2 != null) {
System.out.println(redlist.equals(recover1));
System.out.println(blacklist.equals(recover2));
wzc ;
if(wzc%2==0)
{
redlist = recover1;
blacklist = recover2;
}
System.out.println("2222");
repaint();
System.out.println("33333");
if (RED.equals(StateOfRB)) {
StateOfRB = BLACK;
} else {
StateOfRB = RED;
}
}
}
}); 
frame.setMenuBar(mb);
frame.addWindowListener(windowlistener);
frame.setVisible(true);
}
private void setResizable(boolean b) {}
public void paint(Graphics g) {
pointView(g);paintvehicle(g);
}
private void pointView(Graphics g) {
for (int y = 0; y < 9; y ) {
for (int x = 0; x < 8; x ) {
if (y != 4) {
g.drawRect(x * 60 30, y * 60 30, 60, 60);
}
}
}
g.drawLine(210, 30, 330, 150);  g.drawLine(330, 30, 210, 150);
g.drawLine(210, 450, 330, 570); g.drawLine(330, 450, 210, 570);
g.drawLine(30, 270, 30, 330); g.drawLine(510, 270, 510, 330);
g.drawString("楚  河", 150, 300);g.drawString("汉  界", 340, 300);
if (RED.equals(StateOfRB))   g.drawString("红旗走", 540, 300);
 else   g.drawString("黑旗走", 540, 300);
}
private void paintvehicle(Graphics g) {
if (ChessState == PUT) {
g.drawImage(box, Click_Start.x - 30, Click_Start.y - 30, 60, 60,null);
}
for (Chessman c : blacklist) {
g.drawImage(c.getImage(), c.getPoint().x - 30, c.getPoint().y - 30,60, 60, null);
}
for (Chessman rc : redlist) {
g.drawImage(rc.getImage(), rc.getPoint().x - 30,rc.getPoint().y - 30, 60, 60, null);
}
}
}
abstract class AbstractChessman implements Chessman {
public BufferedImage Image;
public Point point;
public String name;
}

// 仕
class Bodyguard extends AbstractChessman {
private String color;
public Bodyguard(Point start, String color) {
point = start;
String name = color "_s";
this.color = color;
Class<?> obj;
try {
obj = Class.forName("chessui.ChessUI");
Field[] f = obj.getDeclaredFields();
for (Field field : f) {
field.setAccessible(true);
if (name.equals(field.getName())) {
Image = (BufferedImage) field.get(obj.newInstance());
}
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (IllegalArgumentException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace();
}
}

public BufferedImage getImage() { return Image; }
public Point getPoint() { return point;}
public boolean Move(Point end, List<Chessman> list) {
if (point.x == end.x && point.y == end.y) {
return true;
}
boolean wflag;/*/ 用来标记颜色/*/ 
if ("r".equals(color)) { wflag = true;
} else { wflag = false; }
int startx; int endx; int starty; int endy;
if (wflag) {
startx = 210; endx = 330;
starty = 450; endy = 570;
} else {
startx = 210; endx = 330;
starty = 30; endy = 150;
}
boolean flag1 = point.x >= startx && point.x <= endx
&& point.y >= starty && point.y <= endy;
boolean flag2 = (end.x >= startx) && end.x <= endx && end.y >= starty
&& end.y <= endy;
if (!(flag1 && flag2)) { return false; }
int maxx; int maxy; int minx; int miny;
System.out.println("play1");
if (end.x > point.x) {
maxx = end.x; minx = point.x;
if (end.y > point.y) {
maxy = end.y; miny = point.y;
} else {
maxy = point.y; miny = end.y;
}
if (maxx - minx == 60 && maxy - miny == 60) {
return true;
}
} else {
maxx = point.x; minx = end.x;
if (end.y > point.y) {
maxy = end.y; miny = point.y;
} else {
maxy = point.y; miny = end.y;
}
if (maxx - minx == 60 && maxy - miny == 60) {
return true;
}
}
 return false;
}
public void setPoint(Point point) { this.point = point; }
public boolean getName() { return false; }

}

class Cannon extends AbstractChessman {/*/ 炮/*/ 
public Cannon(Point start, String color) {
            point = start;
String name = color "_p";
Class<?> obj;
try {
obj = Class.forName("chessui.ChessUI");
Field[] f = obj.getDeclaredFields();
for (Field field : f) {
field.setAccessible(true);
if (name.equals(field.getName())) {
Image = (BufferedImage) field.get(obj.newInstance());
}
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (IllegalArgumentException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace();
}
}
public BufferedImage getImage() { return Image; }
public Point getPoint() { return point; }
public boolean Move(Point end, List<Chessman> list) {
if (point.x == end.x && point.y == end.y) {
return true;
}
if (!((point.x == end.x && point.y != end.y) || (point.y == end.y && point.x != end.x))) {
return false;
}/*/必须要走直线/*/
int startz;
int endz;
int wecan = 0;
if (point.x == end.x && point.y != end.y) {
if (point.y > end.y) {
startz = end.y; endz = point.y;
} else {
startz = point.y; endz = end.y;
}
for (Chessman c : list) {
/*/ 看看有没有东西阻碍这我 /*/
Point p = c.getPoint();
for (int i = startz 60; i < endz;) {
if (p.y == i && p.x == point.x) {
wecan ;
}
i = 60;
}
}
} else if (point.y == end.y && point.x != end.x) {
System.out.println("aaa");
if (point.x > end.x) {
startz = end.x;
endz = point.x;
} else {
endz = end.x;
startz = point.x;
}
for (Chessman c : list) {
for (int i = startz 60; i < endz;) {
Point p = c.getPoint();
if (p.x == i && p.y == point.y) {
wecan ;
}
i = 60;
}
}
}

if (!((wecan == 1) || (wecan == 0))) {
return false;
}
System.out.println("play2");
if (wecan == 0) {
boolean flag = true;
for (Chessman c : list) {
Point p = c.getPoint();
if (p.x == end.x && p.y == end.y) {
flag = false;
}
}
return flag;
} else if (wecan == 1) {
boolean flag = false; /*/如果等于1的话落点必须有子/*/
for (Chessman c : list) {
Point p = c.getPoint();
if (p.x == end.x && p.y == end.y) {
flag = true;
}
}
return flag;
}
return false;
}
public void setPoint(Point point) { this.point = point; }
public boolean getName() { return false; }
}
class Chancellor extends AbstractChessman {/*/ 相 /*/ 
 /*/  作为相的话要多一个属性 作为它的参数值来确定他是红是黑  相 /*/ 
private String who = null;
 public Chancellor(Point start, String color) {
    this.point = start; this.who = color;
String name = color "_x";
Class<?> obj;
try {
obj = Class.forName("chessui.ChessUI");
Field[] f = obj.getDeclaredFields();
for (Field field : f) {
field.setAccessible(true);
if (name.equals(field.getName())) {
Image = (BufferedImage) field.get(obj.newInstance());
}
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (IllegalArgumentException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace();
}
}

public BufferedImage getImage() {return Image;}
public Point getPoint() {return point;}
public boolean Move(Point end, List<Chessman> list) {
if (point.x == end.x && point.y == end.y) {
return true;
} /*/  开始判断边界值 /*/ 
if ("r".equals(this.who) && end.y < 330) {
return false;
} else if ("b".equals(this.who) && end.y > 270) {
return false;
}
int x = (this.point.x end.x) / 2;
int y = (this.point.y end.y) / 2;
Point flagP = new Point(x, y);
for (Chessman c : list) {
Point p = c.getPoint();
if (p.x == flagP.x && p.y == flagP.y) {
return false;
}
}
return true;
}
public void setPoint(Point point) {
this.point = point;
}
public boolean getName() {
return false;
}

}
interface Chessman {
public BufferedImage getImage();
public Point getPoint();
boolean Move(Point end, List<Chessman> list);
void setPoint(Point point);
boolean getName();
}
class General extends AbstractChessman {/*/  将 /*/ 
 /*/   将领也会有一个状态值 /*/ 
private String color;
 public General(Point start, String color) {
point = start;
String name = color "_j";
this.color = color;
Class<?> obj;
try {
obj = Class.forName("chessui.ChessUI");
Field[] f = obj.getDeclaredFields();
for (Field field : f) {
field.setAccessible(true);
if (name.equals(field.getName())) {
Image = (BufferedImage) field.get(obj.newInstance());
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
 public BufferedImage getImage() {
return Image;
}
 public Point getPoint() {
return point;

public boolean Move(Point end, List<Chessman> list) {
if (point.x == end.x && point.y == end.y) {
return true;
}
 boolean wflag;/*/ 用来标记颜色/*/ 
if ("r".equals(color)) wflag = true;
   else        wflag = false;
int startx; int endx;
 int starty; int endy;
if (wflag) {
startx = 210; endx = 330;
starty = 450; endy = 570;
} else {
startx = 210; endx = 330;
starty = 30; endy = 150;
}
boolean flag1 = point.x >= startx && point.x <= endx
&& point.y >= starty && point.y <= endy;
boolean flag2 = (end.x >= startx) && end.x <= endx && end.y >= starty
&& end.y <= endy;
if (!(flag1 && flag2)) {
return false;
}
if (point.x == end.x && point.y != end.y) {/*/ 给定横竖位移的方法/*/ 
int max; int min;
if (point.y > end.y) {
max = point.y; min = end.y;
} else {
max = end.y; min = point.y;
}
if (max - min == 60) {
return true;
}
} else if (point.y == end.y && point.x != end.x) {
int max; int min;
if (point.x > end.x) {
max = point.x; min = end.x;
} else {
max = end.x; min = point.x;
}
if (max - min == 60) {
return true;
}
}
return false;
}
public void setPoint(Point point) {
this.point = point;
}
public boolean getName() {
return true;
}
}
class Rook extends AbstractChessman {/*/ 车/*/
public Rook(Point start, String color) {
point = start; String name = color "_c";
Class<?> obj;
try {
obj = Class.forName("chessui.ChessUI");
Field[] f = obj.getDeclaredFields();
for (Field field : f) {
field.setAccessible(true);
if (name.equals(field.getName())) {
Image = (BufferedImage) field.get(obj.newInstance());
}
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (IllegalArgumentException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace();
}
}
public BufferedImage getImage() { return Image; }
public Point getPoint() { return point; }
public boolean Move(Point end, List<Chessman> list) {
/*/ 车是走直线的我们只用判断它是不是可以走直线即可 /*/ 
if (point.x == end.x && point.y == end.y)  return true;
if (!((point.x == end.x && point.y != end.y) || (point.y == end.y && point.x != end.x))) {
return false;
}
int startz; int endz;
boolean flag = true;
if (point.x == end.x && point.y != end.y) {
if (point.y > end.y) {
startz = end.y; endz = point.y;
} else { startz = point.y; endz = end.y;
}

for (Chessman c : list) {
Point p = c.getPoint(); /*/ 看看有没有东西阻碍这我/*/ 
for (int i = startz 60; i < endz;) {
                    if (p.y == i && p.x == point.x) {
flag = false;
}
i = 60;
}
}
} else if (point.y == end.y && point.x != end.x) {
if (point.x > end.x) {
startz = end.x; endz = point.x;
} else {
endz = end.x; startz = point.x;

for (Chessman c : list) {
for (int i = startz 60; i < endz;) {
Point p = c.getPoint();
if (p.x == i && p.y == point.y) {
System.out.println("中间有人");
flag = false;
}
i = 60;
}
}
}
return flag;
}
public void setPoint(Point point) {
this.point = point;
}
public boolean getName() {
return false;
}
}
class Horse extends AbstractChessman {
public Horse(Point start, String color) {
point = start;
String name = color "_m";
Class<?> obj;
try {
obj = Class.forName("chessui.ChessUI");
Field[] f = obj.getDeclaredFields();
for (Field field : f) {
field.setAccessible(true);
if (name.equals(field.getName())) {
Image = (BufferedImage) field.get(obj.newInstance());
}
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (IllegalArgumentException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace();
}
}
public BufferedImage getImage() {
return Image;
}
public Point getPoint() {
return point;
}
public boolean Move(Point end, List<Chessman> list) {
if (point.x == end.x && point.y == end.y) {
return true;
}
// 马走日子 且会被别马腿
// 1 . 先判断走的是不是日字
// 1.横着走
int endx;   int startx;
int starty; int endy;
// 1-1 赋值
if (end.x > this.point.x) {
startx = point.x; endx = end.x;
} else {
startx = end.x; endx = point.x;
}
if (end.y > this.point.y) {
starty = point.y; endy = end.y;
} else {
starty = end.y; endy = point.y;
}
boolean issun = false;
if ((endx - startx == 120) && ((endy - starty) == 60)) {
issun = true;
} else if (((endx - startx) == 60) && ((endy - starty) == 120)) {
issun = true;
}
if (!issun) {
return false;
}
boolean flag = true;
Point flagP = null;
if (((endx - startx) == 120) && ((endy - starty) == 60)) {
int x = (endx startx) / 2;
int y = point.y;
flagP = new Point(x, y);
for (Chessman c : list) {
Point p = c.getPoint();
if (p.x == flagP.x && p.y == flagP.y) {
flag = false;
}
}

} else {
int y = (endy starty) / 2; int x = point.x;
flagP = new Point(x, y);
for (Chessman c : list) {
Point p = c.getPoint();
if (p.x == flagP.x && p.y == flagP.y) {
flag = false;
}
}
}
System.out.println(flag);
return flag;
}
public void setPoint(Point point) {
this.point = point;
}
public boolean getName() {
return false;
}
}
class Soldier extends AbstractChessman {/*/ 卒/*/
 private String color;/*/也要有一个标记用来判断范围/*/
 public Soldier(Point start, String color) {
point = start;
String name = color "_z";
this.color = color;
Class<?> obj;
try {
obj = Class.forName("chessui.ChessUI");
Field[] f = obj.getDeclaredFields();
for (Field field : f) {
field.setAccessible(true);
if (name.equals(field.getName())) {
Image = (BufferedImage) field.get(obj.newInstance());
}
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (IllegalArgumentException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace();
}
}
public BufferedImage getImage() {return Image;}
public Point getPoint() {return point;}
public boolean Move(Point end, List<Chessman> list) {
if (point.x == end.x && point.y == end.y) { return true; }
boolean flag = false;/*/true 为 红 false 为 黑/*/
if ("r".equals(this.color)) { flag = true;
} /*/ 在看他是不是越过了边界/*/
if (flag) {
if (point.y >= 330 && point.x == end.x && point.y - end.y == 60) {
/*/ 未越过边界/*/
return true;
} else if (point.y < 330) {
if (point.x == end.x && point.y - end.y == 60) {
return true;
}
if (point.y == end.y
&& (point.x - end.x == 60 || end.x - point.x == 60)) {
return true;
}
}
} else {
if (point.y <= 270 && point.x == end.x && end.y - point.y == 60) {
return true;
} else if (point.y > 270) {
if (end.x == point.x && end.y - point.y == 60) {
return true;
}
if (point.y == end.y
&& (end.x - point.x == 60 || point.x - end.x == 60)) {
return true;
}
}
}
return false;
}
public void setPoint(Point point) { this.point = point;}
public boolean getName() { return false; }
}


标签: 象棋 源代码 游戏

实例下载地址

java 象棋源代码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警