Skip to content

Commit

Permalink
better timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmp33 committed Aug 27, 2021
1 parent 158d0fc commit d507c6a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/catalan/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Game {
private final List<Card> deck;
private final List<Card> thePlay; // cards list that participate in the play
private final List<Player> players;
private final Instant startTime;
private int gameTime;
private int round;
private String gameMode;
private boolean ai; // if turn on AI's bot
Expand Down Expand Up @@ -85,11 +85,10 @@ public Game() {
new Card("rei d'oro", "oros", 10, 4)
));

// shuffle the deck, initialize the play and list of players and save game's start time
// shuffle the deck and initialize the play and list of players
Collections.shuffle(deck);
thePlay = new ArrayList<>();
players = new ArrayList<>();
startTime = Instant.now();
}

// welcome message
Expand Down Expand Up @@ -155,7 +154,6 @@ private void askPrintCardsInfo() {
} else if (input.equals("no")) break;
System.out.println("Introdueix \"si\" o \"no\"...");
}
System.out.println();
}

// deal 3 first cards to each player
Expand Down Expand Up @@ -217,6 +215,10 @@ private void printAllCards() {
private void mainLoop() {
System.out.println("El jugador " + players.get(0).getName() + " comença tirant");

// save game's start time
Instant startTime = Instant.now();
System.out.println("[!] Cronòmetre activat!");

String trumpCard = latestCard().getName();
while (true) {
// if the deck is not empty, update the new trump card
Expand All @@ -234,7 +236,11 @@ private void mainLoop() {
validateThePlay();

// if any player hasn't cards, game finishes
if (playersWithoutCards() == players.size()) break;
if (playersWithoutCards() == players.size()) {
gameTime = (int) Duration.between(startTime, Instant.now()).toSeconds();
System.out.println("\n[!] Cronòmetre parat!");
break;
}

// else, for each player, take a card
for (Player p : players) p.takeCard();
Expand Down Expand Up @@ -307,7 +313,7 @@ private String centerStr(int lnLength, String s) {

// print how much the game took to finish
private void printGameTime() {
int sec = (int) Duration.between(startTime, Instant.now()).toSeconds();
int sec = gameTime;
int min = 0;
while (sec >= 60) {
sec -= 60;
Expand Down
18 changes: 12 additions & 6 deletions src/com/labrisca/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Game {
private final List<Card> deck;
private final List<Card> thePlay; // cards list that participate in the play
private final List<Player> players;
private final Instant startTime;
private int gameTime;
private int round;
private String gameMode;
private boolean ai; // if turn on AI's bot
Expand Down Expand Up @@ -85,11 +85,10 @@ public Game() {
new Card("coin's king", "coin", 10, 4)
));

// shuffle the deck, initialize the play and list of players and save game's start time
// shuffle the deck and initialize the play and list of players
Collections.shuffle(deck);
thePlay = new ArrayList<>();
players = new ArrayList<>();
startTime = Instant.now();
}

// welcome message
Expand Down Expand Up @@ -155,7 +154,6 @@ private void askPrintCardsInfo() {
} else if (input.equals("no")) break;
System.out.println("Input \"yes\" or \"no\"...");
}
System.out.println();
}

// deal 3 first cards to each player
Expand Down Expand Up @@ -216,6 +214,10 @@ private void printAllCards() {
private void mainLoop() {
System.out.println(players.get(0).getName() + " starts throwing");

// save game's start time
Instant startTime = Instant.now();
System.out.println("[!] Timer started!");

String trumpCard = latestCard().getName();
while (true) {
// if the deck is not empty, update the new trump card
Expand All @@ -233,7 +235,11 @@ private void mainLoop() {
validateThePlay();

// if any player hasn't cards, game finishes
if (playersWithoutCards() == players.size()) break;
if (playersWithoutCards() == players.size()) {
gameTime = (int) Duration.between(startTime, Instant.now()).toSeconds();
System.out.println("\n[!] Timer stopped!");
break;
}

// else, for each player, take a card
for (Player p : players) p.takeCard();
Expand Down Expand Up @@ -306,7 +312,7 @@ private String centerStr(int lnLength, String s) {

// print how much the game took to finish
private void printGameTime() {
int sec = (int) Duration.between(startTime, Instant.now()).toSeconds();
int sec = gameTime;
int min = 0;
while (sec >= 60) {
sec -= 60;
Expand Down

0 comments on commit d507c6a

Please sign in to comment.