-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaunchGame.java
291 lines (257 loc) · 6.45 KB
/
LaunchGame.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
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
import java.util.*;
import java.util.Random;
class TicTacToe
{
static char[][] board;
public TicTacToe()
{
board = new char[3][3];
initBoard();
}
void initBoard()
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
board[i][j]= ' ';
}
}
}
static void dispBoard()
{
System.out.println("-------------");
for(int i=0; i<board.length; i++)
{
System.out.print("| ");
for(int j=0; j<board[i].length; j++)
{
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("-------------");
}
}
static void placeMark(int row, int col, char mark)
{
if(row >=0 && row<=2 &&
col>=0 && col<=2)
{
board[row][col] = mark;
}
else
{
System.out.println("Invalid Position");
}
}
static boolean checkColWin()
{
for(int j=0; j<=2; j++)
{
if(board[0][j]!=' ' && board[0][j] == board[1][j] && board[1][j] == board[2][j])
{
return true;
}
}
return false;
}
static boolean checkRowWin()
{
for(int i=0; i<=2; i++)
{
if(board[i][0]!= ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
return true;
}
}
return false;
}
static boolean checkDiagWin()
{
if(board[0][0]!= ' ' && board[0][0] == board[1][1]
&& board[1][1] == board[2][2]
||board[0][2]!= ' ' && board[0][2] == board[1][1]
&& board[1][1] == board[2][0])
{
return true;
}
else
{
return false;
}
}
static boolean checkDraw()
{
for(int i=0; i<=2; i++)
{
for(int j=0; j<=2; j++)
{
if(board[i][j] == ' ')
{
return false;
}
}
}
return true;
}
}
abstract class Player
{
String name;
char mark;
abstract void makeMove();
boolean isValidMove(int row, int col)
{
if(row >= 0 && row <=2 &&
col >=0 && col<=2)
{
if(TicTacToe.board[row][col] == ' ')
{
return true;
}
}
return false;
}
}
class HumanPlayer extends Player
{
HumanPlayer(String name, char mark)
{
this.name = name;
this.mark = mark;
}
void makeMove()
{
Scanner scan = new Scanner(System.in);
int row;
int col;
do
{
System.out.println("Enter the row and col");
row = scan.nextInt();
col = scan.nextInt();
}while(! isValidMove(row,col));
TicTacToe.placeMark(row, col, mark);
}
}
class AIPlayer extends Player
{
AIPlayer(String name, char mark)
{
this.name = name;
this.mark = mark;
}
void makeMove()
{
Scanner scan = new Scanner(System.in);
int row;
int col;
do
{
Random r = new Random();
row = r.nextInt(3);
col = r.nextInt(3);
}while(! isValidMove(row,col));
TicTacToe.placeMark(row, col, mark);
}
}
public class LaunchGame
{
public static void main(String[] args)
{
TicTacToe t = new TicTacToe();
System.out.println("Hi, WELCOME TO TIC TAC TOE GAME");
System.out.println("1.Want to play with friend");
System.out.println("2.Want to play with AI");
System.out.println("3.Exit");
System.out.println("Enter your choice");
int ip;
Scanner s = new Scanner(System.in);
ip = s.nextInt();
if(ip == 1)
{
System.out.println("Enter Player1 name");
Scanner n1 = new Scanner(System.in);
String name1 = n1.nextLine();
System.out.println("Enter Player2 name");
Scanner n2 = new Scanner(System.in);
String name2 = n2.nextLine();
HumanPlayer p1 = new HumanPlayer(name1,'X');
HumanPlayer p2 = new HumanPlayer(name2,'O');
Player cp;
cp = p1;
while(true)
{
System.out.println(cp.name + " Turn");
cp.makeMove();
TicTacToe.dispBoard();
if(TicTacToe.checkColWin() || TicTacToe.checkRowWin() || TicTacToe.checkDiagWin())
{
System.out.println(cp.name +" Has Won");
break;
}
else if(TicTacToe.checkDraw())
{
System.out.println("Game is draw");
break;
}
else
{
if(cp == p1)
{
cp = p2;
}
else
{
cp = p1;
}
}
}
}
else if(ip == 2)
{
System.out.println("Enter Your name");
Scanner n1 = new Scanner(System.in);
String name1 = n1.nextLine();
HumanPlayer p1 = new HumanPlayer(name1,'X');
AIPlayer p2 = new AIPlayer("AI",'O');
Player cp;
cp = p1;
while(true)
{
System.out.println(cp.name + " Turn");
cp.makeMove();
TicTacToe.dispBoard();
if(TicTacToe.checkColWin() || TicTacToe.checkRowWin() || TicTacToe.checkDiagWin())
{
System.out.println(cp.name +" Has Won");
break;
}
else if(TicTacToe.checkDraw())
{
System.out.println("Game is draw");
break;
}
else
{
if(cp == p1)
{
cp = p2;
}
else
{
cp = p1;
}
}
}
}
else if(ip == 3)
{
System.out.println("Your are leaving");
}
else
{
System.out.println("Enter only 1 , 2 or 3");
}
}
}