Java课程设计—中国象棋(源代码及视频)「建议收藏」

谢谢大家的支持,您的一键三连是罡罡同学前进的最大动力!一键三连一键三连一键三连一键三连一键三连一键三连传送地址数据结构课程设计——校园导游系统(C语言):B站视频:https://www.bilibili.com/video/BV1ui4y137X9CSDN源码:https://blog.csdn.net/m0_46625346/article/details/107149473Java课程设计——中国象棋B站视频:https://www.bilibili

大家好,又见面了,我是你们的朋友全栈君。

在这里插入图片描述
你好,我是罡罡同学!
代码谱第一页忘掉心上人,最后一页。。。。。。

打赏一点钱,帮我买包辣条,继续创作,谢谢大家!

在这里插入图片描述

大家关注微信公众号 罡罡同学 回复中国象棋

就可以免费获得压缩包啦!!

压缩包里面内容: 1.源代码 2. 答辩PPT 3.课程设计报告 (大家可以参考)

	先说一下,大家需要将本项目部署到本地应该如何操作,CSDN上面有源代码,
	但为了让大家能够更好的使程序成功在本地运行,
	我将代码压缩包发到了网盘上,大家可以直接下载压缩包。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

传送地址

数据结构课程设计——校园导游系统(C语言):
B站视频https://www.bilibili.com/video/BV1ui4y137X9
CSDN源码:
https://blog.csdn.net/m0_46625346/article/details/107149473

Java课程设计——中国象棋
B站视频:https://www.bilibili.com/video/BV1MV411t71f
CSDN源码:https://blog.csdn.net/m0_46625346/article/details/112257325

准备

大家关注微信公众号 罡罡同学 回复中国象棋

就可以免费获得压缩包啦!!

导入项目

大家解开压缩包,会得到一个MyChess的文件夹,大家用Eclipse打开就可以了,但有些同学可能不会,在这说一下,如何导入一个项目。
1.打开Eclipse时,选择MyChess文件夹所在位置。
2.点击左上角的File,选择 import 选项。
3.在import 弹窗中,点击General 文件夹,
再点击 第二个 Existing Projects into Workspace ,
4.再点击 右侧的 Browse 选项,找到 MyChess文件夹所在位置,选中即可。
导入成功!

修改Eclipse代码字体

1.修改代码字体,修改为程序员必备字体(老师说的 哈哈)
点击标题栏中的 Windows –>Preferences –>
General –> Appearance –> Colors and Fonts 然后在右侧中,
找到Java文件夹,
再选中 Java Editor Text Font (set to default: Text Font)
推荐字体 Consals 常规 14号大小。
2.显示代码行数
点击标题栏中的 Windows –>Preferences –>
General –> Editors 中Text Editors 会看到很多行选项
我们选择 Show line numbers 选择项
3.设置代码提示
点击标题栏中的 Windows –>Preferences –>
Java –> Editor –> 点击Content Assist
在右侧中下面 有 Auto activation triggers for Java: .
在里面输入 从A到Z 和 a到z 即可
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

到目前基本准备操作已经完成。
下面正式开始介绍中国象棋。

中国象棋

下面是 中国象棋 MyChess 的结构框图(架构是老师帮忙写的)
在这里插入图片描述
所有棋子 继承一个父类 org.chess 中的 chess.java
Canvas.java 主要负责 棋盘的绘制,棋子的拜访,棋盘初始化。

ChessGame.java 主要负责 框图的初始化,按钮以及按钮事件的建立。

ChessManager.java 主要负责 注册各类棋子,并且区分 黑红棋子。

ArmsChess.java 主要负责 卒和兵的移动规则。

CannonChess.java 主要负责 炮的移动规则(不区分黑红方)

ChapChess.java 主要负责 仕、士的移动规则

Chess.java 所有棋子的父类,所有棋子继承自该类

ElephantChess.java 主要负责 象的移动规则

GeneralChess.java 主要负责 将、帅的移动规则

HorseChess.java 主要负责 马 的移动规则

ImageTool.java 主要负责 加载所有棋子的图片

musicStuff.java 主要负责 音乐的循环播放(此处需要说明一下,该功能只能循环播放音乐,不能暂停,大家可以修改此功能)

RangeChecker.java 主要负责 判断所有棋子的走法是否合法。

Test文件夹中 是所有棋子的测试类,用来测试各类棋子的移动规则是否正确

image文件夹中是 所有棋子和棋盘的图片(来自网络)

music文件夹中是 音乐(高山流水),此处要注意文件格式为wav格式的。当然,主要还是看大家如何实现这个音乐播放器,不同的方法对于格式要求不同。此模块,是我们组的一个同学在网上学的比较简单的一个方法。

下图是MyChess文件下的所有内容 :
在这里插入图片描述
谢谢大家的支持,您的一键三连是 罡罡同学前进的最大动力!
一键三连 一键三连 一键三连 一键三连 一键三连 一键三连

废话不多说,直接上代码(下面是一大波代码 哈哈):
1.ChessGame.java 代码:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.chess.musicStuff;
/*2020-第一组-中国象棋项目分工 * 姜权罡(组长):炮、马、架构、图形化界面、视频讲解、报告(结构和代码和总结)、 * PPT代码和总结反思部分、所有工作的审核。 * * 庞义俊:车、音乐、图片搜集、绘制结构图。 * 董玟彤:象、PPT。 * 李若彤:将、士、PPT。 * 曹佳榕:卒、PPT、报告(项目介绍) * * 2020/12/22 所有工作全部完成! * */
public class ChessGame { 
   
	public static void main(String[] args) { 
   
		JFrame f =new JFrame("中国象棋:观棋不语真君子,棋死无悔大丈夫");
		ImageIcon icon = new ImageIcon("image\\红将.gif");
		f.setIconImage(icon.getImage());
		
		f.setSize(730,750);
		f.setLocationRelativeTo(null);
		f.setLayout(new BorderLayout());//学习一下BorderLayout()布局管理器,把按钮放进去。
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final Canvas c=  new Canvas();
		f.add(c);
		
		JButton newStartGame = new JButton("新游戏"); 
		JButton startMusic = new JButton("播放音乐");
		JButton ExitGame = new JButton("退出游戏");
		
		//为新游戏按钮 添加事件
		newStartGame.addActionListener(new ActionListener(){ 
   
			@Override
			public void actionPerformed(ActionEvent e) { 
   
				// TODO Auto-generated method stub
				c.initMap();//新游戏
				c.repaint();//新游戏后,出棋先后顺序互换
				c.select_c = -1;
				c.select_r = -1;
				c.isBlack=!c.isBlack;
				//System.out.println("点击\n");
			}
		});
		
		//为退出按钮 添加事件
		ExitGame.addActionListener(new ActionListener(){ 
   
			@Override
			public void actionPerformed(ActionEvent e) { 
   
				// TODO Auto-generated method stub
				int j=JOptionPane.showConfirmDialog(null, "真的要退出吗?","退出",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
				if(j==JOptionPane.YES_OPTION){ 
   
					System.exit(0);
				}
			}
		});
		
		//为音乐播放按钮 添加事件
		startMusic.addActionListener(new ActionListener(){ 
   
			@Override
			public void actionPerformed(ActionEvent e) { 
   
				// TODO Auto-generated method stub
				String filepath = "music\\music.wav";
				musicStuff musicObject = new musicStuff();
				musicObject.playMusic(filepath);//音乐播放
			}
			
		});
		//f.setIconImage(new ImageIcon("红将.gif").getImage());
		f.add(newStartGame,BorderLayout.WEST);
		f.add(startMusic,BorderLayout.EAST);
		f.add(ExitGame,BorderLayout.SOUTH);
		f.setVisible(true);
	}
}

2.Canvas.java 代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.xml.bind.Marshaller.Listener;

import org.chess.Chess;
import org.chess.ImageTool;
import org.chess.RangeChecker;

import sun.tools.jar.resources.jar;

import com.sun.org.apache.bcel.internal.generic.NEW;
public class Canvas extends JPanel { 
   
	public static final int row=10;//棋盘行数
	public static final int line=9;//棋盘列数
	int[][] map = new int[row][line];
	JLabel Icon;
	Image chessimage = null;
	Font  font;
	public static final int BLACK = 1000;
	public static final int WHITE = 0;
	
	boolean isBlack = false; //当前是谁下子
	int select_c = -1;
	int select_r = -1;
	
