Skip to content

Commit 7911fb3

Browse files
game.java
1 parent 98f3cf4 commit 7911fb3

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

Tic-Tac-Toe/game.java

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// A simple program to demonstrate
2+
// Tic-Tac-Toe Game.
3+
import java.util.*;
4+
5+
public class GFG {
6+
7+
static String[] board;
8+
static String turn;
9+
10+
11+
// CheckWinner method will
12+
// decide the combination
13+
// of three box given below.
14+
static String checkWinner()
15+
{
16+
for (int a = 0; a < 8; a++) {
17+
String line = null;
18+
19+
switch (a) {
20+
case 0:
21+
line = board[0] + board[1] + board[2];
22+
break;
23+
case 1:
24+
line = board[3] + board[4] + board[5];
25+
break;
26+
case 2:
27+
line = board[6] + board[7] + board[8];
28+
break;
29+
case 3:
30+
line = board[0] + board[3] + board[6];
31+
break;
32+
case 4:
33+
line = board[1] + board[4] + board[7];
34+
break;
35+
case 5:
36+
line = board[2] + board[5] + board[8];
37+
break;
38+
case 6:
39+
line = board[0] + board[4] + board[8];
40+
break;
41+
case 7:
42+
line = board[2] + board[4] + board[6];
43+
break;
44+
}
45+
//For X winner
46+
if (line.equals("XXX")) {
47+
return "X";
48+
}
49+
50+
// For O winner
51+
else if (line.equals("OOO")) {
52+
return "O";
53+
}
54+
}
55+
56+
for (int a = 0; a < 9; a++) {
57+
if (Arrays.asList(board).contains(
58+
String.valueOf(a + 1))) {
59+
break;
60+
}
61+
else if (a == 8) {
62+
return "draw";
63+
}
64+
}
65+
66+
// To enter the X Or O at the exact place on board.
67+
System.out.println(
68+
turn + "'s turn; enter a slot number to place "
69+
+ turn + " in:");
70+
return null;
71+
}
72+
73+
// To print out the board.
74+
/* |---|---|---|
75+
| 1 | 2 | 3 |
76+
|-----------|
77+
| 4 | 5 | 6 |
78+
|-----------|
79+
| 7 | 8 | 9 |
80+
|---|---|---|*/
81+
82+
static void printBoard()
83+
{
84+
System.out.println("|---|---|---|");
85+
System.out.println("| " + board[0] + " | "
86+
+ board[1] + " | " + board[2]
87+
+ " |");
88+
System.out.println("|-----------|");
89+
System.out.println("| " + board[3] + " | "
90+
+ board[4] + " | " + board[5]
91+
+ " |");
92+
System.out.println("|-----------|");
93+
System.out.println("| " + board[6] + " | "
94+
+ board[7] + " | " + board[8]
95+
+ " |");
96+
System.out.println("|---|---|---|");
97+
}
98+
99+
public static void main(String[] args)
100+
{
101+
Scanner in = new Scanner(System.in);
102+
board = new String[9];
103+
turn = "X";
104+
String winner = null;
105+
106+
for (int a = 0; a < 9; a++) {
107+
board[a] = String.valueOf(a + 1);
108+
}
109+
110+
System.out.println("Welcome to 3x3 Tic Tac Toe.");
111+
printBoard();
112+
113+
System.out.println(
114+
"X will play first. Enter a slot number to place X in:");
115+
116+
while (winner == null) {
117+
int numInput;
118+
119+
// Exception handling.
120+
// numInput will take input from user like from 1 to 9.
121+
// If it is not in range from 1 to 9.
122+
// then it will show you an error "Invalid input."
123+
try {
124+
numInput = in.nextInt();
125+
if (!(numInput > 0 && numInput <= 9)) {
126+
System.out.println(
127+
"Invalid input; re-enter slot number:");
128+
continue;
129+
}
130+
}
131+
catch (InputMismatchException e) {
132+
System.out.println(
133+
"Invalid input; re-enter slot number:");
134+
continue;
135+
}
136+
137+
// This game has two player x and O.
138+
// Here is the logic to decide the turn.
139+
if (board[numInput - 1].equals(
140+
String.valueOf(numInput))) {
141+
board[numInput - 1] = turn;
142+
143+
if (turn.equals("X")) {
144+
turn = "O";
145+
}
146+
else {
147+
turn = "X";
148+
}
149+
150+
printBoard();
151+
winner = checkWinner();
152+
}
153+
else {
154+
System.out.println(
155+
"Slot already taken; re-enter slot number:");
156+
}
157+
}
158+
159+
// If no one win or lose from both player x and O.
160+
// then here is the logic to print "draw".
161+
if (winner.equalsIgnoreCase("draw")) {
162+
System.out.println(
163+
"It's a draw! Thanks for playing.");
164+
}
165+
166+
// For winner -to display Congratulations! message.
167+
else {
168+
System.out.println(
169+
"Congratulations! " + winner
170+
+ "'s have won! Thanks for playing.");
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)