1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <string>
#include <iostream>
using namespace std;
constexpr int MAX_ROWS{ 15 };
constexpr int MAX_COLS{ 15 };
// Globals
//names of ships
const string names[] = { "Aircraft Carrier", "Battleship", "Submarine", "Cruiser", "Destroyer" };
//representation of thoe ships by letter
const char shipLetter[] = { 'A', 'B', 'S', 'C', 'D' };
//ships lengths
const int lengths[] = { 5, 4, 3, 3, 2 };
void placeEnemyShips(char shipBoard[MAX_COLS][MAX_ROWS]);
void displayPlayerBoard(char shipBoard[MAX_COLS][MAX_ROWS]);
void clear();
int charToNum(char);
char numToChar(int);
void pause();
int getColNum(int shipNumber)
{
char last_col = numToChar(MAX_COLS);
char xChar;
int x;
cout << "Column where the front of the ship should be? (A-" << last_col << "): ";
cin >> xChar;
//check if user has not entered a valid character placement for the gameboard
x = charToNum(xChar);
while (x < 0 || x> MAX_COLS - 1)
{ //messages if user did not enter the proper board placement
cout << "Please enter a valid starting column position.\n";
cout << "You are placing the " << names[shipNumber] << ".\n";
cout << "Its length is " << lengths[shipNumber] << ".\n";
cout << "Column Where should the front of the ship be? (A-" << last_col << "): ";
//prompt user to enter column character again
cin >> xChar;
x = charToNum(xChar);
}
return x;
}
int getRowNum(int shipNumber)
{
int y;
cout << "\n\nPlease enter the starting row. (1-" << MAX_ROWS << "): ";
cin >> y;
//check if the value of y is valid
while (y < 1 || y > MAX_ROWS)
{ //messages if user did not enter the proper board placement
cout << "Please enter a valid starting row position.\n";
cout << "You are placing the " << names[shipNumber] << ".\n";
cout << "Its length is " << lengths[shipNumber] << ".\n";
cout << "Please enter the starting row (1-" << MAX_ROWS << "): ";
cin >> y;
}
return y;
}
int getDir(char shipBoard[MAX_COLS][MAX_ROWS])
{
int dir;
//choose ships direction - 1 for vertical, 2 if the ship is to lie horizontal
cout << "What direction would you like the ship to face?\n";
cout << "1. Vertical\n";
cout << "2. Horizontal\n";
//user input for ship direction
cin >> dir;
//check if user input a valid direction
while (dir != 1 && dir != 2)
{
clear();
displayPlayerBoard(shipBoard);
//tell user selected direction was not valid
cout << "Please enter a valid direction:\n\n";
cout << "What direction would you like the ship to face?\n";
cout << "1.) Vertical\n";
cout << "2.) Horizontal\n";
cin >> dir;
}
return dir;
}
// Return true if row and col refer to a cell on the board
// Assumes row and col have already been biased to zero.
bool inbounds(int row, int col)
{
if (row < 0 || row >= MAX_ROWS)
return false;
if (col < 0 || col >= MAX_COLS)
return false;
return true;
}
// Return true if cell referred to by x and y is out of bounds.
// or already contains a ship.
bool occupied(char shipBoard[MAX_COLS][MAX_ROWS], int x, int y)
{
if (!inbounds(x, y))
return true; // Treat out of bounds as occupied
if (isalpha(shipBoard[x][y]))
return true; // Already a character there
return false; // Not occupied
}
// Return true if any cell of the ship is out of bounds
// or already contains a ship.
bool isShipsOverlap(char shipBoard[MAX_COLS][MAX_ROWS], int startX, int startY, int dir, int length)
{
if (dir == 1)
{ // vertical - Iterate through the rows
for (int x = startX; x < startX + length; x++)
{ // test if occupied or out of bounds
if (occupied(shipBoard, x, startY))
return true; // Occupied or out of bounds
}
}
else
{ // horizontal - Iterate through the columns
for (int y = startY; y < startY + length; y++)
{ // test if occupied or out of bounds
if (occupied(shipBoard, startX, y))
return true; // Occupied or out of bounds
}
}
return false; // Not occupied
}
void placeOneShip(char shipBoard[MAX_COLS][MAX_ROWS], int startX, int startY, int dir, int length, char letter)
{
if (dir == 0)
{ // Horizontal - Iterate through the columns
for (int x = startX; x < startX + length; x++)
{ //ship will be positioned with letters marking the ship's orientation
shipBoard[x][startY] = letter;
}
}
else
{ // Vertical - Iterate through the rows
for (int y = startY; y < startY + length; y++)
{ //ship will be positioned with letters marking the ship's orientation
shipBoard[startX][y] = letter;
}
}
}
|