Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在命令提示符上显示船舶时遇到问题!_C - Fatal编程技术网

C 在命令提示符上显示船舶时遇到问题!

C 在命令提示符上显示船舶时遇到问题!,c,C,我用C语言构建了一个战舰游戏。我有下面的代码来验证随机放置的战舰的点数没有超过棋盘限制。代码运行不太顺畅,在生成随机点时应用程序挂起 你能给我推荐一些优化吗 while(1 == 1) { //Generates the x, y coordenates of the point int x = rand() % 9; int y = rand() % 9; //Calculating the ship direction

我用C语言构建了一个战舰游戏。我有下面的代码来验证随机放置的战舰的点数没有超过棋盘限制。代码运行不太顺畅,在生成随机点时应用程序挂起

你能给我推荐一些优化吗

while(1 == 1)
  {  
      //Generates the x, y coordenates of the point
      int x = rand() % 9;
      int y = rand() % 9;      

      //Calculating the ship direction
      char direction = ((rand() % 10) > 5) ? 'V' : 'H';   

      if(direction == 'H')
      {
           //Verifies that the ship placed on the acquired x coordenate of the point does not exceed the board size
           //if so recalculates the value of x               
           while(!(((x + ships[i].length) - 1) < 10)) x = (rand() % 5);
      }
      else
      {
           //Verifies that the ship placed on the acquired y coordenate of the point does not exceed the board size
           //if so recalculates the value of y
           while(!(((y + ships[i].length) - 1) < 10)) y = (rand() % 5);
      }                              

      //Calculating the coordenates for each point of the ship                             
      for(j = 0; j < ships[i].length; j++)
      {
          if(direction == 'H')
          {                                      
             points[j].x = (x + j);
             points[j].y = y;
          }    
          else
          {
             points[j].x = x;
             points[j].y = (y + j);                                                        
          }

          //Validating that the coordenate asigned to a point has not been assigned to another ship              
          if(verifyPos(points[j].x, points[j].y, ships, length)) 
          {           
              invalid = 1;
              break;
          }
      }

      //if all the points of the ship are valid, move to the next ship
      //if not recalculate the initial point and the subsequent coordenates
      if(invalid == 0) break;       
  }

  ships[i].points = points;
 }   
}
while(1==1)
{  
//生成点的x,y坐标
int x=rand()%9;
int y=rand()%9;
//计算船舶航向
字符方向=((rand()%10)>5)?'V':'H';
如果(方向='H')
{
//验证放置在获取的点x坐标上的船舶是否不超过板尺寸
//如果是,则重新计算x的值
而(!(((x+ships[i].length)-1<10)x=(rand()%5);
}
其他的
{
//验证放置在获取的点y坐标上的船舶是否不超过板尺寸
//如果是,则重新计算y的值
而(!((y+ships[i].length)-1<10)y=(rand()%5);
}                              
//计算船舶各点的坐标
对于(j=0;j
您需要为随机化程序设定种子

#include <time.h>

srand(time(NULL));
#包括
srand(时间(空));

这是一个很简单的方法。

一个改进的方法是选择船要朝V或H的方向前进,然后只使用它将适合的船板部分,因此如果船是4长的,并且竖直部分仅使用第1-7行(10排船板中的第1-7行)作为竖直部分的起点。那么你根本不需要检查它是否合适

查看代码,我看不出指定的点在哪里

points[j].x = x;
points[j].y = (y + j); 

如果船舶无效,则清除。据我所知,积分数组中可能充满了无效的积分。

请不要发布同一个问题两次。在这种情况下,正确的做法是编辑你的旧(已关闭)问题,并尝试重新打开它。我是新来的,我不知道如何重新打开我的旧帖子。因此,我决定再次解释我的问题。没关系,但你可能需要阅读以更好地理解这个过程。你能给出一些具体的例子/场景并描述“它不起作用”的含义吗?你的问题“太笼统了”。不幸的是,这意味着如果我们回答了所有问题,我们将有效地为您完成家庭作业(我们不想破坏所有的乐趣)。那么告诉我,您以前是如何调用
srand()
的?可以粘贴准确的行吗?重要的是
srand()
在while循环之外,并且只能调用一次
rand()
是一个伪数生成器,这意味着每次使用相同的数字进行播种时,它都会给您相同的结果。我使用的方法与您提到的方法相同!!:(@hexa我对她的问题的解释是,她说随机生成的船的位置在两艘船之间是相同的。问题不是当她运行程序时,第一艘船在同一地点(IMHO)。while循环只检查一艘飞船的位置是否适合棋盘,但不检查它是否与另一艘已经存在的飞船重叠。我猜这基本上是一个随机战舰游戏生成器类型的东西,你不能有重叠的飞船。在这种情况下,她需要更改她的
while()
循环以检查其他约束。问题是对rand()函数的第二次调用挂起。这就像它总是生成相同的数字,而不管种子是什么。