Skip to content

Commit 27ab34a

Browse files
Merge pull request #270 from mtwn105/tic-tac-toe
Added program for tic tac toe game
2 parents bdf7b67 + 44e46dc commit 27ab34a

1 file changed

Lines changed: 254 additions & 0 deletions

File tree

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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+
* AUTHOR: Amit Wani (@mtwn105)
9+
*
10+
*/
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.Random;
15+
import java.util.Scanner;
16+
17+
public class TicTacToe {
18+
19+
public static void main(String[] args) {
20+
21+
System.out.print("!!Welcome to Tic-Tac-Toe!!\n" +
22+
"Decide your opponent:\n" +
23+
"1) Play with computer\n" +
24+
"2) Play with a friend\n" +
25+
"Enter your choice –");
26+
27+
Scanner scanner = new Scanner(System.in);
28+
int choice = scanner.nextInt();
29+
30+
scanner.nextLine();
31+
32+
// Game Board
33+
String[] game = new String[]{"*", "*", "*", "*", "*", "*", "*", "*", "*"};
34+
35+
// Player Marks
36+
String player1Mark = "X";
37+
String player2Mark = "O";
38+
39+
// Flag for checking if game is over
40+
boolean isGameOver = false;
41+
int nextTurn = 0;
42+
43+
// If Playing vs Bot
44+
if (choice == 1) {
45+
46+
// Get Player Name
47+
System.out.print("\nEnter your name –");
48+
String playerName = scanner.nextLine();
49+
50+
String computerName = "T-Bot";
51+
52+
System.out.println("Hey " + playerName + ", I am Computer. My name is ‘" + computerName + "’.");
53+
54+
System.out.print("\nWho’ll go first?\n" +
55+
"1) T-Bot\n" +
56+
"2) Me\n" +
57+
"Enter your choice –");
58+
59+
nextTurn = scanner.nextInt();
60+
61+
// Printing Initial Game Board
62+
printGameBoard(game);
63+
64+
// Playing until game is over
65+
while (!isGameOver) {
66+
67+
// Get Empty spaces remaining on the game board
68+
List<Integer> emptySpaces = findEmptySpaces(game);
69+
70+
// If no empty spaces remaining & no winner is decided then its a draw
71+
if (emptySpaces.size() == 0) {
72+
// DRAW
73+
System.out.println("RESULTS TIME: GAME IS A DRAW !!");
74+
break;
75+
}
76+
77+
if (nextTurn == 1) {
78+
79+
System.out.print("T-Bot’s turn: ");
80+
81+
// For Bot, Picking random empty space
82+
Random random = new Random();
83+
int nextMove = emptySpaces.get(random.nextInt(emptySpaces.size()));
84+
85+
System.out.print(nextMove);
86+
87+
// Update the game board with bot's move
88+
updateGameBoard(game, nextMove, player1Mark);
89+
90+
nextTurn = 2;
91+
} else {
92+
93+
System.out.print(playerName + "’s turn: ");
94+
95+
// Get Player's Next Move
96+
playerNextMove(scanner, game, player2Mark, playerName, emptySpaces);
97+
98+
nextTurn = 1;
99+
}
100+
101+
// Print board after every move
102+
printGameBoard(game);
103+
104+
// Check Result
105+
isGameOver = checkResult(game, player1Mark, computerName, playerName);
106+
107+
}
108+
109+
} else if (choice == 2) {
110+
111+
// Get Player Details
112+
System.out.print("\nEnter your name of player 1 –");
113+
String player1Name = scanner.nextLine();
114+
System.out.print("\nEnter your name of player 2 –");
115+
String player2Name = scanner.nextLine();
116+
117+
System.out.print("\nWho’ll go first?\n" +
118+
"1) " + player1Name + "\n" +
119+
"2) " + player2Name + "\n" +
120+
"Enter your choice –");
121+
122+
nextTurn = scanner.nextInt();
123+
124+
// Printing Initial Game Board
125+
printGameBoard(game);
126+
127+
// Playing until game is over
128+
while (!isGameOver) {
129+
130+
// Get Empty spaces remaining on the game board
131+
List<Integer> emptySpaces = findEmptySpaces(game);
132+
133+
// If no empty spaces remaining & no winner is decided then its a draw
134+
if (emptySpaces.size() == 0) {
135+
// DRAW
136+
System.out.println("RESULTS TIME: GAME IS A DRAW !!");
137+
break;
138+
}
139+
140+
if (nextTurn == 1) {
141+
142+
System.out.print(player1Name + "’s turn: ");
143+
144+
// Get Player's Next Move
145+
playerNextMove(scanner, game, player1Mark, player1Name, emptySpaces);
146+
147+
nextTurn = 2;
148+
} else {
149+
150+
System.out.print(player2Name + "’s turn: ");
151+
152+
// Get Player's Next Move
153+
playerNextMove(scanner, game, player2Mark, player2Name, emptySpaces);
154+
155+
nextTurn = 1;
156+
}
157+
158+
// Print board after every move
159+
printGameBoard(game);
160+
161+
// Check Result
162+
isGameOver = checkResult(game, player1Mark, player1Name, player2Name);
163+
164+
}
165+
166+
}
167+
168+
}
169+
170+
private static boolean checkResult(String[] game, String playerMark, String player1Name, String player2Name) {
171+
// Check if game is over after every move
172+
String result = checkIfGameOver(game);
173+
174+
boolean isGameOver = false;
175+
176+
if (result != null) {
177+
isGameOver = true;
178+
String winner = result.equals(playerMark) ? player1Name : player2Name;
179+
System.out.println("RESULTS TIME:\n" + winner + " WON !!");
180+
}
181+
return isGameOver;
182+
}
183+
184+
private static void playerNextMove(Scanner scanner, String[] game, String playerMark, String playerName, List<Integer> emptySpaces) {
185+
int nextMove = scanner.nextInt();
186+
boolean available = isSpaceEmpty(emptySpaces, nextMove);
187+
while (!available) {
188+
System.out.println("Invalid input. Space already filled.");
189+
System.out.print(playerName + "’s turn: ");
190+
191+
nextMove = scanner.nextInt();
192+
available = isSpaceEmpty(emptySpaces, nextMove);
193+
}
194+
195+
updateGameBoard(game, nextMove, playerMark);
196+
}
197+
198+
private static boolean isSpaceEmpty(List<Integer> emptySpaces, int nextMove) {
199+
return emptySpaces.stream().anyMatch(x -> x == nextMove);
200+
}
201+
202+
private static String checkIfGameOver(String[] game) {
203+
204+
// check columns
205+
for (int i = 0; i < 3; i++) {
206+
if (game[i].equals(game[i + 3]) && game[i + 3].equals(game[i + 6]) && !game[i].equals("*")) {
207+
return game[i];
208+
}
209+
}
210+
211+
// check rows
212+
for (int i = 0; i < 7; i += 3) {
213+
if (game[i].equals(game[i + 1]) && game[i + 1].equals(game[i + 2]) && !game[i].equals("*")) {
214+
return game[i];
215+
}
216+
}
217+
218+
// check diagonal
219+
if (game[0].equals(game[4]) && game[4].equals(game[8]) && !game[0].equals("*")) {
220+
return game[0];
221+
}
222+
if (game[2].equals(game[4]) && game[4].equals(game[6]) && !game[2].equals("*")) {
223+
return game[2];
224+
}
225+
226+
return null;
227+
}
228+
229+
private static void updateGameBoard(String[] game, int nextMove, String playerMark) {
230+
game[nextMove - 1] = playerMark;
231+
}
232+
233+
private static List<Integer> findEmptySpaces(String[] game) {
234+
235+
List<Integer> emptySpaces = new ArrayList<>();
236+
237+
for (int i = 1; i <= game.length; i++) {
238+
if (game[i - 1].equals("*")) {
239+
emptySpaces.add(i);
240+
}
241+
}
242+
243+
return emptySpaces;
244+
}
245+
246+
private static void printGameBoard(String[] game) {
247+
System.out.println();
248+
for (int i = 1; i < 10; i++) {
249+
System.out.print(game[i - 1] + "\t");
250+
if (i % 3 == 0)
251+
System.out.println();
252+
}
253+
}
254+
}

0 commit comments

Comments
 (0)