I have built a light version of a battleships program and would love for anyone to critique it. The game does not feature OOP, AI nor multiple boards. I aimed more for functionality than correct variable names and other related things just to get it working temporarily so I'm sorry if some things are named badly.
The player will only simply continue to guess on a singular board to see if their shots hit the target amount of ship until the game is completed.
I attempted to make the program adjust dynamically to what difficulty the player has set. For example, if the player were to choose easy, then the game board has 2x2 squares. Medium has 4x4 and Hard has 8x8.
// Battleships.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
namespace game
{
int rows = 0;
int columns = 0;
int maxNumbShips = 0;
const char WATER = '~';
const char HIT = 'X';
const char MISS = '/';
const char SHIP = 'S';
const char EMPTY = ' ';
}
void initBoard(std::vector<std::vector<char>>& playingBoard);
void displayBoard(std::vector<std::vector<char>> playingBoard);
void initShips(std::vector<std::vector<char>>& ships);
bool isGameOver(int shipsDestroyed);
void shoot(std::vector<std::vector<char>>& playingBoard, std::vector<std::vector<char>>& ships, int& shipsDestroyed, int& turns);
int enterRowPosition();
int enterColPosotion();
void selectDifficulty();
int main()
{
std::vector<std::vector<char>> playingBoard;
std::vector<std::vector<char>> ships;
int shipsDestroyed = 0; //How many ships have been destroyed by the player
int turns = 0;
selectDifficulty();
initBoard(playingBoard);
initShips(ships);
displayBoard(playingBoard);
while (!isGameOver(shipsDestroyed))
{
shoot(playingBoard, ships, shipsDestroyed, turns);
displayBoard(playingBoard);
}
std::cout << "You destroyed all of the ships in: " << turns << " turns.";
std::string barn;
std::cin >> barn;
return 0;
}
void initBoard(std::vector<std::vector<char>>& playingBoard)
{
std::vector<char> tempVector;
for (int i = 0; i < game::rows; i++)
{
tempVector.push_back(game::WATER);
}
for (int i = 0; i < game::columns; i++)
{
playingBoard.push_back(tempVector);
}
}
void initShips(std::vector<std::vector<char>>& ships)
{
std::vector<char> tempVector;
for (int i = 0; i < game::rows; i++)
{
tempVector.push_back(game::EMPTY);
}
for (int i = 0; i < game::columns; i++)
{
ships.push_back(tempVector);
}
srand(time(NULL));
bool emptyPosition = false;
for (int ship = 0; ship < game::maxNumbShips; ship++)
{
do
{
int x = rand() % game::rows;
int y = rand() % game::columns;
if (ships[x][y] == game::SHIP)
{
emptyPosition = false;
}
else
{
ships[x][y] = game::SHIP;
emptyPosition = true;
}
} while (!emptyPosition);
}
}
void displayBoard(std::vector<std::vector<char>> playingBoard)
{
std::cout << "\n";
for (int row = 0; row < game::rows; row++)
{
std::cout << "\t";
for (int col = 0; col < game::columns; col++)
{
std::cout << playingBoard[row][col];
std::cout << "\t";
}
std::cout << "\n";
}
}
bool isGameOver(int shipsDestroyed)
{
return shipsDestroyed >= game::maxNumbShips;
}
void shoot(std::vector<std::vector<char>>& playingBoard, std::vector<std::vector<char>>& ships, int& shipsDestroyed, int& turns)
{
int row = enterRowPosition();
int col = enterColPosotion();
if (ships[row][col] == game::SHIP)
{
playingBoard[row][col] = game::HIT;
shipsDestroyed++;
}
else
{
playingBoard[row][col] = game::MISS;
}
turns++;
}
int enterRowPosition()
{
int row = 0;
do
{
std::cout << "Enter row: ";
std::cin >> row;
}
while (row < 0 || row >= game::rows);
return row;
}
int enterColPosotion()
{
int col = 0;
do
{
std::cout << "Enter column: ";
std::cin >> col;
} while (col < 0 || col >= game::columns);
return col;
}
void selectDifficulty()
{
int choice = 0;
std::cout << "Select difficulty.\n";
std::cout << "1: Easy.\n2: Medium. \n3: Hard.\n";
std::cin >> choice;
switch (choice)
{
case 1:
game::rows = 2;
game::columns = 2;
game::maxNumbShips = 1;
break;
case 2:
game::rows = 4;
game::columns = 4;
game::maxNumbShips = 4;
break;
case 3:
game::rows = 8;
game::columns = 8;
game::maxNumbShips = 8;
break;
default:
std::cout << "Please enter valid information.";
break;
}
}