forked from ronijpandey/Java-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
127 lines (100 loc) · 3.51 KB
/
TicTacToe.java
File metadata and controls
127 lines (100 loc) · 3.51 KB
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
import java.util.Scanner;
/**
* class TicTacToe takes in user input and prints the game to the screen
*/
public class TicTacToe {
public static void main(String args[]) {
int flag = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter name\nPlayer 1 :");
// get name1
String name1 = in.nextLine();
System.out.println("Player 2 :");
// get name2
String name2 = in.nextLine();
// 2D array of 3 elements each
char[][] m = new char[3][3];
Setm(m);
int r, c, count = 0, u = 1;
// 9 moves in TicTacToe game
while (count < 9) {
RefPos(u);
r = in.nextInt();
c = in.nextInt();
if (m[r][c] != ' ') {
continue;
}
count++;
if (u == 1) {
m[r][c] = '0';
u = 2;
Printm(m);
} else if (u == 2) {
m[r][c] = 'x';
u = 1;
Printm(m);
}
if (count >= 5) {
flag = Check(m);
if (flag > 0) {
if (u == 1) {
System.out.println("\nPlayer 2 : " + name2 + ", Won!!");
return;
} else {
System.out.println("\nPlayer 1 : " + name1 + ", Won!!");
return;
}
}
}
}
System.out.println("\nMatch Drawn - Tie!!");
}
public static int Check(char[][] m) {
for (int i = 0; i < 3; i++) {
if ((m[i][0] == m[i][1]) && (m[i][0] == m[i][2]) && (m[i][1] == m[i][2]) && (m[i][0] == m[i][2]) && m[i][0] != ' ') {
return 1;
}
if ((m[0][i] == m[1][i]) && (m[0][i] == m[2][i]) && (m[1][i] == m[2][i]) && (m[i][0] == m[2][i]) && m[0][i] != ' ') {
return 2;
}
}
if ((m[0][0] == m[1][1]) && (m[0][0] == m[2][2]) && (m[1][1] == m[2][2]) && (m[0][0] == m[2][2]) && m[0][0] != ' ') {
return 3;
}
if ((m[2][0] == m[1][1]) && (m[2][0] == m[0][2]) && (m[1][1] == m[0][2]) && (m[2][0] == m[1][1]) && m[2][0] != ' ') {
return 4;
}
return 0;
}
public static void Printm(char[][] m) {
System.out.println("\n\n\n\n\n");
for (int i = 0; i < 3; ++i) {
System.out.print("\t\t\t");
for (int j = 0; j < 3; j++) {
System.out.print(m[i][j]);
if (j != 2) {
System.out.print(" | ");
}
}
if (i != 2) {
System.out.println("\n\t\t\t__|___|___");
}
}
}
public static void Setm(char[][] m) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
m[i][j] = ' ';
}
}
}
public static void RefPos(int u) {
//System.out.println("\nReference Position:\n");
//System.out.println("(0,0) | (0,1) | (0,2)");
//System.out.println("______|________|______");
//System.out.println("(1,0) | (1,1) | (1,2)");
//System.out.println("______|________|______");
//System.out.println("(2,0) | (2,1) | (2,2)");
System.out.println("Player " + u + ", Enter values:");
}
}