Skip to content

Commit de34a47

Browse files
authored
Add files via upload
Tic toe game for beginner Android developer
1 parent f7bc670 commit de34a47

File tree

2 files changed

+487
-0
lines changed

2 files changed

+487
-0
lines changed

MainActivity.java

Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
package com.example.aashutosh.tic_tac_toe;
2+
3+
import android.content.Intent;
4+
import android.os.SystemClock;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.os.Bundle;
7+
import android.view.View;
8+
import android.widget.Button;
9+
import android.widget.Toast;
10+
11+
public class MainActivity extends AppCompatActivity {
12+
13+
Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
14+
int turn;
15+
16+
@Override
17+
protected void onCreate(Bundle savedInstanceState) {
18+
super.onCreate(savedInstanceState);
19+
setContentView(R.layout.activity_main);
20+
21+
b1=(Button)findViewById(R.id.btn);
22+
b2=(Button)findViewById(R.id.btn2);
23+
b3=(Button)findViewById(R.id.btn3);
24+
b4=(Button)findViewById(R.id.btn4);
25+
b5=(Button)findViewById(R.id.btn5);
26+
b6=(Button)findViewById(R.id.btn6);
27+
b7=(Button)findViewById(R.id.btn7);
28+
b8=(Button)findViewById(R.id.btn8);
29+
b9=(Button)findViewById(R.id.btn9);
30+
31+
turn=1;
32+
33+
b1.setOnClickListener(new View.OnClickListener() {
34+
@Override
35+
public void onClick(View view)
36+
{
37+
if(b1.getText().toString().equals(""))
38+
{
39+
if(turn == 1)
40+
{
41+
turn = 2;
42+
b1.setText("x");
43+
}
44+
else if(turn == 2)
45+
{
46+
turn = 1;
47+
b1.setText("o");
48+
}
49+
}
50+
endGame();
51+
}
52+
});
53+
54+
b2.setOnClickListener(new View.OnClickListener() {
55+
@Override
56+
public void onClick(View view)
57+
{
58+
if (b2.getText().toString().equals(""))
59+
{
60+
if (turn == 1)
61+
{
62+
turn = 2;
63+
b2.setText("x");
64+
}
65+
else if (turn == 2)
66+
{
67+
turn = 1;
68+
b2.setText("o");
69+
}
70+
}
71+
endGame();
72+
}
73+
});
74+
75+
b3.setOnClickListener(new View.OnClickListener() {
76+
@Override
77+
public void onClick(View view)
78+
{
79+
if (b3.getText().toString().equals(""))
80+
{
81+
if (turn == 1)
82+
{
83+
turn = 2;
84+
b3.setText("x");
85+
}
86+
else if (turn == 2)
87+
{
88+
turn = 1;
89+
b3.setText("o");
90+
}
91+
}
92+
endGame();
93+
}
94+
});
95+
96+
b4.setOnClickListener(new View.OnClickListener() {
97+
@Override
98+
public void onClick(View view)
99+
{
100+
if (b4.getText().toString().equals(""))
101+
{
102+
if (turn == 1)
103+
{
104+
turn = 2;
105+
b4.setText("x");
106+
}
107+
else if (turn == 2)
108+
{
109+
turn = 1;
110+
b4.setText("o");
111+
}
112+
}
113+
114+
endGame();
115+
}
116+
});
117+
118+
b5.setOnClickListener(new View.OnClickListener() {
119+
@Override
120+
public void onClick(View view)
121+
{
122+
if (b5.getText().toString().equals(""))
123+
{
124+
if (turn == 1)
125+
{
126+
turn = 2;
127+
b5.setText("x");
128+
}
129+
else if (turn == 2)
130+
{
131+
turn = 1;
132+
b5.setText("o");
133+
}
134+
}
135+
136+
endGame();
137+
}
138+
});
139+
140+
b6.setOnClickListener(new View.OnClickListener() {
141+
@Override
142+
public void onClick(View view)
143+
{
144+
if (b6.getText().toString().equals(""))
145+
{
146+
if (turn == 1)
147+
{
148+
turn = 2;
149+
b6.setText("x");
150+
}
151+
else if (turn == 2)
152+
{
153+
turn = 1;
154+
b6.setText("o");
155+
}
156+
}
157+
158+
endGame();
159+
}
160+
});
161+
162+
b7.setOnClickListener(new View.OnClickListener() {
163+
@Override
164+
public void onClick(View view)
165+
{
166+
if (b7.getText().toString().equals(""))
167+
{
168+
if (turn == 1)
169+
{
170+
turn = 2;
171+
b7.setText("x");
172+
}
173+
else if (turn == 2)
174+
{
175+
turn = 1;
176+
b7.setText("o");
177+
}
178+
}
179+
180+
endGame();
181+
}
182+
});
183+
184+
b8.setOnClickListener(new View.OnClickListener() {
185+
@Override
186+
public void onClick(View view)
187+
{
188+
if (b8.getText().toString().equals(""))
189+
{
190+
if (turn == 1)
191+
{
192+
turn = 2;
193+
b8.setText("x");
194+
}
195+
else if (turn == 2)
196+
{
197+
turn = 1;
198+
b8.setText("o");
199+
}
200+
}
201+
202+
endGame();
203+
}
204+
});
205+
206+
b9.setOnClickListener(new View.OnClickListener() {
207+
@Override
208+
public void onClick(View view)
209+
{
210+
if (b9.getText().toString().equals(""))
211+
{
212+
if (turn == 1)
213+
{
214+
turn = 2;
215+
b9.setText("x");
216+
}
217+
else if (turn == 2)
218+
{
219+
turn = 1;
220+
b9.setText("o");
221+
}
222+
}
223+
224+
endGame();
225+
}
226+
});
227+
228+
229+
}
230+
public void endGame()
231+
232+
{
233+
String a,b,c,d,e,f,g,h,i;
234+
boolean end = false;
235+
236+
a = b1.getText().toString();
237+
b = b2.getText().toString();
238+
c = b3.getText().toString();
239+
240+
d = b4.getText().toString();
241+
e = b5.getText().toString();
242+
f = b6.getText().toString();
243+
244+
g = b7.getText().toString();
245+
h = b8.getText().toString();
246+
i = b9.getText().toString();
247+
248+
if(a.equals("x") && b.equals("x") && c.equals("x"))
249+
{
250+
Toast.makeText(MainActivity.this,"winner_player_x",Toast.LENGTH_LONG).show();
251+
end = true;
252+
}
253+
if(a.equals("x") && e.equals("x") && i.equals("x"))
254+
{
255+
Toast.makeText(MainActivity.this,"winner_player_x",Toast.LENGTH_LONG).show();
256+
end = true;
257+
}
258+
if(a.equals("x") && d.equals("x") && g.equals("x"))
259+
{
260+
Toast.makeText(MainActivity.this,"winner_player_x",Toast.LENGTH_LONG).show();
261+
end = true;
262+
}
263+
if(b.equals("x") && e.equals("x") && h.equals("x"))
264+
{
265+
Toast.makeText(MainActivity.this,"winner_player_x",Toast.LENGTH_LONG).show();
266+
end = true;
267+
}
268+
if(c.equals("x") && f.equals("x") && i.equals("x"))
269+
{
270+
Toast.makeText(MainActivity.this,"winner_player_x",Toast.LENGTH_LONG).show();
271+
end = true;
272+
}
273+
if(c.equals("x") && e.equals("x") && g.equals("x"))
274+
{
275+
Toast.makeText(MainActivity.this,"winner_player_x",Toast.LENGTH_LONG).show();
276+
end = true;
277+
}
278+
if(d.equals("x") && e.equals("x") && f.equals("x"))
279+
{
280+
Toast.makeText(MainActivity.this,"winner_player_x",Toast.LENGTH_LONG).show();
281+
end = true;
282+
}
283+
if(g.equals("x") && h.equals("x") && i.equals("x"))
284+
{
285+
Toast.makeText(MainActivity.this,"winner_player_x",Toast.LENGTH_LONG).show();
286+
end = true;
287+
}
288+
289+
290+
291+
if(a.equals("o") && b.equals("o") && c.equals("o"))
292+
{
293+
Toast.makeText(MainActivity.this,"winner_player_o",Toast.LENGTH_LONG).show();
294+
end = true;
295+
}
296+
if(a.equals("o") && e.equals("o") && i.equals("o"))
297+
{
298+
Toast.makeText(MainActivity.this,"winner_player_o",Toast.LENGTH_LONG).show();
299+
end = true;
300+
}
301+
if(a.equals("o") && d.equals("o") && g.equals("o"))
302+
{
303+
Toast.makeText(MainActivity.this,"winner_player_o",Toast.LENGTH_LONG).show();
304+
end = true;
305+
}
306+
if(b.equals("o") && e.equals("o") && h.equals("o"))
307+
{
308+
Toast.makeText(MainActivity.this,"winner_player_o",Toast.LENGTH_LONG).show();
309+
end = true;
310+
}
311+
if(c.equals("o") && f.equals("o") && i.equals("o"))
312+
{
313+
Toast.makeText(MainActivity.this,"winner_player_o",Toast.LENGTH_LONG).show();
314+
end = true;
315+
}
316+
if(c.equals("o") && e.equals("o") && g.equals("o"))
317+
{
318+
Toast.makeText(MainActivity.this,"winner_player_o",Toast.LENGTH_LONG).show();
319+
end = true;
320+
}
321+
if(d.equals("o") && e.equals("o") && f.equals("o"))
322+
{
323+
Toast.makeText(MainActivity.this,"winner_player_o",Toast.LENGTH_LONG).show();
324+
end = true;
325+
}
326+
if(g.equals("o") && h.equals("o") && i.equals("o"))
327+
{
328+
Toast.makeText(MainActivity.this,"winner_player_o",Toast.LENGTH_LONG).show();
329+
end = true;
330+
}
331+
332+
333+
if(end)
334+
335+
{
336+
337+
b1.setEnabled(false);
338+
b2.setEnabled(false);
339+
b3.setEnabled(false);
340+
b4.setEnabled(false);
341+
b5.setEnabled(false);
342+
b6.setEnabled(false);
343+
b7.setEnabled(false);
344+
b8.setEnabled(false);
345+
b9.setEnabled(false);
346+
347+
348+
/*SystemClock.sleep(7000);*/
349+
350+
}
351+
352+
Button rstbtn = (Button) findViewById(R.id.play_again);
353+
rstbtn.setOnClickListener(new View.OnClickListener() {
354+
@Override
355+
public void onClick(View v) {
356+
Intent current = getIntent();
357+
finish();
358+
startActivity(current);
359+
}
360+
});
361+
362+
363+
}
364+
365+
}

0 commit comments

Comments
 (0)