1

first of all everything, this message will be my first message right here. Thank you for helping me and if my problem is too easy I am sorry about that.

I wanted to create a basic chess clock as a practice and improve myself but I stuck at a point. I didn't find a clear answer to my question for more than 2 days of exploring the internet.

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 works 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.

YaMiN
  • 2,833
  • 2
  • 9
  • 30
  • You can use threads or [ExecutorService](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html). – YaMiN Apr 10 '22 at 15:01
  • 2
    Maybe approach it more like this: [A timer](https://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java) which ticks once a second. When it ticks, depending on if it is currently black or white's turn, you update their time counts accordingly. While/sleep loops have a tendancy to 'drift' from true time, given it doesn't take exactly 1 second per iteration. – Luke Briggs Apr 10 '22 at 15:11
  • Two semaphores and one unit? When input, signal a semaphore and wait on the other. – Martin James Apr 10 '22 at 15:41

2 Answers2

1

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;
            }
        }
    }
}
Woodchuck
  • 3,068
  • 2
  • 30
  • 55
0

I am very close to solving my problem, I used multi tread as you advised me here. Now the problem is I can't manage consol because when I need to send an input signal it doesn't work because the timer working every second and ignoring user input.

I used 1 package, and 3 different classes which extends the parent class.

and I have one more problem, I using "\u000C" (console clearing code) it was working before but right now I tried so many things but doesn't work.

package SatrancSaati;

import mehmetHocaCalisma.Islemler;

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 {
        SiraBeyazda b = new SiraBeyazda();
        Thread beyazOynuyor = new Thread(b);
        SiraSiyahta s = new SiraSiyahta ();
        Thread siyahOynuyor = new Thread(s);

        while (true) {

            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) {
                beyazOynuyor.stop();
                siyahOynuyor.start();



            } else if (kiminSirasi == 2) {
                siyahOynuyor.stop();
                beyazOynuyor.start();

            }
            Thread.sleep(100);
        }

    }
}



package SatrancSaati;

public class SiraBeyazda extends SatrancSaatiRunner implements Runnable {

    @Override
    public void run() {
        System.out.println("***Hamle Beyazda***");
        while (true) {
            siyahZamani++;
            beyazZamani--;
            System.out.print("Beyaz: " + beyazZamani + " ");
            System.out.print("Siyah: " + siyahZamani);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (beyazZamani <= 0 || siyahZamani <= 0) {
                break;
            }
            System.out.println("\u000C");
        }
    }
}




package SatrancSaati;

public class SiraSiyahta extends SatrancSaatiRunner implements Runnable {


    @Override
    public void run() {
        System.out.println("***Hamle Siyahta***");
        while (true) {
            beyazZamani++;
            siyahZamani--;
            System.out.print("Beyaz: " + beyazZamani + " ");
            System.out.print("Siyah: " + siyahZamani);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (beyazZamani <= 0 || siyahZamani <= 0) {
                break;
            }
            System.out.println("\u000C");
        }
    }
}
  • I solved my problem, the problem was that I planned to use two multi-threads at the same time and a control where I had to stop them from running and start over again. It was a problem for me to try to finish the process that started in multi tread transactions before it was finished. Since the file was not closed, when I tried to run the same thread again, so I ran it twice and I was getting exeption, – Barış Can Ateş Apr 11 '22 at 10:09