Description:
This project is a classic Battleship game written in C++, where two players, Master and Slave, interact with each other through auxiliary files.
-
Master defines the field size, the number of ships, and starts the game.
-
Slave automatically reads all settings from Master.
- Project Features
- Project Structure
- Build
- Master Commands
- Slave Commands
- Turn Order
- Example Game Flow
- Auxiliary Files
- The game is designed to run in two independent terminals. Each executable file (
master_main.exeandslave_main.exe) is launched separately. - Master sets the game configuration and sees only their own ships.
- Slave receives the configuration automatically and also sees only their own ships.
- The opponent’s ships and their coordinates are not displayed in order to prevent cheating.
- Turn logic is organized through the
queue.txtfile:1means it is Master’s turn2means it is Slave’s turn
Battleship/
├── bin/
│ ├── master_main.cpp
│ └── slave_main.cpp
├── lib/
│ ├── Master/
│ ├── Slave/
│ ├── Settings/
│ ├── ReadWriteQueue/
│ ├── Ship/
│ └── RandomShips/
│ ├── Utils/
│ ├── Commands/
├── CMakeLists.txt
└── README.md-
bincontains the executable entry points:master_main.cppslave_main.cpp
-
libcontains the game logic, including theMaster,Slave,Settings,RandomShips, and other classes. -
CMakeLists.txtis the build script used by CMake.
-
Make sure that CMake version 3.10+ and a C++ compiler, such as
g++orclang++, are installed. -
From the repository root directory,
Battleship/, run:mkdir build cd build cmake .. cmake --build . --config Release
-
After the build is complete, the following files will appear in the
build/Releasedirectory:
master_main.exeslave_main.exe
set mode masterset width <N>set height <N>set count <type> <count>
For example,set count 4 1adds one ship of length 4.start— starts the game, places Master’s ships, and waits until Slave places their ships.shot X Y— makes a shot. If the shot misses, the turn passes to Slave. If the shot hits, Master keeps the turn.stop— stops the current game.exit— exits the application completely.
Master: Enter commands (e.g. set mode master, set width 10, set height 10, set count 4 1, start):
> set mode master
> set width 10
> set height 10
> set count 1 4 # Four ships of length 1
> set count 2 2 # Two ships of length 2
> startshot X Y— makes a shot if it is Slave’s turn.exit— exits the game.- All other settings, such as width, height, and so on, are not set by Slave. They are received automatically from Master.
-
The turn order is stored in the
queue.txtfile:1= Master,2= Slave. -
If a player misses, the turn passes to the opponent. If a player hits or sinks a ship, they keep the turn.
-
When the turn changes, the new player’s terminal displays a message like:
[Slave] Now it's your turn.or
[Master] Now it's your turn.
-
Slave is launched first and displays:
Slave: Waiting for game to start...
-
Master is launched second and enters the commands:
> set mode master > set width 10 > set height 10 > set count 1 4 > set count 2 2 > start
-
Master places their own ships in
master_ships.txt, then waits until Slave places their ships inslave_ships.txt. -
When ship placement is complete, the shooting phase begins.
Master enters
shot X Yand immediately sees a message such as:Master: You hit a Slave ship at (X, Y).or:
Master: You missed.If Master misses, the turn passes to Slave, and the Slave terminal displays:
[Slave] Now it's your turn. -
Slave makes a shot using the
shot X Ycommand and receives a response, such asYou hit,You missed, and so on. -
Once all ships of one player have been destroyed, the game displays the winner.
Example:
Master: All Slave ships have been sunk. Master wins! -
If needed, the player can enter
stopto stop the current game while staying in the application, orexitto exit completely.
The following files will be created in the ../game_data/ directory relative to the executable .exe files:
- settings.txt — stores settings such as width, height, number of ships, and so on.
- master_ships.txt / slave_ships.txt — store the positions of Master’s and Slave’s ships.
- queue.txt — stores the current player, either
1or2. - shots.txt — stores shot logs.