Java 实现国际象棋 可视化 代码
作者:互联网
要求
modify the previous pogram further by using interface. You can choose either of the followng way:
1 write a Movable interface that includes the abstract Move method
2 write a Movable interface that includes the abstract method to decide if it is a legal movement.
Then modify the indivisual chesspieces not only extends the ChessPiece superclass, but also implements the Movable interface.
一句话,用接口实现国际象棋
代码
哎,这是上学期的代码了,忘了忘了,懒得再看一遍了。
图片资源还有的,需要的私聊我
//主类
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class ChessBoard extends JPanel implements MOVE{
Chess chess[][]=new Chess[9][9];
final static String King="K";
final static String Knight="N";
final static String Queen="Q";
final static String Privates="P";
final static String Biship="B";
final static String Rook="R";
final static int White=1;
final static int Black=0;
int Size_X,Size_Y;
int Local_X,Local_Y;
public static Point SelectedPoint=new Point();
public static Point AimPoint=new Point();
public static Point MousePoint=new Point();
JFrame f=new JFrame("国际象棋棋盘");
JMenuBar jb1;
JMenuItem jm1,jm2,jm3,jm4;
int grids,gridsize;
Graphics g=f.getGraphics();
Listener ls=new Listener();
public boolean SearchWay() {
int IsWhite=chess[SelectedPoint.x][SelectedPoint.y].IsWhite==1?1:-1;
int Pre_x=chess[SelectedPoint.x][SelectedPoint.y].Pre_x;
int Pre_y=chess[SelectedPoint.x][SelectedPoint.y].Pre_y;
int Next_x=chess[SelectedPoint.x][SelectedPoint.y].Next_x;
int Next_y=chess[SelectedPoint.x][SelectedPoint.y].Next_y;
int StepTimes=chess[SelectedPoint.x][SelectedPoint.y].StepTimes;
String Name=chess[SelectedPoint.x][SelectedPoint.y].Chess_Name;
if(Name.equals(Privates)) {
if(Pre_x==Next_x) {
if(StepTimes>=0)
if((Next_y-Pre_y)*IsWhite==1&&!chess[Next_x][Next_y].Statement)
return true;
else System.out.println("Legal 1\n");
if(StepTimes==0)
if((Next_y-Pre_y)*IsWhite==2&&!chess[Next_x][Next_y].Statement&&!chess[Next_x][Next_y+1].Statement)
return true;
else System.out.println("Legal 3\n");
}
else if(Math.abs(Pre_x-Next_x)*(Next_y-Pre_y)*IsWhite==1&&
chess[Next_x][Next_y].Statement&&
IsWhite*(chess[Next_x][Next_y].IsWhite==1?1:-1)==-1)
return true;
else System.out.println("Legal 3\n");
return false;
}
if(Name.equals(Biship)) {
if(Math.abs(Next_x-Pre_x)==Math.abs(Pre_y-Next_y)) {
for(int i=1;i<Math.abs(Next_x-Pre_x);i++) {
int dx=(Next_x-Pre_x)/Math.abs(Next_x-Pre_x);
int dy=(Next_y-Pre_y)/Math.abs(Next_y-Pre_y);
if(chess[Pre_x+i*dx][Pre_y+i*dy].Statement)
return false;
}
if(!chess[Next_x][Next_y].Statement||(chess[Next_x][Next_y].Statement&&IsWhite*(chess[Next_x][Next_y].IsWhite==1?1:-1)==-1))
return true;
return false;
}
return false;
}
if(Name.equals(King)) {
if((Math.abs(Next_x-Pre_x)*Math.abs(Pre_y-Next_y)==1||
(Math.abs(Next_x-Pre_x)+Math.abs(Pre_y-Next_y)==1)
&&(!chess[Next_x][Next_y].Statement||
chess[Next_x][Next_y].Statement&&
IsWhite*(chess[Next_x][Next_y].IsWhite==1?1:-1)==-1)))
return true;
return false;
}
if(Name.equals(Knight)) {
if(Math.abs(Next_x-Pre_x)*Math.abs(Pre_y-Next_y)==2) {
if(!chess[Next_x][Next_y].Statement||(chess[Next_x][Next_y].Statement&&
IsWhite*(chess[Next_x][Next_y].IsWhite==1?1:-1)==-1))
return true;
}
return false;
}
if(Name.equals(Queen)) {
if(Math.abs(Next_x-Pre_x)*Math.abs(Pre_y-Next_y)==0) {
int dx=Next_x==Pre_x?0:(Next_x-Pre_x)/Math.abs(Next_x-Pre_x);
int dy=Next_y==Pre_y?0:(Next_y-Pre_y)/Math.abs(Next_y-Pre_y);
for(int i=0;i<Math.max(Math.abs(Next_x-Pre_x),Math.abs(Pre_y-Next_y));i++)
if(chess[Pre_x+i*dx][Pre_y+i*dy].Statement)
return false;
if(!chess[Next_x][Next_y].Statement||(chess[Next_x][Next_y].Statement&&
IsWhite*(chess[Next_x][Next_y].IsWhite==1?1:-1)==-1))
return true;
}
if(Math.abs(Next_x-Pre_x)==Math.abs(Pre_y-Next_y)) {
for(int i=1;i<Math.abs(Next_x-Pre_x);i++) {
int dx=(Next_x-Pre_x)/Math.abs(Next_x-Pre_x);
int dy=(Next_y-Pre_y)/Math.abs(Next_y-Pre_y);
if(chess[Pre_x+i*dx][Pre_y+i*dy].Statement)
return false;
}
if(!chess[Next_x][Next_y].Statement||(chess[Next_x][Next_y].Statement&&
IsWhite*(chess[Next_x][Next_y].IsWhite==1?1:-1)==-1))
return true;
return false;
}
return false;
}
if(Name.equals(Rook)) {
if(Math.abs(Next_x-Pre_x)*Math.abs(Pre_y-Next_y)==0) {
int dx=Next_x==Pre_x?0:(Next_x-Pre_x)/Math.abs(Next_x-Pre_x);
int dy=Next_y==Pre_y?0:(Next_y-Pre_y)/Math.abs(Next_y-Pre_y);
for(int i=0;i<Math.max(Math.abs(Next_x-Pre_x),Math.abs(Pre_y-Next_y));i++)
if(chess[Pre_x+i*dx][Pre_y+i*dy].Statement)
return false;
if(!chess[Next_x][Next_y].Statement||(chess[Next_x][Next_y].Statement&&
IsWhite*(chess[Next_x][Next_y].IsWhite==1?1:-1)==-1))
return true;
return false;
}
return false;
}
return false;
}
public boolean IsMoveLegal() {
int IsWhite=chess[SelectedPoint.x][SelectedPoint.y].IsWhite==1?1:-1;
String Name=chess[SelectedPoint.x][SelectedPoint.y].Chess_Name;
boolean Selected=chess[SelectedPoint.x][SelectedPoint.y].Selected;
boolean Statement=chess[SelectedPoint.x][SelectedPoint.y].Statement;
boolean WhiteTurn=chess[SelectedPoint.x][SelectedPoint.y].WhiteTurn;
if(Selected&&Statement&&((WhiteTurn&&IsWhite==1)||(!WhiteTurn&&IsWhite==-1))){
if(SearchWay())
return true;
return false;
}
else System.out.println("Legal 0\n");
// System.out.println(Selected+" "+Statement+" "+WhiteTurn+" "+IsWhite+"\n");
return false;
}
public void ChessInit() {
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
chess[j][i]=new Chess(1,1,"O",1);
for(int i=1;i<=8;i++) {
chess[i][2].SetChess(i, 2, Privates, White);
}
chess[1][1].SetChess(1,1,Rook,White);
chess[8][1].SetChess(8,1,Rook,White);
chess[2][1].SetChess(2,1,Knight,White);
chess[7][1].SetChess(7,1,Knight,White);
chess[3][1].SetChess(3,1,Biship,White);
chess[6][1].SetChess(6,1,Biship,White);
chess[4][1].SetChess(4,1,Queen,White);
chess[5][1].SetChess(5,1,King,White);
for(int i=1;i<=8;i++) {
chess[i][7].SetChess(i, 7, Privates, Black);
}
chess[1][8].SetChess(1,8,Rook,Black);
chess[8][8].SetChess(8,8,Rook,Black);
chess[2][8].SetChess(2,8,Knight,Black);
chess[7][8].SetChess(7,8,Knight,Black);
chess[3][8].SetChess(3,8,Biship,Black);
chess[6][8].SetChess(6,8,Biship,Black);
chess[4][8].SetChess(4,8,Queen,Black);
chess[5][8].SetChess(5,8,King,Black);
}
public void Init() {
ChessInit();
Size_X=700;
Size_Y=745;
Local_X=450;
Local_Y=50;
grids=8;
gridsize=86;
MousePoint.x=1;
MousePoint.y=1;
}
public void move() {
System.out.println(AimPoint.x+" "+AimPoint.y+"\n");
//System.out.println(SelectedPoint.x+" "+SelectedPoint.y+"@@\n");
chess[SelectedPoint.x][SelectedPoint.y].Selected=true;
chess[SelectedPoint.x][SelectedPoint.y].Next_x=AimPoint.x;
chess[SelectedPoint.x][SelectedPoint.y].Next_y=AimPoint.y;
if(IsMoveLegal()) {
chess[SelectedPoint.x][SelectedPoint.y].Pre_x=AimPoint.x;
chess[SelectedPoint.x][SelectedPoint.y].Pre_y=AimPoint.y;
chess[SelectedPoint.x][SelectedPoint.y].StepTimes++;
chess[SelectedPoint.x][SelectedPoint.y].WhiteTurn=
chess[SelectedPoint.x][SelectedPoint.y].WhiteTurn==true?false:true;
chess[AimPoint.x][AimPoint.y]=chess[SelectedPoint.x][SelectedPoint.y];
//System.out.println(chess[AimPoint.x][AimPoint.y].Statement+"&&\n");
//System.out.println(AimPoint.x+" "+AimPoint.y+"\n");
//System.out.println(SelectedPoint.x+" "+SelectedPoint.y+"@@\n");
Clear();
//System.out.println(chess[AimPoint.x][AimPoint.y].Statement+"&&\n");
//System.out.println(chess[SelectedPoint.x][SelectedPoint.y].Statement+"&&\n");
repaint();
//System.out.println(chess[AimPoint.x][AimPoint.y].Statement+"&&\n");
}
else System.out.println("Move Legal\n");
}
public void Clear() {
//chess[SelectedPoint.x][SelectedPoint.y].Statement=false;
chess[SelectedPoint.x][SelectedPoint.y].StepTimes=0;
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(new ImageIcon(getClass().getResource("ChessBoard.png")).getImage(),0, 0, 700, 700, this);
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
if(chess[i][j].Statement) {
String Color=chess[i][j].IsWhite==1?"W":"B";
//System.out.println(Color+chess[i][j].Chess_Name+".png");
g.drawImage(new ImageIcon(getClass().getResource(Color+chess[i][j].Chess_Name+".png")).getImage(),23+(chess[i][j].Pre_x-1)*87,28+(chess[i][j].Pre_y-1)*85,40,40, this);
}
g.setColor(Color.RED);
g.drawRect((MousePoint.x-1)*87+10, (MousePoint.y-1)*87+4+10, 67, 67);
}
public void BoardMenu() {
f.setSize(Size_X,Size_Y);
f.setTitle("国际象棋1.0");
Point point=new Point(Local_X,Local_Y);
f.setLocation(point);
f.setResizable(false);
f.addMouseListener(ls);
f.add(this);
ls.SetG(g);
ls.SetCB(this);
jb1=new JMenuBar();
jm1=new JMenu("重开一局");
jm2=new JMenu("退回上一步");
jm3=new JMenu("退出程序");
jm4=new JMenu("赞助捐款QQ:347801313");
jb1.add(jm1);
jb1.add(jm2);
jb1.add(jm3);
jb1.add(jm4);
f.add(jb1,BorderLayout.NORTH);
f.setVisible(true);
}
ChessBoard(){
Init();
BoardMenu();
}
public static void main(String[] args) {
ChessBoard cb=new ChessBoard();
}
}
//接口
public interface MOVE {
public void move();
public boolean IsMoveLegal();
public boolean SearchWay();
public void Clear();
}
//抽象类
abstract public class ChessPiec {
boolean Statement=false; //有无子
int IsWhite; //颜色
int StepTimes=0; //已经走了多少步
int Pre_x,Next_x=-10; //初始位置及下一步位置
int Pre_y,Next_y=-10;
String Chess_Name="O"; //棋的种类
static boolean WhiteTurn=false;
boolean Selected=false;
abstract public boolean IsMoveLegal();
}
//监听类
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
class Listener extends MouseAdapter {
int x,y;
ChessBoard cb;
Graphics g;
public void SetG(Graphics g) {
this.g=g;
}
public void SetCB(ChessBoard cb) {
this.cb=cb;
}
public void mouseClicked(MouseEvent e) {
x=(int)e.getX()/87+1;
y=(int)(e.getY()-45)/87+1;
cb.MousePoint.x=x;cb.MousePoint.y=y;
cb.repaint();
System.out.println("Click1\n"+x+" "+y);
}
public void mousePressed(MouseEvent e){ //鼠标按住拖动时调用
x=(int)e.getX()/87+1;
y=(int)(e.getY()-45)/87+1;
cb.SelectedPoint.x=x;cb.SelectedPoint.y=y;
cb.MousePoint.x=x;cb.MousePoint.y=y;
System.out.println("Click2\n"+x+" "+y);
cb.repaint();
}
public void mouseReleased(MouseEvent e){ //鼠标移动时调用
x=(int)e.getX()/87+1;
y=(int)(e.getY()-45)/87+1;
cb.AimPoint.x=x;cb.AimPoint.y=y;
System.out.println("Click3\n"+x+" "+y);
cb.move();
}
}
//棋子类
import java.util.*;
public class Chess extends ChessPiec {
Chess(int xx,int yy,String name,int Iswhite){
Pre_x=xx;
Pre_y=yy;
Chess_Name=name;
Statement=false;
StepTimes=0;
this.IsWhite=Iswhite;
}
public void SetChess(int xx,int yy,String name,int Iswhite){
Pre_x=xx;
Pre_y=yy;
Chess_Name=name;
Statement=true;
StepTimes=0;
this.IsWhite=Iswhite;
}//颜色,状态,名字,初始坐标,步数初始化
public boolean IsMoveLegal() { return true;}
class Point{
public int x;
public int y;
Point(int x,int y){
this.x=x;
this.y=y;
}
public int Get_x() {
return x;
}
public int Get_y() {
return y;
}
}
}
//以上为:吃子操作
本次作业发生了较多的错误,总结如下:
- Eclipse不需要较多版本的Java,否则会报错
- Eclipse的非src包不会默认同Package,需要到bilud Path里面设置
- 使用paint()的方法:定义一个打印图片的类,之后用Jframe.add(new 图片类())来显示图片。
- 不要害怕陌生的,没有学过的代码,敢于尝试才会有进步。
- Java的官方类比较方便,学会使用其API会使效率更高。
标签:Pre,Java,int,国际象棋,Next,可视化,chess,import,SelectedPoint 来源: https://blog.csdn.net/cj12345657582255/article/details/115606821