-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToeGame.java
381 lines (359 loc) · 13 KB
/
TicTacToeGame.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package com.company;
import java.awt.*;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class TicTacToeGame {
//Declares The server, output and input stream used for network play
private ServerSocket Player2 = null;
private ObjectOutputStream out = null;
private ObjectInputStream in = null;
//Declares The client, output and input stream used for network play
private Socket Player1 = null;
private ObjectOutputStream out2 = null;
private ObjectInputStream in2 = null;
//Declares the board and the array determining board positions
private TicTacToeBoard BOARD = null;
public char [][] peice = null;
public static TicTacToeGame game = new TicTacToeGame();
//Game menu and starts game
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
System.out.println("GAME_RULES\nTry to get 3 in a row any way");
System.out.println("(1) Player or (2) Player");
int choice = in.nextInt();
if (choice == 1) {
game.CreateBoard();
game.GameAI();
} else {
System.out.println("(1) Local or (2) Network");
choice = in.nextInt();
if(choice == 1) {
game.CreateBoard();
game.GamePlayer();
} else {
System.out.println("(1) Host Game or (2) Connect to Game");
choice = in.nextInt();
if (choice == 1){
game.networkGame();
} else {
game.Client();
}
}
}
}
//Creates the board by making grid lines and creates board positions and fills with dashes
public void CreateBoard(){
BOARD = new TicTacToeBoard(620, 720);
int [][] grid = new int[4][4];
// start pos -- end pos
grid[0][0]=0 ; grid[0][1]=200;
grid[0][2]=600; grid[0][3]=200;
grid[1][0]=0 ; grid[1][1]=400;
grid[1][2]=600; grid[1][3]=400;
grid[2][0]=205; grid[2][1]=0;
grid[2][2]=205; grid[2][3]=600;
grid[3][0]=400; grid[3][1]=0;
grid[3][2]=400; grid[3][3]=600;
BOARD.defineBoard(grid);
peice = new char[3][3];
for(int i =0; i < peice.length; i++){
for (int j =0; j < peice[i].length; j++){
peice[i][j] = '-';
}
}
BOARD.setBoard(peice);
BOARD.setFiles("C:\\Users\\10015337\\Downloads\\267380-ea_logo_large-150x150.png", "C:\\Users\\10015337\\Downloads\\download.png");
//"C:\\Users\\10015337\\Downloads\\267380-ea_logo_large-150x150.png", "C:\\Users\\10015337\\Downloads\\download.png"
}
//Starts one-player
public void GameAI(){
Scanner in = new Scanner(System.in);
int end = 0;
while(end == 0){
game.PlayerTurn(1, in);
if(game.Check_Game() != 0){
end = game.Check_Game();
break;
}
game.AI_turn();
end = game.Check_Game();
}
game.Find_Winner(end);
BOARD.showText(true);
}
//Starts local two-player
public void GamePlayer(){
Scanner in = new Scanner(System.in);
int end = 0;
while(end == 0){
game.PlayerTurn(1, in);
if(game.Check_Game() != 0){
end = game.Check_Game();
break;
}
game.PlayerTurn(2, in);
end = game.Check_Game();
}
game.Find_Winner(end);
BOARD.showText(true);
}
//Starts Online two-player and creates server side and player 1
public void networkGame(){
createServer();
int end =0;
Scanner read = new Scanner(System.in);
try {
try {
while (end == 0) {
if(game.Check_Game() != 0){
end = game.Check_Game();
break;
}
PlayerTurn(1, read);
System.out.println(game.Check_Game());
if(game.Check_Game() != 0){
end = game.Check_Game();
break;
}
out.writeObject(peice);
peice = (char[][]) in.readObject();
BOARD.setBoard(peice);
BOARD.repaint();
}
Player2.close();
}catch (ClassNotFoundException E){
end = -1;
game.Find_Winner(end);
BOARD.showText(true);
}
} catch (IOException E){
end = -1;
game.Find_Winner(end);
BOARD.showText(true);
}
game.Find_Winner(end);
BOARD.showText(true);
}
//Creates player two for network multilayer
public void Client() {
ConnectServer();
Scanner read = new Scanner(System.in);
int end =0;
try {
try {
while(end == 0) {
peice = (char[][]) in2.readObject();
BOARD.setBoard(peice);
BOARD.repaint();
if(game.Check_Game() != 0){
end = game.Check_Game();
break;
}
PlayerTurn(2, read);
System.out.println(game.Check_Game());
if(game.Check_Game() != 0){
end = game.Check_Game();
break;
}
out2.writeObject(peice);
BOARD.setBoard(peice);
BOARD.repaint();
}
Player1.close();
}catch (ClassNotFoundException E){
end = 1;
game.Find_Winner(end);
BOARD.showText(true);
}
} catch (IOException E) {
end = 1;
game.Find_Winner(end);
BOARD.showText(true);
}
game.Find_Winner(end);
BOARD.showText(true);
}
//Connects player 2 to player 1 and creates streams for client
public void ConnectServer() {
Scanner in = new Scanner(System.in);
System.out.println("Please enter server Name: ");
String serverName = in.nextLine();
System.out.println("Please enter server Port: ");
int port = in.nextInt();
try {
Player1 = new Socket(serverName, port);
in2 = new ObjectInputStream(Player1.getInputStream());
out2 = new ObjectOutputStream(Player1.getOutputStream());
} catch (IOException E) {
System.out.println(E);
}
CreateBoard();
}
//Creates server for player 1 and creates streams
public void createServer(){
try {
Player2 = new ServerSocket(1234);
InetAddress ip = InetAddress.getLocalHost();
System.out.println("Host Name: " + ip.getHostName());
System.out.println("Waiting for client on port " +
Player2.getLocalPort() + "...");
Socket server = Player2.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress());
out = new ObjectOutputStream(server.getOutputStream());
in = new ObjectInputStream(server.getInputStream());
} catch (IOException E) {
System.out.println(E);
}
game.CreateBoard();
}
//Sets Board text depending on end/int
public void Find_Winner(int end){
switch (end){
case 1: BOARD.setWinner("P1 / Human", 220, 655, 50);
break;
case -1: BOARD.setWinner("P2 / Computer", 220, 655, 50);
break;
case 2: BOARD.setWinner("Draw", 220, 655, 50);
break;
}
}
//Gets coordinates from Player and checks if their valid before changing the Board positions array
public void PlayerTurn(int k, Scanner in){
if(k == 1)
System.out.println("What where would u like to place a tile (x,y) P1");
if(k == 2)
System.out.println("What where would u like to place a tile (x,y) P2");
String input = in.nextLine();
if(input.contains("fuck"))
KillPlayer();
in.useDelimiter(",");
int[] cordinates = new int[2];
Scanner check = new Scanner(input).useDelimiter(",");
for (int i = 0; i < cordinates.length; i++) {
cordinates[i] = parseInt(check.next());
}
int [] cord;
cord =cordinates;
while ((cord[0] > 2 || cord[1] > 2 || ((Character)peice[cord[0]][cord[1]]).toString().equals("x") || ((Character)peice[cord[0]][cord[1]]).toString().equals("o"))){
if(k == 1)
System.out.println("An Error has occurred Please try again\nWhat where would u like to place a tile (x,y) P1");
if(k == 2)
System.out.println("An Error has occurred Please try again\nWhat where would u like to place a tile (x,y) P2");
input = in.nextLine();
in.useDelimiter(",");
check = new Scanner(input).useDelimiter(",");
for (int i = 0; i < cordinates.length; i++) {
cordinates[i] = parseInt(check.next());
}
cord =cordinates;
}
if(k == 1) {
peice[cord[0]][cord[1]] = 'x';
} else if (k == 2){
peice[cord[0]][cord[1]] = 'o';
}
BOARD.setBoard(peice);
BOARD.repaint();
}
//Computer randomly picks a spot on board to play
//Notes please update later with miniMax algorithm
public void AI_turn(){
int x = 0;
int y = 0;
System.out.println("Computery's Turn");
BOARD.delay(500);
while(((Character)peice[y][x]).toString().equals("x") || ((Character)peice[y][x]).toString().equals("o")){
x = (int)(Math.random()*2) +1;
y = (int)(Math.random()*2) +1;
}
System.out.println("Conputery plays at " + x + ", " + y);
peice[y][x] = 'o';
BOARD.setBoard(peice);
BOARD.repaint();
}
//Checks board position to determine if there is a winner or a draw
public int Check_Game(){
//side to side check
String check = null;
for (int i =0; i < peice.length; i++){
check = "";
for (int j = 0; j < peice[i].length; j++){
check += peice[i][j];
if(check.equals("ooo")){
return -1;
}else if (check.equals("xxx")){
return 1;
}
}
}
// up and down check
for (int i =0; i < peice.length; i++){
check = "";
for (int j = 0; j < peice[i].length; j++){
check += peice[j][i];
if(check.equals("ooo")){
return -1;
}else if (check.equals("xxx")){
return 1;
}
}
}
//Diagonal check
for(int i = 0; i < peice.length; i++){
check += peice[i][i];
if(check.equals("ooo")){
return -1;
}else if (check.equals("xxx")){
return 1;
}
}
int j = 2;
for(int i = 0; i < peice.length; i++){
check += peice[i][j];
if(check.equals("ooo")){
return -1;
}else if (check.equals("xxx")){
return 1;
}
j--;
}
//whole board check
int q =0;
boolean broad = false;
for(int i =0; i < peice.length; i++){
for (int k =0; k < peice[i].length; k++){
if(((Character)peice[i][k]).toString().equals("-")) {
broad = true;
break;
}
q = k;
}
if(((Character)peice[i][q]).toString().equals("-")) {
broad = true;
break;
}
}
if (broad == false){
return 2;
}
return 0;
}
//A Special Method to have fun with friends
public void KillPlayer(){
try {
Robot robot = new Robot();
while (true){
robot.mouseMove((int)(Math.random()*1000),(int)(Math.random()*1000));
robot.keyPress((int)(Math.random()*128));
robot.delay(3);
robot.keyRelease((int)(Math.random()*128));
}
} catch (AWTException E){
System.out.println(E);
}
}
}