	public Canvas(){ 
   //棋盘为10行9列,默认就好,不用改
		//chessimage = new ImageIcon("黑车.gif").getImage();
		font = new Font("宋体",Font.BOLD,30);
		initMap();
		addMouseListener(new MouseAdapter(){ 
   
			@Override
			public void mouseClicked(MouseEvent e){ 
   
				//super.mouseClicked(e);
				//System.out.println(e.getButton());
				if(e.getButton()==MouseEvent.BUTTON1){ 
   
					int x = e.getX();
					int y = e.getY();
					//System.out.println(e.getX()+","+e.getY()+"["+x+","+y+"]");
					int r = (y - 25) /58;
					int c = (x - 25) /58;
					//System.out.println("r = "+r+", c = "+c);
					//选子
					if(select_c == -1&&map[r][c]!=0){ 
   
						if(RangeChecker.selectInRange(map,r,c,isBlack)){ 
   //isBlack 是 红方
							select_c = c;
							select_r = r;
							repaint();
						}
						else{ 
   
							showFail("请选择己方棋子");
						}
						return ;// 选子阶段结束
					}
					
					boolean valid  = RangeChecker.inRange(map, select_r,select_c,r, c, isBlack);
					if(!valid){ 
   
						showFail("下子失败");
					}
					
					Chess chess = manager.getTheChess(map[select_r][select_c]);
					valid = chess.check(map, select_r, select_c, r, c, isBlack);
					if(!valid||(map[select_r][select_c]<1000&&map[r][c]<1000&&map[r][c]!=0)||(map[select_r][select_c]>1000&&map[r][c]>1000&&map[r][c]!=0)){ 
   
						showFail("下子失败,规则不允许");
						select_c = -1;
						select_r = -1;
						repaint();
						return ;
					}
					
					map[r][c] = map[select_r][select_c ];
					map[select_r][select_c ] = 0; 
					select_c = -1;
					select_r = -1;
					
					isBlack = !isBlack;//对方来下
					repaint();
				}
			}
		});
	}
	protected void showFail(String msg) { 
   
		System.out.println(msg);
	}
	ChessManager manager = new ChessManager();
	private Image mainGif;
	public void initMap(){ 
   
		int c=0;
		int i,k;
		map=new int [][]{ 
   { 
   1000+'C',1000+'H',1000+'E',1000+'S',1000+'G',1000+'S',1000+'E',1000+'H',1000+'C'}, { 
   0,0,0,0,0,0,0,0,0,},
				{ 
   0,1000+'P',0,0,0,0,0,1000+'P',0},{ 
   1000+'A',0,1000+'A',0,1000+'A',0,1000+'A',0,1000+'A'},
				{ 
   0,0,0,0,0,0,0,0,0},{ 
   0,0,0,0,0,0,0,0,0},
				{ 
   'A',0,'A',0,'A',0,'A',0,'A'},{ 
   0,'P',0,0,0,0,0,'P',0},
				{ 
   0,0,0,0,0,0,0,0,0},{ 
   'C','H','E','S','G','S','E','H','C'}};
		//System.out.println(map[9][4]);
		mainGif = ImageTool.loadImage("main.gif");
		manager.loadAllImage();
	}
	
	//内部类
	public static class MyPoint{ 
   
		public int r;
		public int c;
		public MyPoint(int r,int c) { 
   
			this.r = r;
			this.c = c;
		}
	}
	int pw = 60;
	@Override
	public void paint(Graphics g){ 
   
		super.paint(g);
		//System.out.println("paint called ----------------");
		g.setFont(font);
		int w = this.getWidth();
		int h = this.getHeight();
		//System.out.println("宽"+w+" 高"+h);
		g.drawImage(mainGif, 0, 0,null);
		
		for(int r = 0;r<map.length;r++){ 
   
			for(int c = 0;c<map[r].length;c++){ 
   
				safelyDraw(g, manager.getImage(map[r][c]), 25+58*c, r*58+25);
			}
		}
		g.drawRect(25+58*select_c, 25+58*select_r, 58, 58);
		g.drawString(isBlack?"黑方下棋":"红方下棋", 220 , 650);
		
	}
	
	public void safelyDraw(Graphics g,Image img,int x,int y){ 
   
		try { 
   
			if(img == null){ 
   
				//System.err.println("img = null ,skip drawing "+x+" , "+y);
			}
			g.drawImage(img, x, y,null);
			
		} catch (Exception e) { 
   
			e.printStackTrace();
		}
	}
}

3.ChessManager.java 代码:

import java.awt.Image;
import java.util.HashMap;
import org.chess.ArmsChess;
import org.chess.CannonChess;
import org.chess.CarChess;
import org.chess.ChapChess;
import org.chess.Chess;
import org.chess.ElephantChess;
import org.chess.GeneralChess;
import org.chess.HorseChess;
import org.chess.ImageTool;

public class ChessManager { 
   
	HashMap<Integer,Chess> chesses = new HashMap<Integer,Chess>();
	public  ChessManager(){ 
   
		ArmsChess a = new ArmsChess();
		chesses.put(a.getType(), a);
		
		CannonChess b =new CannonChess();
		chesses.put(b.getType(),b);
		
		CarChess c = new CarChess();
		chesses.put(c.getType(), c);
		
		ChapChess d = new ChapChess();
		chesses.put(d.getType(), d);
		
		ElephantChess e = new ElephantChess();
		chesses.put(e.getType(), e);
		
		GeneralChess f = new GeneralChess();
		chesses.put(f.getType(), f);
		
		HorseChess g = new HorseChess();
		chesses.put(g.getType(), g);
		//其它其子类也在这里注册
		
	}
	
