0
ダイナミックアレイとリンクリストを使用する1人の戦艦ゲームを作成しています。このゲームでは、ユーザーがボードのサイズと場所を指定することができます。ボードはランダムな船の配置を生成し、ユーザーが座標を入力して、船に衝突したかどうかを確認することができます。私は、ノードを作成し、ソートされた位置にノードを追加する2つのリンクされたリスト関数を作成しました。 CreateNode
のロジックを変更して、ランダムな船体配置を作成するのに問題があります。解決方法についてのC++リンクリストとダイナミックアレイの使用
私の推測:これを行う方法について
任意の提案ですか?
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;
}
私は私の列挙shipType
を使用し、サイズが異なる船のランダム配置を作成するために、私のCreateNode
機能を変更しようとしています。
// 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;
}
}
tが、おそらくこれは* *コンパイルされた場合でも、簡単に(そして実際に役立つ)になります。 – WhozCraig