Skip to content

Commit 3271e3c

Browse files
committed
Added program for tic tac toe game
1 parent 2a49e54 commit 3271e3c

1 file changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package dev.amitwani;
2+
3+
/*
4+
*
5+
* A program for end user so that he/she can play tic-tac-toe game with computer
6+
* or game can be played by two end users.
7+
*
8+
*/
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.Random;
13+
import java.util.Scanner;
14+
15+
public class TicTacToe {
16+
17+
public static void main(String[] args) {
18+
19+
System.out.print("!!Welcome to Tic-Tac-Toe!!\n" +
20+
"Decide your opponent:\n" +
21+
"1) Play with computer\n" +
22+
"2) Play with a friend\n" +
23+
"Enter your choice –");
24+
25+
Scanner scanner = new Scanner(System.in);
26+
int choice = scanner.nextInt();
27+
28+
scanner.nextLine();
29+
30+
String[] game = new String[]{"*", "*", "*", "*", "*", "*", "*", "*", "*"};
31+
String player1Mark = "X";
32+
String player2Mark = "O";
33+
boolean isGameOver = false;
34+
int nextTurn = 0;
35+
36+
if (choice == 1) {
37+
38+
System.out.print("\nEnter your name –");
39+
String playerName = scanner.nextLine();
40+
String computerName = "T-Bot";
41+
42+
System.out.println("Hey " + playerName + ", I am Computer. My name is ‘" + computerName + "’.");
43+
44+
System.out.print("\nWho’ll go first?\n" +
45+
"1) T-Bot\n" +
46+
"2) Me\n" +
47+
"Enter your choice –");
48+
49+
nextTurn = scanner.nextInt();
50+
51+
printGameBoard(game);
52+
53+
while (!isGameOver) {
54+
55+
List<Integer> emptySpaces = findEmptySpaces(game);
56+
57+
if (nextTurn == 1) {
58+
59+
System.out.print("T-Bot’s turn: ");
60+
61+
Random random = new Random();
62+
int nextMove = emptySpaces.get(random.nextInt(emptySpaces.size()));
63+
64+
System.out.print(nextMove);
65+
66+
updateGameBoard(game, nextMove, player1Mark);
67+
68+
nextTurn = 2;
69+
} else {
70+
71+
System.out.print(playerName + "’s turn: ");
72+
73+
playerNextMove(scanner, game, player2Mark, playerName, emptySpaces);
74+
75+
nextTurn = 1;
76+
}
77+
78+
printGameBoard(game);
79+
80+
String result = checkIfGameOver(game);
81+
82+
if (result != null) {
83+
isGameOver = true;
84+
String winner = result.equals(player1Mark) ? "T-Bot" : playerName;
85+
System.out.println("RESULTS TIME:\n" + winner + " WON !!");
86+
}
87+
88+
}
89+
90+
} else if (choice == 2) {
91+
92+
System.out.print("\nEnter your name of player 1 –");
93+
String player1Name = scanner.nextLine();
94+
System.out.print("\nEnter your name of player 2 –");
95+
String player2Name = scanner.nextLine();
96+
97+
System.out.print("\nWho’ll go first?\n" +
98+
"1) " + player1Name + "\n" +
99+
"2) " + player2Name + "\n" +
100+
"Enter your choice –");
101+
102+
nextTurn = scanner.nextInt();
103+
104+
printGameBoard(game);
105+
106+
while (!isGameOver) {
107+
108+
List<Integer> emptySpaces = findEmptySpaces(game);
109+
110+
if (nextTurn == 1) {
111+
112+
System.out.print(player1Name + "’s turn: ");
113+
114+
playerNextMove(scanner, game, player1Mark, player1Name, emptySpaces);
115+
116+
nextTurn = 2;
117+
} else {
118+
119+
System.out.print(player2Name + "’s turn: ");
120+
121+
playerNextMove(scanner, game, player2Mark, player2Name, emptySpaces);
122+
123+
nextTurn = 1;
124+
}
125+
126+
printGameBoard(game);
127+
128+
String result = checkIfGameOver(game);
129+
130+
if (result != null) {
131+
isGameOver = true;
132+
String winner = result.equals(player1Mark) ? player1Name : player2Name;
133+
System.out.println("RESULTS TIME:\n" + winner + " WON !!");
134+
}
135+
136+
}
137+
138+
}
139+
140+
}
141+
142+
private static void playerNextMove(Scanner scanner, String[] game, String playerMark, String playerName, List<Integer> emptySpaces) {
143+
int nextMove = scanner.nextInt();
144+
boolean available = isSpaceEmpty(emptySpaces, nextMove);
145+
while (!available) {
146+
System.out.println("Invalid input. Space already filled.");
147+
System.out.print(playerName + "’s turn: ");
148+
149+
nextMove = scanner.nextInt();
150+
available = isSpaceEmpty(emptySpaces, nextMove);
151+
}
152+
153+
updateGameBoard(game, nextMove, playerMark);
154+
}
155+
156+
private static boolean isSpaceEmpty(List<Integer> emptySpaces, int nextMove) {
157+
return emptySpaces.stream().anyMatch(x -> x == nextMove);
158+
}
159+
160+
private static String checkIfGameOver(String[] game) {
161+
162+
// check columns
163+
for (int i = 0; i < 3; i++) {
164+
if (game[i].equals(game[i + 3]) && game[i + 3].equals(game[i + 6]) && !game[i].equals("*")) {
165+
return game[i];
166+
}
167+
}
168+
169+
// check rows
170+
for (int i = 0; i < 7; i += 3) {
171+
if (game[i].equals(game[i + 1]) && game[i + 1].equals(game[i + 2]) && !game[i].equals("*")) {
172+
return game[i];
173+
}
174+
}
175+
176+
// check diagonal
177+
if (game[0].equals(game[4]) && game[4].equals(game[8]) && !game[0].equals("*")) {
178+
return game[0];
179+
}
180+
if (game[2].equals(game[4]) && game[4].equals(game[6]) && !game[2].equals("*")) {
181+
return game[2];
182+
}
183+
184+
return null;
185+
}
186+
187+
private static void updateGameBoard(String[] game, int nextMove, String playerMark) {
188+
game[nextMove - 1] = playerMark;
189+
}
190+
191+
private static List<Integer> findEmptySpaces(String[] game) {
192+
193+
List<Integer> emptySpaces = new ArrayList<>();
194+
195+
for (int i = 1; i <= game.length; i++) {
196+
if (game[i - 1].equals("*")) {
197+
emptySpaces.add(i);
198+
}
199+
}
200+
201+
return emptySpaces;
202+
}
203+
204+
private static void printGameBoard(String[] game) {
205+
System.out.println();
206+
for (int i = 1; i < 10; i++) {
207+
System.out.print(game[i - 1] + "\t");
208+
if (i % 3 == 0)
209+
System.out.println();
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)