[心得] ChatGPT

楼主: enthos (影斯作业系统)   2022-12-07 13:24:01
用AI写MUD不是梦,不过限制是4000字。
AI 也会 LPMud。
1.
// https://chat.openai.com/chat
// Me: please write a mud in c
// Sure, here's a simple example of a mud written in C:
// Copy code
#include <stdio.h>
/* Define constants for the game */
#define MAX_ROOMS 10
#define MAX_ITEMS 10
/* Define structs for rooms, items, and the player */
struct room {
char *description;
struct room *north;
struct room *south;
struct room *east;
struct room *west;
struct item *items;
};
struct item {
char *description;
struct item *next;
};
struct player {
struct room *location;
struct item *inventory;
};
/* Define some global variables for the game */
struct room rooms[MAX_ROOMS];
struct player player;
/* Function prototypes */
void create_rooms();
void init_game();
void print_location();
void print_inventory();
void go(char *direction);
void get_item(char *item_name);
void drop_item(char *item_name);
int main() {
char input[100];
char *command;
char *argument;
init_game();
while (1) {
printf("\n> ");
scanf("%[^\n]s", input);
/* Parse the input into a command and argument */
command = strtok(input, " ");
argument = strtok(NULL, " ");
if (strcmp(command, "go") == 0) {
go(argument);
} else if (strcmp(command, "get") == 0) {
get_item(argument);
} else if (strcmp(command, "drop") == 0) {
drop_item(argument);
} else if (strcmp(command, "location") == 0) {
print_location();
} else if (strcmp(command, "inventory") == 0) {
print_inventory();
} else if (strcmp(command, "quit") == 0) {
break;
} else {
printf("I don't understand that command.\n");
}
}
return 0;
}
/* Create all the rooms in the game */
void create_rooms() {
/* Room descriptions */
rooms[0].description = "You are in a dark cave.";
rooms[1].description = "You are in a bright forest.";
rooms[2].description = "You are in a cold ice cave.";
rooms[3].description = "You are on a grassy plain.";
rooms[4].description = "You are in a hot desert.";
/* Room connections */
rooms[0].north = &rooms[1];
rooms[1].south = &rooms[0];
rooms[1].west = &rooms[2];
rooms[2].east = &rooms[1];
rooms[2].north = &rooms[3];
rooms[3].south = &rooms[2];
rooms[3].west = &rooms[4];
rooms[4].east = &rooms[3];
/* Room items */
rooms[0].items = NULL;
rooms[1
// Me: show me the next page
// An error occurred. If this issue persists please contact us through
// our help center at help.openai.com.
// Me: and then?
// Too many requests, please slow down
2.
// Me: write a mud in c
// Here is an example of a simple text-based game written in C:
// Copy code
#include <stdio.h>
#include <string.h>
// Declare the main function
int main() {
// Print a welcome message
printf("Welcome to the MUD!\n");
// Set up the player's initial location
char location[20] = "town square";
// Set up a variable to store the player's input
char input[20];
// Set up a flag to indicate when the game should end
int end_game = 0;
// Run the game loop
while (end_game == 0) {
// Print the player's current location
printf("You are in the %s.\n", location);
// Print a prompt and get the player's input
printf("> ");
scanf("%s", input);
// Check the player's input and change the location accordingly
if (strcmp(input, "north") == 0) {
strcpy(location, "northern land");
} else if (strcmp(input, "south") == 0) {
strcpy(location, "southern land");
} else if (strcmp(input, "east") == 0) {
strcpy(location, "eastern land");
} else if (strcmp(input, "west") == 0) {
strcpy(location, "western land");
} else if (strcmp(input, "quit") == 0) {
// If the player entered "quit", end the game
end_game = 1;
} else {
// If the player entered something else, print an error message
printf("I don't understand what you mean.\n");
}
}
// Print a goodbye message
printf("Thanks for playing the MUD!\n");
return 0;
}
// This is just a simple example to illustrate how a text-based game
// could be written in C. It could be expanded upon and improved
// in many different ways, such as adding more locations, items,
// and features.
3.
// Ref: https://applealmond.com/posts/167138
// https://beta.openai.com/playground/p/default-chat
// write a MUD in C
// AI:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// world data structure
struct Room {
char* name;
int north;
int east;
int south;
int west;
struct Item *items;
};
struct Item {
char* name;
int canPickup;
};
struct Player {
int currentRoom;
struct Item *inventory;
};
// AI functions
void AI(struct Room *rooms, struct Player *player) {
// get current room and find an item in it
struct Room *currRoom = &rooms[player->currentRoom];
if (currRoom->items != NULL) {
struct Item *item = currRoom->items;
// pick up item if possible
if (item->canPickup == 1) {
// add item to player's inventory
player->inventory = item;
}
// else move to next room
else {
// pick random direction
int dir = rand() % 4;
if (dir == 0) {
// move north
player->currentRoom = currRoom->north;
}
else if (dir == 1) {
// move east
player->currentRoom = currRoom->east;
}
else if (dir == 2) {
// move south
player->currentRoom = currRoom->south;
}
else if (dir == 3) {
// move west
player->currentRoom = currRoom->west;
}
}
}
// else move to next room
else {
// pick random direction
int dir = rand() % 4;
if (dir == 0) {
// move north
player->currentRoom = currRoom->north;
}
else if (dir == 1) {
// move east
player->currentRoom = currRoom->east;
}
else if (dir == 2) {
// move south
player->currentRoom = currRoom->south;
}
else if (dir == 3) {
// move west
player->currentRoom = currRoom->west;
}
}
}
int main(int argc, char **argv) {
// initialize world
struct Room rooms[10];
rooms[0].name = "room1";
rooms[0].north = 0;
rooms[0].east = 2;
rooms[0].south = 3;
rooms[0].west = 0;
rooms[0].items = malloc(sizeof(struct Item));
rooms[0].items->name = "key";
rooms[0].items->canPickup = 1;
rooms[1].name = "room2";
// initialize player
struct Player player;
player.currentRoom = 0;
player.inventory = NULL;
// start AI loop
while (1) {
AI(rooms, &player);
}
return 0;
}
4.
Write a MUD in C with chinese text.
AI:
#include <stdio.h>
#include <string.h>
// Chinese text structure
typedef struct ChineseText {
char text[65];
} ChineseText;
// MUD map structure
typedef struct Map {
int width;
int height;
ChineseText text[20][20]; // 20x20 map
int enemies[20][20]; // number of enemies in a location
} Map;
// Player character structure
typedef struct Character {
int xPos, yPos; // Position on the map
int HP;
int att, def; // Attack and defense stats
} Character;
int main(int argc, char *argv[]) {
// Initialize the game
Map map;
Character player;
player.xPos = 0;
player.yPos = 0;
player.HP = 100;
player.att = 10;
player.def = 5;
// Load the map
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++) {
strcpy(map.text[x][y].text, "此位置暂无内容");
// no content at this location
map.enemies[x][y] = 0; // no enemies at start
}
}
// Main game loop
while (player.HP > 0) {
// Print out a description of the current location
printf("你现在站在:%s\n",
map.text[player.xPos][player.yPos].text);
printf("附近有 %d 个敌人\n",
map.enemies[player.xPos][player.yPos]);
// Take user input
char input[32];
fgets(input, 32, stdin);
// Parse the user's input
if (strcmp(input, "前进") == 0) {
// Move the player position forward one spot
player.xPos++;
} else if (strcmp(input, "后退") == 0) {
// Move the player position backward one spot
player.xPos
作者: tsetsethatha (吉星麦造~~~我来了)   2022-12-07 20:11:00
赞啦!!收入精华区z-3-20 :D

Links booklink

Contact Us: admin [ a t ] ucptt.com