Reputation: 324
I want to create a basic chess clock but I got stuck.
package SatrancSaati;
import java.util.Scanner;
public class SatrancSaatiRunner {
static int beyazZamani = 60;
static int siyahZamani = 60;
static boolean BeyazinSirasi = false;
static boolean SiyahinSirasi = false;//rakip baslar
static boolean zamanVarMi = true;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 999; i++) {
Scanner scanner = new Scanner(System.in);
System.out.println("Beyaz hamle yaptiktan sonra 1 e basmali");
System.out.println("Siyah hamle yaptiktan sonra 2 e basmali");
int kiminSirasi = scanner.nextInt();
if (kiminSirasi == 1) {
saatCalistirSiyah(BeyazinSirasi, siyahZamani, zamanVarMi, beyazZamani, siyahZamani);
} else if (kiminSirasi == 2) {
saatCalistirBeyaz(BeyazinSirasi, siyahZamani, zamanVarMi, beyazZamani, siyahZamani);
}
}
}
private static void saatCalistirBeyaz(boolean beyazinSirasi, int siyahZamani, boolean zamanVarMi, int beyazZamani, int siyahZamani1) throws InterruptedException {
System.out.println("***Hamle Beyazda***");
while (true) {
siyahZamani++;
beyazZamani--;
System.out.print("Beyaz: " + beyazZamani + " ");
System.out.print("Siyah: " + siyahZamani);
System.out.print("\u000C");
Thread.sleep(1000);
if (beyazZamani <= 0 || siyahZamani <= 0) {
break;
}
}
}
private static void saatCalistirSiyah(boolean beyazinSirasi, int siyahZamani, boolean zamanVarMi,
int beyazZamani, int siyahZamani1) throws InterruptedException {
System.out.println("***Hamle Siyahta***");
while (true) {
beyazZamani++;
siyahZamani--;
System.out.print("Beyaz: " + beyazZamani + " ");
System.out.print("Siyah: " + siyahZamani);
System.out.print("\u000C");
Thread.sleep(1000);
if (beyazZamani <= 0 || siyahZamani <= 0) {
break;
}
}
}
}
This is my basic code, I want that code to work like this:
when I click 1 on IntelliJ then black's time starts dropping and white time increases by the same amount. while this code running when I click 2 I want to stop the other method and start the opposite method that increases white's time and decreases black's time by the same amount. This is very difficult because when I start one method it works until finished. That's my problem.
Upvotes: 0
Views: 551
Reputation: 4414
I don't exactly follow what you're looking for. But it sounds like you're looking to execute those methods in a multi-threaded way.
If that's the case, you appear to be missing some things, such as the synchronized keyword and the Runnable interface.
The following is an example to perhaps get you started down the right path. It runs as it is, but I'm sure you'll need to tweak it to accomplish your goal:
package SatrancSaatiRunner ;
import java.util.Scanner;
public class SatrancSaatiRunner {
static int beyazZamani = 60;
static int siyahZamani = 60;
static boolean BeyazinSirasi = false;
static boolean SiyahinSirasi = false;// rakip baslar
static boolean zamanVarMi = true;
public static void main(String[] args) throws InterruptedException {
SatrancSaatiRunner s = new SatrancSaatiRunner();
for (int i = 0; i < 999; i++) {
//Scanner scanner = new Scanner(System.in);
System.out.println("Beyaz hamle yaptiktan sonra 1 e basmali");
System.out.println("Siyah hamle yaptiktan sonra 2 e basmali");
//int kiminSirasi = scanner.nextInt();
//if (kiminSirasi == 1) {
// saatCalistirSiyah(BeyazinSirasi, siyahZamani, zamanVarMi, beyazZamani, siyahZamani);
//} else if (kiminSirasi == 2) {
// saatCalistirBeyaz(BeyazinSirasi, siyahZamani, zamanVarMi, beyazZamani, siyahZamani);
//}
//}
}
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
s.saatCalistirSiyah(BeyazinSirasi, siyahZamani, zamanVarMi, beyazZamani, siyahZamani);
} catch (InterruptedException e) {
System.out.println(e);
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
s.saatCalistirBeyaz(BeyazinSirasi, siyahZamani, zamanVarMi, beyazZamani, siyahZamani);
} catch (InterruptedException e) {
System.out.println(e);
}
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void saatCalistirBeyaz(boolean beyazinSirasi, int siyahZamani, boolean zamanVarMi, int beyazZamani,
int siyahZamani1) throws InterruptedException {
synchronized (this) {
System.out.println("***Hamle Beyazda***");
while (true) {
siyahZamani++;
beyazZamani--;
System.out.print("Beyaz: " + beyazZamani + " ");
System.out.print("Siyah: " + siyahZamani);
System.out.print("\u000C");
Thread.sleep(1000);
if (beyazZamani <= 0 || siyahZamani <= 0) {
break;
}
}
}
}
private void saatCalistirSiyah(boolean beyazinSirasi, int siyahZamani, boolean zamanVarMi, int beyazZamani,
int siyahZamani1) throws InterruptedException {
System.out.println("***Hamle Siyahta***");
while (true) {
beyazZamani++;
siyahZamani--;
System.out.print("Beyaz: " + beyazZamani + " ");
System.out.print("Siyah: " + siyahZamani);
System.out.print("\u000C");
Thread.sleep(1000);
if (beyazZamani <= 0 || siyahZamani <= 0) {
break;
}
}
}
}
Upvotes: 1