#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <conio.h>
#include <sstream>
using namespace std;
//Below is an enumerated data type so that I can use the labels 'VERTICAL', and 'HORIZONTAL' to designate the
//direction of a ship.
enum ShipDirection {VERTICAL, HORIZONTAL};
//This is the ship class, it is a blue print for a ship.
//The game of battleship utilizes 5 ships of various sizes.
class Ship
{
private:
int numOfHits;
ShipDirection shipDir;
int startRow;
int startCol;
string shipType;
public:
Ship()
{
shipType = "";
shipDir = VERTICAL;
startRow = 0;
startCol = 0;
numOfHits = 0;
}
Ship(int nHits, string name)
{
shipType = name;
shipDir = VERTICAL;
startRow = 0;
startCol = 0;
numOfHits = nHits;
}
//Created sets and gets for all of the data members in the private: section of this class.
void setStartRow(int sR)
{
startRow = sR;
}
int getStartRow()
{
return startRow;
}
void setStartCol(int sC)
{
startCol = sC;
}
int getStartCol()
{
return startCol;
}
int getNumberOfHits()
{
return numOfHits;
}
ShipDirection getShipDir()
{
return shipDir;
}
void setShipDir(ShipDirection sD)
{
shipDir = sD;
}
string getNameOfShip()
{
return shipType;
}
void setNameOfShip(string nOS)
{
shipType = nOS;
}
};
//This is the board class. This class represents the board. The board class contains a 2 dimensional array.
//That array is filled with zeros initially. If there is a ship at a certain coordinate at that array, it will contain a
//'1', if there is a space of a ship that has been hit at a certain coordinate of the board array, it will contain a '2'.
class Board
{
private:
int board[10][10];
public:
Board()
{
for (int row = 0; row < 10; row++)
{
for (int col = 0; col<10; col++)
{
board[row][col] = 0;
}
}
}
//This function places a ship on the board
void placeShip(Ship ship)
{
//Create a random number between 0 and 1 to represent the direction of the ship
int sD = rand()%2;
int sR = rand()%10;
int sC = rand()%10;
if (sD==0)
{
ship.setShipDir(VERTICAL);
}
else
{
ship.setShipDir(HORIZONTAL);
}
while(!isClearSpace(sR, sC, ship.getNumberOfHits(), ship.getShipDir()))
{
sR = rand()%10;
sC = rand()%10;
}
ship.setStartRow(sR);
ship.setStartCol(sC);
setShip(sR, sC,ship.getNumberOfHits(),ship.getShipDir());
}
bool isClearSpace(int startRow, int startCol, int size, ShipDirection shipDir)
{
if(shipDir == VERTICAL)
{
if(startRow + size > 9)
{
return false;
}
for(int row = startRow; row < startRow+size; row++)
{
if(board[row][startCol] !=0)
{
return false;
}
}
}
else if(shipDir==HORIZONTAL)
{
if(startCol + size > 9)
{
return false;
}
for(int col = startCol; col < startCol+size; col++)
{
if(board[startRow][col] !=0)
{
return false;
}
}
}
return true;
}
void setShip(int startRow, int startCol, int size, ShipDirection shipDir)
{
if(shipDir == VERTICAL)
{
for(int row = startRow; row < startRow+size; row++)
{
board[row][startCol] = 1;
}
}
else if(shipDir==HORIZONTAL)
{
for(int col = startCol; col < startCol+size; col++)
{
board[startRow][col] = 1;
}
}
}
//Print board
void printBoard()
{
cout << " 0 1 2 3 4 5 6 7 8 9" << endl;
char rowLabel = 'A';
for(int row = 0; row < 10; row++)
{
cout << rowLabel << " ";
rowLabel++;
for(int col = 0; col < 10; col++)
{
cout << board[row][col] << " ";
}
cout << endl;
}
}
//This takes in a row and a col
//it then checks to see if the guess is a hit or not
//if there is not a zero at that place it has to be
//a 1 or a 2, I will consider that to be a hit
//my version of the game does not take into account
//whether the user hits the boat at the same place
//more then once
bool isHit(int row, int col)
{
if(board[row][col] != '1')
{
board[row][col] = '2';
return true;
}
if(board[row][col] = '0')
{
return false;
}
}
//Checks the entire board
//if there are any 1's there is boat still on the board that is not yet hit
//so return false if it finds a 1, otherwise the game is over
bool isGameWon()
{
for(int row = 0; row < 10; row++)
for(int col = 0; col < 10; col++)
if(board[row][col] = 1)
{
return false; //game is not yet won
}
return true;
}
//Checks to see if this particular ship is sunk by checking to see
//if the board at this ship's position is all 2s.
bool isShipSunk(Ship ship, int startRow, int startCol, int size, ShipDirection shipDir)
{
if(shipDir == VERTICAL)
{
if(startRow + size > 9)
{
return false;
}
for(int row = startRow; row < startRow+size; row++)
{
if(board[row][startCol] !='2')
{
return false;
}
}
}
else if(shipDir==HORIZONTAL)
{
if(startCol + size > 9)
{
return false;
}
for(int col = startCol; col < startCol+size; col++)
{
if(board[startRow][col] !='2')
{
return false;
}
}
}
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
//Seed the random number generator with the current time
srand(time(0));
char guessRow;
int guessCol;
char playAgain();
char keepPlaying;
bool isGameWon;
bool isHit;
//While the player intends to keep playing
do
{
//Create each of the five ships
Ship carrier = Ship(5, "Carrier");
Ship battleship = Ship(4, "Battleship");
Ship submarine = Ship(3, "Submarine");
Ship destroyer = Ship(3, "Destroyer");
Ship patrolboat = Ship(2, "Patrol boat");
//Create the board
Board board = Board();
//place ships on board
board.placeShip(carrier);
board.placeShip(battleship);
board.placeShip(submarine);
board.placeShip(destroyer);
board.placeShip(patrolboat);
//print the board with the ships on it temporarily for testing, when the game is finished I will take this out.
board.printBoard();
do
{
//Asks User for Coordinates of their choice.
cout << "\n\nPlease enter your coordinates: \n";
cout << "Enter row: ";
cin >> guessRow;
guessRow = toupper(guessRow);
cout << "Enter column: ";
cin >> guessCol;
cout << endl;
if (isHit != false)//"Run-Time Check Failure #3 - The variable 'isHit' is being used without being initialized."
{
cout << "Its a hit \n";
}
//check each ship to see if it was sunk.
//Need help here to to work out hits with sinks
/*if(board.isShipSunk(carrier))
{ cout << "You sunk my Carrier!"; }
if(board.isShipSunk(battleship))
{ cout << "You sunk my Battleship!"; }
if(board.isShipSunk(submarine))
{ cout << "You sunk my Submarine!"; }
if(board.isShipSunk(destroyer))
{ cout << "You sunk my Destroyer!"; }
if(board.isShipSunk(patrolboat))
{ cout << "You sunk my Partrol boat!"; }*/
//do that on all 5 ships
else
{
cout << "It was a miss! \n";
}
}
while (isGameWon != true );//"Run-Time Check Failure #3 - The variable 'isGameWon' is being used without being initialized."
//ask the player if he or she intends to keep playing
keepPlaying = playAgain();
if ( keepPlaying == 'N')
{
cout << "Goodbye \n";
system("pause");
return 0;
}
}
while (keepPlaying == 'Y');
}
//play again
char playAgain()
{
char keepPlaying;
cout << "Would you like to play again? (y or n) ";
do
{
cin >> keepPlaying;
keepPlaying = toupper(keepPlaying);
if (keepPlaying!='Y' && keepPlaying != 'N')
cout << "Enter only a 'Y' or an 'N', please!" << endl << endl;
} while ( keepPlaying !='Y' && keepPlaying != 'N');
return keepPlaying;
}
Can anyone help me to solve this problem? Thanks in advance Assignment 6 Strings Write a program which makes Caesar encryption. Caesar encryption is based on circular shifting of the letters with a given amount. Your program will encrypt the input and it will shift the letters by the given amount (if it passes z, it will continue from a), it will not change spaces (" "). You can assume that no other characters will be entered.
I am need help with this math problem, is there anyone can help me to solve it, with step pls so that I am able to learn how to do it by myself next time r2 secdx r2 secdx
Can anyone help me solve this problem plz. Thanks 1- first choices are ( amount of improvement in his game, golfer, type of equipment used) 2- second choices are (golfer, type of equipment used, amount of improvement in his game) Identify the explanatory variable and the response variable A golfer wants to determine if the type of equipment used every year can be used to predict the amount of improvement in his game The explanatory variable is the The response...
Could anyone help me solve this problem in C++? Write a function that sorts a linked list. (Use any sort algorithm, make sure to state which one your going to use.)
Hello, Can anyone here help me out understand how to solve this problem (Solution has been provided) Thank you: Question: Answer: О.it F (A,B,C,D): Zm (1,4, 5, 7, 9, 12, 13 mplemen using a 4 t-1 MuX, MSD as selectorts. use à bok to vepresent Mux. 5 point) CD- 4 to1 I,
can anyone can help me solve this question? It's about magentic flux, thanks! R. Vierminal. =て mossm
Anyone can help me to solve my discrete math Let X be a set and suppose there is an injection f : Z → X. Prove by contradiction that X is infinite
Can anyone please help me solve this and include the steps on how to do it? Thanks! 7. An electron with velocity -4.10m/s enters a region with the uniform electric field E 12,0002 N/C. Calculate the deflection of the electron after it has traveled 2 cm in the î direction
Can anyone help me with this Linear Algebra question? dy =1 +1 dt X-- Solve the system: 5.) dx dy 2y=e + dt d
I have no idea how to solve these types of problems. Can anyone help me out? If n=20, μ=15, =20 and S=10, to do a one-sided hypothesis test with significance level, α=1%, you will read a t-value table of: