The output is exactly what the exercise expected. But I still get the error
The player should not display a victory message if the variable isWinnerFound is already set to true (i.e. the winner has already been found).
Anyone knows why is that?
Thanks in advance! package com.codegym.task.task16.task1627;
import java.util.ArrayList;
import java.util.List;
/**
* Three people play the game. Each player (Gamer) is characterized by two parameters:
* last name (name) and the actions-per-second (rating).
* Display the actions taken and determine the winner and the losers
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
OnlineGame onlineGame = new OnlineGame();
onlineGame.start();
}
public static class OnlineGame extends Thread {
public static volatile boolean isWinnerFound = false;
public static List<String> actions = new ArrayList<>();
static {
actions.add("Start game");
actions.add("Gather resources");
actions.add("Grow economy");
actions.add("Kill enemies");
}
protected Gamer gamer1 = new Gamer("Smith", 3);
protected Gamer gamer2 = new Gamer("Jones", 1);
protected Gamer gamer3 = new Gamer("Gates", 5);
public void run() {
gamer1.start();
gamer2.start();
gamer3.start();
while (!isWinnerFound) {
}
gamer1.interrupt();
gamer2.interrupt();
gamer3.interrupt();
}
}
public static class Gamer extends Thread {
private int rating;
public Gamer(String name, int rating) {
super(name);
this.rating = rating;
}
@Override
public void run() {
for (String action : OnlineGame.actions) {
System.out.println(getName() + ":" + action);
try {
Thread.sleep(1000 / rating);
} catch (InterruptedException e) {
System.out.println(getName() + ":lost!");
return;
}
}
System.out.println(getName() + ":won!");
OnlineGame.isWinnerFound = true;
}
}
}