comparison Jttt/src/Jttt.java @ 0:2787f5e749ae

INIT
author prymula <prymula76@outlook.com>
date Thu, 21 Sep 2023 22:33:57 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2787f5e749ae
1
2
3 //package Jttt;
4
5 /*
6 * Lipiec 2019
7 */
8
9 import java.awt.*; // Toolkit
10 import javax.swing.JFrame;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13 import javax.swing.JButton;
14 import javax.swing.JComponent;
15 import javax.swing.JMenuBar;
16 import javax.swing.JMenu;
17 import javax.swing.JMenuItem;
18 import javax.swing.JOptionPane;
19 import javax.swing.Box;
20
21 //import java.awt.Toolkit;
22 //import java.awt.BorderLayout;
23 import java.awt.event.ActionListener;
24 import java.awt.event.ActionEvent;
25 import java.util.Timer;
26 import java.util.*; //Timer
27
28 public class Jttt extends JPanel
29 implements ActionListener {
30 static JButton [][]button = new JButton[3][3];
31 String sbutton = "MyButton";
32 static FieldGame[][]field = new FieldGame[3][3];
33 static WhoseQueue whosequeue = WhoseQueue.CROSS;
34 static JLabel label = new JLabel("Ruch KRZYZA");
35 static boolean action = true;
36 static int nmove=0;
37 static JMenuBar menuBar;
38 JMenu menu, info;
39 JMenuItem menuItem, menuExit, menuInfo;
40 static JFrame frame;
41 static Timer timer;
42 static boolean ai=false;
43 static boolean firstMoveAi=true;
44
45 public enum WhoseQueue {
46 CROSS, WHELL
47 }
48 public enum FieldGame {
49 CROSS, WHELL, FREE
50 }
51
52
53 public Jttt() {
54 super(new GridBagLayout());
55 GridBagLayout gridbag = (GridBagLayout)getLayout();
56 GridBagConstraints c = new GridBagConstraints();
57 int k=0;
58
59 for (int i=0; i<3; i++) {
60 for(int j=0; j<3; j++) {
61 field[i][j]=FieldGame.FREE;
62 }
63 }
64
65 menuBar = new JMenuBar();
66 menu = new JMenu("Plik");
67 menuBar.add(menu);
68 // przenosi INFO na prawa strone
69 menuBar.add(Box.createHorizontalGlue());
70 info = new JMenu("Info");
71 // kierunek wyswietlania menu
72 info.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
73 menuBar.add(info);
74 menuItem = new JMenuItem("Nowa Gra vs. HUMAN", null);
75 menuItem.setActionCommand("MenuItem1");
76 menuItem.addActionListener(this);
77 menuItem.setToolTipText("Rozpoczyna nowa gre pomiedzy dwoma graczami.");
78 menu.add(menuItem);
79 menuItem = new JMenuItem("Nowa Gra vs. CPU", null);
80 menuItem.setActionCommand("MenuItem11");
81 menuItem.addActionListener(this);
82 menuItem.setToolTipText("Rozpoczyna nowa gre pomiedzy komputerem \"X\" a graczem \"O\"");
83 menu.add(menuItem);
84 menu.addSeparator();
85 menuExit = new JMenuItem("Zakoncz", null);
86 menuExit.setActionCommand("MenuItem2");
87 menuExit.addActionListener(this);
88 menuExit.setToolTipText("Wyjscie z programu");
89 menu.add(menuExit);
90 menuInfo = new JMenuItem("O Programie", null);
91 menuInfo.setActionCommand("MenuItem3");
92 menuInfo.addActionListener(this);
93 menuInfo.setToolTipText("Informacje o programie");
94 info.add(menuInfo);
95
96
97 for (int i=0; i<3; i++) {
98 for (int j=0; j<3; j++, k++) {
99 button[i][j] = new JButton("?");
100 button[i][j].setPreferredSize(new Dimension(80, 80));
101 c.fill = GridBagConstraints.HORIZONTAL;
102 c.gridx = j;
103 c.gridy = i;
104 gridbag.setConstraints(button[i][j], c);
105 //add(button0, BorderLayout.CENTER);
106 add(button[i][j]);
107 button[i][j].setActionCommand(sbutton+k);
108 button[i][j].addActionListener(this);
109 button[i][j].setFont(new Font("Arial", Font.PLAIN, 40));
110 //button[i][j].setFont(new Font("Courier", Font.PLAIN, 40));
111 }
112 }
113 label.setPreferredSize(new Dimension(100, 20));
114 c.fill = GridBagConstraints.HORIZONTAL;
115 c.gridwidth = 3;
116 c.gridx = 0;
117 c.gridy = 3;
118 gridbag.setConstraints(label, c);
119 add(label);
120
121
122 }
123
124 public void actionPerformed(ActionEvent e) {
125
126 int k=0;
127 for(int i=0; i<3; i++) {
128 for(int j=0; j<3; j++, k++){
129 if(e.getActionCommand().equals(sbutton+k) && action && nmove<9)
130 {
131 if(field[i][j].equals(FieldGame.FREE)) {
132 if (whosequeue.equals(WhoseQueue.CROSS) && !ai) {
133 Toolkit.getDefaultToolkit().beep();
134 nmove++;
135 button[i][j].setText("X");
136 field[i][j]=FieldGame.CROSS;
137 // powinno byc WhoseQueue.CROSS
138 // czy wygrana
139 if (win(FieldGame.CROSS)) {
140 label.setText("Wygral KRZYZ");
141 action=false;
142 //System.out.println("pierwszy-KRZYZ");
143 }
144 // to moze remis
145 else if (nmove==9) {
146 label.setText("Remis");
147 action=false;
148 //System.out.println("+REMIS nmove:"+nmove);
149 }
150 //w przeciwnym wypadku ruch kolejnego gracza
151 else {
152 whosequeue=WhoseQueue.WHELL;
153 label.setText("Ruch KOLA");
154 }
155 //System.out.println("KRZYZ nmove:"+nmove);
156 }
157 else if (whosequeue.equals(WhoseQueue.WHELL)) {
158 Toolkit.getDefaultToolkit().beep();
159 nmove++;
160 button[i][j].setText("O");
161 field[i][j]=FieldGame.WHELL;
162 // powinno byc WhoseQueue.WHELL
163 // czy wygrana
164 if (win(FieldGame.WHELL)) {
165 label.setText("Wygral KOLO");
166 action=false;
167 //System.out.println("pierwszy-KOLO");
168 }
169 // to moze remis
170 else if (nmove==9) {
171 label.setText("Remis");
172 action=false;
173 //System.out.println("-REMIS nmove:"+nmove);
174 }
175 // w przeciwnym wypadku ruch kolejengo gracza
176 else {
177 whosequeue=WhoseQueue.CROSS;
178 label.setText("Ruch KRZYZA");
179 }
180 // System.out.println("KOLO nmove:"+nmove);
181 }
182 }
183 }
184 }
185 }
186 // Nowa gra
187 if(e.getActionCommand().equals("MenuItem1")) {
188 for (int i=0; i<3; i++) {
189 for(int j=0; j<3; j++) {
190 field[i][j]=FieldGame.FREE;
191 button[i][j].setText("?");
192 }
193 }
194 nmove=0;
195 action=true;
196 ai=false;
197 if (whosequeue.equals(WhoseQueue.CROSS)) {
198 label.setText("Ruch KRZYZA");
199 }
200 else label.setText("Ruch KOLA");
201 }
202 // Zakonczenie
203 else if(e.getActionCommand().equals("MenuItem2")) {
204 System.exit(0);
205 }
206 // O Programie
207 else if(e.getActionCommand().equals("MenuItem3")) {
208
209 JOptionPane.showMessageDialog(frame,
210 "Moja pierwsza aplikacja\nstworzona w systemie Windows 10\nPrzemyslaw R. Pietraszczyk\nStworzono w oparciu o OpenJDK\nLipiec 2019",
211 "Jttt 0.210503-0",
212 JOptionPane.PLAIN_MESSAGE);
213
214 }
215 // gra z komputerem
216 else if(e.getActionCommand().equals("MenuItem11")) {
217 for (int i=0; i<3; i++) {
218 for(int j=0; j<3; j++) {
219 field[i][j]=FieldGame.FREE;
220 button[i][j].setText("?");
221 }
222 }
223 nmove=0;
224 action=true;
225 ai=true;
226 firstMoveAi=true;
227 if (whosequeue.equals(WhoseQueue.CROSS)) {
228 label.setText("Ruch KRZYZA");
229 }
230 else label.setText("Ruch KOLA");
231 }
232 }
233 //private boolean win (WhoseQueue wq) {
234 static private boolean win (FieldGame wq) {
235
236 int accumulation=0;
237 // sprawdza w poziomie
238 for (int j=0; j<3; j++) {
239 for (int i=0; i<3; i++) {
240 if (field[j][i].equals(wq)) accumulation++;
241 }
242 if (accumulation==3) {
243 //wygral wq
244 return true;
245 }
246 accumulation=0;
247 }
248 // sprawdza w pionie
249 for (int j=0; j<3; j++) {
250 for (int i=0; i<3; i++) {
251 if (field[i][j].equals(wq)) accumulation++;
252 }
253 if (accumulation==3) {
254 //wygral wq
255 return true;
256 }
257 accumulation=0;
258 }
259 if (field[0][0].equals(wq) && field[1][1].equals(wq) && field[2][2].equals(wq)) {
260 // wygral wq
261 return true;
262 }
263 else if (field[0][2].equals(wq) && field[1][1].equals(wq) && field[2][0].equals(wq)){
264 //wygral wq
265 return true;
266 }
267 return false;
268 }
269
270 /**
271 * Create the GUI and show it. For thread safety,
272 * this method should be invoked from the
273 * event-dispatching thread.
274 */
275 private static void createAndShowGUI() {
276 //Create and set up the window.
277 //JFrame frame = new JFrame("Jtic");
278 frame = new JFrame("Jtictactoe");
279 timer = new Timer();
280 int frameWidth = 260;
281 int frameHeight = 380;
282 // ustawia okno po prawe na dole
283 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
284 frame.setBounds((int) screenSize.getWidth() - frameWidth,
285 (int) screenSize.getHeight() - frameHeight, frameWidth, frameHeight);
286 frame.setResizable(false);
287 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
288
289 //Create and set up the content pane.
290 JComponent newContentPane = new Jttt();
291 newContentPane.setOpaque(true); //content panes must be opaque
292 frame.setJMenuBar(menuBar);
293 frame.setContentPane(newContentPane);
294
295
296 //Display the window.
297 frame.pack();
298 frame.setVisible(true);
299
300 timer.schedule(new CheckAiMove(), 0, 5000);
301
302 }
303
304 public static void main(String[] args) {
305 //Schedule a job for the event-dispatching thread:
306 //creating and showing this application's GUI.
307 javax.swing.SwingUtilities.invokeLater(new Runnable() {
308 public void run() {
309 createAndShowGUI();
310 }
311 });
312 }
313 static class CheckAiMove extends TimerTask {
314 Random rand = new Random();
315 int i,j;
316 boolean [][]aiField= new boolean[3][3];
317 public void run() {
318 if (whosequeue.equals(WhoseQueue.CROSS) && ai && action) {
319 //int i,j;
320 nmove++;
321 if (firstMoveAi) {
322 do {
323 i = rand.nextInt(3);
324 j = rand.nextInt(3);
325 //System.out.println("Random i:"+i);
326 //System.out.println("Random j:"+j);
327 }while(field[i][j]!=FieldGame.FREE);
328 firstMoveAi=false;
329 }
330 else
331 checkAiField();
332 Toolkit.getDefaultToolkit().beep();
333 button[i][j].setText("X");
334 field[i][j]=FieldGame.CROSS;
335 // Jesli wygrana komputera
336 if (win(FieldGame.CROSS)) {
337 label.setText("Wygral KRZYZ");
338 action=false;
339 //System.out.println("ROBOT Wygrana KRZYZA nmove:"+nmove);
340 }
341 // to moze remis
342 else if (nmove==9){
343 label.setText("Remis");
344 action=false;
345 //System.out.println("ROBOT Remis nmove:"+nmove);
346 }
347 // w przeciwnym razie ruch kola
348 else {
349 whosequeue=WhoseQueue.WHELL;
350 label.setText("Ruch KOLA");
351 //System.out.println("ROBOT Ruch KOLA nmove:"+nmove);
352 }
353 }
354 }
355 private void checkAiField() {
356 boolean out=false;
357 int accumulation=0, priority=0;
358 boolean find=false;
359
360
361 if (!out) {
362 // sprawdza wolne miejsce w poziomie z lewej na prawa
363
364 for (int a=0;a<3;a++) {
365 for (int b=0;b<3;b++) {
366 if (field[a][b]==FieldGame.CROSS &&
367 field[a][b]!=FieldGame.WHELL) accumulation++;
368 if (field[a][b]==FieldGame.WHELL) accumulation=0;
369 if (field[a][b]==FieldGame.FREE &&
370 accumulation!=0) {i=a; j=b;out=true;find=true;priority=accumulation;System.out.println("w Poziomych 1");break;}
371 }
372 accumulation=0;
373 if (out) break;
374 }
375 }
376 // sprawdza wolne miejsce w poziomie z prawej na lewa
377 if (!out) {
378 //out=false;
379 //System.out.println("w Poziomych 2");
380 for (int a=2;a>=0;a--) {
381 for (int b=2;b>=0;b--) {
382 if (field[a][b]==FieldGame.CROSS &&
383 field[a][b]!=FieldGame.WHELL) accumulation++;
384 if (field[a][b]==FieldGame.WHELL) accumulation=0;
385 if (field[a][b]==FieldGame.FREE &&
386 accumulation!=0) {i=a; j=b;out=true;find=true;priority=accumulation;System.out.println("w Poziomych 2");break;}
387 }
388 accumulation=0;
389 if (out) break;
390 }
391 }
392 // sprawdza wolne miejsce pionowo z gory na dol
393 if (!out) {
394
395 //out=false;
396 for (int a=0;a<3;a++) {
397 for (int b=0;b<3;b++) {
398 if (field[b][a]==FieldGame.CROSS &&
399 field[b][a]!=FieldGame.WHELL) accumulation++;
400 if (field[b][a]==FieldGame.WHELL) accumulation=0;
401 if (field[b][a]==FieldGame.FREE &&
402 accumulation!=0) {i=b; j=a;out=true;find=true;priority=accumulation;System.out.println("w Pionowych 1");break;}
403 }
404 accumulation=0;
405 if (out) break;
406 }
407 }
408 // sprawdza wolne miejsce pionowo z dolu do gory
409 if (!out) {
410 for (int a=2;a>=0;a--) {
411 for (int b=2;b>=0;b--) {
412 if (field[b][a]==FieldGame.CROSS &&
413 field[b][a]!=FieldGame.WHELL) accumulation++;
414 if (field[b][a]==FieldGame.WHELL) accumulation=0;
415 if (field[b][a]==FieldGame.FREE &&
416 accumulation!=0) {i=b; j=a;out=true;find=true;priority=accumulation;System.out.println("w Pionowych 2");break;}
417 }
418 accumulation=0;
419 if (out) break;
420 }
421 }
422 // szukanie miejsca na ukos
423 if (priority<2) {
424 out=false;
425 if (!out) {
426 if (field[0][0].equals(FieldGame.CROSS) &&
427 field[1][1].equals(FieldGame.CROSS) &&
428 field[2][2].equals(FieldGame.FREE)) {
429 i=2; j=2;out=true;find=true;priority=2;System.out.println("unslash od gory");
430
431 }
432 }
433 if (!out) {
434 if (field[0][0].equals(FieldGame.CROSS) &&
435 field[2][2].equals(FieldGame.CROSS) &&
436 field[1][1].equals(FieldGame.FREE)) {
437 i=1; j=1;out=true;find=true;priority=2;System.out.println("unslash od gory z dziura w srodku");
438
439 }
440 }
441 if (!out){
442 if (field[0][2].equals(FieldGame.CROSS) &&
443 field[1][1].equals(FieldGame.CROSS) &&
444 field[2][0].equals(FieldGame.FREE)) {
445 i=2; j=0;out=true;find=true;priority=2;System.out.println("slash od gory");
446
447 }
448 }
449 if (!out) {
450 if (field[2][2].equals(FieldGame.CROSS) &&
451 field[1][1].equals(FieldGame.CROSS) &&
452 field[0][0].equals(FieldGame.FREE)) {
453 i=0; j=0;out=true;find=true;priority=2;System.out.println("slash od dolu z dziura w srodku");
454
455 }
456 }
457 if (!out) {
458 if (field[2][2].equals(FieldGame.CROSS) &&
459 field[0][0].equals(FieldGame.CROSS) &&
460 field[1][1].equals(FieldGame.FREE)) {
461 i=1; j=1;out=true;find=true;priority=2;System.out.println("slash od dolu");
462
463 }
464 }
465 if (!out) {
466 if (field[2][0].equals(FieldGame.CROSS) &&
467 field[1][1].equals(FieldGame.CROSS) &&
468 field[0][2].equals(FieldGame.FREE)) {
469 i=0; j=2;out=true;find=true;priority=2;System.out.println("slash od dolu");
470
471 }
472 }
473 }
474
475
476
477 System.out.println("priority: "+priority);
478 if(priority<2) {
479 out=false;
480 if (!out) {
481 // proboje zablokowac w poziomie z lewej na prawa
482 for (int a=0;a<3;a++) {
483 for (int b=0;b<3;b++) {
484 if (field[a][b]==FieldGame.WHELL) accumulation++;
485 if (field[a][b]==FieldGame.CROSS) accumulation=0;
486 if (field[a][b]==FieldGame.FREE &&
487 accumulation==2) {i=a; j=b;out=true;find=true;
488 System.out.println("Blok w poziomie 1");break;}
489 }
490 accumulation=0;
491 if (out) break;
492 }
493 }
494 if (!out) {
495 // proboje zablokowac w poziomie z prawej nanlewa
496 for (int a=2;a>=0;a--) {
497 for (int b=2;b>=0;b--) {
498 if (field[a][b]==FieldGame.WHELL) accumulation++;
499 if (field[a][b]==FieldGame.CROSS) accumulation=0;
500 if (field[a][b]==FieldGame.FREE &&
501 accumulation==2) {i=a; j=b;out=true;find=true;
502 System.out.println("Blok w poziomie 2");break;}
503 }
504 accumulation=0;
505 if (out) break;
506 }
507 }
508 if (!out) {
509 // proboje zablokowac w pionie z lewej na prawa
510 for (int a=0;a<3;a++) {
511 for (int b=0;b<3;b++) {
512 if (field[b][a]==FieldGame.WHELL) accumulation++;
513 if (field[b][a]==FieldGame.CROSS) accumulation=0;
514 if (field[b][a]==FieldGame.FREE &&
515 accumulation==2) {i=b; j=a;out=true;find=true;
516 System.out.println("Blok w pionie 1");break;}
517 }
518 accumulation=0;
519 if (out) break;
520 }
521 }
522 if (!out) {
523 // proboje zablokowac w pionie z dolu do gory
524 for (int a=2;a>=0;a--) {
525 for (int b=2;b>=0;b--) {
526 if (field[b][a]==FieldGame.WHELL) accumulation++;
527 if (field[b][a]==FieldGame.CROSS) accumulation=0;
528 if (field[b][a]==FieldGame.FREE &&
529 accumulation==2) {i=b; j=a;out=true;find=true;
530 System.out.println("Blok w pionie 2");break;}
531 }
532 accumulation=0;
533 if (out) break;
534 }
535 }
536 } // priority
537
538
539 // blokowanie na ukos
540 if (priority<2) {
541 out=false;
542 if (!out) {
543 if (field[0][0].equals(FieldGame.WHELL) &&
544 field[1][1].equals(FieldGame.WHELL) &&
545 field[2][2].equals(FieldGame.FREE)) {
546 i=2; j=2;out=true;find=true;priority=2;System.out.println("blokowanie unslash od gory");
547
548 }
549 }
550 if (!out) {
551 if (field[0][0].equals(FieldGame.WHELL) &&
552 field[2][2].equals(FieldGame.WHELL) &&
553 field[1][1].equals(FieldGame.FREE)) {
554 i=1; j=1;out=true;find=true;priority=2;System.out.println("blokowanie unslash od gory z dziura w srodku");
555
556 }
557 }
558 if (!out){
559 if (field[0][2].equals(FieldGame.WHELL) &&
560 field[1][1].equals(FieldGame.WHELL) &&
561 field[2][0].equals(FieldGame.FREE)) {
562 i=2; j=0;out=true;find=true;priority=2;System.out.println("blokowanie slash od gory");
563
564 }
565 }
566 if (!out) {
567 if (field[2][2].equals(FieldGame.WHELL) &&
568 field[1][1].equals(FieldGame.WHELL) &&
569 field[0][0].equals(FieldGame.FREE)) {
570 i=0; j=0;out=true;find=true;priority=2;System.out.println("blokowanie slash od dolu z dziura w srodku");
571
572 }
573 }
574 if (!out) {
575 if (field[2][2].equals(FieldGame.WHELL) &&
576 field[0][0].equals(FieldGame.WHELL) &&
577 field[1][1].equals(FieldGame.FREE)) {
578 i=1; j=1;out=true;find=true;priority=2;System.out.println("blokowanie slash od dolu");
579
580 }
581 }
582 if (!out) {
583 if (field[2][0].equals(FieldGame.WHELL) &&
584 field[1][1].equals(FieldGame.WHELL) &&
585 field[0][2].equals(FieldGame.FREE)) {
586 i=0; j=2;out=true;find=true;priority=2;System.out.println("blokowanie slash od dolu");
587
588 }
589 }
590 }
591
592
593 System.out.println("checkAiField i: "+i+" j: "+j);
594 // te petle sa na wszelki wypadek
595 if (!find) {
596 for (int a=0;a<3;a++) {
597 for (int b=0;b<3;b++)
598 if (field[a][b]==FieldGame.FREE) {i=a; j=b;System.out.println("FIND");}
599
600 }
601
602 }
603 }
604 }
605 }