summaryrefslogtreecommitdiff
path: root/src/Jttt.java
blob: 6a289fb0e0ed93b1325fa11c70812880695f193d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605


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

                }

            }
        }
    }
}