-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.java
191 lines (186 loc) · 4.02 KB
/
TicTacToe.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
import java.io.*;
public class TicTacToe
{
char ar[][] = new char[3][3];
int pos[] = new int[10];
static String player1 = "Player1", player2 = "Player2";
static char ch1 = 'X',ch2 = 'O';
BufferedReader br;
public void choose()throws IOException
{
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(player1+": Do you want X or O");
ch1=(char)br.read();
if(ch1!='X'&&ch1!='O')
{
System.out.println("You made wrong selection. Please try again");
System.exit(0);
}
else
{//20
System.out.print(player1+": You will play with "+ch1);
ch2 = (ch1=='X')?'O':'X';
System.out.println(" and "+player2+": You will play with "+ch2);
}
}
public void startgame()
{
for(int i = 1;i<=2; i++)
{
for(int j = 1;j<=2; j++)
{
System.out.print(" |");
}
System.out.println();
System.out.println("____________");
}
System.out.print(" |");
System.out.println(" |");
}
public void playGame(int a, char ch)
{
int i,j;//54
if(a>=1&&a<=3)
{
i=0;
j=a-1;
}
else if(a<=6)
{
i=1;
j=a-4;
}
else // 65
{
i=2;
j=a-7;
}
ar[i][j]=ch;
}
public void initializeArr()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
ar[i-1][j-1] = ' ';
}
}
}
public void display()
{
for(int i = 1;i<=2; i++)
{
for(int j = 1;j<=2; j++)
{
System.out.print(" "+ar[i-1][j-1]+" |");
}
System.out.println(ar[i-1][2]);
System.out.println("____________");
}
for(int k = 1;k<=2; k++)
{
System.out.print(" "+ar[2][k-1]+" |");
}
System.out.println(ar[2][2]);
}
public boolean search1(int a)
{
System.out.println("checkpoint 2");//executed
int ub = pos.length, lb = 0, mid;
while(ub>=lb)
{
mid = (lb+ub)/2;
System.out.println("checkpoint 3");
if(pos[mid] == a)
return true;
else if(pos[mid]>a)
ub = pos[mid];
else
lb = pos[mid];
}
System.out.println("checkpoint 4");//not executed
return false;
}
public boolean search(int a)
{
//System.out.println("checkpoint 5");
for(int i = 0;i<pos.length;i++)
{
if(pos[i] == a)
return true;
}
//System.out.println("checkpoint 6");
return false;
}
public void checkWinner(char ch)
{
String player = (ch ==ch1)?player1:player2;
if(ar[0][0]==ch&&ar[0][1]==ch&&ar[0][2]==ch||
ar[1][0]==ch&&ar[1][1]==ch&&ar[1][2]==ch||
ar[2][0]==ch&&ar[2][1]==ch&&ar[2][2]==ch||
ar[0][0]==ch&&ar[1][0]==ch&&ar[2][0]==ch||
ar[0][1]==ch&&ar[1][1]==ch&&ar[2][1]==ch||
ar[0][2]==ch&&ar[1][2]==ch&&ar[2][2]==ch||
ar[0][0]==ch&&ar[1][1]==ch&&ar[2][2]==ch||
ar[0][2]==ch&&ar[1][1]==ch&&ar[2][0]==ch)
{
System.out.println("Congratulations "+player+". You are the Winner.");
System.exit(0);
}
else
{
}
}
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int p;
TicTacToe ttt = new TicTacToe();
ttt.pos[0]=0;
System.out.println("Player 1: Enter your name");
player1 = br.readLine();
System.out.println("Player 2: Enter your name");
player2 = br.readLine();ttt.choose();
ttt.startgame();
ttt.initializeArr();
char ch;
int n = 9,l = 1;
try{
for(int i = 0;i<n;i++)
{
ch = (i%2==0)?ch1:ch2;
String player = (ch ==ch1)?player1:player2;
System.out.println(player+": Choose your position(1-9)");
p = Integer.parseInt(br.readLine());
if(p>9)
{
i=i-1;
continue;
}
else
{
if(ttt.search(p)==false)
{
//System.out.println("Rhythma");
ttt.pos[l++] = p; // p cannot be repeated
}
else
{
i=i-1;
System.out.println("This position is already occupied.\nTry Again");
continue;
}
}
ttt.playGame(p,ch);
ttt.display();
ttt.checkWinner(ch);
}
System.out.println("It's a tie");
}
catch(Exception e)
{
System.out.println(e);
}
}
}