-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCode.java
181 lines (160 loc) · 5.87 KB
/
MyCode.java
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
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Scanner;
public class MyCode{
public static void main(String[] args) {
char[][] cells={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
JButton[][] buttons=new JButton[3][3];
JFrame frame=new JFrame();
frame.setSize(500,500);
frame.setTitle(" Toe Toa X-O ");
frame.setLayout(new GridLayout(3,3));
for(int i=0;i<cells.length;i++) {
for (int y = 0; y < cells[i].length; y++) {
JButton btn = new JButton(String.valueOf(cells[i][y]));
btn.setFont(new Font("Arial",Font.ROMAN_BASELINE,34));
buttons[i][y]=btn;
final int row=i,col=y;
btn.setText(String.valueOf(cells[i][y]));
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("clicked Button has "+ "x :"+row+"& y:"+col);
btn.setBackground(Color.magenta);
}
});
frame.add(btn);
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
// Scanner scanner=new Scanner(System.in);
// while (true) {
// if(CheckExist(cells)){
//// scanner.close();
// System.out.println("GAME IS ENDED \n RELOADED AND PLAY AGAIN 👽");
// break;
// }
// if (CheckWining(cells,'X')){
// System.out.println("====== You Won =====");
// break;
// }
// if (CheckWining(cells,'O')){
// System.out.println("====== You Lost =====");
// break;
// }
//
//// PlayerMove(frame, cells);
//
//// Computer move
// ComputerTurn(cells);
//
// Print(cells);
// }
}
private static boolean CheckWining(char[][] cells,char symbol){
if ((cells[0][0]==symbol && cells[0][1]==symbol && cells[0][2]==symbol)||
(cells[1][0]==symbol && cells[1][1]==symbol&&cells[1][2]==symbol)||
( cells[2][0]==symbol && cells[2][1]==symbol&&cells[2][2]==symbol)||
(cells[0][2]==symbol && cells[1][2]==symbol&&cells[2][2]==symbol)||
(cells[2][0]==symbol && cells[1][1]==symbol&&cells[0][2]==symbol)||
(cells[0][0]==symbol && cells[1][0]==symbol&&cells[2][0]==symbol)||
(cells[2][1]==symbol && cells[1][1]==symbol &&cells[0][1]==symbol)||
(cells[0][2]==symbol && cells[1][1]==symbol &&cells[2][2]==symbol)
){
return true;
}
return false;
}
private static boolean CheckExist(char[][] cells){
for (int i=0;i<cells.length;i++){
for (int j=0;j<i;j++){
if(cells[i][j]==' ')return false;
}
}
return true;
}
// private static void PlayerMove(JFrame frame, char[][] cells) {
// frame.getB
// String InputUser;
// while (true) {
// System.out.println("Start with a number 1--->9");
// InputUser = scanner.nextLine();
//// check if valid move
// if (CheckisValid(cells, Integer.parseInt(InputUser))) {
//break;
// } else {
//System.out.println("invalid num");
// }
// }
// //ASSIGN CELL WITH CORRESPONDENT
// Fill(InputUser, cells,'X');
// }
private static boolean CheckisValid(char[][] cells,int Input){
switch (Input){
case 1:
return ( cells[0][0]==' ');
case 2:
return ( cells[0][1]==' ');
case 3:
return ( cells[0][2]==' ');
case 4:
return cells[1][0]==' ';
case 5:
return cells[1][1]==' ';
case 6:
return cells[1][2]==' ';
case 7:
return cells[2][0]==' ';
case 8:
return cells[2][1]==' ';
case 9:
return cells[2][2]==' ';
default:
return false;
}
}
private static void ComputerTurn(char[][] cells) {
Random random=new Random();
int ComputerInput;
while (true){
ComputerInput=random.nextInt(9)+1;
System.out.println("computer input"+ComputerInput);
if (CheckisValid(cells,ComputerInput)){
break;
}}
Fill(Integer.toString(ComputerInput), cells,'O');
}
private static void Print(char[][] cells) {
System.out.println(cells[0][0]+" |"+ cells[0][1]+" |"+ cells[0][2]+"\n__+__+__\n"+ cells[1][0]+" |"+ cells[1][1]+" |"+ cells[1][2]+"\n__+__+__\n"+ cells[2][0]+" |"+ cells[2][1]+" |"+ cells[2][2]);
}
private static void Fill(String inputUser, char[][] cells,char symbol) {
switch (inputUser){
case "1":
cells[0][0]=symbol;break;
case "2":
cells[0][1]=symbol;break;
case "3":
cells[0][2]=symbol;break;
case "4":
cells[1][0]=symbol;break;
case "5":
cells[1][1]=symbol;break;
case "6":
cells[1][2]=symbol;break;
case "7": cells[2][0]=symbol;break;
case "8":
cells[2][1]=symbol;break;
case "9":
cells[2][2]=symbol;break;
default:
System.out.println("ENTER VALID NUMBER");
break;
}
}
}