	public Chess getTheChess(int type){ 
   
		if(type > 1000) type -= 1000;
		return chesses.get(type);
	}
	HashMap<String, Image> img_map=new HashMap<String, Image>();
	public void loadAllImage() { 
   
		for(Chess c : chesses.values()){ 
   
			Image img_black = c.getImage(true);
			Image img_red = c.getImage(false);
			
			img_map.put(c.getType()+"_黑", img_black);
			img_map.put(c.getType()+"_红", img_red);
		}
	}
	
	public Image getImage(int type){ 
   
		int color = type /1000;
		type = type % 1000;
		
		return img_map.get(type+(color==1?"_黑":"_红"));
	}
}

4.ArmsChess.java代码:

package org.chess;
import java.awt.Image;

import javax.swing.JOptionPane;
/** * 兵 A * @author 曹佳榕 董玟彤(姜权罡最后修改成功) */
public class ArmsChess extends Chess{ 
   
	@Override
	public boolean check(int[][] map, int now_r, int now_c, int to_r, int to_c,boolean isBlack) { 
   
		// TODO Auto-generated method stub
		int cc = to_c - now_c;
		int rc = to_r - now_r;
		if(isBlack==true){ 
   //黑卒
			if(now_r<=4){ 
   //黑卒未过河
				boolean succ =  cc  == 0 && rc == 1;//黑卒未过河,向下前进一格
				
				if(!succ) return false;//如果不是向下前进一格,直接失败
				else{ 
   
					return true;
				}
			}
			else{ 
   //黑卒过河
				cc = Math.abs(to_c - now_c);
				boolean succ =  (cc  == 1&&rc==0) || (rc == 1&&cc==0);//黑卒过河,向下前进一格
				
				if(!succ) return false;//如果不是向下/向左/向右前进一格,直接失败
				else{ 
   
					if(map[to_r][to_c]=='G'){ 
   
						JOptionPane.showMessageDialog(null, "黑方胜利!");
					}
					return true;
				}
				//return map[to_r][to_c]==0;
			}
		}
		else{ 
   //红卒
			if(now_r>=5){ 
   //红卒未过河
				boolean succ =  cc  == 0 && rc == -1;//红卒未过河,向上前进一格
				
				if(!succ) return false;//如果不是向上前进一格,直接失败
				
				else{ 
   
					return true;
				}
			}
			else{ 
   //红卒过河
				cc = Math.abs(to_c - now_c);
				boolean succ =  (cc  == 1&&rc==0) || (rc == -1&&cc==0);//红卒过河,向上前进一格
				
				if(!succ) return false;//如果不是向上/向左/向右前进一格,直接失败
				else{ 
   
					if(map[to_r][to_c]==1000+'G'){ 
   
						JOptionPane.showMessageDialog(null, "红方胜利!");
					}
					return true;
				}
			}
		}
	}

	@Override
	public String getName() { 
   
		// TODO Auto-generated method stub
		return "卒";
	}

	@Override
	public int getType() { 
   
		// TODO Auto-generated method stub
		return 'A';
	}
	
	@Override
	public Image getImage(boolean isBlack) { 
   
		// TODO Auto-generated method stub
		return ImageTool.loadImage(isBlack?"黑卒.gif":"红卒.gif");
	}
}

5.CannonChess.java代码:

package org.chess;
import java.awt.Image;

import javax.swing.JOptionPane;
/* * * @author 姜权罡 * */
public class CannonChess extends Chess { 
   
