I'm creating a one player battleship game that uses a dynamic array and linked list. The game should allow the user to specify the board size and number of ships that would be place. The board generates random ship placement and allows user to enter coordinates to see if they hit or misses the ship. I created a two linked list functions that creates a node and adds a node in a sorted position. I'm having trouble modifying the logic in CreateNode to create a random ship placement.
My guess on how to solve:
declare variable to hold ship position
allocate space for new node
implement a for loop to search through BoardArray
Any suggestions on how to do this?
const int MAX_ROWS = 10;
const int MAX_COLS = 10;
const int MAX_SHIPSIZE = 5;
const int MAX_SHIPS = 5;
struct NodeType
{
int component;
NodeType *link;
};
enum shipType{carrier,destroyer,battle,sub};
const
enum Orientation {Vertical, Horizontal, None};
struct PositionType
{
int row;
int col;
};
struct Ship
{
int size;
bool status[MAX_SHIPSIZE];
PositionType position;
Orientation orientation;
};
// Create and return a new node.
NodeType *CreateNode();
// Add a given node to a list in a sorted position.
void AddNode(NodeType *&listPtr, NodeType *newNodePtr);
// Find a value and remove that node from a list.
char *BoardArray;
BoardArray = new char [MAX_ROWS][MAX_COLS];
void InitializeBoard(char *BoardArray);
void DisplayBoard(BoardArray, int rowsUsed, int colsUsed);
void InitializeShips(Ship[]);
void InitializeShip(Ship&);
void PlaceShip(BoardArray, Ship&, PositionType, Orientation);
bool IsValid(Ship, PositionType);
int main()
{
srand(time(NULL));
Ship ship[MAX_SHIPS];
PositionType pos;
InitializeBoard(BoardArray);
DisplayBoard(BoardArray);
InitializeShips(ship);
NodeType *lastPtr = nullptr;
NodeType *listPtr = nullptr;
NodeType *currPtr = nullptr;
NodeType *newNodePtr = nullptr;
}
I am trying to modify my CreateNode function to use my enum shipType and create a random placement of ships that are different sizes.
// Create and return a new node.
NodeType *CreateNode(){
NodeType *newNodePtr;
// 1 - Allocate space for new node
newNodePtr = new NodeType;
// 2 - Assign values to node
cout << "Enter value for new node: ";
cin >> newValue;
newNodePtr->component = newValue;
newNodePtr->link = nullptr;
// 3 - Return node
return newNodePtr;
}
// Add a given node to a list in a sorted position.
void AddNode(NodeType *&listPtr, NodeType *newNodePtr){
NodeType *currPtr = listPtr;
NodeType *prevPtr = nullptr;
// 1 - Find position in list
while ((currPtr != nullptr) &&
(newNodePtr->component > currPtr->component)){
prevPtr = currPtr;
currPtr = currPtr->link;
}
cout << endl;
// 2 - Insert node
// First node in list
if ((prevPtr == nullptr) && (currPtr == nullptr)){
cout << "New list\n";
listPtr = newNodePtr;
}
// Beginning of list
else if (prevPtr == nullptr){
cout << "Add to front of list\n";
newNodePtr->link = listPtr;
listPtr = newNodePtr;
}
// End of list
else if (currPtr == nullptr){
cout << "Add to end of list\n";
prevPtr->link = newNodePtr;
}
// Middle-ish
else {
cout << "Add to middle of list\n";
newNodePtr->link = currPtr;
prevPtr->link = newNodePtr;
}
}