Javatpoint Logo
Javatpoint Logo

Dots and Boxes Game Java Source code

Dots and Boxes, also known as the "Dot Game" or "Squares," is a classic pen-and-paper game that has been enjoyed by people of all ages for decades. In this article, we'll walk you through the process of creating a Dots and Boxes game in Java from scratch. By the end of this section, we have a complete Java source code for the game.

Prerequisites

  1. Before we dive into the code, make sure you have the following prerequisites:
  2. Java Development Kit (JDK) installed on your computer.
  3. A code editor (e.g., Eclipse, IntelliJ IDEA, or Visual Studio Code).

Understanding the Rules

In Dots and Boxes, two players take turns connecting dots on a grid of dots to form squares. When a player completes a square, they score a point and get another turn. The game ends when all possible lines are drawn, and the player with the most squares wins.

DotsAndBoxes.java

Output:

  |   |  
---------
  |   |  
---------
  |   |  
Player X's turn
Enter row (1-3): 1
Enter column (1-3): 1
X |   |  
---------
  |   |  
---------
  |   |  
Player O's turn
Enter row (1-3): 2
Enter column (1-3): 1
X |   |  
---------
O |   |  
---------
  |   |  
Player X's turn
Enter row (1-3): 1
Enter column (1-3): 2
X | X |  
---------
O |   |  
---------
  |   |  
Player O's turn
Enter row (1-3): 2
Enter column (1-3): 2
X | X |  
---------
O | O |  
---------
  |   |  
Player X's turn
Enter row (1-3): 1
Enter column (1-3): 3
X | X | X
---------
O | O |  
---------
  |   |  
Game Over!
Player X wins!

Next TopicDRY Principle Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA