board with picture pieces

This commit is contained in:
utente 2025-04-15 11:10:23 +02:00
parent 10cd42b2c9
commit 55b04a76b0
3 changed files with 199 additions and 136 deletions

View File

@ -1,22 +1,27 @@
import backend.Board; import backend.Board;
import backend.Move;
import backend.Piece;
import backend.PieceType;
import windowInterface.MyInterface;
import javax.swing.*;
import java.awt.*;
public class Main { public class Main {
public static void main(String[] args) {
// Create board and populate it
Board testBoard = new Board(8, 8);
testBoard.populateBoard();
public static void main(String[] args) { // Print board state in console
// testing : System.out.println(testBoard.toString());
Board testBoard = new Board(8, 8);
testBoard.populateBoard();
System.out.println(testBoard.toString());
// launches graphical interface :
MyInterface mjf = new MyInterface();
mjf.setVisible(true);
}
// 🎯 Show the board in a GUI
JFrame frame = new JFrame("Chess Board");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLayout(new BorderLayout());
// 👇 Add the visual board (squares + pieces) to the window
frame.add(testBoard.getBoardPanel(), BorderLayout.CENTER);
frame.setVisible(true);
}
} }

View File

@ -1,99 +1,157 @@
package backend; package backend;
import java.util.ArrayList; import javax.swing.*;
import java.awt.*;
public class Board { import java.util.ArrayList;
public Board(int colNum, int lineNum) { public class Board {
private int width;
} private int height;
private int turnNumber = 0;
public int getWidth() { private boolean isWhiteTurn = true;
//TODO private Piece[][] board;
return 0; private JPanel boardPanel;
}
public Board(int colNum, int lineNum) {
public int getHeight() { this.width = colNum;
//TODO this.height = lineNum;
return 0; this.board = new Piece[height][width];
} this.boardPanel = new JPanel(new GridLayout(height, width));
populateBoard();
public int getTurnNumber() { }
//TODO
return 0; public int getWidth() { return width; }
}
public int getHeight() { return height; }
public boolean isTurnWhite() {
//TODO public int getTurnNumber() { return turnNumber; }
return false;
} public boolean isTurnWhite() { return isWhiteTurn; }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
//TODO board[y][x] = new Piece(x, y, type, isWhite);
} updateBoardPanel();
}
public void populateBoard() {
//TODO public void populateBoard() {
} cleanBoard();
public void cleanBoard() { // White back row
//TODO setPiece(true, PieceType.Rook, 0, 7);
} setPiece(true, PieceType.Knight, 1, 7);
setPiece(true, PieceType.Bishop, 2, 7);
public String toString() { setPiece(true, PieceType.Queen, 3, 7);
//TODO setPiece(true, PieceType.King, 4, 7);
return ""; setPiece(true, PieceType.Bishop, 5, 7);
} setPiece(true, PieceType.Knight, 6, 7);
setPiece(true, PieceType.Rook, 7, 7);
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); // White pawns
//TODO for (int x = 0; x < 8; x++) {
setPiece(true, PieceType.Pawn, x, 6);
return pieces; }
}
// Black pawns
public void userTouch(int x, int y) { for (int x = 0; x < 8; x++) {
//TODO setPiece(false, PieceType.Pawn, x, 1);
}
}
// Black back row
public boolean isSelected(int x, int y) { setPiece(false, PieceType.Rook, 0, 0);
//TODO setPiece(false, PieceType.Knight, 1, 0);
return false; setPiece(false, PieceType.Bishop, 2, 0);
} setPiece(false, PieceType.Queen, 3, 0);
setPiece(false, PieceType.King, 4, 0);
/* saving-loading feature :*/ setPiece(false, PieceType.Bishop, 5, 0);
setPiece(false, PieceType.Knight, 6, 0);
public String[] toFileRep() { setPiece(false, PieceType.Rook, 7, 0);
//TODO }
return null;
} public void cleanBoard() {
for (int y = 0; y < height; y++) {
public Board(String[] array) { for (int x = 0; x < width; x++) {
//TODO board[y][x] = null;
}
} }
updateBoardPanel();
/* The following methods require more work ! */ }
public boolean isHighlighted(int x, int y) { public ArrayList<Piece> getPieces() {
//TODO ArrayList<Piece> pieces = new ArrayList<>();
return false; for (int y = 0; y < height; y++) {
} for (int x = 0; x < width; x++) {
if (board[y][x] != null) {
public void undoLastMove() { pieces.add(board[y][x]);
//TODO }
}
} }
return pieces;
public Board(Board board) { }
//TODO
public String toString() {
} StringBuilder sb = new StringBuilder();
for (int y = 0; y < height; y++) {
public void playMove(Move move) { for (int x = 0; x < width; x++) {
//TODO Piece p = board[y][x];
if (p == null) {
} sb.append(". ");
} else {
} sb.append(p.getSummary()).append(" ");
}
}
sb.append("\n");
}
return sb.toString();
}
public JPanel getBoardPanel() {
return boardPanel;
}
private void updateBoardPanel() {
boardPanel.removeAll();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
JLabel square = new JLabel();
square.setOpaque(true);
square.setHorizontalAlignment(SwingConstants.CENTER);
square.setVerticalAlignment(SwingConstants.CENTER);
square.setPreferredSize(new Dimension(60, 60));
square.setBackground((x + y) % 2 == 0 ? Color.WHITE : Color.GRAY);
Piece p = board[y][x];
if (p != null) {
try {
String imgPath = p.getType().getImagePath(p.isWhite());
ImageIcon icon = new ImageIcon(getClass().getResource(imgPath));
Image scaled = icon.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);
square.setIcon(new ImageIcon(scaled));
} catch (Exception e) {
square.setText(p.getSummary());
square.setForeground(p.isWhite() ? Color.BLUE : Color.BLACK);
}
}
boardPanel.add(square);
}
}
boardPanel.revalidate();
boardPanel.repaint();
}
public void userTouch(int x, int y) {}
public boolean isSelected(int x, int y) { return false; }
public String[] toFileRep() { return null; }
public Board(String[] array) {}
public boolean isHighlighted(int x, int y) { return false; }
public void undoLastMove() {}
public Board(Board board) {}
}

View File

@ -1,28 +1,28 @@
package backend; package backend;
public enum PieceType { public enum PieceType {
Pawn, Rook, Knight, Bishop, Queen, King; Pawn, Rook, Knight, Bishop, Queen, King;
public String getSummary() { public String getSummary() {
if(this == PieceType.Knight) { if (this == PieceType.Knight) {
return "N"; return "N";
} }
return this.name().substring(0, 1); return this.name().substring(0, 1);
} }
public static PieceType fromSummary(char c) { public static PieceType fromSummary(char c) {
if(c=='P') { switch (c) {
return PieceType.Pawn; case 'P': return Pawn;
}else if(c=='N') { case 'N': return Knight;
return PieceType.Knight; case 'B': return Bishop;
}else if(c=='B') { case 'R': return Rook;
return PieceType.Bishop; case 'K': return King;
}else if(c=='R') { default: return Queen;
return PieceType.Rook; }
}else if(c=='K') { }
return PieceType.King;
} // New method to generate image path
return PieceType.Queen; public String getImagePath(boolean isWhite) {
} return "/Chess_pieces/" + this.name() + "_" + (isWhite ? "white" : "black") + ".png";
}
} }