0% found this document useful (0 votes)
32 views2 pages

Battleship

This C++ program implements a simple Battleship game where the board is initialized with random ship placements, the user makes guesses to try and sink all ships, and it tracks the number of attempts. It includes functions to initialize the board, display it, check guesses, and contains the game loop and logic.

Uploaded by

sotaroedits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

Battleship

This C++ program implements a simple Battleship game where the board is initialized with random ship placements, the user makes guesses to try and sink all ships, and it tracks the number of attempts. It includes functions to initialize the board, display it, check guesses, and contains the game loop and logic.

Uploaded by

sotaroedits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

const int BOARD_SIZE = 5;

// Function to initialize the game board with ships


void initializeBoard(vector<vector<char>>& board) {
srand(time(nullptr));
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
board[i][j] = '.';
}
}
int shipCount = BOARD_SIZE;
while (shipCount > 0) {
int row = rand() % BOARD_SIZE;
int col = rand() % BOARD_SIZE;
if (board[row][col] == '.') {
board[row][col] = 'S'; // 'S' represents a ship
shipCount--;
}
}
}

// Function to display the game board


void displayBoard(const vector<vector<char>>& board) {
cout << " ";
for (int i = 0; i < BOARD_SIZE; ++i) {
cout << i << " ";
}
cout << endl;
for (int i = 0; i < BOARD_SIZE; ++i) {
cout << i << " ";
for (int j = 0; j < BOARD_SIZE; ++j) {
cout << board[i][j] << " ";
}
cout << endl;
}
}

// Function to check if the player's guess is a hit or a miss


bool checkGuess(const vector<vector<char>>& board, int row, int col) {
return board[row][col] == 'S';
}

int main() {
vector<vector<char>> board(BOARD_SIZE, vector<char>(BOARD_SIZE));

initializeBoard(board);

cout << "Welcome to Battleship!" << endl;


cout << "Try to sink all the ships in the least number of attempts." << endl;

int attempts = 0;
while (true) {
cout << "Enter your guess (row column): ";
int row, col;
cin >> row >> col;

if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
cout << "Invalid guess. Please enter row and column within the board
range." << endl;
continue;
}

if (checkGuess(board, row, col)) {


cout << "Hit!" << endl;
board[row][col] = 'X'; // 'X' represents a hit
} else {
cout << "Miss!" << endl;
board[row][col] = 'O'; // 'O' represents a miss
}

attempts++;

displayBoard(board);

bool allShipsSunk = true;


for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] == 'S') {
allShipsSunk = false;
break;
}
}
if (!allShipsSunk) break;
}

if (allShipsSunk) {
cout << "Congratulations! You sunk all the ships in " << attempts << "
attempts." << endl;
break;
}
}

return 0;
}

You might also like