🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this short tutorial, we will write a Java program to develop the Tic-Tac-Toe game. The Tic-Tac-Toe is a very common game that is fairly easy to play. The rules of the game are simple and well-known.
Learn Java at https://www.javaguides.net/p/java-tutorial-learn-java-programming.html
In this tutorial, we are going to develop the console-based Tic Tac Toe game. In this game, the user is prompted to choose one of the nine squares in the grid. The grid chosen by the player is then shown by the corresponding sign of the player. The first player marks A and second player marks B.
Tic-Tac-Toe Game in Java
package net.javaguides.springboot;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Tic-Tac-Toe game in Java
* @author https://www.javaguides.net/
*
*/
public class TicTacToe {
private static String[] board = new String[9];
private static String turn = "A";
public static void main(String[] args) {
String winner = null;
populateEmptyBoard();
System.out.println("Welcome to 2 Player Tic Tac Toe.");
System.out.println("--------------------------------");
printBoard();
System.out.println("A's will play first. Enter a slot number to place A in:");
try (Scanner in = new Scanner(System.in)) {
while (winner == null) {
int numInput;
try {
numInput = in .nextInt();
if (!(numInput > 0 && numInput <= 9)) {
System.out.println("Invalid input; re-enter slot number:");
continue;
}
} catch (InputMismatchException e) {
System.out.println("Invalid input; re-enter slot number:");
continue;
}
if (board[numInput - 1].equals(String.valueOf(numInput))) {
board[numInput - 1] = turn;
if (turn.equals("A")) {
turn = "B";
} else {
turn = "A";
}
printBoard();
winner = checkWinner();
} else {
System.out.println("Slot already taken; re-enter slot number:");
continue;
}
}
if (winner.equalsIgnoreCase("draw")) {
System.out.println("It's a draw! Thanks for playing.");
} else {
System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");
}
}
}
static String checkWinner() {
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
if (line.equals("AAA")) {
return "A";
} else if (line.equals("BBB")) {
return "B";
}
}
for (int a = 0; a < 9; a++) {
if (Arrays.asList(board).contains(String.valueOf(a + 1))) {
break;
} else if (a == 8)
return "draw";
}
System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:");
return null;
}
private static void printBoard() {
String createBoard = "/---|---|---\\ \n" + "| " + board[0] + " | " + board[1] + " | " + board[2] + " | \n" +
"|-----------| \n" + "| " + board[3] + " | " + board[4] + " | " + board[5] + " | \n" +
"|-----------| \n" + "| " + board[6] + " | " + board[7] + " | " + board[8] + " | \n" +
"/---|---|---\\ \n";
System.out.println(createBoard);
}
private static void populateEmptyBoard() {
for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a + 1);
}
}
}
Output:
Welcome to 2 Player Tic Tac Toe.
--------------------------------
/---|---|---\
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
/---|---|---\
A's will play first. Enter a slot number to place A in:
1
/---|---|---\
| A | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
/---|---|---\
B's turn; enter a slot number to place B in:
2
/---|---|---\
| A | B | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
/---|---|---\
A's turn; enter a slot number to place A in:
4
/---|---|---\
| A | B | 3 |
|-----------|
| A | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
/---|---|---\
B's turn; enter a slot number to place B in:
5
/---|---|---\
| A | B | 3 |
|-----------|
| A | B | 6 |
|-----------|
| 7 | 8 | 9 |
/---|---|---\
A's turn; enter a slot number to place A in:
7
/---|---|---\
| A | B | 3 |
|-----------|
| A | B | 6 |
|-----------|
| A | 8 | 9 |
/---|---|---\
Congratulations! A's have won! Thanks for playing.
Java programs
- Java Program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains only Digits
- How to Check if the String Contains only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Check if Input String is Palindrome
- Java Program to Find all Permutations of String
- How to Remove or Trim All White Spaces from a String in Java
- How to Remove Leading and Trailing White Space From a String in Java
- Java Program to Count Duplicate Characters in a String
- Remove Character from String in Java (Java 8)
- Java Program to Count Vowels and Consonants in a String (Java 8)
- 4 Ways to Find First Non-Repeated Character in String in Java
- Java Program to Remove Duplicate Elements in an Array
- Java Program to Find Largest Element in an Array
- Java Program to Reverse an Array Without Using Another Array
- Java Program to Check the Equality of Two Arrays
- Java Program to Check Armstrong Number
- Java program to check prime number
- Java Program to Swap Two Numbers
- Java Program to Find Factorial of a Number
- Java Program to Reverse a Number
- Java Program to Swap Two Strings Without Using Third Variable
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment