view Jttt/src/Jttt.java @ 17:e9c503d896a6

flappyufo-0.230919-1_add_pkgbuild
author Przemyslaw <prymula76@outlook.com>
date Sat, 24 Feb 2024 17:47:35 +0100
parents 2787f5e749ae
children
line wrap: on
line source



//package Jttt;

/*
 * Lipiec 2019
 */

import java.awt.*;  // Toolkit
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.Box;

//import java.awt.Toolkit;
//import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Timer;
import java.util.*; //Timer

public class Jttt extends JPanel
        implements ActionListener {
    static JButton [][]button = new JButton[3][3];
    String sbutton = "MyButton";
    static FieldGame[][]field = new FieldGame[3][3];
    static WhoseQueue whosequeue = WhoseQueue.CROSS;
    static JLabel label = new JLabel("Ruch KRZYZA");
    static boolean action = true;
    static int nmove=0;
    static JMenuBar menuBar;
    JMenu menu, info;
    JMenuItem menuItem, menuExit, menuInfo;
    static JFrame frame;
    static Timer timer;
    static boolean ai=false;
    static boolean firstMoveAi=true;

    public enum WhoseQueue {
        CROSS, WHELL
    }
    public enum FieldGame {
        CROSS, WHELL, FREE
    }


    public Jttt() {
        super(new GridBagLayout());
        GridBagLayout gridbag = (GridBagLayout)getLayout();
        GridBagConstraints c = new GridBagConstraints();
        int k=0;

        for (int i=0; i<3; i++) {
            for(int j=0; j<3; j++) {
                field[i][j]=FieldGame.FREE;
            }
        }

        menuBar = new JMenuBar();
        menu = new JMenu("Plik");
        menuBar.add(menu);
        // przenosi INFO na prawa strone
        menuBar.add(Box.createHorizontalGlue());
        info = new JMenu("Info");
        // kierunek wyswietlania menu
        info.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        menuBar.add(info);
        menuItem = new JMenuItem("Nowa Gra vs. HUMAN", null);
        menuItem.setActionCommand("MenuItem1");
        menuItem.addActionListener(this);
        menuItem.setToolTipText("Rozpoczyna nowa gre pomiedzy dwoma graczami.");
        menu.add(menuItem);
        menuItem = new JMenuItem("Nowa Gra vs. CPU", null);
        menuItem.setActionCommand("MenuItem11");
        menuItem.addActionListener(this);
        menuItem.setToolTipText("Rozpoczyna nowa gre pomiedzy komputerem \"X\" a graczem \"O\"");
        menu.add(menuItem);
        menu.addSeparator();
        menuExit = new JMenuItem("Zakoncz", null);
        menuExit.setActionCommand("MenuItem2");
        menuExit.addActionListener(this);
        menuExit.setToolTipText("Wyjscie z programu");
        menu.add(menuExit);
        menuInfo = new JMenuItem("O Programie", null);
        menuInfo.setActionCommand("MenuItem3");
        menuInfo.addActionListener(this);
        menuInfo.setToolTipText("Informacje o programie");
        info.add(menuInfo);


        for (int i=0; i<3; i++) {
            for (int j=0; j<3; j++, k++) {
                button[i][j] = new JButton("?");
                button[i][j].setPreferredSize(new Dimension(80, 80));
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx = j;
                c.gridy = i;
                gridbag.setConstraints(button[i][j], c);
                //add(button0, BorderLayout.CENTER);
                add(button[i][j]);
                button[i][j].setActionCommand(sbutton+k);
                button[i][j].addActionListener(this);
                button[i][j].setFont(new Font("Arial", Font.PLAIN, 40));
                //button[i][j].setFont(new Font("Courier", Font.PLAIN, 40));
            }
        }
        label.setPreferredSize(new Dimension(100, 20));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 3;
        gridbag.setConstraints(label, c);
        add(label);


    }

    public void actionPerformed(ActionEvent e) {

        int k=0;
        for(int i=0; i<3; i++) {
            for(int j=0; j<3; j++, k++){
                if(e.getActionCommand().equals(sbutton+k) && action && nmove<9)
                {
                    if(field[i][j].equals(FieldGame.FREE)) {
                        if (whosequeue.equals(WhoseQueue.CROSS) && !ai) {
                            Toolkit.getDefaultToolkit().beep();
                            nmove++;
                            button[i][j].setText("X");
                            field[i][j]=FieldGame.CROSS;
                            // powinno byc WhoseQueue.CROSS
                            // czy wygrana
                            if (win(FieldGame.CROSS)) {
                                label.setText("Wygral KRZYZ");
                                action=false;
                                //System.out.println("pierwszy-KRZYZ");
                            }
                            // to moze remis
                            else if (nmove==9) {
                                label.setText("Remis");
                                action=false;
                                //System.out.println("+REMIS nmove:"+nmove);
                            }
                            //w przeciwnym wypadku ruch kolejnego gracza
                            else {
                                whosequeue=WhoseQueue.WHELL;
                                label.setText("Ruch KOLA");
                            }
                            //System.out.println("KRZYZ nmove:"+nmove);
                        }
                        else if (whosequeue.equals(WhoseQueue.WHELL)) {
                            Toolkit.getDefaultToolkit().beep();
                            nmove++;
                            button[i][j].setText("O");
                            field[i][j]=FieldGame.WHELL;
                            // powinno byc WhoseQueue.WHELL
                            // czy wygrana
                            if (win(FieldGame.WHELL)) {
                                label.setText("Wygral KOLO");
                                action=false;
                                //System.out.println("pierwszy-KOLO");
                            }
                            // to moze remis
                            else if (nmove==9) {
                                label.setText("Remis");
                                action=false;
                                //System.out.println("-REMIS nmove:"+nmove);
                            }
                            // w przeciwnym wypadku ruch kolejengo gracza
                            else {
                                whosequeue=WhoseQueue.CROSS;
                                label.setText("Ruch KRZYZA");
                            }
                           // System.out.println("KOLO nmove:"+nmove);
                        }
                    }
                }
            }
        }
        // Nowa gra
        if(e.getActionCommand().equals("MenuItem1")) {
            for (int i=0; i<3; i++) {
                for(int j=0; j<3; j++) {
                    field[i][j]=FieldGame.FREE;
                    button[i][j].setText("?");
                }
            }
            nmove=0;
            action=true;
            ai=false;
            if (whosequeue.equals(WhoseQueue.CROSS)) {
                label.setText("Ruch KRZYZA");
            }
            else label.setText("Ruch KOLA");
        }
        // Zakonczenie
        else if(e.getActionCommand().equals("MenuItem2")) {
            System.exit(0);
        }
        // O Programie
        else if(e.getActionCommand().equals("MenuItem3")) {

            JOptionPane.showMessageDialog(frame,
                    "Moja pierwsza aplikacja\nstworzona w systemie Windows 10\nPrzemyslaw R. Pietraszczyk\nStworzono w oparciu o OpenJDK\nLipiec 2019",
                    "Jttt 0.210503-0",
                    JOptionPane.PLAIN_MESSAGE);

        }
        // gra z komputerem
        else if(e.getActionCommand().equals("MenuItem11")) {
            for (int i=0; i<3; i++) {
                for(int j=0; j<3; j++) {
                    field[i][j]=FieldGame.FREE;
                    button[i][j].setText("?");
                }
            }
            nmove=0;
            action=true;
            ai=true;
            firstMoveAi=true;
            if (whosequeue.equals(WhoseQueue.CROSS)) {
                label.setText("Ruch KRZYZA");
            }
            else label.setText("Ruch KOLA");
        }
    }
    //private boolean win (WhoseQueue wq) {
    static private boolean win (FieldGame wq) {

        int accumulation=0;
        // sprawdza w poziomie
        for (int j=0; j<3; j++) {
            for (int i=0; i<3; i++) {
                if (field[j][i].equals(wq)) accumulation++;
            }
            if (accumulation==3) {
                //wygral wq
                return true;
            }
            accumulation=0;
        }
        // sprawdza w pionie
        for (int j=0; j<3; j++) {
            for (int i=0; i<3; i++) {
                if (field[i][j].equals(wq)) accumulation++;
            }
            if (accumulation==3) {
                //wygral wq
                return true;
            }
            accumulation=0;
        }
        if (field[0][0].equals(wq) && field[1][1].equals(wq) && field[2][2].equals(wq)) {
            // wygral wq
            return true;
        }
        else if (field[0][2].equals(wq) && field[1][1].equals(wq) && field[2][0].equals(wq)){
            //wygral wq
            return true;
        }
        return false;
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        //JFrame frame = new JFrame("Jtic");
        frame = new JFrame("Jtictactoe");
        timer = new Timer();
        int frameWidth = 260;
        int frameHeight = 380;
        // ustawia okno po prawe na dole
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setBounds((int) screenSize.getWidth() - frameWidth,
                (int) screenSize.getHeight() - frameHeight, frameWidth, frameHeight);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new Jttt();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setJMenuBar(menuBar);
        frame.setContentPane(newContentPane);


        //Display the window.
        frame.pack();
        frame.setVisible(true);

        timer.schedule(new CheckAiMove(), 0, 5000);

    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    static class CheckAiMove extends TimerTask {
        Random rand = new Random();
        int i,j;
        boolean [][]aiField= new boolean[3][3];
        public void run() {
            if (whosequeue.equals(WhoseQueue.CROSS) && ai && action) {
                //int i,j;
                nmove++;
                if (firstMoveAi) {
                    do {
                        i = rand.nextInt(3);
                        j = rand.nextInt(3);
                        //System.out.println("Random i:"+i);
                        //System.out.println("Random j:"+j);
                    }while(field[i][j]!=FieldGame.FREE);
                    firstMoveAi=false;
                }
                else
                    checkAiField();
                Toolkit.getDefaultToolkit().beep();
                button[i][j].setText("X");
                field[i][j]=FieldGame.CROSS;
                // Jesli wygrana komputera
                if (win(FieldGame.CROSS)) {
                    label.setText("Wygral KRZYZ");
                    action=false;
                    //System.out.println("ROBOT Wygrana KRZYZA nmove:"+nmove);
                }
                // to moze remis
                else if (nmove==9){
                    label.setText("Remis");
                    action=false;
                    //System.out.println("ROBOT Remis nmove:"+nmove);
                }
                // w przeciwnym razie ruch kola
                else {
                    whosequeue=WhoseQueue.WHELL;
                    label.setText("Ruch KOLA");
                    //System.out.println("ROBOT Ruch KOLA nmove:"+nmove);
                }
            }
        }
        private void checkAiField() {
            boolean out=false;
            int accumulation=0, priority=0;
            boolean find=false;


            if (!out) {
                // sprawdza wolne miejsce w poziomie z lewej na prawa

                for (int a=0;a<3;a++) {
                    for (int b=0;b<3;b++) {
                        if (field[a][b]==FieldGame.CROSS &&
                                field[a][b]!=FieldGame.WHELL) accumulation++;
                        if (field[a][b]==FieldGame.WHELL) accumulation=0;
                        if (field[a][b]==FieldGame.FREE &&
                                accumulation!=0) {i=a; j=b;out=true;find=true;priority=accumulation;System.out.println("w Poziomych 1");break;}
                    }
                    accumulation=0;
                    if (out) break;
                }
            }
            // sprawdza wolne miejsce w poziomie z prawej na lewa
            if (!out) {
                //out=false;
                //System.out.println("w Poziomych 2");
                for (int a=2;a>=0;a--) {
                    for (int b=2;b>=0;b--) {
                        if (field[a][b]==FieldGame.CROSS &&
                                field[a][b]!=FieldGame.WHELL) accumulation++;
                        if (field[a][b]==FieldGame.WHELL) accumulation=0;
                        if (field[a][b]==FieldGame.FREE &&
                                accumulation!=0) {i=a; j=b;out=true;find=true;priority=accumulation;System.out.println("w Poziomych 2");break;}
                    }
                    accumulation=0;
                    if (out) break;
                }
            }
            // sprawdza wolne miejsce pionowo z gory na dol
            if (!out) {

                //out=false;
                for (int a=0;a<3;a++) {
                    for (int b=0;b<3;b++) {
                        if (field[b][a]==FieldGame.CROSS &&
                                field[b][a]!=FieldGame.WHELL) accumulation++;
                        if (field[b][a]==FieldGame.WHELL) accumulation=0;
                        if (field[b][a]==FieldGame.FREE &&
                                accumulation!=0) {i=b; j=a;out=true;find=true;priority=accumulation;System.out.println("w Pionowych 1");break;}
                    }
                    accumulation=0;
                    if (out) break;
                }
            }
            // sprawdza wolne miejsce pionowo z dolu do gory
            if (!out) {
                for (int a=2;a>=0;a--) {
                    for (int b=2;b>=0;b--) {
                        if (field[b][a]==FieldGame.CROSS &&
                                field[b][a]!=FieldGame.WHELL) accumulation++;
                        if (field[b][a]==FieldGame.WHELL) accumulation=0;
                        if (field[b][a]==FieldGame.FREE &&
                                accumulation!=0) {i=b; j=a;out=true;find=true;priority=accumulation;System.out.println("w Pionowych 2");break;}
                    }
                    accumulation=0;
                    if (out) break;
                }
            }
            // szukanie miejsca na ukos
            if (priority<2) {
                out=false;
                if (!out) {
                    if (field[0][0].equals(FieldGame.CROSS) &&
                            field[1][1].equals(FieldGame.CROSS) &&
                            field[2][2].equals(FieldGame.FREE)) {
                        i=2; j=2;out=true;find=true;priority=2;System.out.println("unslash od gory");

                    }
                }
                if (!out) {
                    if (field[0][0].equals(FieldGame.CROSS) &&
                            field[2][2].equals(FieldGame.CROSS) &&
                            field[1][1].equals(FieldGame.FREE)) {
                        i=1; j=1;out=true;find=true;priority=2;System.out.println("unslash od gory z dziura w srodku");

                    }
                }
                if (!out){
                    if (field[0][2].equals(FieldGame.CROSS) &&
                            field[1][1].equals(FieldGame.CROSS) &&
                            field[2][0].equals(FieldGame.FREE)) {
                        i=2; j=0;out=true;find=true;priority=2;System.out.println("slash od gory");

                    }
                }
                if (!out) {
                    if (field[2][2].equals(FieldGame.CROSS) &&
                            field[1][1].equals(FieldGame.CROSS) &&
                            field[0][0].equals(FieldGame.FREE)) {
                        i=0; j=0;out=true;find=true;priority=2;System.out.println("slash od dolu z dziura w srodku");

                    }
                }
                if (!out) {
                    if (field[2][2].equals(FieldGame.CROSS) &&
                            field[0][0].equals(FieldGame.CROSS) &&
                            field[1][1].equals(FieldGame.FREE)) {
                        i=1; j=1;out=true;find=true;priority=2;System.out.println("slash od dolu");

                    }
                }
                if (!out) {
                    if (field[2][0].equals(FieldGame.CROSS) &&
                            field[1][1].equals(FieldGame.CROSS) &&
                            field[0][2].equals(FieldGame.FREE)) {
                        i=0; j=2;out=true;find=true;priority=2;System.out.println("slash od dolu");

                    }
                }
            }



            System.out.println("priority: "+priority);
            if(priority<2) {
                out=false;
                if (!out) {
                    // proboje zablokowac w poziomie z lewej na prawa
                    for (int a=0;a<3;a++) {
                        for (int b=0;b<3;b++) {
                            if (field[a][b]==FieldGame.WHELL) accumulation++;
                            if (field[a][b]==FieldGame.CROSS) accumulation=0;
                            if (field[a][b]==FieldGame.FREE &&
                                    accumulation==2) {i=a; j=b;out=true;find=true;
                                System.out.println("Blok w poziomie 1");break;}
                        }
                        accumulation=0;
                        if (out) break;
                    }
                }
                if (!out) {
                    // proboje zablokowac w poziomie z prawej nanlewa
                    for (int a=2;a>=0;a--) {
                        for (int b=2;b>=0;b--) {
                            if (field[a][b]==FieldGame.WHELL) accumulation++;
                            if (field[a][b]==FieldGame.CROSS) accumulation=0;
                            if (field[a][b]==FieldGame.FREE &&
                                    accumulation==2) {i=a; j=b;out=true;find=true;
                                System.out.println("Blok w poziomie 2");break;}
                        }
                        accumulation=0;
                        if (out) break;
                    }
                }
                if (!out) {
                    // proboje zablokowac w pionie z lewej na prawa
                    for (int a=0;a<3;a++) {
                        for (int b=0;b<3;b++) {
                            if (field[b][a]==FieldGame.WHELL) accumulation++;
                            if (field[b][a]==FieldGame.CROSS) accumulation=0;
                            if (field[b][a]==FieldGame.FREE &&
                                    accumulation==2) {i=b; j=a;out=true;find=true;
                                System.out.println("Blok w pionie 1");break;}
                        }
                        accumulation=0;
                        if (out) break;
                    }
                }
                if (!out) {
                    // proboje zablokowac w pionie z dolu do gory
                    for (int a=2;a>=0;a--) {
                        for (int b=2;b>=0;b--) {
                            if (field[b][a]==FieldGame.WHELL) accumulation++;
                            if (field[b][a]==FieldGame.CROSS) accumulation=0;
                            if (field[b][a]==FieldGame.FREE &&
                                    accumulation==2) {i=b; j=a;out=true;find=true;
                                System.out.println("Blok w pionie 2");break;}
                        }
                        accumulation=0;
                        if (out) break;
                    }
                }
            } // priority


            // blokowanie na ukos
            if (priority<2) {
                out=false;
                if (!out) {
                    if (field[0][0].equals(FieldGame.WHELL) &&
                            field[1][1].equals(FieldGame.WHELL) &&
                            field[2][2].equals(FieldGame.FREE)) {
                        i=2; j=2;out=true;find=true;priority=2;System.out.println("blokowanie unslash od gory");

                    }
                }
                if (!out) {
                    if (field[0][0].equals(FieldGame.WHELL) &&
                            field[2][2].equals(FieldGame.WHELL) &&
                            field[1][1].equals(FieldGame.FREE)) {
                        i=1; j=1;out=true;find=true;priority=2;System.out.println("blokowanie unslash od gory z dziura w srodku");

                    }
                }
                if (!out){
                    if (field[0][2].equals(FieldGame.WHELL) &&
                            field[1][1].equals(FieldGame.WHELL) &&
                            field[2][0].equals(FieldGame.FREE)) {
                        i=2; j=0;out=true;find=true;priority=2;System.out.println("blokowanie slash od gory");

                    }
                }
                if (!out) {
                    if (field[2][2].equals(FieldGame.WHELL) &&
                            field[1][1].equals(FieldGame.WHELL) &&
                            field[0][0].equals(FieldGame.FREE)) {
                        i=0; j=0;out=true;find=true;priority=2;System.out.println("blokowanie slash od dolu z dziura w srodku");

                    }
                }
                if (!out) {
                    if (field[2][2].equals(FieldGame.WHELL) &&
                            field[0][0].equals(FieldGame.WHELL) &&
                            field[1][1].equals(FieldGame.FREE)) {
                        i=1; j=1;out=true;find=true;priority=2;System.out.println("blokowanie slash od dolu");

                    }
                }
                if (!out) {
                    if (field[2][0].equals(FieldGame.WHELL) &&
                            field[1][1].equals(FieldGame.WHELL) &&
                            field[0][2].equals(FieldGame.FREE)) {
                        i=0; j=2;out=true;find=true;priority=2;System.out.println("blokowanie slash od dolu");

                    }
                }
            }


            System.out.println("checkAiField i: "+i+"  j: "+j);
            // te petle sa na wszelki wypadek
            if (!find) {
                for (int a=0;a<3;a++) {
                    for (int b=0;b<3;b++)
                        if (field[a][b]==FieldGame.FREE) {i=a; j=b;System.out.println("FIND");}

                }

            }
        }
    }
}