Merge branch 'main' of

https://gitarero.ecam.fr/romain.murphy/OOP_2B1_Project.git into main
This commit is contained in:
samuelsmith 2025-05-13 13:40:19 +02:00
commit bac93938e8
11 changed files with 79 additions and 18 deletions

View File

@ -16,7 +16,7 @@ public class Main {
// launches graphical interface : // launches graphical interface :
MyInterface mjf = new MyInterface(); MyInterface mjf = new MyInterface();
mjf.introduction(); // mjf.introduction();
mjf.setVisible(true); mjf.setVisible(true);
} }

View File

@ -14,7 +14,7 @@ public class Bishop extends Piece {
return getPossibleMoves(board, null); return getPossibleMoves(board, null);
} }
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) { public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();; ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();;

View File

@ -20,6 +20,7 @@ public class Board {
private ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>(); private ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
private LinkedList<BoardHistory> boardHistory = new LinkedList<>(); private LinkedList<BoardHistory> boardHistory = new LinkedList<>();
public boolean timerSwitch = false; public boolean timerSwitch = false;
public boolean timerStart = true;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
@ -214,6 +215,7 @@ public class Board {
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
if ((this.select == false && board.get(y).get(x) != null && this.board.get(y).get(x).isWhite() == turnColor) || (select == true && board.get(y).get(x) != null && board.get(y).get(x).isWhite() == board.get(ym).get(xm).isWhite())) { if ((this.select == false && board.get(y).get(x) != null && this.board.get(y).get(x).isWhite() == turnColor) || (select == true && board.get(y).get(x) != null && board.get(y).get(x).isWhite() == board.get(ym).get(xm).isWhite())) {
timerStart = false;
this.xm = x; this.xm = x;
this.ym = y; this.ym = y;
select = true; select = true;
@ -408,6 +410,9 @@ public class Board {
this.timerSwitch = timerSwitch; this.timerSwitch = timerSwitch;
} }
public boolean getTimerStart() {
return timerStart;
}
} }
///// /////

View File

@ -65,6 +65,9 @@ public class Game extends Thread {
System.out.println("Click out of bounds"); System.out.println("Click out of bounds");
return; return;
} }
if (board.getTimerStart()) {
mjf.startTimer(board.isTurnColor());
}
if(!isAITurn()) { if(!isAITurn()) {
if (aiPlayer.getAllLegalMoves(board.getBoard(), board.isTurnWhite(),board.getLastMove()).size() != 0){ if (aiPlayer.getAllLegalMoves(board.getBoard(), board.isTurnWhite(),board.getLastMove()).size() != 0){
board.userTouch(x, y); board.userTouch(x, y);

View File

@ -14,7 +14,7 @@ public class King extends Piece {
return getPossibleMoves(board, null); return getPossibleMoves(board, null);
} }
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) { public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>(); ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();

View File

@ -14,7 +14,7 @@ public class Queen extends Piece {
return getPossibleMoves(board, null); return getPossibleMoves(board, null);
} }
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) { public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>(); ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();

View File

@ -14,7 +14,7 @@ public class Rook extends Piece {
return getPossibleMoves(board, null); return getPossibleMoves(board, null);
} }
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) { public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board, Move lastMove) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>(); ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();

View File

@ -10,6 +10,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel; import javax.swing.JPanel;
import backend.Game; import backend.Game;
@ -30,6 +31,8 @@ public class JPanelChessBoard extends JPanel {
private boolean selectedPieceIsWhite; private boolean selectedPieceIsWhite;
private PieceType selectedPieceType; private PieceType selectedPieceType;
private boolean pieceAdderMode; private boolean pieceAdderMode;
private Image backgroundImage;
public JPanelChessBoard(MyInterface itf) { public JPanelChessBoard(MyInterface itf) {
super(); super();
@ -38,6 +41,7 @@ public class JPanelChessBoard extends JPanel {
selectedPieceIsWhite = true; selectedPieceIsWhite = true;
selectedPieceType = PieceType.Pawn; selectedPieceType = PieceType.Pawn;
pieceSelectorMode = false; pieceSelectorMode = false;
backgroundImage = new ImageIcon(getClass().getResource("image_OOP.png")).getImage();
try { try {
spriteSheet = ImageIO.read(new File("pieces/newPieces.png")); spriteSheet = ImageIO.read(new File("pieces/newPieces.png"));
@ -81,7 +85,8 @@ public class JPanelChessBoard extends JPanel {
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
this.setBackground(new Color(238,236,208)); g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
// this.setBackground(new Color(238,236,208));
if(pieceSelectorMode) { if(pieceSelectorMode) {
g.drawImage( g.drawImage(
spriteSheet, spriteSheet,
@ -95,6 +100,9 @@ public class JPanelChessBoard extends JPanel {
} }
if (myGame != null) { if (myGame != null) {
// Draw Interface from state of simulator // Draw Interface from state of simulator
g.setColor(new Color(238, 236, 208)); // Your default color
g.fillRect(0, 0, getWidth()+1, getHeight()+1);
repaint();
float cellWidth = cellWidth(); float cellWidth = cellWidth();
float cellHeight = cellHeight(); float cellHeight = cellHeight();
@ -141,6 +149,22 @@ public class JPanelChessBoard extends JPanel {
for (Piece piece : myGame.getPieces()) { for (Piece piece : myGame.getPieces()) {
drawPiece(g,piece); drawPiece(g,piece);
} }
g.setColor(Color.BLACK);
for (int x = 0; x < myGame.getWidth(); x++) {
char letter = (char) ('a' + x);
int xLetter = Math.round(x * cellWidth() + cellWidth() / 2) - 4;
int yLetter = getHeight() - 5;
g.drawString(String.valueOf(letter), xLetter, yLetter);
}
for (int y = 0; y < myGame.getHeight(); y++) {
int number = myGame.getHeight() - y;
int xNumber = 5;
int yNumber = Math.round(y * cellHeight() + cellHeight() / 2) + 5;
g.drawString(String.valueOf(number), xNumber, yNumber);
}
} }
} }

View File

@ -51,12 +51,20 @@ public class MyInterface extends JFrame {
private JLabel whiteTimerLabel; private JLabel whiteTimerLabel;
private JLabel blackTimerLabel; private JLabel blackTimerLabel;
public TimerManager timerManager; public TimerManager timerManager;
/** /**
* Create the frame. * Create the frame.
*/ */
public MyInterface() { public MyInterface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 650, 650); setBounds(10, 10, 800, 800);
contentPane = new JPanel(); contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0)); contentPane.setLayout(new BorderLayout(0, 0));
@ -180,8 +188,7 @@ public class MyInterface extends JFrame {
game.setDefaultSetup(); game.setDefaultSetup();
if (timerManager == null) if (timerManager == null)
timerManager = new TimerManager(whiteTimerLabel, blackTimerLabel, () -> gameOverByTimeout(), time); timerManager = new TimerManager(whiteTimerLabel, blackTimerLabel, () -> gameOverByTimeout(), time);
timerManager.reset(); timerManager.reset(time);
timerManager.start(true); // white starts
} }
public void clickButtonAdder() { public void clickButtonAdder() {
panelDraw.toggleAdderMode(); panelDraw.toggleAdderMode();
@ -236,8 +243,8 @@ public class MyInterface extends JFrame {
game.setAutoPlayerDepth(depth); game.setAutoPlayerDepth(depth);
if (timerManager == null) if (timerManager == null)
timerManager = new TimerManager(whiteTimerLabel, blackTimerLabel, () -> gameOverByTimeout(), time); timerManager = new TimerManager(whiteTimerLabel, blackTimerLabel, () -> gameOverByTimeout(), time);
timerManager.reset(); timerManager.reset(time);
timerManager.start(game.getTurnColor()); // timerManager.start(game.getTurnColor());
game.start(); game.start();
// this.repaint(); // this.repaint();
// update(game.getTurnNumber(), game.getTurnColor()); // update(game.getTurnNumber(), game.getTurnColor());
@ -296,11 +303,28 @@ public class MyInterface extends JFrame {
} }
public void showGameOverMessage(String message) { public void showGameOverMessage(String message) {
JOptionPane.showMessageDialog(this, message, "Game Over", JOptionPane.INFORMATION_MESSAGE);
game.toggleAI(false); game.toggleAI(false);
game.toggleAI(true); game.toggleAI(true);
chckbxWhiteAI.setSelected(false); chckbxWhiteAI.setSelected(false);
chckbxBlackAI.setSelected(false); chckbxBlackAI.setSelected(false);
Object[] options = {"YES", "NO", "Start a new game"};
int choice = JOptionPane.showOptionDialog(this, "GAME OVER \nDo you want to exit? ",
"Game Over",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1] // default: NO
);
switch (choice) {
case 0: System.exit(0); // exit
case 1: ;
case 2: clicButtonStart();
break;
default: ;
}
} }
private int askDifficulty() { private int askDifficulty() {
@ -327,8 +351,8 @@ public class MyInterface extends JFrame {
Object[] options = {"1 min", "2 Min", "5 Min", "10 Min"}; Object[] options = {"1 min", "2 Min", "5 Min", "10 Min"};
int choice = JOptionPane.showOptionDialog( int choice = JOptionPane.showOptionDialog(
this, this,
"Select Difficulty Level:", "Select the duration of the game:",
"Difficulty", "Duration",
JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, JOptionPane.QUESTION_MESSAGE,
null, null,
@ -377,6 +401,9 @@ public class MyInterface extends JFrame {
return 1; return 1;
} }
public void startTimer(boolean isWhite) {
timerManager.start(isWhite);
}
public void gameOverByTimeout() { public void gameOverByTimeout() {

View File

@ -60,7 +60,9 @@ public class TimerManager {
timer.stop(); timer.stop();
} }
public void reset() { public void reset(int time) {
whiteSeconds = time;
blackSeconds = time;
updateLabel(whiteLabel, whiteSeconds); updateLabel(whiteLabel, whiteSeconds);
updateLabel(blackLabel, blackSeconds); updateLabel(blackLabel, blackSeconds);
} }

View File

Before

Width:  |  Height:  |  Size: 367 KiB

After

Width:  |  Height:  |  Size: 367 KiB