annotate Jttt/src/Jttt.java @ 15:f61684ae8245

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