C编写的饥饿游戏控制台示例代码如何实现随机生成草原和森林

C编写的饥饿游戏控制台示例代码如何实现随机生成草原和森林


2024年4月3日发(作者:)

C编写的饥饿游戏控制台示例代码如何实现

随机生成草原和森林

#include

#include

#include

#define ROW 10

#define COL 20

// Function to generate random map

void generateMap(char map[][COL]) {

srand(time(0)); // Seed the random number generator

for (int i = 0; i < ROW; i++) {

for (int j = 0; j < COL; j++) {

int randomNum = rand() % 10; // Generate a random number

between 0 and 9

if (randomNum < 7) { // 70% chance of grassland

map[i][j] = '.';

} else if (randomNum < 9) { // 20% chance of forest

map[i][j] = '*';

} else { // 10% chance of water

map[i][j] = '~';

}

}

}

}

// Function to display map

void displayMap(char map[][COL]) {

for (int i = 0; i < ROW; i++) {

for (int j = 0; j < COL; j++) {

printf("%c ", map[i][j]);

}

printf("n");

}

}

int main() {

char map[ROW][COL];

generateMap(map);

printf("Welcome to the Hunger Games Console Game!n");

printf("You are in a virtual world with random generated terrain.n");

while (1) {

printf("n");

displayMap(map);

printf("nOptions:n");

printf("1. Moven");

printf("2. Huntn");

printf("3. Quitn");

int choice;

printf("Enter your choice: ");

scanf("%d", &choice);

if (choice == 1) {

int row, col;

printf("Enter the coordinates to move (row and column): ");

scanf("%d %d", &row, &col);

if (row < 0 || row >= ROW || col < 0 || col >= COL) {

printf("Invalid coordinates!n");

continue;

}

if (map[row][col] == '~') {

printf("You cannot move to water!n");

continue;

}

printf("Moved to (%d, %d).n", row, col);

} else if (choice == 2) {

int row, col;

printf("Enter the coordinates to hunt (row and column): ");

scanf("%d %d", &row, &col);

if (row < 0 || row >= ROW || col < 0 || col >= COL) {

printf("Invalid coordinates!n");

continue;

}

if (map[row][col] == '.') {

printf("You found a wild animal in the grassland!n");

} else if (map[row][col] == '*') {

printf("You found a wild animal in the forest!n");

} else {

printf("There are no animals in the water!n");

}

} else if (choice == 3) {

printf("Quitting ");

break;

} else {

printf("Invalid choice!n");

}

}

return 0;

}


发布者:admin,转转请注明出处:http://www.yc00.com/web/1712085932a2002805.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信