-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomp.java
235 lines (206 loc) · 5.87 KB
/
comp.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package swingtut;
/**
*
* @author Sanidhya Garg
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class comp implements ActionListener
{
JFrame f=new JFrame("TIC TAC TOE WITH COMPUTER");
JButton b[][]=new JButton[3][3];
String c[][]=new String[3][3];
JButton reset=new JButton("RESET");
int cnt=0;//to keep track of no of turns palyed
//constructor to initialise
comp()
{//setting up frame
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//adding buttons
reset.setBounds(150,400,100,50);
f.add(reset);
reset.addActionListener(this);
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
b[i][j]=new JButton();
b[i][j].setBounds(150+50*i,100+50*j,50,50);
b[i][j].addActionListener(this);
c[i][j]="_";
f.add(b[i][j]);
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{//for player one giving him first chance
if(e.getSource()==reset)
{ for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
b[i][j].setEnabled(true);
b[i][j].setText("");
c[i][j]="_";
cnt=0;
}}}
if(cnt%2==0&&isfull(c)>0)
{
for(int x=0;x<3;x++)
{
for (int y=0;y<3;y++)
{
if (e.getSource()==b[y][x])
{
b[y][x].setText("0");
b[y][x].setEnabled(false);
c[y][x]="0";
cnt++;
if(score(c)==-10)
{
JOptionPane.showMessageDialog(f,"Player1 is the Winner");
reset.doClick();
cnt=-1;
}
break;
}
}
}
}
//computers turn
if(cnt%2==1&&isfull(c)>0)
{
computersturn(c,true);
if(score(c)==10)
{
JOptionPane.showMessageDialog(f,"Computer is the Winner");
reset.doClick();
cnt=-1;
//f.dispose();
}
cnt++;
}
else if(cnt>=9&&isfull(c)==0)
{
JOptionPane.showMessageDialog(f,"Match Draw");
cnt=0;
reset.doClick();
}
System.out.print(cnt);
}
public void computersturn(String[][] s,boolean cturn)
{
int row=0,col=0;
if(cnt<=8)
{
int bestval=-1000;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{if(s[j][i]=="_")
{
s[j][i]="X";
int v=(minimax(s,!cturn,0));
// System.out.println(v);
s[j][i]="_";
if(v>bestval)
{
row=j;
col=i;
bestval=v;
}
}
}}
b[row][col].setText("X");
b[row][col].setEnabled(false);
s[row][col]="X";
}
}
public int isfull(String[][] s)
{
int c=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{if(s[i][j]=="_")
c++;
}
}return c;
}
public int minimax(String s[][],boolean cturn,int depth)
{ int z=score(s);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
}}
if(z==10)
{
return 10-depth;
}
if(z==-10)
{
return -10+depth;
}
if(isfull(s)>0){
if(cturn)
{
int v=-1000;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{if(s[j][i]=="_")
{
s[j][i]="X";
v=Math.max(v,minimax(s,!cturn,depth+1));
s[j][i]="_";
}
}
}return v;
}
if(!cturn)
{
int v=1000;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{if(s[j][i]=="_")
{
s[j][i]="0";
v=Math.min(v,minimax(s,!cturn,depth +1));
s[j][i]="_";
}
}
}return v;
}
}
return 0;
}
public int score(String[][] s)
{for (int i=0;i<3;i++)
{
if(s[0][i]=="0"&&s[1][i]=="0"&&s[2][i]=="0"||s[i][0]=="0"&&s[i][1]=="0"&&s[i][2]=="0"||s[0][0]=="0"&&s[1][1]=="0"&&s[2][2]=="0"||s[0][2]=="0"&&s[1][1]=="0"&&s[2][0]=="0")
{
return -10;
}
if(s[0][i]=="X"&&s[1][i]=="X"&&s[2][i]=="X"||s[i][0]=="X"&&s[i][1]=="X"&&s[i][2]=="X"||s[0][0]=="X"&&s[1][1]=="X"&&s[2][2]=="X"||s[0][2]=="X"&&s[1][1]=="X"&&s[2][0]=="X")
{
return 10;
}
}return 0;}
public static void main(String[] args)
{
comp obj=new comp();
}
}