-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicToe.java
More file actions
118 lines (109 loc) · 3.08 KB
/
TicToe.java
File metadata and controls
118 lines (109 loc) · 3.08 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
import java.util.*;
public class TicToe {
public static void main(String args[]) {
TicTacToe t = new TicTacToe();
HumanPlayer p1 = new HumanPlayer("Sumit", 'X');
HumanPlayer p2 = new HumanPlayer("Chotu", 'O');
HumanPlayer cp;
cp = p1;
while(true) {
System.out.println(cp.name + " turn : ");
cp.makeMove();
TicTacToe.dispBoard();
if(TicTacToe.checkRowWin() || TicTacToe.checkColWin() || TicTacToe.checkDiaWin()) {
System.out.println(cp.name + " has won");
break;
} else {
if(cp == p1) {
cp = p2;
}
else {
cp = p1;
}
}
}
}
}
class TicTacToe {
static char ch[][];
public TicTacToe() {
ch = new char[3][3];
initBoard();
}
void initBoard() {
for(int i = 0; i<3; i++) {
for(int j = 0; j<3; j++) {
ch[i][j] = ' ';
}
}
}
static void dispBoard() {
System.out.print("-------------");
System.out.println();
for(int i = 0; i<3; i++) {
System.out.print("| ");
for(int j = 0; j<3; j++) {
System.out.print(ch[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) {
ch[row][col] = mark;
} else {
System.out.println("Invalid Position");
}
}
static boolean checkRowWin() {
for(int i = 0; i<3; i++) {
if(ch[i][0] != ' ' && ch[i][0] == ch[i][1] && ch[i][1] == ch[i][2]) {
return true;
}
}
return false;
}
static boolean checkColWin() {
for(int i = 0; i<3; i++) {
if(ch[0][i] != ' ' && ch[0][i] == ch[1][i] && ch[1][i] == ch[2][i]) {
return true;
}
}
return false;
}
static boolean checkDiaWin() {
for(int i = 0; i<3; i++) {
if(ch[0][0] != ' ' && ch[0][0] == ch[1][1] && ch[1][1] == ch[2][2] && ch[2][0] == ch[1][1] && ch[1][1] == ch[0][2]) {
return true;
}
}
return false;
}
}
class HumanPlayer {
String name;
char mark;
HumanPlayer(String name, char mark) {
this.name = name;
this.mark = mark;
}
void makeMove() {
Scanner sc = new Scanner(System.in);
int row, col;
do {
System.out.println("Enter row and col : ");
row = sc.nextInt();
col = sc.nextInt();
} while(!isValidMove(row, col));
TicTacToe.placeMark(row, col, mark);
}
boolean isValidMove(int row, int col) {
if(row>=0 && row<=2 && col>=0 && col<=2) {
if(TicTacToe.ch[row][col] == ' ') {
return true;
}
}
return false;
}
}