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 :
MyInterface mjf = new MyInterface();
mjf.introduction();
// mjf.introduction();
mjf.setVisible(true);
}

View File

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

View File

@ -20,6 +20,7 @@ public class Board {
private ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
private LinkedList<BoardHistory> boardHistory = new LinkedList<>();
public boolean timerSwitch = false;
public boolean timerStart = true;
public Board(int colNum, int lineNum) {
this.width = colNum;
@ -214,6 +215,7 @@ public class Board {
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())) {
timerStart = false;
this.xm = x;
this.ym = y;
select = true;
@ -408,6 +410,9 @@ public class Board {
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");
return;
}
if (board.getTimerStart()) {
mjf.startTimer(board.isTurnColor());
}
if(!isAITurn()) {
if (aiPlayer.getAllLegalMoves(board.getBoard(), board.isTurnWhite(),board.getLastMove()).size() != 0){
board.userTouch(x, y);

View File

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

View File

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

View File

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

View File

@ -10,6 +10,7 @@ import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import backend.Game;
@ -30,6 +31,8 @@ public class JPanelChessBoard extends JPanel {
private boolean selectedPieceIsWhite;
private PieceType selectedPieceType;
private boolean pieceAdderMode;
private Image backgroundImage;
public JPanelChessBoard(MyInterface itf) {
super();
@ -38,6 +41,7 @@ public class JPanelChessBoard extends JPanel {
selectedPieceIsWhite = true;
selectedPieceType = PieceType.Pawn;
pieceSelectorMode = false;
backgroundImage = new ImageIcon(getClass().getResource("image_OOP.png")).getImage();
try {
spriteSheet = ImageIO.read(new File("pieces/newPieces.png"));
@ -81,7 +85,8 @@ public class JPanelChessBoard extends JPanel {
@Override
protected void paintComponent(Graphics 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) {
g.drawImage(
spriteSheet,
@ -95,6 +100,9 @@ public class JPanelChessBoard extends JPanel {
}
if (myGame != null) {
// 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 cellHeight = cellHeight();
@ -141,6 +149,22 @@ public class JPanelChessBoard extends JPanel {
for (Piece piece : myGame.getPieces()) {
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 blackTimerLabel;
public TimerManager timerManager;
/**
* Create the frame.
*/
public MyInterface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 650, 650);
setBounds(10, 10, 800, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
@ -180,8 +188,7 @@ public class MyInterface extends JFrame {
game.setDefaultSetup();
if (timerManager == null)
timerManager = new TimerManager(whiteTimerLabel, blackTimerLabel, () -> gameOverByTimeout(), time);
timerManager.reset();
timerManager.start(true); // white starts
timerManager.reset(time);
}
public void clickButtonAdder() {
panelDraw.toggleAdderMode();
@ -236,8 +243,8 @@ public class MyInterface extends JFrame {
game.setAutoPlayerDepth(depth);
if (timerManager == null)
timerManager = new TimerManager(whiteTimerLabel, blackTimerLabel, () -> gameOverByTimeout(), time);
timerManager.reset();
timerManager.start(game.getTurnColor());
timerManager.reset(time);
// timerManager.start(game.getTurnColor());
game.start();
// this.repaint();
// update(game.getTurnNumber(), game.getTurnColor());
@ -296,11 +303,28 @@ public class MyInterface extends JFrame {
}
public void showGameOverMessage(String message) {
JOptionPane.showMessageDialog(this, message, "Game Over", JOptionPane.INFORMATION_MESSAGE);
game.toggleAI(false);
game.toggleAI(false);
game.toggleAI(true);
chckbxWhiteAI.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() {
@ -327,8 +351,8 @@ public class MyInterface extends JFrame {
Object[] options = {"1 min", "2 Min", "5 Min", "10 Min"};
int choice = JOptionPane.showOptionDialog(
this,
"Select Difficulty Level:",
"Difficulty",
"Select the duration of the game:",
"Duration",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
@ -377,6 +401,9 @@ public class MyInterface extends JFrame {
return 1;
}
public void startTimer(boolean isWhite) {
timerManager.start(isWhite);
}
public void gameOverByTimeout() {

View File

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

View File

Before

Width:  |  Height:  |  Size: 367 KiB

After

Width:  |  Height:  |  Size: 367 KiB