Commit 2d9512db authored by jafar.mahmood's avatar jafar.mahmood

Add BinarySearchHacker thread and tested it and add the printing of the number...

Add BinarySearchHacker thread and tested it and add the printing of the number of attempts for each hacker.
parent 1f966df3
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -8,8 +8,11 @@ public class AscendingHackerThread extends Hacker { ...@@ -8,8 +8,11 @@ public class AscendingHackerThread extends Hacker {
@Override @Override
public void hack() { public void hack() {
for (int guess = 0; guess <= 9999; guess++) { for (int guess = 0; guess <= 9999; guess++) {
attempts++;
if (vault.isCorrectPassword(guess)) { if (vault.isCorrectPassword(guess)) {
System.out.println(this.getName() + " guessed the password " + guess); System.out.println(this.getName() + " guessed the password " + guess);
// print attempts from all hackers before exiting
Hacker.printAllAttempts();
System.exit(0); System.exit(0);
} }
} }
......
public class BinarySearchHackerThread extends Hacker {
public BinarySearchHackerThread(Vault vault) {
super(vault);
this.setName("BinarySearchHackerThread");
this.setPriority(Thread.MAX_PRIORITY);
}
@Override
public void hack() {
int low = 0;
int high = 9999;
while (low <= high) {
int mid = (low + high) / 2;
attempts++;
if (vault.isCorrectPassword(mid)) {
System.out.println(this.getName() + " guessed the password.");
Hacker.printAllAttempts();
System.exit(0);
} else if (mid < vault.getSecretCode()) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
}
...@@ -5,12 +5,14 @@ public class DescendingHackerThread extends Hacker { ...@@ -5,12 +5,14 @@ public class DescendingHackerThread extends Hacker {
this.setName("DescendingHackerThread"); this.setName("DescendingHackerThread");
this.setPriority(Thread.MAX_PRIORITY); this.setPriority(Thread.MAX_PRIORITY);
} }
@Override @Override
public void hack() { public void hack() {
for (int guess = 9999; guess >= 0; guess--) { for (int guess = 9999; guess >= 0; guess--) {
attempts++;
if (vault.isCorrectPassword(guess)) { if (vault.isCorrectPassword(guess)) {
System.out.println(this.getName() + " guessed the password " + guess); System.out.println(this.getName() + " guessed the password " + guess);
Hacker.printAllAttempts();
System.exit(0); System.exit(0);
} }
} }
......
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public abstract class Hacker extends Thread { public abstract class Hacker extends Thread {
protected final Vault vault; protected final Vault vault;
private static final List<Hacker> ALL_HACKERS = Collections.synchronizedList(new ArrayList<>());
protected int attempts = 0;
public Hacker(Vault vault) { public Hacker(Vault vault) {
this.vault = vault; this.vault = vault;
ALL_HACKERS.add(this);
} }
public abstract void hack(); public abstract void hack();
@Override @Override
public void run() { hack(); } public void run() { hack(); }
public int getAttempts() { return attempts; }
// helper to print attempts of all hackers
public static void printAllAttempts() {
synchronized (ALL_HACKERS) {
for (Hacker h : ALL_HACKERS) {
System.out.println(h.getName() + " attempted: " + h.getAttempts() + " times");
}
}
}
} }
...@@ -3,10 +3,12 @@ public class Main { ...@@ -3,10 +3,12 @@ public class Main {
Vault vault = new Vault(); Vault vault = new Vault();
Thread ascendingHacker = new AscendingHackerThread(vault); Thread ascendingHacker = new AscendingHackerThread(vault);
Thread descendingHacker = new DescendingHackerThread(vault); Thread descendingHacker = new DescendingHackerThread(vault);
Thread binarySearchHacker = new BinarySearchHackerThread(vault);
Thread police = new PoliceThread(10); Thread police = new PoliceThread(10);
ascendingHacker.start(); ascendingHacker.start();
descendingHacker.start(); descendingHacker.start();
binarySearchHacker.start();
police.start(); police.start();
} }
} }
...@@ -13,6 +13,7 @@ public class PoliceThread extends Thread { ...@@ -13,6 +13,7 @@ public class PoliceThread extends Thread {
Thread.sleep(1000); Thread.sleep(1000);
} }
System.out.println("Police have arrived! Game over for hackers."); System.out.println("Police have arrived! Game over for hackers.");
Hacker.printAllAttempts();
System.exit(0); System.exit(0);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
......
...@@ -22,6 +22,10 @@ public class Vault { ...@@ -22,6 +22,10 @@ public class Vault {
return this.secretCode == guess; return this.secretCode == guess;
} }
public int getSecretCode() {
return secretCode;
}
private static int randBetween(int min, int max) { private static int randBetween(int min, int max) {
if (min > max) throw new IllegalArgumentException("min > max"); if (min > max) throw new IllegalArgumentException("min > max");
if (max == Integer.MAX_VALUE) { if (max == Integer.MAX_VALUE) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment