-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe_v321.java
80 lines (77 loc) · 3.18 KB
/
TicTacToe_v321.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
import java.util.Scanner;
public class TicTacToe_v321
{
public String player1 = "O";
public String player2 = "X";
public String nameP1;
public String nameP2;
String[][] arr;
boolean over = false;
int term;
ArrayPrinter ap = new ArrayPrinter();
public void setPlayer(String p1, String p2)
{
nameP1 = p1;
nameP2 = p2;
}
public String[][] game(String[][] arr)
{
this.arr = arr;
Scanner sc = new Scanner(System.in);
term = 1;
while (term <= 9)
{
// int row = sc.nextInt() - 1;
// int col = sc.nextInt() - 1;
int num = sc.nextInt();
int row = (num / 10) - 1;
int col = (num % 10) - 1;
if (term % 2 == 0) {
System.out.println(" - " + nameP2);
this.arr[row][col] = player2;
ap.printGame(this.arr);
System.out.println();
} else {
System.out.println(" - " + nameP1);
this.arr[row][col] = player1;
ap.printGame(this.arr);
System.out.println();
}
if (term >= 4) {
check(this.arr);
}
if (over) {
break;
}
term++;
}
sc.close();
return this.arr;
}
public void check(String[][] arr)
{
if ((arr[0][0] == player1 && arr[0][1] == player1 && arr[0][2] == player1)
|| (arr[1][0] == player1 && arr[1][1] == player1 && arr[1][2] == player1)
|| (arr[2][0] == player1 && arr[2][1] == player1 && arr[2][2] == player1)
|| (arr[0][0] == player1 && arr[1][0] == player1 && arr[2][0] == player1)
|| (arr[0][1] == player1 && arr[1][1] == player1 && arr[2][1] == player1)
|| (arr[0][2] == player1 && arr[1][2] == player1 && arr[2][2] == player1)
|| (arr[0][0] == player1 && arr[1][1] == player1 && arr[2][2] == player1)
|| (arr[0][2] == player1 && arr[1][1] == player1 && arr[2][0] == player1)) {
System.out.println("***** CONGRATULATIONS *****\n ***** " + nameP1 + " *****");
over = true;
} else if ((arr[0][0] == player2 && arr[0][1] == player2 && arr[0][2] == player2)
|| (arr[1][0] == player2 && arr[1][1] == player2 && arr[1][2] == player2)
|| (arr[2][0] == player2 && arr[2][1] == player2 && arr[2][2] == player2)
|| (arr[0][0] == player2 && arr[1][0] == player2 && arr[2][0] == player2)
|| (arr[0][1] == player2 && arr[1][1] == player2 && arr[2][1] == player2)
|| (arr[0][2] == player2 && arr[1][2] == player2 && arr[2][2] == player2)
|| (arr[0][0] == player2 && arr[1][1] == player2 && arr[2][2] == player2)
|| (arr[0][2] == player2 && arr[1][1] == player2 && arr[2][0] == player2)) {
System.out.println("***** CONGRATULATIONS *****\n ***** " + nameP2 + " *****");
over = true;
} else if (term == 9 && !over) {
System.out.println("\nBETTER LUCK NEXT TIME!\n:(");
}
}
}