Java版本实现中国象棋

news/2024/12/12 7:21:01/

预览效果

中国象棋

游戏介绍:中国象棋是起源于中国的一种棋,属于二人对抗性游戏的一种,在中国有着悠久的历史,由于用具简单,趣味性强,成为流行极为广泛的棋艺活动。阿巴阿巴阿巴

代码结构:Button类和Rect类是直接使用前面画板中的类,Qizi类是所有棋子的父类

 每个棋子都要继承Qizi父类并重写getNext方法,该方法是返回棋子可以前进的位置

Qizi类代码:

package demo01;import java.util.ArrayList;public abstract class Qizi {
//    //    行数(高)
//    public static int height=9;
//    //    列数(宽)
//    int width=8;
//    棋子的位置public Point point;
//    棋子的身份public String Id;
//    棋子的属于红方还是黑方public String color;//    棋子图片public String image;public Qizi() {}public Qizi(Point point, String id, String color, String image) {this.point = point;Id = id;this.color = color;this.image = image;}//    返回棋子可以前进的位置abstract Point[] getNext(Qizi[] qipan);
}//棋子車
class Ju extends Qizi{public Ju() {}public Ju(Point point, String id, String color, String image) {super(point, id, color, image);}public Point[] getNext(Qizi[] qipan) {ArrayList<Point> points=new ArrayList<Point>();Point pt=new Point(point);
//        向左搜索可以前进的格子while (true){
//            向左移动一格pt.x--;
//            判断这一格是否在棋盘内if (pt.x<0){break;}
//            判断这一格内是否有棋子Qizi tmp=null;for (int i = 0; i < qipan.length; i++) {if(pt.equals(qipan[i].point)==true){tmp=qipan[i];break;}}//            第一种情况,格子是空的
//            第二种情况,格子里面是对方棋子
//            第三种情况,格子里面是己方棋子if (tmp==null){points.add(new Point(pt));}else if(color.equals(tmp.color)==false){points.add(new Point(pt));break;}else{break;}}//        向右搜索可以前进的格子pt.set(point);while (true){
//            向右移动一格pt.x++;
//            判断这一格是否在棋盘内if (pt.x>=Chess001.width){break;}
//            判断这一格内是否有棋子Qizi tmp=null;for (int i = 0; i < qipan.length; i++) {if(pt.equals(qipan[i].point)==true){tmp=qipan[i];break;}}
//            第一种情况,格子是空的
//            第二种情况,格子里面是对方棋子
//            第三种情况,格子里面是己方棋子if (tmp==null){points.add(new Point(pt));}else if(color.equals(tmp.color)==false){points.add(new Point(pt));break;}else{break;}}//        向上搜索可以前进的格子pt.set(point);while (true){
//            向上移动一格pt.y--;
//            判断这一格是否在棋盘内if (pt.y<0){break;}
//            判断这一格内是否有棋子Qizi tmp=null;for (int i = 0; i < qipan.length; i++) {if(pt.equals(qipan[i].point)==true){tmp=qipan[i];break;}}
//            第一种情况,格子是空的
//            第二种情况,格子里面是对方棋子
//            第三种情况,格子里面是己方棋子if (tmp==null){points.add(new Point(pt));}else if(color.equals(tmp.color)==false){points.add(new Point(pt));break;}else{break;}}//        向下搜索可以前进的格子pt.set(point);while (true){
//            向下移动一格pt.y++;
//            判断这一格是否在棋盘内if (pt.y>=Chess001.height){break;}
//            判断这一格内是否有棋子Qizi tmp=null;for (int i = 0; i < qipan.length; i++) {if(pt.equals(qipan[i].point)==true){tmp=qipan[i];break;}}
//            第一种情况,格子是空的
//            第二种情况,格子里面是对方棋子
//            第三种情况,格子里面是己方棋子if (tmp==null){points.add(new Point(pt));}else if(color.equals(tmp.color)==false){points.add(new Point(pt));break;}else{break;}}Point[] points1 = new Point[points.size()];for (int i = 0; i < points.size(); i++) {points1[i]=new Point(points.get(i));}return points1;}}//棋子兵
class Bin extends Qizi{public Bin() {}public Bin(Point point, String id, String color, String image) {super(point, id, color, image);}Point[] getNext(Qizi[] qipan) {// 存储当前子可以前进的所有位置ArrayList<Point> points=new ArrayList<Point>();Point pt=new Point(point);if (color.equals("红色")==true && point.y<=4|| color.equals("黑色")==true &&point.y>4){
//        判断兵是否可以左移一格pt.x--;
//        1.该点在棋盘内if (pt.x>=0&&pt.x<Chess001.width){
//            判断该位置上是否是我方棋子boolean bTeam=false;for (int i = 0; i < qipan.length; i++) {if (qipan[i].point.equals(pt)==true){if (qipan[i].color.equals(color)==true){bTeam=true;}break;}}if (bTeam==false){//        2.该点上没有本方棋子points.add(new Point(pt));}}//        判断兵是否可以右移一格pt.set(point);pt.x++;
//        1.该点在棋盘内if (pt.x>=0&&pt.x<Chess001.width){
//            判断该位置上是否是我方棋子boolean bTeam=false;for (int i = 0; i < qipan.length; i++) {if (qipan[i].point.equals(pt)==true){if (qipan[i].color.equals(color)==true){bTeam=true;}break;}}if (bTeam==false){//        2.该点上没有本方棋子points.add(new Point(pt));}}}if (color.equals("红色")==true){//        判断兵是否可以上移一格pt.set(point);pt.y--;
//        1.该点在棋盘内if (pt.y>=0&&pt.y<Chess001.height){
//            判断该位置上是否是我方棋子boolean bTeam=false;for (int i = 0; i < qipan.length; i++) {if (qipan[i].point.equals(pt)==true){if (qipan[i].color.equals(color)==true){bTeam=true;}break;}}if (bTeam==false){//        2.该点上没有本方棋子points.add(new Point(pt));}}}//        判断兵是否可以下移一格if(color.equals("黑色")==true){pt.set(point);pt.y++;
//        1.该点在棋盘内if (pt.y>=0&&pt.y<Chess001.height){
//            判断该位置上是否是我方棋子boolean bTeam=false;for (int i = 0; i < qipan.length; i++) {if (qipan[i].point.equals(pt)==true){if (qipan[i].color.equals(color)==true){bTeam=true;}break;}}if (bTeam==false){//        2.该点上没有本方棋子points.add(new Point(pt));}}}Point[] points1 = new Point[points.size()];for (int i = 0; i < points.size(); i++) {points1[i]=new Point(points.get(i));}return points1;}
}//棋子马
class Ma extends Qizi{public Ma() {}public Ma(Point point, String id, String color, String image) {super(point, id, color, image);}Point[] getNext(Qizi[] qipan) {// 存储当前子可以前进的所有位置ArrayList<Point> points=new ArrayList<Point>();Point pt=new Point(point);
//        处理马向左跳的情况pt.x--;//判断马是否撇脚boolean bHefa=true;for (int i = 0; i < qipan.length; i++) {
//            撇脚if(pt.equals(qipan[i].point)==true){bHefa=false;break;}}
//        没撇脚if (bHefa==true){pt.set(point);pt.x-=2;pt.y-=1;
//            判断目标点是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (qipan[i].color.equals(color)==true){bHefa=false;}break;}}// 判断目标点是否在棋盘里面if (bHefa==true){if (pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<Chess001.height){points.add(new Point(pt));}}pt.set(point);pt.x-=2;pt.y+=1;bHefa=true;//            判断目标点是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (qipan[i].color.equals(color)==true){bHefa=false;}break;}}// 判断目标点是否在棋盘里面if (bHefa==true){if (pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<Chess001.height){points.add(new Point(pt));}}}//
//        处理马向右跳的情况pt.set(point);pt.x++;//判断马是否撇脚bHefa=true;for (int i = 0; i < qipan.length; i++) {
//            撇脚if(pt.equals(qipan[i].point)==true){bHefa=false;break;}}
//        没撇脚if (bHefa==true){pt.set(point);pt.x+=2;pt.y-=1;
//            判断目标点是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (qipan[i].color.equals(color)==true){bHefa=false;}break;}}// 判断目标点是否在棋盘里面if (bHefa==true){if (pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<Chess001.height){points.add(new Point(pt));}}pt.set(point);pt.x+=2;pt.y+=1;bHefa=true;//            判断目标点是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (qipan[i].color.equals(color)==true){bHefa=false;}break;}}// 判断目标点是否在棋盘里面if (bHefa==true){if (pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<Chess001.height){points.add(new Point(pt));}}}//        处理马向上跳的情况pt.set(point);pt.y--;//判断马是否撇脚bHefa=true;for (int i = 0; i < qipan.length; i++) {
//            撇脚if(pt.equals(qipan[i].point)==true){bHefa=false;break;}}
//        没撇脚if (bHefa==true){pt.set(point);pt.x-=1;pt.y-=2;
//            判断目标点是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (qipan[i].color.equals(color)==true){bHefa=false;}break;}}// 判断目标点是否在棋盘里面if (bHefa==true){if (pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<Chess001.height){points.add(new Point(pt));}}pt.set(point);pt.x+=1;pt.y-=2;bHefa=true;//            判断目标点是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (qipan[i].color.equals(color)==true){bHefa=false;}break;}}// 判断目标点是否在棋盘里面if (bHefa==true){if (pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<Chess001.height){points.add(new Point(pt));}}}//        处理马向下跳的情况pt.set(point);pt.y++;//判断马是否撇脚bHefa=true;for (int i = 0; i < qipan.length; i++) {
//            撇脚if(pt.equals(qipan[i].point)==true){bHefa=false;break;}}
//        没撇脚if (bHefa==true){pt.set(point);pt.x-=1;pt.y+=2;
//            判断目标点是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (qipan[i].color.equals(color)==true){bHefa=false;}break;}}// 判断目标点是否在棋盘里面if (bHefa==true){if (pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<Chess001.height){points.add(new Point(pt));}}pt.set(point);pt.x+=1;pt.y+=2;bHefa=true;//            判断目标点是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (qipan[i].color.equals(color)==true){bHefa=false;}break;}}// 判断目标点是否在棋盘里面if (bHefa==true){if (pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<Chess001.height){points.add(new Point(pt));}}}//Point[] points1 = new Point[points.size()];for (int i = 0; i < points.size(); i++) {points1[i]=new Point(points.get(i));}return points1;}
}//棋子象
class Xiang extends Qizi{public Xiang() {}public Xiang(Point point, String id, String color, String image) {super(point, id, color, image);}Point[] getNext(Qizi[] qipan) {// 存储当前子可以前进的所有位置ArrayList<Point> points=new ArrayList<Point>();Point pt=new Point(point);boolean bHefa=true;//            处理象向左上走
//            判断是否压象眼pt.x--;pt.y--;for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){bHefa=false;break;}}if (bHefa==true){//判断目标位置是否有本方棋子pt.set(point);pt.x-=2;pt.y-=2;
//            判断目标位置是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (color.equals(qipan[i].color)==true){bHefa=false;}break;}}//判断目标点是否在棋盘内if (bHefa==true){if (color.equals("黑色")==true){if(pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<=4){points.add(new Point(pt));}}else{if(pt.x>=0&&pt.x<Chess001.width&&pt.y>=5&&pt.y<Chess001.height){points.add(new Point(pt));}}}}//            处理象向右上走pt.set(point);bHefa=true;
//            判断是否压象眼pt.x++;pt.y--;for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){bHefa=false;break;}}if (bHefa==true){//判断目标位置是否有本方棋子pt.set(point);pt.x+=2;pt.y-=2;
//            判断目标位置是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (color.equals(qipan[i].color)==true){bHefa=false;}break;}}//判断目标点是否在棋盘内if (bHefa==true){if (color.equals("黑色")==true){if(pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<=4){points.add(new Point(pt));}}else{if(pt.x>=0&&pt.x<Chess001.width&&pt.y>=5&&pt.y<Chess001.height){points.add(new Point(pt));}}}}//            处理象向左下走pt.set(point);bHefa=true;
//            判断是否压象眼pt.x--;pt.y++;for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){bHefa=false;break;}}if (bHefa==true){//判断目标位置是否有本方棋子pt.set(point);pt.x-=2;pt.y+=2;
//            判断目标位置是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (color.equals(qipan[i].color)==true){bHefa=false;}break;}}//判断目标点是否在棋盘内if (bHefa==true){if (color.equals("黑色")==true){if(pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<=4){points.add(new Point(pt));}}else{if(pt.x>=0&&pt.x<Chess001.width&&pt.y>=5&&pt.y<Chess001.height){points.add(new Point(pt));}}}}//            处理象向右下走pt.set(point);bHefa=true;
//            判断是否压象眼pt.x++;pt.y++;for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){bHefa=false;break;}}if (bHefa==true){//判断目标位置是否有本方棋子pt.set(point);pt.x+=2;pt.y+=2;
//            判断目标位置是否有本方棋子for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){if (color.equals(qipan[i].color)==true){bHefa=false;}break;}}//判断目标点是否在棋盘内if (bHefa==true){if (color.equals("黑色")==true){if(pt.x>=0&&pt.x<Chess001.width&&pt.y>=0&&pt.y<=4){points.add(new Point(pt));}}else{if(pt.x>=0&&pt.x<Chess001.width&&pt.y>=5&&pt.y<Chess001.height){points.add(new Point(pt));}}}}//Point[] points1 = new Point[points.size()];for (int i = 0; i < points.size(); i++) {points1[i]=new Point(points.get(i));}return points1;}
}//棋子士
class Shi extends Qizi{public Shi() {}public Shi(Point point, String id, String color, String image) {super(point, id, color, image);}Point[] getNext(Qizi[] qipan) {// 存储当前子可以前进的所有位置ArrayList<Point> points=new ArrayList<Point>();Point pt=new Point(point);boolean bHefa=true;Point []ptMubiao={new Point(point.x-1,point.y-1),new Point(point.x+1,point.y+1),new Point(point.x+1,point.y-1),new Point(point.x-1,point.y+1)};for (int i = 0; i < ptMubiao.length; i++) {bHefa=true;
//            判断目标点是否有本方棋子for (int i1 = 0; i1 < qipan.length; i1++) {if (ptMubiao[i].equals(qipan[i1].point)==true){if (color.equals(qipan[i1].color)==true){bHefa=false;}break;}}if(bHefa == true){//            判断目标点是否在范围内if (color.equals("黑色")==true){if (ptMubiao[i].x>=3&&ptMubiao[i].x<=5&&ptMubiao[i].y>=0&&ptMubiao[i].y<=2){points.add(new Point(ptMubiao[i]));}}else {if (ptMubiao[i].x>=3&&ptMubiao[i].x<=5&&ptMubiao[i].y>=7&&ptMubiao[i].y<=9){points.add(new Point(ptMubiao[i]));}}}}//Point[] points1 = new Point[points.size()];for (int i = 0; i < points.size(); i++) {points1[i]=new Point(points.get(i));}return points1;}
}//棋子将class Jiang extends Qizi{public Jiang() {}public Jiang(Point point, String id, String color, String image) {super(point, id, color, image);}Point[] getNext(Qizi[] qipan) {// 存储当前子可以前进的所有位置ArrayList<Point> points=new ArrayList<Point>();Point pt=new Point(point);boolean bHefa=true;Point []ptMubiao={new Point(point.x,point.y-1),new Point(point.x,point.y+1),new Point(point.x+1,point.y),new Point(point.x-1,point.y)};for (int i = 0; i < ptMubiao.length; i++) {bHefa=true;
//            判断目标点是否有本方棋子for (int i1 = 0; i1 < qipan.length; i1++) {if (ptMubiao[i].equals(qipan[i1].point)==true){if (color.equals(qipan[i1].color)==true){bHefa=false;}break;}}if(bHefa == true){//            判断目标点是否在范围内if (color.equals("黑色")==true){if (ptMubiao[i].x>=3&&ptMubiao[i].x<=5&&ptMubiao[i].y>=0&&ptMubiao[i].y<=2){points.add(new Point(ptMubiao[i]));}}else {if (ptMubiao[i].x>=3&&ptMubiao[i].x<=5&&ptMubiao[i].y>=7&&ptMubiao[i].y<=9){points.add(new Point(ptMubiao[i]));}}}}//Point[] points1 = new Point[points.size()];for (int i = 0; i < points.size(); i++) {points1[i]=new Point(points.get(i));}return points1;}
}//棋子炮
class Pao extends Qizi{public Pao() {}public Pao(Point point, String id, String color, String image) {super(point, id, color, image);}Point[] getNext(Qizi[] qipan) {// 存储当前子可以前进的所有位置ArrayList<Point> points=new ArrayList<Point>();Point pt=new Point(point);//      处理炮向左boolean bEat=false;while (true){pt.x--;//如果超出范围直接退出if (pt.x<0||pt.x>=Chess001.width||pt.y<0||pt.y>=Chess001.width){break;}boolean bYouzi=false;//目标格子是否有棋子存在boolean bFriend=false;//目标格子是否有本方棋子存在for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){bYouzi=true;if (color.equals(qipan[i].color)==true){bFriend=true;}break;}}if (bEat==false){
//                第一阶段:移动棋子//如果目标是空格的话把该该格子加到数组中if (bYouzi==false){points.add(new Point(pt));}else {//                如果目标格子有棋子,那么进入到第二该段bEat=true;}//                第二该段:吃掉棋子}else{if(bYouzi==false){}else{//如果遇到对方棋子就吃掉if (bFriend==true ){break;}else{//如果遇到本方棋子就退出points.add(new Point(pt));break;}}}}//      处理炮向右pt.set(point);
//        bEat false表示第一阶段:移动阶段
//                true表示第二阶段:吃子阶段bEat=false;while (true){pt.x++;//如果超出范围直接退出if (pt.x<0||pt.x>=Chess001.width||pt.y<0||pt.y>=Chess001.width){break;}boolean bYouzi=false;//目标格子是否有棋子存在boolean bFriend=false;//目标格子是否有本方棋子存在for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){bYouzi=true;if (color.equals(qipan[i].color)==true){bFriend=true;}break;}}if (bEat==false){
//                第一阶段:移动棋子//如果目标是空格的话把该该格子加到数组中if (bYouzi==false){points.add(new Point(pt));}else {//                如果目标格子有棋子,那么进入到第二该段bEat=true;}//                第二该段:吃掉棋子}else{if(bYouzi==false){}else{//如果遇到对方棋子就吃掉if (bFriend==true ){break;}else{//如果遇到本方棋子就退出points.add(new Point(pt));break;}}}}//      处理炮向上pt.set(point);
//        bEat false表示第一阶段:移动阶段
//                true表示第二阶段:吃子阶段bEat=false;while (true){pt.y--;//如果超出范围直接退出if (pt.x<0||pt.x>=Chess001.width||pt.y<0||pt.y>=Chess001.width){break;}boolean bYouzi=false;//目标格子是否有棋子存在boolean bFriend=false;//目标格子是否有本方棋子存在for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){bYouzi=true;if (color.equals(qipan[i].color)==true){bFriend=true;}break;}}if (bEat==false){
//                第一阶段:移动棋子//如果目标是空格的话把该该格子加到数组中if (bYouzi==false){points.add(new Point(pt));}else {//                如果目标格子有棋子,那么进入到第二该段bEat=true;}//                第二该段:吃掉棋子}else{if(bYouzi==false){}else{//如果遇到对方棋子就吃掉if (bFriend==true ){break;}else{//如果遇到本方棋子就退出points.add(new Point(pt));break;}}}}//      处理炮向下pt.set(point);
//        bEat false表示第一阶段:移动阶段
//                true表示第二阶段:吃子阶段bEat=false;while (true){pt.y++;//如果超出范围直接退出if (pt.x<0||pt.x>=Chess001.width||pt.y<0||pt.y>=Chess001.width){break;}boolean bYouzi=false;//目标格子是否有棋子存在boolean bFriend=false;//目标格子是否有本方棋子存在for (int i = 0; i < qipan.length; i++) {if (pt.equals(qipan[i].point)==true){bYouzi=true;if (color.equals(qipan[i].color)==true){bFriend=true;}break;}}if (bEat==false){
//                第一阶段:移动棋子//如果目标是空格的话把该该格子加到数组中if (bYouzi==false){points.add(new Point(pt));}else {//                如果目标格子有棋子,那么进入到第二该段bEat=true;}//                第二该段:吃掉棋子}else{if(bYouzi==false){}else{//如果遇到对方棋子就吃掉if (bFriend==true ){break;}else{//如果遇到本方棋子就退出points.add(new Point(pt));break;}}}}//Point[] points1 = new Point[points.size()];for (int i = 0; i < points.size(); i++) {points1[i]=new Point(points.get(i));}return points1;}
}

Chess类代码

package demo01;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;public class Chess002 extends JFrame implements KeyListener, ActionListener, MouseListener {//    行数(高)public static int height=10;//    列数(宽)public static int width=9;//    格子的大小public static int size=86;//    标题栏的高度public static int title=113-43-5;public  static int left =75-43;public static int right=35+300;public  static int bottom=35;Button[] button=new Button[2];//     棋盘中所有的棋子Qizi []qipan=new Qizi[32];//    设置图片private Image image=null;Graphics gf=null;//  设置棋盘Image image1=Toolkit.getDefaultToolkit().getImage("C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\main.gif");//    当前选中的棋子,默认为空Qizi select=null;//    游戏状态 0表示等待红方走 1表示黑方走 2表示红方胜 3表示黑方胜int zhuangtai=0;public void Tian(){button[0]=new Button(new Rect(110*8,title+1*size,200,100),"重新开始");button[1]=new Button(new Rect(110*8,title+3*size,200,100),"红方请落子");button[1].Text=Color.red;button[1].Back=Color.WHITE;button[0].Back=Color.WHITE;qipan[0]=new Ju(new Point(0,0),"車","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑车.gif");qipan[1]=new Ju(new Point(8,0),"車","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑车.gif");qipan[2]=new Ju(new Point(0,9),"車","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红车.gif");qipan[3]=new Ju(new Point(8,9),"車","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红车.gif");qipan[4]=new Bin(new Point(0,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[5]=new Bin(new Point(2,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[6]=new Bin(new Point(4,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[7]=new Bin(new Point(6,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[8]=new Bin(new Point(8,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[9]=new Bin(new Point(0,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[10]=new Bin(new Point(2,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[11]=new Bin(new Point(4,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[12]=new Bin(new Point(6,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[13]=new Bin(new Point(8,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[14]=new Ma(new Point(1,0),"马","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑马.gif");qipan[15]=new Ma(new Point(7,0),"马","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑马.gif");qipan[16]=new Ma(new Point(1,9),"马","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红马.gif");qipan[17]=new Ma(new Point(7,9),"马","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红马.gif");qipan[18]=new Xiang(new Point(2,0),"象","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑象.gif");qipan[19]=new Xiang(new Point(6,0),"象","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑象.gif");qipan[20]=new Xiang(new Point(2,9),"象","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红象.gif");qipan[21]=new Xiang(new Point(6,9),"象","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红象.gif");qipan[22]=new Shi(new Point(3,0),"士","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑士.gif");qipan[23]=new Shi(new Point(5,0),"士","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑士.gif");qipan[24]=new Shi(new Point(3,9),"士","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红士.gif");qipan[25]=new Shi(new Point(5,9),"士","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红士.gif");qipan[26]=new Jiang(new Point(4,0),"将","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑将.gif");qipan[27]=new Jiang(new Point(4,9),"将","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红将.gif");qipan[28]=new Pao(new Point(1,2),"炮","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑炮.gif");qipan[29]=new Pao(new Point(7,2),"炮","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑炮.gif");qipan[30]=new Pao(new Point(1,7),"炮","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红炮.gif");qipan[31]=new Pao(new Point(7,7),"炮","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红炮.gif");//设置窗口的宽高this.setSize(width*size+left+right ,height*size+title+bottom);
//        设置标题this.setTitle("中国象棋");//设置窗口能看见this.setVisible(true);
//        设置窗口顶点坐标的位置this.setLocation(100,10);//设置鼠标监听事件(也就是当鼠标有什么反应都会调用此函数)this.addMouseListener(this);}@Overridepublic void paint(Graphics g) {//初始化临时图片//解决闪烁问题if(image==null){image=this.createImage(width*size+left+right,height*size+title+bottom);}if(this.gf ==null){this.gf =image.getGraphics();}//        擦除背景gf.setColor(Color.gray);gf.fill3DRect(0,0,width*size,title+height*size,true);//        绘制背景gf.setColor(Color.orange);gf.fill3DRect(0,title+0,width*size,title+height*size,true);//      画棋盘int w = (int)(558.0*1.5);int h = (int)(620*1.5);gf.drawImage(image1,0,30,w,h,null);//        画棋子for (int i = 0; i < qipan.length; i++) {Image image=Toolkit.getDefaultToolkit().getImage(qipan[i].image);gf.drawImage(image,qipan[i].point.x*size+left,qipan[i].point.y*size+title,size,size,null);}if(select!=null){// 标注选中的棋子gf.setColor(Color.cyan);gf.drawRect(select.point.x*size+left,select.point.y*size+title,size,size);// 绘制出该棋子可以移动的位置Point [] points=select.getNext(qipan);for (int i1 = 0; i1 < points.length; i1++) {gf.setColor(new Color(2, 145, 219, 250));
//                    gf.fillOval(points[i1].x*size+left,points[i1].y*size+title,size,size);gf.fillOval(points[i1].x*size+left+(size-20)/2,points[i1].y*size+title+(size-20)/2,20,20);}}//        绘制按钮for (int i = 0; i < button.length; i++) {
//            绘制按钮选中颜色if (button[i].bXuanzhong==true){gf.setColor(Color.pink);gf.fillRect(button[i].rect.x,button[i].rect.y,button[i].rect.width,button[i].rect.height);//                绘制按钮为选中时的颜色}else {gf.setColor(button[i].Back);gf.fillRect(button[i].rect.x,button[i].rect.y,button[i].rect.width,button[i].rect.height);}
//           当按钮是悬停状态时,字体颜色设置为白色if(button[i].bXuanTing==true){gf.setColor(Color.white);//           当按钮是非悬停状态时,字体颜色设置为黑色}else{gf.setColor(button[i].Text);}gf.setFont(new Font("华文新魏", 10, 35)); //设置字体gf.drawString(button[i].name,button[i].rect.x+(button[i].rect.width-button[i].name.length()*35)/2,button[i].rect.y+(button[i].rect.height+35/2)/2);}g.drawImage(image,0,0,null);}public void actionPerformed(ActionEvent e) {}public void keyTyped(KeyEvent e) {}public void keyPressed(KeyEvent e) {}public void keyReleased(KeyEvent e) {}public void mouseClicked(MouseEvent e) {}//    鼠标按压事件public void mousePressed(MouseEvent e) {//        处理鼠标左键if(e.getButton()==e.BUTTON1) {
//            鼠标点击的真实坐标int realx = e.getX();int realy = e.getY();//            被点击的按钮, 空表示没有按钮被点击, 如果点击了某个按钮, 那么name就记录按钮的名字String name="";//            判断鼠标左键按压的地方在不在按钮内部for (int i = 0; i < button.length; i++) {if(button[i].isInButton(realx,realy)==true){
//                    记录鼠标左键按压按钮的名字name=button[i].name;break;}}//            处理各种按钮的按压事件if ("重新开始".equals(name) == true) {System.out.println(name);qipan = new Qizi[32];qipan[0]=new Ju(new Point(0,0),"車","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑车.gif");qipan[1]=new Ju(new Point(8,0),"車","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑车.gif");qipan[2]=new Ju(new Point(0,9),"車","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红车.gif");qipan[3]=new Ju(new Point(8,9),"車","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红车.gif");qipan[4]=new Bin(new Point(0,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[5]=new Bin(new Point(2,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[6]=new Bin(new Point(4,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[7]=new Bin(new Point(6,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[8]=new Bin(new Point(8,3),"兵","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑卒.gif");qipan[9]=new Bin(new Point(0,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[10]=new Bin(new Point(2,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[11]=new Bin(new Point(4,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[12]=new Bin(new Point(6,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[13]=new Bin(new Point(8,6),"兵","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红卒.gif");qipan[14]=new Ma(new Point(1,0),"马","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑马.gif");qipan[15]=new Ma(new Point(7,0),"马","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑马.gif");qipan[16]=new Ma(new Point(1,9),"马","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红马.gif");qipan[17]=new Ma(new Point(7,9),"马","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红马.gif");qipan[18]=new Xiang(new Point(2,0),"象","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑象.gif");qipan[19]=new Xiang(new Point(6,0),"象","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑象.gif");qipan[20]=new Xiang(new Point(2,9),"象","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红象.gif");qipan[21]=new Xiang(new Point(6,9),"象","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红象.gif");qipan[22]=new Shi(new Point(3,0),"士","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑士.gif");qipan[23]=new Shi(new Point(5,0),"士","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑士.gif");qipan[24]=new Shi(new Point(3,9),"士","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红士.gif");qipan[25]=new Shi(new Point(5,9),"士","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红士.gif");qipan[26]=new Jiang(new Point(4,0),"将","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑将.gif");qipan[27]=new Jiang(new Point(4,9),"将","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红将.gif");qipan[28]=new Pao(new Point(1,2),"炮","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑炮.gif");qipan[29]=new Pao(new Point(7,2),"炮","黑色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\黑炮.gif");qipan[30]=new Pao(new Point(1,7),"炮","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红炮.gif");qipan[31]=new Pao(new Point(7,7),"炮","红色","C:\\Users\\meisir666\\Desktop\\象棋\\src\\main\\java\\demo01\\image\\红炮.gif");//  button[1]=new Button(new Rect(110*8,title+3*size,200,100),"红方请落子");button[1].Text=Color.red;button[1].name = "红方请落子";select = null;zhuangtai = 0;repaint();return;}Point ptGezi=new Point(-1,-1);
//            int gezix=-1;
//            int geziy=-1;//            将真是坐标转换为格子坐标if(realx<0+left ||realx>width*size+left||realy<title||realy>height*size+title){select=null;}else {//说明这个点在棋盘里ptGezi.x=(realx-left)/size;ptGezi.y=(realy-title)/size;if(select==null){// 如果点中了落子方的棋子那么将该子设置为选中状态Qizi click=null;//                    遍历棋子,测试是否点中棋子for (int i = 0; i < qipan.length; i++) {if (ptGezi.equals(qipan[i].point)==true){click=qipan[i];break;}}//                    选中棋子if (click!=null){if (zhuangtai==0&&click.color.equals("红色")||zhuangtai==1&&click.color.equals("黑色")){select=click;}}}else {
//                  获取选中棋子,可以前进的位置Point[] points=select.getNext(qipan);// 第一种情况,目标点是可以前进的位置// 如果该位置上有对方的棋子,那么就吃掉对方棋子boolean bGo = false;for (int i = 0; i < points.length; i++) {if (points[i].equals(ptGezi)==true){bGo=true;break;}}// 第二个点,是可以前进的位置if(bGo==true){Qizi qzEat = null;ArrayList<Qizi> list = new ArrayList<Qizi>();for (int i = 0; i < qipan.length; i++) {if( qipan[i].point.equals(ptGezi)==true) {// 吃掉对方的棋子qzEat = qipan[i];}else {list.add(qipan[i]);}}if(qzEat == null){select.point.set(ptGezi);select=null;}else {// 删除被吃掉的棋子qipan = new Qizi[list.size()];for (int i = 0; i < qipan.length; i++) {qipan[i] = list.get(i);}// 移动选中的棋子select.point.set(qzEat.point);select=null;// 判断被吃掉的棋子是否是将,如果是的话,就输了if(qzEat.Id.equals("将") == true){if(qzEat.color.equals("黑色")==true) {zhuangtai =2;button[1].name="红方胜";button[1].Text=Color.red;} else{zhuangtai =3;button[1].name="黑方胜";button[1].Text=Color.black;}}}if(zhuangtai ==0){zhuangtai = 1;button[1].name="黑方请落子";button[1].Text=Color.black;}else if(zhuangtai ==1){zhuangtai = 0;button[1].name="红方请落子";button[1].Text=Color.red;}// 第二个,不是可以前进的位置}else {select = null;// 如果点击的位置为本方棋子,那么继续选中这个棋子for (int i = 0; i < qipan.length; i++) {if (ptGezi.equals(qipan[i].point)==true){if (zhuangtai==0&&qipan[i].color.equals("红色")|| zhuangtai==1&&qipan[i].color.equals("黑色")){select=qipan[i];break;}}}}// 第二种情况,目标点不在可前进的位置//  如果选中了,自己方的棋子,则把该棋子设置为选中桩体}}}repaint();}public void mouseReleased(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public static void main(String[] args) {Chess002 chess=new Chess002();chess.Tian();}}


http://www.ppmy.cn/news/185103.html

相关文章

三种版本的中国象棋

一.图片资源&#xff1a; 二.图片存放位置&#xff1a; 三.三种不同版本的中国象棋源代码 三种源代码运行之前都需要 点击项目-属性 找到这个地方&#xff0c;把字符集改成“使用多字节字符集”&#xff1a; 一.版本1&#xff1a;中国象棋简洁版&#xff08;部分特效无…

中国象棋【附源码】

中国象棋【附源码】 我的网站已经上线了 http://javapub.net.cn/ 博主介绍&#xff1a; &#x1f680;自媒体 JavaPub 独立维护人&#xff0c;全网粉丝15w&#xff0c;csdn博客专家、java领域优质创作者&#xff0c;51ctoTOP10博主&#xff0c;知乎/掘金/华为云/阿里云/InfoQ等…

暗棋单机版_中国象棋暗棋下载_中国象棋暗棋安卓版下载 v1.6.8 安卓单机版_飞飞巴士下载...

介绍 中国象棋,博大精深,老一代人留下在的文化遗产,它的精髓所在决定了它注定要流芳百世。在中国人的眼里,那就是谁不会玩象棋?你不会玩象棋,你爸妈知道吗? 棋艺没有最好,只有更好,永无止境。想更好的提升自己的棋艺,而有没有合适的练手?那今天小编推出的一款掌上象…

使用C++写的中国象棋(单机版)

C在游戏领域应用很广&#xff0c;其最大有点是运行效率高&#xff0c;对内存的操作比较直接&#xff0c;因此&#xff0c;我利用此有点写了中国象棋的简洁版&#xff0c;来进行一些简单的下棋操作。接下来是代码 #ifndef PUTTEXT_H_ #define PUTTEXT_H_ #include <windows.…

网络中国象棋小游戏的实现

开学了&#xff0c;去图书馆借了几本书&#xff0c;没有找到想要的C网络编程&#xff0c;倒是找到了几本LINUX的书&#xff0c;以及一本《Visual C#经典游戏编程开发》。翻了翻发现里面有个可以联网对弈的中国象棋游戏&#xff0c;一直写ASP.NET的网页&#xff0c;也想试着写写…

c#开发技术 中国象棋(单机)

前言 该中国象棋为单机版程序&#xff0c;实现了全部的象棋走棋&#xff0c;并且实现了悔棋&#xff0c;落子标记等功能。唯一不足的是&#xff0c;该程序没有实现联机功能&#xff0c;后续有时间持续跟进的。 界面效果图 图1 部分代码 //棋子的行走规则 public bool RulesFo…

Java实现中国象棋(人机对战)

目录 简介 成品视频 实现思路 界面实现分为了三块 棋盘抽象类 按钮组抽象类 棋子绘制接口 棋盘界面实现 棋子的实现 按钮组的实现 监听工厂和监听类 棋盘绘制类的实现 开始游戏实现 停止游戏实现 游戏抽象类 游戏实现类 可走路线和吃棋判断实现 车(ju) 炮 …

C++中国象棋

ssdut c的大作业&#xff0c;在控制台的界面实现人人对弈&#xff0c;比较适合初学&#xff0c;自己设计了一些简单算法&#xff0c;两百多行完成。 以下正文&#xff1a; 完成中国象棋游戏&#xff0c;实现如下功能&#xff1a; 1.实现人与人之间象棋的对弈。 2.每次走子之…