C编写的饥饿游戏控制台示例代码如何实现随机生成草原和森林
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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. Move\n");
printf("2. Hunt\n");
printf("3. Quit\n");
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;
printf怎么实现的 }
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 \n");
break;
} else {
printf("Invalid choice!\n");
}
}
return 0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论