2016-09-14 95 views
-1

我在嘗試將障礙物移向2x2陣列上的目標時遇到了一些問題。我有一個函數,它將兩個整數作爲參數。該函數然後創建一個大小爲10的2x2數組,填充空閒空間,創建一個障礙物(障礙物每次保持不變)。然後它創建目標點(每次將它分配到同一個點)和球點(它位於數組位置[x] [y]中)。如何在C++中通過網格來操作/移動對象

目標是讓球向目標移動,直到撞到障礙物,然後繞過障礙物,同時跟蹤障礙物周圍每個空間離目標多遠,然後返回到最近點並繼續朝着目標前進。

我試圖開發一個moveToGoal函數,該方法將球移向目標,因爲沒有障礙,但我在訪問printGrid函數之外的網格時遇到了很多麻煩。如果我在打印網格功能之外創建網格,我無法訪問它,因爲它超出了範圍。我知道這可能看起來很混亂,但我會盡力回答任何有關它的問題。這是我到目前爲止:

#include <stdio.h> 
#include <vector> 
#include <cstdlib> 
#include <ctime> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <unistd.h> 

using namespace std; 

void printGrid(int x, int y)  //Used to clear old grids, and print a 
{         //new grid with input location for ball 
    system("CLS"); 
    string grid [10][10];   //Initialize grid array 
    for (int i=0; i<10; i++) 
    { 
     for (int j=0; j<10; j++) 
     { 
      grid[i][j] = ' ';  //Constructs grid with freespace 
     } 
    } 
    for (int i=2; i<8; i++)   //Constructs obstacle(s) 
    { 
     grid[5][i]='O'; 
    } 
    grid[2][5] = 'G';    //Sets Goal location 
    grid[x][y] = 'B';    //Sets current ball location(starts 8,5) 
    for (int i=0; i<10; i++)  //Prints finished grid 
    { 
     for (int j=0; j<10; j++) 
     { 
      cout<<grid[i][j]; 
     } 
     cout<<endl; 
    } 
} 

void moveToGoal(int x, int y) 
{ 
    printGrid(x-1, y); 
} 

int main() 
{ 
    moveToGoal(8,5); 
    sleep(1); 
    moveToGoal(7,5); 
    sleep(1); 
    moveToGoal(6,5); 
    sleep(1); 
    moveToGoal(5,5); 
    sleep(1); 
    moveToGoal(4,5); 
    sleep(1); 
    moveToGoal(3,5); 
} 

任何幫助,將不勝感激!

+0

爲什麼你需要睡眠呼叫? –

+0

「2x2」數組是一個長度爲2和寬度爲2的數組。您的意思是「2維數組」嗎? – kfsone

+0

您的網格對於printGrid函數是本地的。 如果您需要在此函數之外調整它,您需要將它作爲函數參數來回傳遞,或者將其作爲全局變量使用。 –

回答

0

嘗試這種情況:

string grid [10][10];   // the grid array is now global 

void printGrid(int x, int y)  //Used to clear old grids, and print a 
{         //new grid with input location for ball 
    system("CLS"); 
    for (int i=0; i<10; i++) // clean the old grid 
    { 
     for (int j=0; j<10; j++) 
     { 
      grid[i][j] = ' ';  //Constructs grid with freespace 
     } 
    } 
    for (int i=2; i<8; i++)   //Constructs obstacle(s) 
    { 
     grid[5][i]='O'; 
    } 
    grid[2][5] = 'G';    //Sets Goal location 
    grid[x][y] = 'B';    //Sets current ball location(starts 8,5) 
    for (int i=0; i<10; i++)  //Prints finished grid 
    { 
     for (int j=0; j<10; j++) 
     { 
      cout<<grid[i][j]; 
     } 
     cout<<endl; 
    } 
} 
0

在下面的代碼

int f() { 
    int i = 1; 
    i++; 
    std::cout << i << "\n"; 
} 

int main() { 
    f(); 
    f(); 
} 

在功能f可變i是一個局部變量。這意味着每次調用f()時都會創建一個新實例。所以上面代碼的輸出是2,兩次,不是2,然後是3

在您的代碼中,網格位於printGrid的本地,因此每次調用printGrid時都會創建網格。

解決方案是使網格成爲傳遞給它的參數或全局變量。將網格作爲參數傳遞是首選,您可能會考慮將其放置在結構或類中,以使其更易於執行。

#include <array> 
#include <utility> 
#include <iostream> 

class Grid 
{ 
    // type alias for a grid of 10x10 chars 
    using grid_t = std::array<std::array<char, 10>, 10>; 

    grid_t grid_; // our actual grid data: note grid_[y][x]. 
    int ballX_ = -1, ballY_ = -1; 

public: 
    // Constructor 
    Grid(int goalX, int goalY) 
    { 
     // populate the grid with free space 
     for (auto&& row: grid_) { 
      std::fill(row.begin(), row.end(), ' '); 
     } 
     // add obstacles 
     for (size_t i = 2; i < 8; ++i) 
      grid_[i][5] = 'O'; 
     // place the goal 
     grid_[goalY][goalX] = 'G'; 
    } 

    // place, move or remove the ball on the grid 
    void placeBall(int x, int y) 
    { 
     // Remove the ball from any previous location 
     if (ballX_ != -1 && ballY != -1) 
      grid_[ballY_][ballX_] = ' '; 

     // update location 
     ballX_ = x, ballY = y; 

     // Place ball on board if location is not -1,-1 
     if (ballX_ != -1 && ballY != -1) 
      grid_[ballY_][ballX_] = 'B'; 
    } 

    // Return the character at a given location 
    char at(int x, int y) const 
    { 
     return grid_[y][x]; 
    } 

    // Query ball position 
    int ballX() const { return ballX_; } 
    int ballY() const { return ballY_; } 

    // print the grid 
    friend ostream& operator << (ostream&, const Grid&); 
}; 

ostream& operator<<(ostream& stream, const Grid& grid) 
{ 
    for (auto&& row: grid.grid_) { 
     for (auto&& col: row) { 
      stream << col << ' '; 
     } 
     stream << '\n'; 
    } 
    return stream; 
} 

// helper for your cls/sleep thing around printing the grid. 
void printGrid(const Grid& grid) 
{ 
    system("CLS"); 
    std::cout << grid; 
    sleep(1); 
} 

void manipulateGrid(Grid& grid) // reference to the caller's grid 
{ 
    printGrid(grid); 
    grid.placeBall(8, 5); 
    printGrid(grid); 
    grid.placeBall(7, 5); 
    // ... 
} 

int main() 
{ 
    Grid g(2, 5); // create grid with [2,5] as the goal 
    manipulateGrid(g); // to demonstrate passing as parameter 
}