java - 国际象棋接口与 Piece IMG 实现
问题描述
我目前正在为我的数据结构类制作一个国际象棋程序作为项目。我们几乎已经完成了大部分代码,但由于某种原因,我无法弄清楚如何将这些部分放在任何帮助上,将不胜感激。我们正在使用 JAVA 和 Swing
主要目标:让 IMG 进入棋盘:
public abstract class UserInterface {
//FIELDS
protected Board board;
//The board that the UI will be used to interact with
//CONSTRUCTORS
public UserInterface(Board board) {
//Constructor for UserInterface implementers
this.board = board;
}
public UserInterface() {
}
//OTHER
public abstract Move promptMove();
public abstract PieceType promptPromotion();
public abstract void updateBoard();
public abstract void playGame(Interactable whiteUser, Interactable blackUser);
}
class CommandInterface extends UserInterface {
private GraphicInterface gui = null;
//CONSTRUCTORS
public CommandInterface(Board board) {
//Main constructor for the CommandInterface
super(board);
gui = new GraphicInterface(board, null);
gui.initializeGui();
}
//OTHER
public void playGame(Interactable whiteUser, Interactable blackUser) {
//Starts a game using the UI and the board.
//[TEST CODE] Probably will clean this up later
board.setUsers(whiteUser, blackUser);
updateBoard();
Scanner input = new Scanner(System.in);
boolean gameOver = false;
while(!gameOver) {
boolean illegalMove;
do {
try {
// System.out.println("Press enter to move");
// String s = input.nextLine();
board.doMove(board.getCurrentUser().getMove());
illegalMove = false;
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
illegalMove = true;
}
if(board.getState() == BoardState.CHECKMATE) {
System.out.println("\n" + board.getTurn().toString() + " is now checkmated. Game over!");
gameOver = true;
illegalMove = false;
} else if(board.getState() == BoardState.CHECK) {
System.out.println("\n" + board.getTurn().toString() + " has been put in check!");
illegalMove = false;
} else if(board.getState() == BoardState.STALEMATE) {
System.out.println("\n" + board.getTurn().toString() + " is now stalemated. Game over!");
gameOver = true;
illegalMove = false;
}
} while(illegalMove);
updateBoard();
}
}
public Move promptMove() {
//Takes user input for a move and returns the Move object
Scanner input = new Scanner(System.in);
String move;
boolean again;
do {
System.out.print("Enter your move (? for help): ");
move = input.nextLine();
if(move.equals("?")) {
//If the user asks for help
System.out.printf("%nHELP: To move, you must type a move in the following format:"
+ "%n<STARTING TILE> <ENDING TILE>"
+ "%nThe starting tile is the tile of the piece you wish to move."
+ "%nThe ending tile is the tile you wish to move your piece to."
+ "%nEach tile is notated with \"<COLUMN><RANK>\", example: \"e5\""
+ "%n%nFull example move: \"a5 g5\"%n");
again = true;
} else if(move.equalsIgnoreCase("CASTLE")) {
System.out.print("Enter the movement of the King: ");
move = input.nextLine();
return new Move(board, move, true);
} else again = false;
} while(again);
//Reprompt if the user asks for help
return new Move(board, move);
//Returns a Move object made from the SMN string
}
public PieceType promptPromotion() {
Scanner input = new Scanner(System.in);
System.out.print("Pawn has promotion available.\nWhich piece to promote to? ");
while(true) {
String type = input.nextLine();
PieceType promotion = PieceType.NONE;
if (type.equalsIgnoreCase("PAWN")) promotion = PieceType.PAWN;
else if (type.equalsIgnoreCase("ROOK")) promotion = PieceType.ROOK;
else if (type.equalsIgnoreCase("KNIGHT")) promotion = PieceType.KNIGHT;
else if (type.equalsIgnoreCase("BISHOP")) promotion = PieceType.BISHOP;
else if (type.equalsIgnoreCase("QUEEN")) promotion = PieceType.QUEEN;
else if (type.equalsIgnoreCase("KING")) promotion = PieceType.KING;
else if (type.equalsIgnoreCase("EARL")) promotion = PieceType.EARL;
else if (type.equalsIgnoreCase("MONK")) promotion = PieceType.MONK;
if(promotion == PieceType.NONE) {
System.out.print("\nInvalid type, please try again: ");
continue;
}
return promotion;
}
}
public void updateBoard() {
//Prints the board in basic ASCII format.
gui.updateBoard();
drawCaptured(Color.WHITE);
System.out.println();
//Adds a line before the board starts printing
int squaresPrinted = 0;
//Keeps track of the total number of squares that has been printed
for(int y = board.getZeroSize(); y >= 0; y--) {
//Loop through y-values from top to bottom
if(y != board.getZeroSize()) System.out.println();
//If not on the first iteration of y-loop, go down a line (avoids leading line break)
System.out.print((y + 1) + " ");
//Print the rank number
for(int x = 0; x <= board.getZeroSize(); x++) {
//Loop through x-values from left to right
if(board.pieceAt(x, y) != null) {
//If there is a piece on the tile
if(squaresPrinted % 2 == 0) System.out.print("[" + board.pieceAt(x, y).getString() + "]");
//If squaresPrinted is even, print "[<pString>]"
else System.out.print("(" + board.pieceAt(x, y).getString() + ")");
//If squaresPrinted is odd, print "(<pString>)"
} else {
//If there is no piece on the tile
if(squaresPrinted % 2 == 0) System.out.print("[ ]");
//If squaresPrinted is even, print "[ ]"
else System.out.print("( )");
//If squaresPrinted is odd, print "( )"
}
squaresPrinted++;
//Increment squaresPrinted for each iteration of the x-loop
}
squaresPrinted++;
//Increment squaresPrinted for each iteration of the y-loop
}
System.out.println();
System.out.print(" ");
//Print an extra line and the leading whitespace for the column identifiers
for(int i = 0; i <= board.getZeroSize(); i++) {
//Repeat <size> times
System.out.print(" " + (char) (i + 97));
//Print the column identifier chars by casting from int
}
drawCaptured(Color.BLACK);
//Prints all black pieces that have been captured
System.out.println();
}
private void drawCaptured(Color color) {
//Prints captured pieces of either color.
System.out.println();
//Prints a blank line
ArrayList<String> capturedPieces = board.getCaptured(color);
if(capturedPieces == null) return;
for(String p : capturedPieces) {
System.out.print(p + " ");
//TODO: Remove trailing whitespace
}
}
}
@SuppressWarnings("ALL")
class GraphicInterface extends UserInterface implements MouseListener {
private JButton[][] chessBoardSquares = null;
private JButton buttonsAndLabels = new JButton();
private JLabel message = null;
private String columnNames = null;
private Board board = null;
private JFrame frame;
public ImageIcon BK;
public ImageIcon WP;
public ImageIcon WR;
public ImageIcon WN;
public ImageIcon WB;
public ImageIcon WQ;
public ImageIcon WK;
public ImageIcon BP;
public ImageIcon BR;
public ImageIcon BN;
public ImageIcon BB;
public ImageIcon BQ;
public ImageIcon blank;
private Object ImageIcon;
public GraphicInterface(Board board, GraphicInterface frame) {
super(board);
// super(board);
this.board = board;
try {
//remove later
String path = "src/chuss/icons/";
new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BK.gif")));
new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WP.gif")));
new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WR.gif")));
WN = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WN.gif")));
WB = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WB.gif")));
WQ = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WQ.gif")));
WK = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WK.gif")));
BP = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BP.gif")));
BR = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BR.gif")));
BN = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BN.gif")));
BB = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BB.gif")));
BQ = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BQ.gif")));
blank = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "blank.png")));
buttonsAndLabels.addMouseListener((MouseListener) this);
buttonsAndLabels.add(buttonsAndLabels);
buttonsAndLabels.setSize(600, 600);
//buttonsAndLabels.getContentPane().setPreferredSize(new Dimension(800, 700));
} catch(IOException e) {
e.getCause();
e.printStackTrace();
}
buttonsAndLabels = new JButton((Action) new BorderLayout(3, 3));
chessBoardSquares = new JButton[8][8];
message = new JLabel("Chuss Champ is ready to play!");
columnNames = "ABCDEFGH";
initializeGui();
}
public GraphicInterface(JButton buttonsAndLabels) {
super();
}
public GraphicInterface(Board board) {
}
@Override
public Move promptMove() {
return null;
}
@Override
public PieceType promptPromotion() {
return null;
}
public final JButton initializeGui() {
// set up the main GUI
assert false;
buttonsAndLabels.setBorder(new EmptyBorder(5, 5, 5, 5));
JToolBar tools = new JToolBar();
tools.setFloatable(false);
buttonsAndLabels.add(tools, BorderLayout.PAGE_START);
tools.add(new JButton("New")); // TODO - add functionality!
tools.add(new JButton("Save")); // TODO - add functionality!
tools.add(new JButton("Restore")); // TODO - add functionality!
tools.addSeparator();
tools.add(new JButton("Resign")); // TODO - add functionality!
tools.addSeparator();
tools.add(message);
buttonsAndLabels.add(new JLabel("?"), BorderLayout.LINE_START);
// create the chess board squares
buttonsAndLabels = new JButton((Action) new GridLayout(0, 9));
buttonsAndLabels.setBorder(new LineBorder(java.awt.Color.BLACK));
buttonsAndLabels.add((Component) ImageIcon);
updateBoard();
Runnable r = () -> {
JFrame f = new JFrame("CHUSS");
f.add(getGui());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// ensures the minimum size is enforced.
f.setMinimumSize(f.getSize());
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
assert false;
return null;
}
public void updateBoard() {
//Prints the board in graphical format.
buttonsAndLabels.removeAll();
Insets buttonMargin = new Insets(0,0,0,0);
for (int ii = 7; ii >= 0; ii--) {
for (int jj = 0; jj < chessBoardSquares.length; jj++) {
JButton b = new JButton();
b.setMargin(buttonMargin);
// our chess pieces are 64x64 px in size, so we'll
// 'fill this in' using a transparent icon..
Piece p = board.pieceAt(jj, ii);
ImageIcon icon = new ImageIcon();
if (p instanceof Pawn && p.getColor() == Color.WHITE) icon = WP;
else if (p instanceof Rook && p.getColor() == Color.WHITE) icon = WR;
else if (p instanceof Knight && p.getColor() == Color.WHITE) icon = WN;
else if (p instanceof Bishop && p.getColor() == Color.WHITE) icon = WB;
else if (p instanceof Queen && p.getColor() == Color.WHITE) icon = WQ;
else if (p instanceof King && p.getColor() == Color.WHITE) icon = WK;
else if (p instanceof Pawn && p.getColor() == Color.BLACK) icon = BP;
else if (p instanceof Rook && p.getColor() == Color.BLACK) icon = BR;
else if (p instanceof Knight && p.getColor() == Color.BLACK) icon = BN;
else if (p instanceof Bishop && p.getColor() == Color.BLACK) icon = BB;
else if (p instanceof Queen && p.getColor() == Color.BLACK) icon = BQ;
else if (p instanceof King && p.getColor() == Color.BLACK) icon = BK;
else if (p == null) icon = blank;
b.setIcon(icon);
if ((jj % 2 == 1 && ii % 2 == 1)
//) {
|| (jj % 2 == 0 && ii % 2 == 0)) {
b.setBackground(java.awt.Color.WHITE);
} else {
b.setBackground(java.awt.Color.GRAY);
}
chessBoardSquares[jj][ii] = b;
buttonsAndLabels.add(chessBoardSquares[jj][ii]);
}
}
initializeGui();
// fill the top row
for (int ii = 0; ii <= 7; ii++) {
buttonsAndLabels.add(
new JLabel(columnNames.substring(ii, ii + 1),
SwingConstants.CENTER));
}
// fill the black non-pawn piece row
for (int ii = 7; ii >= 0; ii--) {
for (int jj = 0; jj < 8; jj++) {
if (jj == 0) {
buttonsAndLabels.add(new JLabel("" + (ii + 1),
SwingConstants.CENTER));
}
buttonsAndLabels.add(chessBoardSquares[jj][ii]);
}
}
buttonsAndLabels.updateUI();
}
@Override
public void playGame(Interactable whiteUser, Interactable blackUser) {
GraphicInterface gui = new GraphicInterface(buttonsAndLabels);
gui.getChessBoard();
}
public JComponent getChessBoard() {
return buttonsAndLabels;
}
public JComponent getGui() {
return buttonsAndLabels;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
解决方案
推荐阅读
- react-native - 如何链接到 react-native 中的电话号码?
- java - Hibernate SessionFactory 和 CrudRepository 的区别
- javascript - 我对订阅所做的每个查询都会返回不同的结果
- swift - 使用 NSAttributedString 绘图时在非视网膜 Mac 上呈现模糊的字体
- json - 在 React 中保持数据更新
- tfs - VS 2019 及旧版本的 TFS
- delphi - 离线时需要给定用户的 Active Directory 组列表
- sql-server - 使用 Powershell SQL Server 模块高效检索表
- javascript - 通过 GET 请求使用上一页的数据加载模板页面?
- dart - (颤振)在网格视图生成器中选择对项目的影响