2017-05-02 6 views
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; 
    } 

} 

答えて

0

あなたはすべてのことよりも簡単だか知っている:

#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; 
    } 
    } 
} 
+0

tが、おそらくこれは* *コンパイルされた場合でも、簡単に(そして実際に役立つ)になります。 – WhozCraig

関連する問題