-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
176 lines (160 loc) · 4.73 KB
/
Copy pathTicTacToe.java
File metadata and controls
176 lines (160 loc) · 4.73 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
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
import java.util.Scanner;
public class TicTacToe {
static char [][] board = new char [3][3];
static int row;
static int column;
public static Scanner sc = new Scanner(System.in);
public static void show_result_board() {
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
public static void win() {
if(board[0][0]=='x' && board[0][1]=='x' && board[0][2]=='x') {
System.out.println("X own the match");
System.exit(0);
}
else if(board[0][0]=='x' && board[1][1]=='x' && board[2][2]=='x') {
System.out.println("X own the match");
System.exit(0);
}
else if(board[0][2]=='x' && board[1][1]=='x' && board[2][0]=='x') {
System.out.println("X own the match");
System.exit(0);
}
else if(board[1][0]=='x' && board[1][1]=='x' && board[1][2]=='x') {
System.out.println("X own the match");
System.exit(0);
}
else if(board[2][0]=='x' && board[2][1]=='x' && board[2][2]=='x') {
System.out.println("X own the match");
System.exit(0);
}
else if(board[0][0]=='x' && board[1][0]=='x' && board[2][0]=='x') {
System.out.println("X own the match");
System.exit(0);
}
else if(board[0][1]=='x' && board[1][1]=='x' && board[2][1]=='x') {
System.out.println("X own the match");
System.exit(0);
}
else if(board[0][2]=='x' && board[1][2]=='x' && board[2][2]=='x') {
System.out.println("X own the match");
System.exit(0);
}
if(board[0][0]=='o' && board[0][1]=='o' && board[0][2]=='o') {
System.out.println("O own the match");
System.exit(0);
}
else if(board[0][0]=='o' && board[1][1]=='o' && board[2][2]=='o') {
System.out.println("O own the match");
System.exit(0);
}
else if(board[0][2]=='o' && board[1][1]=='o' && board[2][0]=='o') {
System.out.println("O own the match");
System.exit(0);
}
else if(board[1][0]=='o' && board[1][1]=='o' && board[1][2]=='o') {
System.out.println("O own the match");
System.exit(0);
}
else if(board[2][0]=='o' && board[2][1]=='o' && board[2][2]=='o') {
System.out.println("O own the match");
System.exit(0);
}
else if(board[0][0]=='o' && board[1][0]=='o' && board[2][0]=='o') {
System.out.println("O own the match");
System.exit(0);
}
else if(board[0][1]=='o' && board[1][1]=='o' && board[2][1]=='o') {
System.out.println("O own the match");
System.exit(0);
}
else if(board[0][2]=='o' && board[1][2]=='o' && board[2][2]=='o') {
System.out.println("O own the match");
System.exit(0);
}
}
public static void get_valid_move() {
while (true) {
System.out.print("Enter a row number: ");
row = sc.nextInt();
System.out.print("Enter a column number: ");
column = sc.nextInt();
if (row >= 0 && row < 3 && column >= 0 && column < 3) {
if (board[row][column] == '-') {
} else {
System.out.println("That spot is already occupied! Please try a different spot.");
}
} else {
System.out.println("Invalid input! Rows and columns must be 0, 1, or 2.");
}
}
}
public static void main (String[] args) {
System.out.println("=== WELCOME TO TIC-TAC-TOE ===");
System.out.println("row are the horizontal spaces or lines which are 3 here");
System.out.println("column are the vertical spaces or lines which are 3 here");
System.out.println("Warning- row could not be entered more than 0,1,2 and same for column");
System.out.println("Specific boxes can be accessed by coordinates. suppose first line is 00--> it means row 0 and column 0. 01--> it mean row 0 and column 1 and so on.");
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
board[i][j] = '-';
}
}
show_result_board();
//1
System.out.println("Enter for X: ");
get_valid_move();
board[row][column] = 'x';
show_result_board();
//2
System.out.println("Enter for o: ");
get_valid_move();
board[row][column] = 'o';
show_result_board();
//3
System.out.println("Enter for X: ");
get_valid_move();
board[row][column] = 'x';
show_result_board();
//4
System.out.println("Enter for o: ");
get_valid_move();
board[row][column] = 'o';
show_result_board();
//5
System.out.println("Enter for X: ");
get_valid_move();
board[row][column] = 'x';
show_result_board();
win();
//6
System.out.println("Enter for o: ");
get_valid_move();
board[row][column] = 'o';
show_result_board();
win();
//7
System.out.println("Enter for X: ");
get_valid_move();
board[row][column] = 'x';
show_result_board();
win();
//8
System.out.println("Enter for o: ");
get_valid_move();
board[row][column] = 'o';
show_result_board();
win();
//9
System.out.println("Enter for X: ");
get_valid_move();
board[row][column] = 'x';
show_result_board();
win();
}
}