/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package guesnumbergame;

import static guesnumbergame.GuesNumberGame.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Penev
 */
public abstract class Player implements Runnable {

    private Thread thread;
    protected String name;
    protected int accuracy = 0;
    protected int bestAnwer = MAX_SECRETNUMBERVALUE + 1;

    public Player(String name) {
        // System.out.println(name + " is creating...");
        this.name = name;
    }

    protected abstract int GuessNumber();

    public void Play() throws InterruptedException {
        while (!finishGame) {

            Thread.sleep(this.accuracy);
            int guess = this.GuessNumber();
            usedNumbers.add(guess);
            // System.out.println(this.name + " guess:" + guess);

            if (guess == secretNumber) {
                // add Winner to results
                results.add(this.name + " - WINNER");

                finishGame = true;
                return;
            } else {
                this.accuracy = Math.abs(secretNumber - guess);

                if (this.accuracy < this.bestAnwer) {
                    this.bestAnwer = guess;
                }
            }
        }

        //add bestAnswer to results
        results.add(this.name + " - " + bestAnwer);
    }

    public void run() {

        // System.out.println(this.name + " is running...");

        try {
            this.Play();
        } catch (InterruptedException ex) {
            Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (results.size() == numberOfPlayers) {
            System.out.println("---RESULTS---");
            System.out.println(String.join("\n", results));
            System.out.println("Secret number was" + secretNumber);
        }
    }

    public void start() {
        // System.out.println(this.name + " is starting...");
        if (thread == null) {
            thread = new Thread(this, this.name);
            thread.start();
        }
    }
}

@@ -759,16 +759,17 @@
 ption %7B%0A
+%0A
         
@@ -872,24 +872,69 @@
 ssNumber();%0A
+            synchronized (usedNumbers) %7B%0A    
             
@@ -957,16 +957,30 @@
 guess);%0A
+            %7D%0A
         
@@ -1075,24 +1075,70 @@
 etNumber) %7B%0A
+%0A                synchronized (results) %7B%0A    
             
@@ -1158,32 +1158,36 @@
 nner to results%0A
+    
                 
@@ -1223,16 +1223,34 @@
 INNER%22);
+%0A                %7D
 %0A%0A      
@@ -1521,32 +1521,69 @@
    %7D%0A        %7D%0A%0A
+        synchronized (results) %7B%0A    
         //add be
@@ -1602,32 +1602,36 @@
 results%0A        
+  
+  
 results.add(this
@@ -1654,24 +1654,34 @@
 bestAnwer);%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -1761,17 +1761,16 @@
 g...%22);%0A
-%0A
         
@@ -1929,32 +1929,69 @@
 ex);%0A        %7D%0A%0A
+        synchronized (results) %7B%0A    
         if (resu
@@ -2027,32 +2027,36 @@
 ) %7B%0A            
+   
+ 
 System.out.print
@@ -2072,24 +2072,28 @@
 SULTS---%22);%0A
+    
             
@@ -2144,32 +2144,36 @@
 ));%0A            
+   
+ 
 System.out.print
@@ -2193,16 +2193,17 @@
 mber was
+ 
 %22 + secr
@@ -2209,24 +2209,38 @@
 retNumber);%0A
+            %7D%0A
         %7D%0A  

Synchronize shared resources