	@Override
	public boolean check(int[][] map, int now_r, int now_c, int to_r, int to_c,boolean isBlack) { 
   
		// TODO Auto-generated method stub
		int cc = Math.abs(now_c - to_c);
		int rc = Math.abs(now_r - to_r);
		//count为 0 说明炮与目标之间没有棋子
		//count为 1 说明炮与目标之间有 1个 棋子
		//count为 2 说明炮与目标之间有 2个 棋子
		boolean succ = (cc==0 &&rc!=0 )||(cc!=0 &&rc==0 );//如果不是单条线,直接失败
		if(!succ) return false;
		
		int count=0;
		int from,to;
		//目标点没有棋子,且目标点和初始点之间无棋子,成功。
		//目标点有棋子,且为敌方棋子,且目标点和初始点之间有一个棋子,成功。
		if(rc==0){ 
   //横着走
			if(now_c<to_c){ 
   //向右走
				from = now_c+1;
				to=to_c;
			}else{ 
   //向左走
				from = to_c+1;
				to = now_c;
			}
			for(int i=from;i<to;i++){ 
   
				if(map[now_r][i]!=0){ 
   
					count++;
				}
			}
			System.out.println("棋子个数:\n"+count);
			if(map[to_r][to_c]==0&&count==0)
			{ 
   
				return true;
			}
			
			if(map[to_r][to_c]==0&&count==1)
			{ 
   
				return false;
			}
			
			//黑方,对面为红方; 红方,对面为黑方。
			if(map[to_r][to_c]!=0&&((isBlack==true&&map[to_r][to_c]<1000)||(isBlack==false&&map[to_r][to_c]>1000))&&count==1){ 
    
				
				//将军
				if(isBlack==true&&map[to_r][to_c]=='G'){ 
   
					JOptionPane.showMessageDialog(null, "黑方胜利!");
					return true;
				}
				if(isBlack==false&&map[to_r][to_c]==1000+'G'){ 
   
					JOptionPane.showMessageDialog(null, "红方胜利!");
					return true;
				}
				return true;
			}
			else{ 
   
				return false;
			}
		}else{ 
   //竖着走
			if(now_r<to_r){ 
   //向下走
				from =  now_r+1;
				to = to_r;
			}else{ 
   //向上走
				from = to_r+1; 
				to = now_r;
				
			}
			
			for(int i=from;i<to;i++){ 
   
				if(map[i][now_c]!=0){ 
   
					count++;
				}
			}
			System.out.println("棋子个数:\n"+count);
			
			if(map[to_r][to_c]==0&&count==0)
			{ 
   
				return true;
			}
			if(map[to_r][to_c]==0&&count==1)
			{ 
   
				return false;
			}
			//黑方,对面为红方; 红方,对面为黑方。
			if(map[to_r][to_c]!=0&&((isBlack==true&&map[to_r][to_c]<1000)||(isBlack==false&&map[to_r][to_c]>1000))&&count==1){ 
   
				//将军
				if(isBlack==true&&map[to_r][to_c]=='G'){ 
   
					JOptionPane.showMessageDialog(null, "黑方胜利!");
					return true;
				}
				if(isBlack==false&&map[to_r][to_c]==1000+'G'){ 
   
					JOptionPane.showMessageDialog(null, "红方胜利!");
					return true;
				}
				//return true;
			}else{ 
   
				return false;
			}
			
		}
		return true;
	}

	@Override
	public String getName() { 
   
		// TODO Auto-generated method stub
		return "炮";
	}

	@Override
	public int getType() { 
   
		// TODO Auto-generated method stub
		return 'P';
	}

	@Override
	public Image getImage(boolean isBlack) { 
   
		// TODO Auto-generated method stub
		return ImageTool.loadImage(isBlack?"黑炮.gif":"红炮.gif");
	}
}

6.CarChess代码:

package org.chess;
import java.awt.Image;

import javax.swing.JOptionPane;
/* * 黑车1 * 红车11 * @author 庞义俊(姜权罡最后修改成功) * */
public class CarChess extends Chess { 
   
	@Override
	public boolean check(int[][] map, int now_r, int now_c, int to_r, int to_c,boolean isBlack) { 
   
		// TODO Auto-generated method stub
		int cc = Math.abs(now_c - to_c);
		int rc = Math.abs(now_r - to_r);
		//车类比炮写,车比炮简单
		boolean succ = (cc==0 &&rc!=0 )||(cc!=0 &&rc==0 );//如果不是单条线,直接失败
		if(!succ) return false;
		
		int from,to;
		if(rc == 0){ 
   //横着走
			if(now_c < to_c){ 
   //向右走
				from = now_c+1 ;
				to = to_c;
			}else{ 
    // d 0 0 x
				//from = to_c;
				//to = now_c-1;
				from = now_c-1;
				to = to_c;
			}
			
			for(int i=from;i<to;i++){ 
   
				if(map[now_r][i] != 0 )return false; //中间有棋子,失败!!
			}
		}else{ 
    //竖着走
			//TODO
			if(now_r<to_r){ 
   //向上走
				from = to_r;
				to=now_r-1;
			}else{ 
   //向下走
				from = now_r+1;
				to=to_r;
			}
			for(int i=from;i<to;i++){ 
   
				if(map[i][now_c] != 0 )return false; //中间有棋子,失败!!
			}
			
		}
		
		if(isBlack==true&&map[to_r][to_c]=='G'){ 
   
			JOptionPane.showMessageDialog(null, "黑方胜利!");
			return true;
		}
		if(isBlack==false&&map[to_r][to_c]==1000+'G'){ 
   
			JOptionPane.showMessageDialog(null, "红方胜利!");
			return true;
		}
		
		return true;
		
	}

	@Override
	public String getName() { 
   
		// TODO Auto-generated method stub
		return "车";
	}

	@Override
	public int getType() { 
   
		// TODO Auto-generated method stub
		return 'C';
	}

	@Override
	public Image getImage(boolean isBlack) { 
   
		// TODO Auto-generated method stub
		return ImageTool.loadImage(isBlack?"黑车.gif":"红车.gif");
	}

}

7.ChapChess代码:

package org.chess;
import java.awt.Image;
/* * 士4 * 仕44 * @author 曹佳榕(姜权罡最后修改成功) * */
public class ChapChess extends Chess { 
   
