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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
{#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
const int Rows = 15;
const int Columns = 15;
void printBoard(char board[][Columns]); // Creates the 15x15 board
void setShips(char board[][Columns], int size); // Randomly places ships
void checkBoard(char board[][Columns], bool &victory); // Checks the board to see if you have won
int main()
{
srand( static_cast<unsigned int >(time(NULL))); // starts the random generator
char board[Rows][Columns]; //sets the array of 15x15
int sizeShip, nShips; // creates the ships and their sizes
nShips = 5;
for (int i =0; i < Rows; i++)
{
for (int j = 0; j < Columns; j++)
{
board[i][j] = '~'; // creates the wave for the ships
}
}
cout << "Welcome to battleship!" << endl << endl; // welcomes the user
while(nShips < 6) // supposed to set sizes of ships
{
for(int i = 1; i <=nShips; i++)
{
if(i<2)
{
sizeShip = 2;
}
if(i>2 && i<4)
{
sizeShip = 3;
}
if(i>4 && i < 6)
{
sizeShip = 4;
}
//while (sizeShip > 1 || sizeShip <6);
//{
//setShips(board, sizeShip); // THIS IS THE PROBLEM sets ships to the board
//}
}
}
int Attacks = 60, iGuess, jGuess; // sets number of guesses to 60
bool victory = false; // bool is false because you haven't played yet
if(nShips != 1) // introducing the game
{
cout << "Here is your game board. There are " << nShips << " enemy battleships hidden on it." << endl;
}
else
{
cout << "Here is your game board. There are " << nShips << " enemy battleships hidden on it." << endl;
}
cout << "You have 60 trys to find all of the ships. Input a row and column coordinate (x y) to attack." << endl;
cout << "Make your first move. Good luck." << endl;
for (int n =1; n <= Attacks && !victory; n++) // subtracting attacks for each guess
{
printBoard(board);
cout << "Attack number" << n << ", x and y: ";
cin >> iGuess >> jGuess;
if(board[iGuess - 1][jGuess - 1] == 'S')
{
cout << "You got a hit! You hit at the coordinates: " << iGuess << " " << jGuess << endl;
board[iGuess - 1][jGuess - 1] = '#';
}
else if (board[iGuess - 1][jGuess - 1] == '~')
{
cout << "I'm sorry. You missed!" << endl;
board[iGuess - 1][jGuess - 1] = 'o';
}
victory = true;
checkBoard(board, victory); // checks if victory is true
}
cout << "Here is the final board: " << endl; // printing the final board
printBoard(board);
cout << "A '~' is open sea, 'o' is a miss, '#' is a hit, and 'S' is part of a ship you did not sink." << endl;
if (victory) // if won or lost
{
cout << "Congratulations, you sunk all of the enemy battleships!" << endl;
}
else
{
cout << "You didn't win. Better luck next time!" << endl;
}
system ("PAUSE");
return 0;
}
void printBoard(char board[][Columns]) // print board function
{
for(int i = 0; i < Rows; i++)
{
for(int j = 0; j < Columns; j++)
{
cout << board[i][j];
}
cout << endl;
}
return;
}
void SetShips(char board[][Columns], int size) // randomly set ships
{
int iStart, jStart, ort;
Randomize:
do
{
iStart = rand()%Rows;
jStart = rand()%Columns;
ort = rand()%2;
}
while (board[iStart][jStart] == 'S');
{
if(ort == 0)
{
while(jStart + size >= Columns) // checks if there is a ship under it
{
jStart = rand()%Columns;
}
for(int j = jStart; j < jStart + size; j++) // checks if it is off the board
{
if(board[iStart][j] == 'S')
{
goto Randomize;
}
}
for (int j = jStart; j < jStart + size; j++)
{
board[iStart][j] = 'S';
}
}
if (ort == 1)
{
while (iStart + size >= Rows) // checks if there is a ship under it
{
iStart = rand()%Rows;
}
for(int i = iStart; i < iStart + size; i++) // checks if ship is off the board
{
if (board[i][jStart] == 'S')
{
goto Randomize;
}
}
for (int i = iStart; i < iStart + size; i++)
{
board[i][jStart] = 'S'; // places ship if no obstacles
}
}
}
return;
}
void checkBoard(char board[][Columns], bool &victory) // checks board to see if you won
{
for(int i = 0; i < Rows; i++)
{
for(int j = 0; j < Columns; j++)
{
if (board[i][j] == 'S') // S means there is a ship, if there is a S you did not win
{
victory = false;
}
}
}
return;
}
}
|