gpt4 book ai didi

c - 战舰: place ships

转载 作者:太空宇宙 更新时间:2023-11-04 08:42:14 26 4
gpt4 key购买 nike

<分区>

我在 AVR 设备上写了一个战舰游戏。一切正常,只有在游戏 map 上放置船只才会出现小问题。有时船会彼此相邻放置,即使我认为我的代码会阻止这种情况。现在已经尝试调试它两天了。如果你们中有人注意到我哪里错了,我想把 place_ships 函数的代码放在这里。

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;
}
}
}
}
}

至此,原来的问题解决了。还有一些船没有放置。运送功能会跳过,有所不同。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com