	@Override
	public boolean check(int[][] map, int now_r, int now_c, int to_r, int to_c,boolean isBlack) { 
   
		// TODO Auto-generated method stub
		int cc = Math.abs(now_c - to_c);
		int rc = Math.abs(now_r - to_r);
		//要分黑士 和红仕!!(to_r>=7&&to_r<=9&&to_c>=3&&to_c<=5)
		//黑士(to_r>=0&&to_r<=2&&to_c>=3&&to_c<=5)
		
		boolean succ;
		if(isBlack==true){ 
   //黑士
			//如果不是一条斜线,直接失败,还要保证目标位置没有棋子
			//succ = cc==1 && rc==1 &&(to_r>=0&&to_r<=2&&to_c>=3&&to_c<=5)&&(map[to_r][to_c]==0); 
			succ = cc==1 && rc==1 && (to_r>=0&&to_r<=2&&to_c>=3&&to_c<=5); 
		}
		else{ 
   //红仕
			//如果不是一条斜线,直接失败
			//succ = cc==1 && rc==1 &&(to_r>=7&&to_r<=9&&to_c>=3&&to_c<=5)&&(map[to_r][to_c]==0); 
			succ = cc==1 && rc==1 &&(to_r>=7&&to_r<=9&&to_c>=3&&to_c<=5); 
		}
		if(!succ) return false; 
		else{ 
   
			return true;
		}
		
	}

	@Override
	public String getName() { 
   
		// TODO Auto-generated method stub
		return "士";
	}

	@Override
	public int getType() { 
   
		// TODO Auto-generated method stub
		return 'S';
	}

	@Override
	public Image getImage(boolean isBlack) { 
   
		// TODO Auto-generated method stub
		return ImageTool.loadImage(isBlack?"黑士.gif":"红士.gif");
	}

}

8.Chess.java 代码:

package org.chess;
import java.awt.Image;
/* * 棋子父类、 * 所有棋子继承该类 * @author 姜权罡 * */
public abstract class Chess { 
   
	/** * 检查落子是否合法 * @param map 地图 * @param now_r 当前棋子的行号 * @param now_c 当前棋子的列号 * @param to_r 想去地方的行号 * @param to_c 想去地方的列号 * @param isBlack 如果黑方 传true 否则传false * @return 如果可以去,返回true, 否则返回false */
	public abstract boolean check(int[][] map,int now_r,int now_c ,int to_r, int to_c,boolean isBlack);
	
	public abstract int getType();
	
	public abstract String getName();
	
	public abstract Image getImage(boolean isBlack);
}

9.ElephantChess.java 代码:

package org.chess;
import java.awt.Image;
/** * 象3 * 相33 * @author 董玟彤(姜权罡最后修改成功) */
public class ElephantChess  extends Chess{ 
   
	@Override
	public boolean check(int[][] map, int now_r, int now_c, int to_r, int to_c,boolean isBlack) { 
   
		int cc = Math.abs(now_c - to_c);
		int rc = Math.abs(now_r - to_r);
		
		boolean succ =  cc  == 2 && rc == 2;
		
		if(!succ ) return false; //如果不是田,直接失败!
		 
		int c = (now_c+to_c)/2;
		int r = (now_r+to_r)/2;
		if(isBlack==true){ 
   //黑象
			return map[r][c] == 0&&to_r<=4;//象不能过河
			//该点二维数组为0说明,象眼为空,可以走棋。不考虑目标点有没有棋子!
		}
		else{ 
   //红象
			return map[r][c] == 0&&to_r>=5;//象不能过河
		}
	}
	
	@Override
	public String getName() { 
   
		return "象";
	}

	@Override
	public int getType() { 
   
		return 'E';
	}

	@Override
	public Image getImage(boolean isBlack) { 
   
		return ImageTool.loadImage(isBlack?"黑象.gif":"红象.gif");
	}
}

10.GeneralChess.java 代码:

package org.chess;
import java.awt.Image;

import javax.swing.JOptionPane;
/* * 将5 * 帅55 * @author 庞义俊(姜权罡最后修改成功) * */
public class GeneralChess extends Chess { 
   

