diff --git a/src/Main.java b/src/Main.java index 2ff297c..4316ab3 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,22 +1,27 @@ 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 static void main(String[] args) { + // Create board and populate it + Board testBoard = new Board(8, 8); + testBoard.populateBoard(); - public static void main(String[] args) { - // testing : - Board testBoard = new Board(8, 8); - testBoard.populateBoard(); - System.out.println(testBoard.toString()); - - // launches graphical interface : - MyInterface mjf = new MyInterface(); - mjf.setVisible(true); - } + // Print board state in console + System.out.println(testBoard.toString()); + // 🎯 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); + } } diff --git a/src/backend/Board.java b/src/backend/Board.java index 16af21f..ef4c0ca 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -1,99 +1,157 @@ -package backend; - -import java.util.ArrayList; - -public class Board { - - public Board(int colNum, int lineNum) { - - } - - public int getWidth() { - //TODO - return 0; - } - - public int getHeight() { - //TODO - return 0; - } - - public int getTurnNumber() { - //TODO - return 0; - } - - public boolean isTurnWhite() { - //TODO - return false; - } - - public void setPiece(boolean isWhite, PieceType type, int x, int y) { - //TODO - } - - public void populateBoard() { - //TODO - } - - public void cleanBoard() { - //TODO - } - - public String toString() { - //TODO - return ""; - } - - public ArrayList getPieces() { - ArrayList pieces = new ArrayList<>(); - //TODO - - return pieces; - } - - public void userTouch(int x, int y) { - //TODO - - } - - public boolean isSelected(int x, int y) { - //TODO - return false; - } - - /* saving-loading feature :*/ - - public String[] toFileRep() { - //TODO - return null; - } - - public Board(String[] array) { - //TODO - - } - - /* The following methods require more work ! */ - - public boolean isHighlighted(int x, int y) { - //TODO - return false; - } - - public void undoLastMove() { - //TODO - - } - - public Board(Board board) { - //TODO - - } - - public void playMove(Move move) { - //TODO - - } - -} +package backend; + +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; + +public class Board { + private int width; + private int height; + private int turnNumber = 0; + private boolean isWhiteTurn = true; + private Piece[][] board; + private JPanel boardPanel; + + public Board(int colNum, int lineNum) { + this.width = colNum; + this.height = lineNum; + this.board = new Piece[height][width]; + this.boardPanel = new JPanel(new GridLayout(height, width)); + populateBoard(); + } + + public int getWidth() { return width; } + + public int getHeight() { return height; } + + public int getTurnNumber() { return turnNumber; } + + public boolean isTurnWhite() { return isWhiteTurn; } + + public void setPiece(boolean isWhite, PieceType type, int x, int y) { + board[y][x] = new Piece(x, y, type, isWhite); + updateBoardPanel(); + } + + public void populateBoard() { + cleanBoard(); + + // White back row + setPiece(true, PieceType.Rook, 0, 7); + setPiece(true, PieceType.Knight, 1, 7); + setPiece(true, PieceType.Bishop, 2, 7); + setPiece(true, PieceType.Queen, 3, 7); + setPiece(true, PieceType.King, 4, 7); + setPiece(true, PieceType.Bishop, 5, 7); + setPiece(true, PieceType.Knight, 6, 7); + setPiece(true, PieceType.Rook, 7, 7); + + // White pawns + for (int x = 0; x < 8; x++) { + setPiece(true, PieceType.Pawn, x, 6); + } + + // Black pawns + for (int x = 0; x < 8; x++) { + setPiece(false, PieceType.Pawn, x, 1); + } + + // Black back row + setPiece(false, PieceType.Rook, 0, 0); + setPiece(false, PieceType.Knight, 1, 0); + setPiece(false, PieceType.Bishop, 2, 0); + setPiece(false, PieceType.Queen, 3, 0); + setPiece(false, PieceType.King, 4, 0); + setPiece(false, PieceType.Bishop, 5, 0); + setPiece(false, PieceType.Knight, 6, 0); + setPiece(false, PieceType.Rook, 7, 0); + } + + public void cleanBoard() { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + board[y][x] = null; + } + } + updateBoardPanel(); + } + + public ArrayList getPieces() { + ArrayList pieces = new ArrayList<>(); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (board[y][x] != null) { + pieces.add(board[y][x]); + } + } + } + return pieces; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + 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) {} +} diff --git a/src/backend/PieceType.java b/src/backend/PieceType.java index b7b5304..63c57de 100644 --- a/src/backend/PieceType.java +++ b/src/backend/PieceType.java @@ -1,28 +1,28 @@ package backend; public enum PieceType { - Pawn, Rook, Knight, Bishop, Queen, King; + Pawn, Rook, Knight, Bishop, Queen, King; - public String getSummary() { - if(this == PieceType.Knight) { - return "N"; - } - return this.name().substring(0, 1); - } - - public static PieceType fromSummary(char c) { - if(c=='P') { - return PieceType.Pawn; - }else if(c=='N') { - return PieceType.Knight; - }else if(c=='B') { - return PieceType.Bishop; - }else if(c=='R') { - return PieceType.Rook; - }else if(c=='K') { - return PieceType.King; - } - return PieceType.Queen; - } - + public String getSummary() { + if (this == PieceType.Knight) { + return "N"; + } + return this.name().substring(0, 1); + } + + public static PieceType fromSummary(char c) { + switch (c) { + case 'P': return Pawn; + case 'N': return Knight; + case 'B': return Bishop; + case 'R': return Rook; + case 'K': return King; + default: return Queen; + } + } + + // ✅ New method to generate image path + public String getImagePath(boolean isWhite) { + return "/Chess_pieces/" + this.name() + "_" + (isWhite ? "white" : "black") + ".png"; + } }