Loading src/chessComponent/ChessComponent.java +74 −4 Changes for src/chessComponent/ChessComponent.java: 74 added lines, 4 removed lines. Original line number Diff line number Diff line package chessComponent; import controller.CheckChessLogic; import controller.ClickController; import model.ChessColor; import model.ChessType; import model.ChessboardPoint; import java.awt.*; Loading @@ -10,11 +12,78 @@ import java.awt.*; * 表示棋盘上非空棋子的格子,是所有非空棋子的父类 */ public class ChessComponent extends SquareComponent { protected String name;// 棋子名字:例如 兵,卒,士等 protected final ChessColor chessColor; protected ChessComponent(ChessboardPoint chessboardPoint, Point location, ChessColor chessColor, ClickController clickController, int size) { super(chessboardPoint, location, chessColor, clickController, size); protected final ChessType chessType; protected boolean alive; public ChessComponent(ChessboardPoint chessboardPoint, Point location, ChessColor chessColor, ChessType chessType, ClickController clickController, int size) { super(chessboardPoint, location, clickController, size); this.chessColor = chessColor; this.chessType = chessType; this.alive = true; } public ChessColor getChessColor() { return chessColor; } public ChessType getChessType() { return chessType; } public boolean isAlive() { return alive; } public void kill(){ this.alive = false; } //TODO: complete deduce public static String deduceChessName(ChessColor chessColor, ChessType chessType) { return switch (chessColor) { case RED -> switch (chessType) { case CANNON -> String.valueOf('炮'); case GENERAL -> String.valueOf('帅'); case ADVISOR -> String.valueOf('仕'); case MINISTER -> String.valueOf('相'); case CHARIOT -> String.valueOf('俥'); case HORSE -> String.valueOf('傌'); case SOLDIER -> String.valueOf('兵'); }; case BLACK -> switch (chessType) { case CANNON -> String.valueOf('砲'); case GENERAL -> String.valueOf('将'); case ADVISOR -> String.valueOf('士'); case MINISTER -> String.valueOf('象'); case CHARIOT -> String.valueOf('車'); case HORSE -> String.valueOf('馬'); default -> String.valueOf('卒'); }; // } return chessType.toString(); case NONE -> null; }; } // handleChess 当前选择的棋子 public boolean canMoveTo(ChessComponent handleChess,SquareComponent[][] chessboard, ChessboardPoint destination) { SquareComponent destinationSquareChess = chessboard[destination.getX()][destination.getY()]; // downcast ChessComponent destinationChess = (ChessComponent) destinationSquareChess; //此处判断 handleChess 是否翻转 return handleChess.isReversal && CheckChessLogic.checkPath(handleChess,chessboard,destinationSquareChess) && CheckChessLogic.checkRank(handleChess,destinationChess) ; // (destinationSquareChess.isReversal|| destinationSquareChess instanceof EmptySlotComponent); //todo: complete this method } private String deduceChessName() { return deduceChessName(this.chessColor, this.chessType); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Loading @@ -29,7 +98,7 @@ public class ChessComponent extends SquareComponent{ //绘制棋子文字 g.setColor(this.getChessColor().getColor()); g.setFont(CHESS_FONT); g.drawString(this.name, this.getWidth() / 4, this.getHeight() * 2 / 3); g.drawString(this.deduceChessName(), this.getWidth() / 4, this.getHeight() * 2 / 3); //绘制棋子被选中时状态 if (isSelected()) { Loading @@ -40,4 +109,5 @@ public class ChessComponent extends SquareComponent{ } } } } src/chessComponent/EmptySlotComponent.java +3 −5 Changes for src/chessComponent/EmptySlotComponent.java: 3 added lines, 5 removed lines. Original line number Diff line number Diff line package chessComponent; import controller.ClickController; import model.ChessColor; import model.ChessType; import model.ChessboardPoint; import java.awt.*; Loading @@ -12,12 +10,12 @@ import java.awt.*; */ public class EmptySlotComponent extends SquareComponent { public EmptySlotComponent(ChessboardPoint chessboardPoint, Point location, ClickController listener, int size) { super(chessboardPoint, location, ChessColor.NONE, listener, size); public EmptySlotComponent(ChessboardPoint chessboardPoint, Point location, ClickController clickController, int size) { super(chessboardPoint, location, clickController, size); } @Override public boolean canMoveTo(SquareComponent[][] chessboard, ChessboardPoint destination) { public boolean canMoveTo(ChessComponent handleChess,SquareComponent[][] chessboard, ChessboardPoint destination) { return false; } Loading src/chessComponent/SquareComponent.java +2 −0 Changes for src/chessComponent/SquareComponent.java: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -126,4 +126,6 @@ public abstract class SquareComponent extends JComponent { g.setColor(squareColor); g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2); } public abstract boolean canMoveTo(ChessComponent handleChess, SquareComponent[][] chessboard, ChessboardPoint destination); } src/controller/CheckChessLogic.java +11 −13 Changes for src/controller/CheckChessLogic.java: 11 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -11,8 +11,6 @@ public class CheckChessLogic { /* path 和 军衔 判定分两个method * 则需要两个return同时为true*/ // todo: 1.change chess1x into chessLocation? // 2.override for CANNON // 对吃棋时的 path 判定 // todo : optimize codes below public static boolean checkPath(ChessComponent handleChess, SquareComponent[][] chessboard, SquareComponent destination) { Loading Loading @@ -42,22 +40,22 @@ public class CheckChessLogic { return check; } /* chess1 是第一个选中棋子 * chess2 是第二个选中棋子,即被吃的棋子 * 此处顺应 enum 索引,以 <= 判断吃棋是否合规*/ // TODO: consider the situation when chess2 is not revealed public static boolean checkRank(ChessComponent chess1, ChessComponent chess2) { /* handleChess 是所执棋子 * destinationChess 是第二个选中棋子,即被吃的棋子 * 此处利用 enum 索引,以 <= 判断吃棋是否合规 */ public static boolean checkRank(ChessComponent handleChess, ChessComponent destinationChess) { boolean check = false; //已翻转的情况 //todo: check and delete '!chess1.isReversal()' if (!chess1.isReversal() && !chess2.isReversal()) { // chess1 == SOLDIER && chess2 == CANNON if (chess1.getChessType() == ChessType.SOLDIER && chess2.getChessType() == ChessType.CANNON) { //todo: check and delete '!handleChess.isReversal()' if ( !destinationChess.isReversal()) { // handleChess == SOLDIER && destinationChess == CANNON if (handleChess.getChessType() == ChessType.SOLDIER && destinationChess.getChessType() == ChessType.CANNON) { check = true; } else { check = chess1.getChessType().ordinal() <= chess2.getChessType().ordinal() ? true : false; // check = handleChess.getChessType().ordinal() <= destinationChess.getChessType().ordinal(); check = (destinationChess.getChessType().compareTo(handleChess.getChessType()) >= 0); } } else if (chess2.isReversal() && chess1.getChessType() == ChessType.CANNON) { } else if (destinationChess.isReversal() && handleChess.getChessType() == ChessType.CANNON) { check = true; } return check; Loading src/controller/ClickController.java +3 −2 Changes for src/controller/ClickController.java: 3 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -28,10 +28,11 @@ public class ClickController { squareComponent.setSelected(false); first.repaint(); first = null; } else if (first.canMoveTo(chessboard.getChessComponents(), squareComponent.getChessboardPoint())) { } else if (first.canMoveTo(first,chessboard.getChessComponents(), squareComponent.getChessboardPoint())) { // todo:check if 'first' is revealed //repaint in swap chess method. chessboard.swapChessComponents(first, squareComponent); // first 是被移动的棋子,squareComponent 是 destination, // first 是所执棋子(handleChess),squareComponent 是 destinationChess, chessboard.clickController.swapPlayer(); first.setSelected(false); Loading Loading
src/chessComponent/ChessComponent.java +74 −4 Changes for src/chessComponent/ChessComponent.java: 74 added lines, 4 removed lines. Original line number Diff line number Diff line package chessComponent; import controller.CheckChessLogic; import controller.ClickController; import model.ChessColor; import model.ChessType; import model.ChessboardPoint; import java.awt.*; Loading @@ -10,11 +12,78 @@ import java.awt.*; * 表示棋盘上非空棋子的格子,是所有非空棋子的父类 */ public class ChessComponent extends SquareComponent { protected String name;// 棋子名字:例如 兵,卒,士等 protected final ChessColor chessColor; protected ChessComponent(ChessboardPoint chessboardPoint, Point location, ChessColor chessColor, ClickController clickController, int size) { super(chessboardPoint, location, chessColor, clickController, size); protected final ChessType chessType; protected boolean alive; public ChessComponent(ChessboardPoint chessboardPoint, Point location, ChessColor chessColor, ChessType chessType, ClickController clickController, int size) { super(chessboardPoint, location, clickController, size); this.chessColor = chessColor; this.chessType = chessType; this.alive = true; } public ChessColor getChessColor() { return chessColor; } public ChessType getChessType() { return chessType; } public boolean isAlive() { return alive; } public void kill(){ this.alive = false; } //TODO: complete deduce public static String deduceChessName(ChessColor chessColor, ChessType chessType) { return switch (chessColor) { case RED -> switch (chessType) { case CANNON -> String.valueOf('炮'); case GENERAL -> String.valueOf('帅'); case ADVISOR -> String.valueOf('仕'); case MINISTER -> String.valueOf('相'); case CHARIOT -> String.valueOf('俥'); case HORSE -> String.valueOf('傌'); case SOLDIER -> String.valueOf('兵'); }; case BLACK -> switch (chessType) { case CANNON -> String.valueOf('砲'); case GENERAL -> String.valueOf('将'); case ADVISOR -> String.valueOf('士'); case MINISTER -> String.valueOf('象'); case CHARIOT -> String.valueOf('車'); case HORSE -> String.valueOf('馬'); default -> String.valueOf('卒'); }; // } return chessType.toString(); case NONE -> null; }; } // handleChess 当前选择的棋子 public boolean canMoveTo(ChessComponent handleChess,SquareComponent[][] chessboard, ChessboardPoint destination) { SquareComponent destinationSquareChess = chessboard[destination.getX()][destination.getY()]; // downcast ChessComponent destinationChess = (ChessComponent) destinationSquareChess; //此处判断 handleChess 是否翻转 return handleChess.isReversal && CheckChessLogic.checkPath(handleChess,chessboard,destinationSquareChess) && CheckChessLogic.checkRank(handleChess,destinationChess) ; // (destinationSquareChess.isReversal|| destinationSquareChess instanceof EmptySlotComponent); //todo: complete this method } private String deduceChessName() { return deduceChessName(this.chessColor, this.chessType); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Loading @@ -29,7 +98,7 @@ public class ChessComponent extends SquareComponent{ //绘制棋子文字 g.setColor(this.getChessColor().getColor()); g.setFont(CHESS_FONT); g.drawString(this.name, this.getWidth() / 4, this.getHeight() * 2 / 3); g.drawString(this.deduceChessName(), this.getWidth() / 4, this.getHeight() * 2 / 3); //绘制棋子被选中时状态 if (isSelected()) { Loading @@ -40,4 +109,5 @@ public class ChessComponent extends SquareComponent{ } } } }
src/chessComponent/EmptySlotComponent.java +3 −5 Changes for src/chessComponent/EmptySlotComponent.java: 3 added lines, 5 removed lines. Original line number Diff line number Diff line package chessComponent; import controller.ClickController; import model.ChessColor; import model.ChessType; import model.ChessboardPoint; import java.awt.*; Loading @@ -12,12 +10,12 @@ import java.awt.*; */ public class EmptySlotComponent extends SquareComponent { public EmptySlotComponent(ChessboardPoint chessboardPoint, Point location, ClickController listener, int size) { super(chessboardPoint, location, ChessColor.NONE, listener, size); public EmptySlotComponent(ChessboardPoint chessboardPoint, Point location, ClickController clickController, int size) { super(chessboardPoint, location, clickController, size); } @Override public boolean canMoveTo(SquareComponent[][] chessboard, ChessboardPoint destination) { public boolean canMoveTo(ChessComponent handleChess,SquareComponent[][] chessboard, ChessboardPoint destination) { return false; } Loading
src/chessComponent/SquareComponent.java +2 −0 Changes for src/chessComponent/SquareComponent.java: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -126,4 +126,6 @@ public abstract class SquareComponent extends JComponent { g.setColor(squareColor); g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2); } public abstract boolean canMoveTo(ChessComponent handleChess, SquareComponent[][] chessboard, ChessboardPoint destination); }
src/controller/CheckChessLogic.java +11 −13 Changes for src/controller/CheckChessLogic.java: 11 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -11,8 +11,6 @@ public class CheckChessLogic { /* path 和 军衔 判定分两个method * 则需要两个return同时为true*/ // todo: 1.change chess1x into chessLocation? // 2.override for CANNON // 对吃棋时的 path 判定 // todo : optimize codes below public static boolean checkPath(ChessComponent handleChess, SquareComponent[][] chessboard, SquareComponent destination) { Loading Loading @@ -42,22 +40,22 @@ public class CheckChessLogic { return check; } /* chess1 是第一个选中棋子 * chess2 是第二个选中棋子,即被吃的棋子 * 此处顺应 enum 索引,以 <= 判断吃棋是否合规*/ // TODO: consider the situation when chess2 is not revealed public static boolean checkRank(ChessComponent chess1, ChessComponent chess2) { /* handleChess 是所执棋子 * destinationChess 是第二个选中棋子,即被吃的棋子 * 此处利用 enum 索引,以 <= 判断吃棋是否合规 */ public static boolean checkRank(ChessComponent handleChess, ChessComponent destinationChess) { boolean check = false; //已翻转的情况 //todo: check and delete '!chess1.isReversal()' if (!chess1.isReversal() && !chess2.isReversal()) { // chess1 == SOLDIER && chess2 == CANNON if (chess1.getChessType() == ChessType.SOLDIER && chess2.getChessType() == ChessType.CANNON) { //todo: check and delete '!handleChess.isReversal()' if ( !destinationChess.isReversal()) { // handleChess == SOLDIER && destinationChess == CANNON if (handleChess.getChessType() == ChessType.SOLDIER && destinationChess.getChessType() == ChessType.CANNON) { check = true; } else { check = chess1.getChessType().ordinal() <= chess2.getChessType().ordinal() ? true : false; // check = handleChess.getChessType().ordinal() <= destinationChess.getChessType().ordinal(); check = (destinationChess.getChessType().compareTo(handleChess.getChessType()) >= 0); } } else if (chess2.isReversal() && chess1.getChessType() == ChessType.CANNON) { } else if (destinationChess.isReversal() && handleChess.getChessType() == ChessType.CANNON) { check = true; } return check; Loading
src/controller/ClickController.java +3 −2 Changes for src/controller/ClickController.java: 3 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -28,10 +28,11 @@ public class ClickController { squareComponent.setSelected(false); first.repaint(); first = null; } else if (first.canMoveTo(chessboard.getChessComponents(), squareComponent.getChessboardPoint())) { } else if (first.canMoveTo(first,chessboard.getChessComponents(), squareComponent.getChessboardPoint())) { // todo:check if 'first' is revealed //repaint in swap chess method. chessboard.swapChessComponents(first, squareComponent); // first 是被移动的棋子,squareComponent 是 destination, // first 是所执棋子(handleChess),squareComponent 是 destinationChess, chessboard.clickController.swapPlayer(); first.setSelected(false); Loading