Closed. This question is off-topic. It is not currently accepting answers. Learn more。
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
5年前关闭。
我在AVR设备上写了一个战舰游戏。一切都很好,只是把船放在游戏地图上不会引起什么问题。有时,即使我认为我的代码可以防止这种情况发生,船只也会被放在彼此的旁边。已经试着调试了两天了。如果你们中有人能注意到我错在哪里的话,我想把船的代码放在这里。
原来的问题解决了。但仍有一些船只没有就位。船的功能是不同的。
我认为情况应该是:
因为我想你应该检查一下,这艘船不会与所有大小的船只和相邻的船只重叠。对y也是一样:
我建议您将此代码提取到函数中,例如CheckOverlap,因为它将简化代码读取和调试。
此外,从循环中上移条件的估值:
更清晰的代码还将是:
一般来说,尽量避免代码重复,把所有的代码都放在函数上。它将减少加倍错误,并大大简化程序的设计,这对您和所有其他读者都有好处。
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
5年前关闭。
我在AVR设备上写了一个战舰游戏。一切都很好,只是把船放在游戏地图上不会引起什么问题。有时,即使我认为我的代码可以防止这种情况发生,船只也会被放在彼此的旁边。已经试着调试了两天了。如果你们中有人能注意到我错在哪里的话,我想把船的代码放在这里。
int data_map[10][10]; //This has the data of where ships are, where player has shot etc.
char game_map[10][10]; //This is printed in UI. O for miss, X for hit.
int ship_sizes[] = {0,1,2,3,3,4,5};
int ships[] = {0,1,2,3,4,5,6};
void place_ships() { //Places the ships in the game map.
int other_ships; //Variabel for counting if there are already other ships in the area where trying place the ship.
for (i=6; i>0; i--) {
while (1) {
other_ships = 0; //Initialize.
ship_direction = rand() % 10; //Get random ship direction (1-4 = horizontal, 6-10 = vertical)
top_x = (rand() % 8) + 1; //Get random x-coordinate, not from map edges
top_y = (rand() % 8) + 1; //Get random y-coordinate, not from map edges
if (ship_direction < 5) {
if ((top_x-ship_sizes[i]) > -2) { //Make sure that ship has room in game map.
for (j=(top_y-1); j<(top_y+2); j++) { //Following 2 for-loops and if-statement inside makes sure that no other ships are in
//the area where the ship is tried to place.
for (k=(top_x+1); k>(top_x-(ship_sizes[i]-2)); k--) {
if ((data_map[j][k] == 1) || (data_map[j][k] == 2) || (data_map[j][k] == 3) || (data_map[j][k] == 4) || (data_map[j][k] == 5) || (data_map[j][k] == 6)) {
other_ships = 1; //Following 2 'breaks' and 'continue' are there for the situation if
break; //there are other ships in the area, stop placing ship and get new random coordinate and try again.
}
}
if (other_ships == 1) {
break;
}
}
if (other_ships == 1) {
continue;
}
for (l=top_x; l>(top_x-ship_sizes[i]); l--) {
data_map[top_y][l] = ships[i]; //If no other ships in the area, place the ship.
}
loading(); //Wait to optimize harware working. There are known timing issues on AVR. And this
//is here to try to avoid them.
break;
}
}
else if (ship_direction > 5) {
if ((top_y-ship_sizes[i]) > -2) { //Make sure that ship has room in game map.
for (j=(top_y+1); j>(top_y-(ship_sizes[i]-2)); j--) { //Following 2 for-loops and if-statement inside makes sure that no other ships are in
//the area where the ship is tryied toplace.
for (k=(top_x-1); k<(top_x+2); k++) {
if ((data_map[j][k] == 1) || (data_map[j][k] == 2) || (data_map[j][k] == 3) || (data_map[j][k] == 4) || (data_map[j][k] == 5) || (data_map[j][k] == 6)) {
other_ships = 1; //Following 2 'breaks' and 'continue' are there for the situation if
break; //there are other ships in the area, stop placing ship and get new random coordinate and try again.
}
}
if (other_ships == 1) {
break;
}
}
if (other_ships == 1) {
continue;
}
for (l=top_y; l>((top_y-(ship_sizes[i]))); l--) {
data_map[l][top_x] = ships[i]; //If no other ships in the area, place the ship.
}
loading(); //Wait to optimize harware working. There are known timing issues on AVR. And this
//is here to try to avoid them.
break;
}
}
}
}
}
原来的问题解决了。但仍有一些船只没有就位。船的功能是不同的。
最佳答案
在最后一个变体中:
for (k=(top_x+1); k>(top_x-(ship_sizes[i]-2)); k--) {
我认为情况应该是:
k > ( top_x - (ship_sizes[i] + 2) ) // not -2 but + 2
因为我想你应该检查一下,这艘船不会与所有大小的船只和相邻的船只重叠。对y也是一样:
top_y-(ship_sizes[i] + 2) // or top_y - ship_sizes[i] - 2
我建议您将此代码提取到函数中,例如CheckOverlap,因为它将简化代码读取和调试。
此外,从循环中上移条件的估值:
int length = top_x - (ship_sizes[i] + 2);
for (k= top_x+1; k > length; k--) {
更清晰的代码还将是:
int IsPlaced (j,k) {
return 1 <= data_map[j][k] && data_map[j][k] <=6;
}
一般来说,尽量避免代码重复,把所有的代码都放在函数上。它将减少加倍错误,并大大简化程序的设计,这对您和所有其他读者都有好处。
关于c - 战列舰:放置船只,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23124462/
10-12 16:15