2016-03-04 94 views
1

我想弄清楚如何将用户输入的船放置在战舰游戏中。 x和y整数是10x10板上的位置。水平方向为0,垂直方向为1。 boat_length显然是船的长度(从2到5)。如果船不能放在10x10板上,如果有另一艘船,我不能将它放在那里。任何帮助是极大的赞赏!战舰游戏C++,放置用户输入船

bool userboat(char boatArray[][BOARD_SIZE],int x, int y, int orientation, int boat_length){ 
    for(int yy = 0; yy < BOARD_SIZE; yy++){ 
    for(int xx = 0; xx < BOARD_SIZE; xx++){ 
     if(yy == y && xx == x){ 
     boatArray[yy][xx] = 'B'; 
     if(orientation == 0){ 
      if(x+boat_length< BOARD_SIZE){ 
      for(int boat = 0; boat<boat_length; boat++){ 
       boatArray[yy][boat] =BOAT; 
      } 
      }else{ 
      return false; 
      break; 
      } 
     } 
     }else{ 
      if(yy+boat_length< BOARD_SIZE){ 
      for(int boat = 1; boat<boat_length; boat++){ 
       boatArray[boat][yy] =BOAT; 
      } 
      }else{ 
      return false; 
      break; 
      } 
     } 
     } 
    } 
    return true; 
} 
+0

那么,什么是您的实际问题?什么部分不工作或者你不知道如何检查这些条件? – NathanOliver

+0

整个代码工作不正常,它没有返回正确的值。这是我对解决方案的尝试,但我想不出其他任何东西 – noobprogger

+1

这听起来像您可能需要学习如何使用调试器来逐步执行代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。进一步阅读:** [如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver

回答

0

您需要更改下面的代码:

boatArray[yy][boat] = BOAT; 

要:

boatArray[yy][xx + boat] = BOAT; 

让我们在来看看一个简单的例子。如果我们想把船放在位置(2,2),我们将整个2D阵列循环,直到达到xx == 2 & & yy == 2对不对? 好。现在我们检查这个位置(boatArray [yy] [xx])是否为空(你应该实现这个)。如果它是空的,我们进行数学计算,看看船是否可以放置在阵列中。 现在你的循环是这样的:

for(int boat = 0; boat<boat_length; boat++) 
      boatArray[yy][boat] = BOAT; 

这意味着boatArray [YY] [0],boatArray [YY] [1],..将得到船的价值。但你不想那样。这些是每一次的第一列。你应该做的,而不是为这个(正如我前面提到的):

boatArray[yy][xx + boat] = BOAT; 

编辑:为其他环同样的事情。

0

使用两个数组,以建立一个地图的方向迈出第一步:

// orientation 
// 
// 7 8 9 
// \|/ 
// 4- -6 
// /|\ 
// 1 2 3 
//     0 1 2 3 4 5 6 7 8 9 
const int dir_x[] = { 0, -1, 0, 1, -1, 0, 1, -1, 0, 1 }; 
const int dir_y[] = { 0, 1, 1, 1, 0, 0, 0, -1, -1, -1 }; 

之前将新的船,如果这个地方是empty首先你应该测试。

#define BOAT 'B' 
#define BOARD_SIZE 10 

bool userboat(char boatArray[][BOARD_SIZE], int x, int y, int orientation, int boat_length) 
{ 
    if (orientation < 1 || orientation > 9 || orientation == 5 || boat_length < 1) 
     return false; 

    int dx = dir_x[orientation]; 
    if (x < 0 || x >= BOARD_SIZE || 
     x + dx * boat_length < 0 || x + dx * boat_length > BOARD_SIZE) // test if x is in bounds 
     return false; 

    int dy = dir_y[orientation]; 
    if (y < 0 || y >= BOARD_SIZE || 
     y + dy * boat_length < 0 || y + dy * boat_length > BOARD_SIZE) // test if y is in bounds 
     return false; 

    // test if the boat can be placed 
    for (int i = 0; i < boat_length; ++ i) 
     if (boatArray[x+dx*i][y+dy*i] == BOAT) // test if a boat is already there 
      return false; 

    // the boat can be placed 
    for (int i = 0; i < boat_length; ++ i) 
     boatArray[x+dx*i][y+dy*i] = BOAT; 
    return true; 
} 


注意,一个C++解决方案可能是这样的:

const char BOAT  = 'B'; 
const int BOARD_SIZE = 10; 
using TBoard = std::array<std::array<char, BOARD_SIZE>, BOARD_SIZE>; 

bool userboat(TBoard &boatArray, int x, int y, int orientation, int boat_length) 
{ 
    static const std::array<int, 10> dir_x{ 0, -1, 0, 1, -1, 0, 1, -1, 0, 1 }; 
    static const std::array<int, 10> dir_y{ 0, 1, 1, 1, 0, 0, 0, -1, -1, -1 }; 

    try 
    { 
     int dx = dir_x.at(orientation); 
     int dy = dir_y.at(orientation); 

     // test if the boat can be placed 
     for (int i = 0; i < boat_length; ++ i) 
      if (boatArray.at(x+dx*i).at(y+dy*i) == BOAT) // test if a boat is already there 
       return false; 

     // the boat can be placed 
     for (int i = 0; i < boat_length; ++ i) 
      boatArray[x+dx*i][y+dy*i] = BOAT; 
    } 
    catch (...) 
    { 
     return false; 
    } 
    return true; 
}