Compare commits
2 Commits
55b04a76b0
...
487cd37be6
| Author | SHA1 | Date |
|---|---|---|
|
|
487cd37be6 | |
|
|
690ee4f1dc |
|
|
@ -1,27 +1,19 @@
|
|||
import backend.Board;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import windowInterface.MyInterface;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Create board and populate it
|
||||
Board testBoard = new Board(8, 8);
|
||||
testBoard.populateBoard();
|
||||
|
||||
// Print board state in console
|
||||
System.out.println(testBoard.toString());
|
||||
public static void main(String[] args) {
|
||||
// testing :
|
||||
Board testBoard = new Board(8, 8);
|
||||
testBoard.populateBoard();
|
||||
System.out.println(testBoard.toString());
|
||||
System.out.println(testBoard.getWidth() + "*" + testBoard.getHeight());
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,157 +1,157 @@
|
|||
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<Piece> getPieces() {
|
||||
ArrayList<Piece> 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) {}
|
||||
}
|
||||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Board {
|
||||
|
||||
private int colNum;
|
||||
private int lineNum;
|
||||
private Piece[][] board;
|
||||
private int turnNumber = 0;
|
||||
private boolean isWhiteTurn = true;
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.colNum = colNum;
|
||||
this.lineNum = lineNum;
|
||||
this.board = new Piece[lineNum][colNum];
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return colNum;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return lineNum;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void populateBoard() {
|
||||
cleanBoard(); // Start with an empty board
|
||||
|
||||
// Pawns
|
||||
for (int x = 0; x < getWidth(); x++) {
|
||||
setPiece(false, PieceType.Pawn, x, 1); // Black pawns
|
||||
setPiece(true, PieceType.Pawn, x, 6); // White pawns
|
||||
}
|
||||
|
||||
// Rooks
|
||||
setPiece(false, PieceType.Rook, 0, 0);
|
||||
setPiece(false, PieceType.Rook, 7, 0);
|
||||
setPiece(true, PieceType.Rook, 0, 7);
|
||||
setPiece(true, PieceType.Rook, 7, 7);
|
||||
|
||||
// Knights
|
||||
setPiece(false, PieceType.Knight, 1, 0);
|
||||
setPiece(false, PieceType.Knight, 6, 0);
|
||||
setPiece(true, PieceType.Knight, 1, 7);
|
||||
setPiece(true, PieceType.Knight, 6, 7);
|
||||
|
||||
// Bishops
|
||||
setPiece(false, PieceType.Bishop, 2, 0);
|
||||
setPiece(false, PieceType.Bishop, 5, 0);
|
||||
setPiece(true, PieceType.Bishop, 2, 7);
|
||||
setPiece(true, PieceType.Bishop, 5, 7);
|
||||
|
||||
// Queens
|
||||
setPiece(false, PieceType.Queen, 3, 0);
|
||||
setPiece(true, PieceType.Queen, 3, 7);
|
||||
|
||||
// Kings
|
||||
setPiece(false, PieceType.King, 4, 0);
|
||||
setPiece(true, PieceType.King, 4, 7);
|
||||
}
|
||||
|
||||
public void cleanBoard() {
|
||||
for (int y = 0; y < getHeight(); y++) {
|
||||
for (int x = 0; x < getWidth(); x++) {
|
||||
board[y][x] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int y = 0; y < getHeight(); y++) {
|
||||
for (int x = 0; x < getWidth(); x++) {
|
||||
Piece piece = board[y][x];
|
||||
if (piece == null) {
|
||||
sb.append(". ");
|
||||
} else {
|
||||
String symbol = piece.getType().getSummary();
|
||||
sb.append(piece.isWhite() ? symbol.toUpperCase() : symbol.toLowerCase()).append(" ");
|
||||
}
|
||||
}
|
||||
sb.append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public ArrayList<Piece> getPieces() {
|
||||
ArrayList<Piece> pieces = new ArrayList<>();
|
||||
for (int y = 0; y < getHeight(); y++) {
|
||||
for (int x = 0; x < getWidth(); x++) {
|
||||
if (board[y][x] != null) {
|
||||
pieces.add(board[y][x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue