Home > Net >  how to run "two different code" together?
how to run "two different code" together?

Time:04-11

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.

CodePudding user response:

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;
            }
        }
    }
}

CodePudding user response:

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");
        }
    }
}
  • Related