C++ C++;使用链表和动态数组
我正在创建一个单人战舰游戏,使用动态数组和链表。游戏应该允许用户指定棋盘大小和将放置的船只数量。该板生成随机船舶位置,并允许用户输入坐标,以查看他们是否击中或未击中船舶。我创建了两个链表函数,用于创建节点并在排序位置添加节点。我在修改C++ C++;使用链表和动态数组,c++,struct,linked-list,dynamic-arrays,C++,Struct,Linked List,Dynamic Arrays,我正在创建一个单人战舰游戏,使用动态数组和链表。游戏应该允许用户指定棋盘大小和将放置的船只数量。该板生成随机船舶位置,并允许用户输入坐标,以查看他们是否击中或未击中船舶。我创建了两个链表函数,用于创建节点并在排序位置添加节点。我在修改CreateNode中的逻辑以创建随机船舶布局时遇到问题 我对如何解决的猜测: declare variable to hold ship position allocate space for new node implement a for loop to se
CreateNode
中的逻辑以创建随机船舶布局时遇到问题
我对如何解决的猜测:
declare variable to hold ship position
allocate space for new node
implement a for loop to search through BoardArray
有什么建议吗
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;
}
我正在尝试修改我的CreateNode
函数,以使用我的enumshipType
并创建不同大小船舶的随机放置
// 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;
}
}
//创建并返回一个新节点。
NodeType*CreateNode(){
NodeType*newNodePtr;
//1-为新节点分配空间
newNodePtr=新节点类型;
//2-为节点指定值
cout>newValue;
newNodePtr->component=newValue;
newNodePtr->link=nullptr;
//3-返回节点
返回newNodePtr;
}
//将给定节点添加到排序位置的列表中。
void AddNode(NodeType*&listPtr,NodeType*newNodePtr){
节点类型*currPtr=listPtr;
节点类型*prevPtr=nullptr;
//1-在列表中查找位置
while((currPtr!=nullptr)&&
(newNodePtr->component>currPtr->component){
prevPtr=currPtr;
currPtr=currPtr->link;
}
你知道什么比这更容易吗
#include<iostream>
#include<iomanip>
#include<ctime>
#include<cctype>
#include<cmath>
using namespace std;
bool** CreateBoard(int &rows, int &cols)
{
cout << "Enter Board Dimensions" << endl;
cin >> *rows >> *cols;
bool** board = new bool[*rows][*cols];
return board;
}
bool IsPossible(bool** board, int ship, int rows, int cols)
{
int possibleCount = 0;
for(int i =0; i < rows; i++)
{
for(int j =0; j < cols; j++)
{
if(!board[i][j])
{
possibleCount++;
if(ship == possibleCount)
{
return true;
}
}
else
{
possibleCount = 0;
}
}
}
return false;
}
void CreateEnemyBoard(bool** board, int rows, int cols)
{
srand(time());
int ships[]={1,2,3,4};
for(int i =0;i < 4;i++)
{
int ship = ships[i];
if(!IsPossible(board, ship, rows, cols))
{
throw "Bad Board";
}
int isPossibleCount = 0;
while(isPossibleCount != ship)
{
int r = abs(rand())%rows;
bool isVerticle = r %2 == 0;
int c = abs(rand()) % cols;
if(isVerticle)
{
isPossibleCount = 0;
for(int cl1 = 0; cl1 < cols; cl1++)
{
if(!board[r][cl1])
{
isPossibleCount++;
if(ship == isPossibleCount)
{
for(int j =cl1;j>cl1-ship;j--)
{
board[r][j] = true;
}
break;
}
}
else
{
isPossibleCount = 0;
}
}
}
else
{
isPossibleCount = 0;
for(int r1 = 0; r1 < r1; r1++)
{
if(!board[r1][c])
{
isPossibleCount++;
if(ship == isPossibleCount)
{
for(int j =r1;j>r1-ship;j--)
{
board[j][c] = true;
}
break;
}
}
else
{
isPossibleCount = 0;
}
}
}
r = abs(rand())%rows;
isVerticle = r %2 == 0;
c = abs(rand()) % cols;
}
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
布尔**CreateBoard(整型&行型、整型&列型)
{
cout*行>>*列;
布尔**板=新布尔[*行][*列];
返回板;
}
布尔是可能的(布尔**板,整数船,整数行,整数列)
{
int-possibleCount=0;
对于(int i=0;icl1 ship;j--)
{
董事会[r][j]=正确;
}
打破
}
}
其他的
{
isPossibleCount=0;
}
}
}
其他的
{
isPossibleCount=0;
对于(int r1=0;r1r1;j--)
{
董事会[j][c]=正确;
}
打破
}
}
其他的
{
isPossibleCount=0;
}
}
}
r=abs(rand())%行;
isVerticle=r%2==0;
c=绝对值(rand())%cols;
}
}
}
t如果编译了它,可能会更容易(实际上也很有用)。