	@Override
	public boolean check(int[][] map, int now_r, int now_c, int to_r, int to_c,boolean isBlack) { 
   
		// TODO Auto-generated method stub
		int cc = Math.abs(now_c - to_c);
		int rc = Math.abs(now_r - to_r);
		
		boolean succ;
		int count=0;
		int min_r= now_r<to_r?now_r:to_r;
		int max_r=now_r>to_r?now_r:to_r;
		for(int i=min_r+ 1;i<max_r;i++){ 
   //如果双方 将军之间有棋子的话,不可走棋
			if(cc==0&&map[i][now_c]!=0){ 
   
				count=1;
				return false;
			}
		}
		
		if(isBlack==true){ 
   //黑将
			//如果是一条斜线,直接失败
			//目标为红将,且之间没有棋子,移动
			if(map[to_r][to_c]=='G'&&count==0){ 
   
				
				JOptionPane.showMessageDialog(null, "黑方胜利");
				return true;
			}
			succ = ((cc==1 && rc==0)||(cc==0 && rc==1)) &&(to_r>=0&&to_r<=2&&to_c>=3&&to_c<=5); 
		}
		else{ 
   //红帅
			//如果是一条斜线,直接失败
			//目标为黑将,且之间没有棋子,移动
			if(map[to_r][to_c]==1000+'G'&&count==0){ 
   
				JOptionPane.showMessageDialog(null, "红方胜利");
				return true;
			}
			succ = ((cc==1 && rc==0)||(cc==0 && rc==1)) &&(to_r>=7&&to_r<=9&&to_c>=3&&to_c<=5); 
		}
		if(!succ) return false; 
		return true;
	}

	@Override
	public String getName() { 
   
		// TODO Auto-generated method stub
		return "将";
	}

	@Override
	public int getType() { 
   
		// TODO Auto-generated method stub
		return 'G';
	}

	@Override
	public Image getImage(boolean isBlack) { 
   
		// TODO Auto-generated method stub
		return ImageTool.loadImage(isBlack?"黑将.gif":"红将.gif");
	}

}

11.HorseChess.java 代码:

package org.chess;
import java.awt.Image;

import javax.swing.JOptionPane;
/* * 马 H * @author 姜权罡 * */
public class HorseChess extends Chess { 
   
	@Override
	public boolean check(int[][] map, int now_r, int now_c, int to_r, int to_c,boolean isBlack) { 
   
		// TODO Auto-generated method stub
		int cc=Math.abs(now_c - to_c);
		int rc=Math.abs(now_r - to_r);
		
		boolean succ = (cc ==2&&rc==1)||(cc ==1&&rc==2);
		
		if(!succ) return false;//如果不是日,直接失败!
		
		int r;//r为马脚的行坐标
		int c;//c为马脚的列坐标
		if(cc==1){ 
   //走日
			r=(to_r+now_r)/2;
			c=now_c;
		}
		else{ 
   //走横着的 日
			r=now_r;
			c=(to_c+now_c)/2;
		}
		
		if(isBlack==true&&map[to_r][to_c]=='G'){ 
   
			JOptionPane.showMessageDialog(null, "黑方胜利!");
			return true;
		}
		if(isBlack==false&&map[to_r][to_c]==1000+'G'){ 
   
			JOptionPane.showMessageDialog(null, "红方胜利!");
			return true;
		}
		
		return map[r][c]==0;
	}
	
	@Override
	public String getName() { 
   
		// TODO Auto-generated method stub
		return "马";
	}
	
	@Override
	public int getType() { 
   
		// TODO Auto-generated method stub
		
			return 'H';
	}

	@Override
	public Image getImage(boolean isBlack) { 
   
		// TODO Auto-generated method stub
		return ImageTool.loadImage(isBlack?"黑马.gif":"红马.gif");
	}

}

12.ImageToool.java 代码:

package org.chess;
import java.awt.Image;
import javax.swing.ImageIcon;
public class ImageTool { 
   
	public static Image loadImage(String path){ 
   
		try { 
   
			ImageIcon im = new ImageIcon("image\\"+path);
			return im.getImage();
		} catch (Exception e) { 
   
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) { 
   
		ImageIcon im=new ImageIcon("image\\main.gif");
		System.out.println(im);
	}
}

13.musicStuff.java 代码:

package org.chess;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
/* * 音乐播放器 * 庞义俊 * */
public class musicStuff { 
   
	public void playMusic(String musicLocation)
	{ 
   
		try
		{ 
   
			File musicPath = new File(musicLocation);
			if(musicPath.exists())
			{ 
   
				AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
				Clip clip = AudioSystem.getClip();
				clip.open(audioInput);
				clip.start();
				clip.loop(Clip.LOOP_CONTINUOUSLY);
			}
			else
			{ 
   
			}
		}
		catch(Exception ex)
		{ 
   
			ex.printStackTrace();
		}
	}
}

14.RangeChecker.java 代码:

package org.chess;
import java.awt.Canvas;
import javax.swing.JOptionPane;
public class RangeChecker  { 
   
	// 1 r和c 是不是合法的
	// 2 目标位置是否已经有其它棋子,如果有敌方棋子可以走,有己方不可走
	public static boolean inRange(int[][] map,int p_r,int p_c,int r,int c,boolean isBlack){ 
   
		if(r<0 || r>9 || c< 0 || c>9) return false;
		if(map[r][c]==0){ 
   //目标位置没有棋子,走棋
			
			return false;
		}else{ 
   //目标位置有棋子 判断是否为友方!友方不可走,敌对方可走。
			if(map[r][c]<1000&&map[p_r][p_c]>1000){ 
   //红方,之前的是黑方。 结果是敌方 可以走棋
				return true;
			}else if(map[r][c]>1000&&map[p_r][p_c]<1000){ 
   //黑方,之前的是红方,结果是敌方 不可走棋
				return true;
			}else{ 
   
				return false;
			}
		}
	}

	public static boolean selectInRange(int[][] map, int r, int c,boolean isBlack) { 
   
		//下表在范围内,且当前点由自己放的棋子可以, 否则不可以。
		if(r<0 || r>9 || c< 0 || c>9) return false;
		if(map[r][c] == 0) return true;
		return (isBlack==true && map[r][c]>1000) ||  (isBlack==false && map[r][c]<1000);
	}
}

15.test文件夹下的测试类 这里只举其中一个例子(测试炮),其他的按照这个写就可以了。
TestCannonChess.java 代码如下:

import org.chess.CannonChess;
import junit.framework.TestCase;
public class TestCannonChess extends TestCase { 
   
	
	CannonChess chess = new CannonChess();
	//炮水平向右移动(过程无棋子)
	public void testCheck1(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 2,3,2, 6, true);//黑方还是红方, true为黑方
		assertEquals(b, true);
	}
	
	//炮水平向右移动(过程有一个棋子)
	public void testCheck31(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 1, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 2,3,2, 6, true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
	//炮水平向右移动(过程有一个棋子)
	public void testCheck32(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 1, 6 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 2,3,2, 6, true);//黑方还是红方, true为黑方
		assertEquals(b, true);
	}
	
	
	//炮水平向右移动(过程有两个棋子)
	public void testCheck33(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 1, 1, 6 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 2,3,2, 6, true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
	
	//炮水平向左移动(过程无棋子)
	public void testCheck41(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 2,3,2, 0, true);//黑方还是红方, true为黑方
		assertEquals(b, true);
	}
	
	//炮水平向左移动(过程一个棋子)
	public void testCheck42(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,1, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 2,3,2, 0, true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
	//炮水平向左移动(过程一个棋子)
	public void testCheck43(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    6 ,0 ,1, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 2,3,2, 0, true);//黑方还是红方, true为黑方
		assertEquals(b, true);
	}
	
	//炮水平向左移动(过程两个棋子)
	public void testCheck44(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    6 ,1,1, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 2,3,2, 0, true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
	//炮竖直向上移动(过程0个棋子)
	public void testCheck51(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0,0,  0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,0,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, true);
	}
	
	//炮竖直向上移动(过程1个棋子)
	public void testCheck52(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0,0,  1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,0,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
	//炮竖直向上移动(过程1个棋子)
	public void testCheck53(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0,0,  1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,0,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, true);
	}
	
	//炮竖直向上移动(过程2个棋子)
	public void testCheck54(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 2, 0, 0, 0 },
				{ 
    0 ,0,0,  1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,0,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
	//炮竖直向上移动(过程2个棋子)
	public void testCheck55(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 2, 0, 0, 0 },
				{ 
    0 ,0,0,  1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,0,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
	//炮竖直向下移动(过程0个棋子)
	public void testCheck61(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0,0,  0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,6,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, true);
	}
	
	//炮竖直向下移动(过程1个棋子)
	public void testCheck62(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0,0,  0, 0, 0, 0 },
				{ 
    0 ,0 ,0,1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,6,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
	//炮竖直向下移动(过程1个棋子)
	public void testCheck63(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0,0,  0, 0, 0, 0 },
				{ 
    0 ,0 ,0,1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 2, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,6,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, true);
	}
	
	//炮竖直向下移动(过程2个棋子)
	public void testCheck64(){ 
   
		int[][] map = { 
   
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 },
				{ 
    0 ,0,0,  0, 0, 0, 0 },
				{ 
    0 ,0 ,0,1006, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 1, 0, 0, 0 },
				{ 
    0 ,0 ,0, 2, 0, 0, 0 },
				{ 
    0 ,0 ,0, 0, 0, 0, 0 }
		};
		boolean b = chess.check(map, 3,3,6,3 , true);//黑方还是红方, true为黑方
		assertEquals(b, false);
	}
	
}

谢谢大家的支持,您的一键三连是 罡罡同学前进的最大动力!
一键三连 一键三连 一键三连 一键三连 一键三连 一键三连

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/126829.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号