Commit b79bcdf5 authored by tammam.alsoleman's avatar tammam.alsoleman

ADD_NumberOfAttempts

parent d05b2bbe
public class AscendingHackerThread extends Thread { public class AscendingHackerThread extends Thread {
private Vault vault; private Vault vault;
private int attempts;
public AscendingHackerThread(Vault vault) { public AscendingHackerThread(Vault vault) {
this.vault = vault; // Store the vault reference this.vault = vault; // Store the vault reference
this.setName("AscendingHacker"); // Give this thread a clear name this.setName("AscendingHacker"); // Give this thread a clear name
this.setPriority(Thread.MAX_PRIORITY); // Give high priority this.setPriority(Thread.MAX_PRIORITY); // Give high priority
this.attempts = 0;
} }
@Override @Override
...@@ -14,14 +16,20 @@ public class AscendingHackerThread extends Thread { ...@@ -14,14 +16,20 @@ public class AscendingHackerThread extends Thread {
// Try every number from 0 to 9999 // Try every number from 0 to 9999
for (int guess = 0; guess < 10000; guess++) { for (int guess = 0; guess < 10000; guess++) {
// Check if this guess is correct // Check if this guess is correct
attempts++;
if (vault.isCorrectPassword(guess)) { if (vault.isCorrectPassword(guess)) {
System.out.println(this.getName() + " cracked the vault! Password: " + guess); System.out.println(this.getName() + " cracked the vault! Password: " + guess);
System.out.println("Total attempts: " + attempts);
System.exit(0); // End the entire program System.exit(0); // End the entire program
} }
} }
// If we get here, we didn't find the password // If we get here, we didn't find the password
System.out.println(this.getName() + " finished but didn't find the password!"); System.out.println(this.getName() + " finished but didn't find the password! Total attempts: " + attempts);
}
public int getAttempts() {
return attempts;
} }
} }
\ No newline at end of file
...@@ -25,7 +25,7 @@ public class BinarySearchHackerThread extends Thread { ...@@ -25,7 +25,7 @@ public class BinarySearchHackerThread extends Thread {
if (result == 0) { if (result == 0) {
// Password is true // Password is true
System.out.println(this.getName() + " cracked the vault! Password: " + mid); System.out.println(this.getName() + " cracked the vault! Password: " + mid);
System.out.println("Binary search completed in " + attempts + " attempts"); System.out.println("Total attempts: " + attempts);
System.exit(0); System.exit(0);
} else if (result == -1) { } else if (result == -1) {
// less than password // less than password
...@@ -35,5 +35,9 @@ public class BinarySearchHackerThread extends Thread { ...@@ -35,5 +35,9 @@ public class BinarySearchHackerThread extends Thread {
high = mid - 1; high = mid - 1;
} }
} }
System.out.println(this.getName() + " finished! Total attempts: " + attempts);
}
public int getAttempts() {
return attempts;
} }
} }
\ No newline at end of file
public class DescendingHackerThread extends Thread { public class DescendingHackerThread extends Thread {
private Vault vault; private Vault vault;
private int attempts;
public DescendingHackerThread(Vault vault) { public DescendingHackerThread(Vault vault) {
this.vault = vault; this.vault = vault;
this.setName("DescendingHacker"); this.setName("DescendingHacker");
this.setPriority(Thread.MAX_PRIORITY); this.setPriority(Thread.MAX_PRIORITY);
this.attempts = 0;
} }
@Override @Override
...@@ -13,11 +15,16 @@ public class DescendingHackerThread extends Thread { ...@@ -13,11 +15,16 @@ public class DescendingHackerThread extends Thread {
// Try every number from 9999 down to 0 // Try every number from 9999 down to 0
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() + " cracked the vault! Password: " + guess); System.out.println(this.getName() + " cracked the vault! Password: " + guess);
System.out.println("Total attempts: " + attempts);
System.exit(0); System.exit(0);
} }
} }
System.out.println(this.getName() + " finished but didn't find the password!"); System.out.println(this.getName() + " finished but didn't find the password! Total attempts: " + attempts);
}
public int getAttempts() {
return attempts;
} }
} }
\ No newline at end of file
...@@ -7,7 +7,7 @@ public class Main { ...@@ -7,7 +7,7 @@ public class Main {
AscendingHackerThread ascendingHacker = new AscendingHackerThread(vault); AscendingHackerThread ascendingHacker = new AscendingHackerThread(vault);
DescendingHackerThread descendingHacker = new DescendingHackerThread(vault); DescendingHackerThread descendingHacker = new DescendingHackerThread(vault);
BinarySearchHackerThread binarySearchHacker = new BinarySearchHackerThread(vault); BinarySearchHackerThread binarySearchHacker = new BinarySearchHackerThread(vault);
PoliceThread police = new PoliceThread(); PoliceThread police = new PoliceThread(ascendingHacker, descendingHacker, binarySearchHacker);
System.out.println("Starting all threads..."); System.out.println("Starting all threads...");
......
public class PoliceThread extends Thread { public class PoliceThread extends Thread {
private AscendingHackerThread ascendingHacker;
private DescendingHackerThread descendingHacker;
private BinarySearchHackerThread binarySearchHacker;
public PoliceThread() { public PoliceThread(AscendingHackerThread asc, DescendingHackerThread desc, BinarySearchHackerThread bin) {
this.setName("Police"); this.setName("Police");
this.ascendingHacker = asc;
this.descendingHacker = desc;
this.binarySearchHacker = bin;
} }
@Override @Override
public void run() { public void run() {
System.out.println("Police are on the way! 10 seconds remaining..."); System.out.println("Police are on the way! 10 seconds remaining...");
// Countdown from 10 to 1
for (int i = 10; i > 0; i--) { for (int i = 10; i > 0; i--) {
try { try {
// Wait for 1 second (1000 milliseconds)
Thread.sleep(1000); Thread.sleep(1000);
System.out.println(i + " seconds remaining..."); System.out.println(i + " seconds remaining...");
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.out.println("Police thread was interrupted!"); System.out.println("Police thread was interrupted!");
e.printStackTrace();
} }
} }
// If we reach here, time is up and police arrived
System.out.println("Game over for you hackers! Police arrived!"); System.out.println("Game over for you hackers! Police arrived!");
System.exit(0); // End the entire program
}
System.out.println("Ascending Hacker attempts: " + ascendingHacker.getAttempts());
System.out.println("Descending Hacker attempts: " + descendingHacker.getAttempts());
System.out.println("Binary Search Hacker attempts: " + binarySearchHacker.getAttempts());
System.exit(0);
}
} }
\ No newline at end of file
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