Below my first code in c++. Its running fine. I feel I could improve a few things in my code. Please feel free to comment
#include <iostream>
#include <ctime>
#include "windows.h"
using namespace std;
class BattleShips {
public:
static const int rows = 2;
static const int elements = 2;
int maxships = 1;
int score = 0;
char matrix[rows][elements];
char matrixAttackedPlaces[rows][elements];
void Clear()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
matrix[i][j] = '0';
matrixAttackedPlaces[i][j] = '*';
}
}
}
void Show()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void ShowAttackedPlaces()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
cout << matrixAttackedPlaces[i][j] << " ";
}
cout << endl;
}
}
int NumberOfShips()
{
int c = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
if (matrix[i][j] == 1)
c++;
}
}
return c;
}
void SetShips()
{
int s = 0;
while (s < maxships)
{
int x = rand() % rows;
int y = rand() % elements;
if (matrix[x][y] != '1')
{
s++;
matrix[x][y] = '1';
}
}
}
bool Attack(int x, int y)
{
matrixAttackedPlaces[x][y] = 'X';
if (matrix[x][y] == '1')
{
matrix[x][y] = '$';
++score;
return true;
}
else matrix[x][y] = 'X';
return false;
}
};
void DrawSeparationLine(int x, char c) {
for (int i = 0; i < x; i++) {
cout << c << " ";
}
cout << endl;
}
void GameEndScreen()
{
system("CLS");
DrawSeparationLine(9, 'x');
DrawSeparationLine(9, '-');
DrawSeparationLine(9, 'x');
cout << "G A M E O V E R \n";
DrawSeparationLine(9, 'x');
DrawSeparationLine(9, '-');
DrawSeparationLine(9, 'x');
}
int main() {
srand(time(NULL));
BattleShips player1;
BattleShips player2;
player1.Clear();
player1.SetShips();
player2.Clear();
player2.SetShips();
int pos1, pos2;
char prompt;
int streak=0;
while (1) {
system("CLS");
player1.ShowAttackedPlaces();
DrawSeparationLine(player1.elements, '-');
player2.Show();
if (player1.score == player1.maxships || player2.score == player2.maxships)
{
if (player1.score == player1.maxships) {
GameEndScreen();
cout << "---- YOU WON ----";
}
else {
GameEndScreen();
cout << "---- YOU LOSE ---";
}
cout << endl;
DrawSeparationLine(9, 'x');
DrawSeparationLine(9, '-');
DrawSeparationLine(9, 'x');
break;
}
//player turn
cout << "\nScore: " << player1.score << " Streak: " << streak << endl;
cout << "Please input location:\n"; cin >> pos1 >> pos2;
if (player1.Attack(pos1, pos2))
{
streak++;
cout << "You got me! :)" << endl;
}
else
{
cout << "Sorry no ship at that position!" << endl;
streak = 0;
}
if (pos1 > player1.elements || pos2 > player1.rows)
cout << "You didn't have a chance to hit anyway. You chose a field outside the map.\n";
//CPU player turn
cout << "It's time for CPU player.";
Sleep(1000);
cout << " .";
Sleep(1000);
cout << " .";
cout<<endl;
while (1)
{
pos1 = rand() % player1.rows;
pos2 = rand() % player1.elements;
if (player2.matrix[pos1][pos2] != 'X')
{
player2.Attack(pos1,pos2);
break;
}
}
cout << "CPU player attacked your base at positions: " << pos1 << ", " << pos2 << "\n";
cout << "Do you want to surrender (y/n)? "; cin >> prompt;
if (prompt == 'y')
break;
}
return 0;
}