fix timer

This commit is contained in:
martinbst 2025-05-07 21:36:00 +02:00
parent 6bb57c1eb8
commit ab467ea11e
10 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,9 @@
BR, , ,BK, ,BR, ,
BP,WP,BP, , ,BP,BP,BP
, , , ,WP, , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
WP,WP,WP, , ,WP,WP,WP
WR,WN,WB,WQ,WK,WB,WN,WR
3W

View File

@ -0,0 +1,9 @@
BR,BN,BB,BQ,BK,BB,BN,BR
,BP,BP,BP,BP,BP,BP,BP
, , , , , , ,
BP, , ,WP, , , ,
, , , , , , ,
, , , , , , ,
WP,WP,WP, ,WP,WP,WP,WP
WR,WN,WB,WQ,WK,WB,WN,WR
4B

View File

@ -0,0 +1,9 @@
, ,BB,BQ,BK, , ,BR
BP,WP,BP,BP, ,BP,BP,BP
, ,BN,BB, ,BP, ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , ,WK, , , ,
3W

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 954 B

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,68 @@
package windowInterface;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class TimerManager {
private int whiteSeconds = 300;
private int blackSeconds = 300;
private boolean whiteTurn = true;
private Timer timer;
private Runnable onTimeout;
private JLabel whiteLabel;
private JLabel blackLabel;
public TimerManager(JLabel whiteLabel, JLabel blackLabel, Runnable onTimeout, int time) {
this.whiteLabel = whiteLabel;
this.blackLabel = blackLabel;
this.onTimeout = onTimeout;
this.whiteSeconds = time;
this.blackSeconds = time;
timer = new Timer(1000, e -> tick());
}
public void start(boolean whiteStarts) {
this.whiteTurn = whiteStarts;
timer.start();
}
public void switchTurn() {
whiteTurn = !whiteTurn;
}
private void tick() {
if (whiteTurn) {
whiteSeconds--;
updateLabel(whiteLabel, whiteSeconds);
if (whiteSeconds <= 0) timeout("White");
} else {
blackSeconds--;
updateLabel(blackLabel, blackSeconds);
if (blackSeconds <= 0) timeout("Black");
}
}
private void updateLabel(JLabel label, int seconds) {
int mins = seconds / 60;
int secs = seconds % 60;
label.setText(String.format("%s: %02d:%02d", label == whiteLabel ? "White" : "Black", mins, secs));
}
private void timeout(String player) {
timer.stop();
onTimeout.run(); // you define what happens on timeout
JOptionPane.showMessageDialog(null, player + " ran out of time! Game over.");
}
public void stop() {
timer.stop();
}
public void reset() {
updateLabel(whiteLabel, whiteSeconds);
updateLabel(blackLabel, blackSeconds);
}
}