[心得] ChatGPT
用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--;
} else if (strcmp(input, "攻击") == 0) {
// Attack the enemies at this location
if (map.enemies[player.xPos][player.yPos] > 0) {
// Calculate attack damage
int damage = player.att -
map.enemies[player.xPos][player.yPos] * 2;
// Subtract damage from enemies
map.enemies[player.xPos][player.yPos] -=
damage;
// Print out result of attack
printf("你对敌人造成了 %d 点伤害\n", damage);
}
}
// Check for game over conditions
if (player.xPos < 0 || player.xPos >= 20 || player.yPos < 0
|| player.yPos >= 20) {
printf("你被踩到了外太空!游戏结束!\n");
break;
}
}
return 0;
}
// Human:
5.
// write a MUD in C with chinese text and tab width 2
// AI:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 中文字符串需转换成Unicode编码
const char *WELCOME_MSG = "\x4f60\x597d\xff0c\x6b22\x8fce\x6765
\x5217\x83f2\x7684\x5192\x9669\xff01\n";
// 定义玩家结构体
typedef struct {
// 游戏中使用的各种变量
int hp;
int mp;
int xp;
int gold;
// 玩家当前所处地图
int map;
int x;
int y;
int dir;
} Player;
// 定义地图结构体
typedef struct {
int width;
int height;
char **map;
} Map;
// 全局变量,用于储存玩家信息
Player player;
// 初始化函数: 新建玩家,创建地图
void init() {
// 初始化玩家信息
player.hp = 100;
player.mp = 100;
player.xp = 0;
player.gold = 0;
player.map = 0;
player.x = 0;
player.y = 0;
player.dir = 0;
// 初始化地图
Map map;
map.width = 10;
map.height = 10;
map.map = malloc(map.width * map.height);
for (int i = 0; i < map.width; i++) {
for (int j = 0; j < map.height; j++) {
if (i == 0 || j == 0 ||
i == map.width-1 || j == map.height-1) {
map.map[i][j] = '#';
} else {
map.map[i][j] = '.';
}
}
}
}
// 用于显示地图的函数
void showMap() {
for (int i = 0; i < map.width; i++) {
for (int j = 0; j < map.height; j++) {
printf("%02x ", map.map[i][j]);
}
printf("\n");
}
}
// 接受玩家的指令,进行相应的处理
void processCommand(char *cmd) {
if (strcmp(cmd, "quit") == 0) {
printf("\x5173\x95ed\xff01\n");
exit(0);
}
}
int main() {
// 游戏开始,输出欢迎消息
printf(WELCOME_MSG);
// 初始化
init();
// 无限循环,接受并处理玩家指令
while (1) {
// 显示地图
showMap();
// 提示玩家输入
printf("\x8bf7\x8f93\x5165\x547d\x4ee4\xff1a");
// 读取玩家输入
char cmd[256] = {0};
scanf("%s", &cmd);
// 处理玩家输入
processCommand(cmd);
}
return 0;
}
6.
// Me: write a LPMud mob code.
// AI:
/* AI for Mob in a LPMud */
inherit "/std/living/ai";
void create()
{
::create();
// Set up default stats
set_str(10);
set_dex(10);
set_con(10);
set_int(10);
set_wis(10);
set_cha(10);
// Set up default attack and defense ratings
set_attack_level(1);
set_defense_level(1);
// Set up default movement speed
set_speed(2);
// Set up default aggression behavior
set_aggressive(FALSE);
// Set up the places where mob will move
set_move_after(600, "/room/path/to/mob/destination1");
set_move_after(1200, "/room/path/to/mob/destination2");
set_move_after(1800, "/room/path/to/mob/destination3");
// Set up a specific message that is displayed upon entering the room
set_enter_room_msg("The mob enters the room silently.");
// Set up response to being attacked
set_on_attack_response("The mob responds with aggressive hatred!\n");
// Set up response to being hit
set_on_hurt_response("The mob grunts in pain.\n");
}
7.
// Me: write MUD game save and load function in C
// function to save the MUD game
void SaveGame(FILE *fptr)
{
// Declare all necessary variables
int i;
Room** rooms = GetRooms();
int numRooms = GetNumRooms();
Character** characters = GetCharacters();
int numCharacters = GetNumCharacters();
// Save all rooms
for(i=0; i<numRooms; i++)
{
fprintf(fptr, "room:[%s][%d][%d]\n",
rooms[i]->name,
rooms[i]->x_location,
rooms[i]->y_location);
}
// Save all characters
for(i=0; i<numCharacters; i++)
{
fprintf(fptr, "character:[%s][%d][%d]\n",
characters[i]->name,
characters[i]->x_location,
characters[i]->y_location);
}
}
// function to load the MUD game
void LoadGame(FILE *fptr)
{
// Declare all necessary variables
char line[256];
char name[32];
int x_location;
int y_location;
while(fgets(line, sizeof(line), fptr))
{
// Parse the line of data
sscanf(line, "%*[^:]:[%[^]]][%d][%d]\n", name,
&x_location, &y_location);
if (strncmp(line, "room:", 5) == 0)
{
// Create a room object with the given parameters
CreateRoom(name, x_location, y_location);
}
else if (strncmp(line, "character:", 10) == 0)
{
// Create a character object with the given parameters
CreateCharacter(name, x_location, y_location);
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.134.104.16 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/mud/M.1670390648.A.7B3.html
推
12/07 15:31,
2年前
, 1F
12/07 15:31, 1F
推
12/07 15:56,
2年前
, 2F
12/07 15:56, 2F
推
12/07 20:11,
2年前
, 3F
12/07 20:11, 3F
推
12/08 09:53,
2年前
, 4F
12/08 09:53, 4F
推
12/09 15:55,
2年前
, 5F
12/09 15:55, 5F
推
12/12 17:06, , 6F
12/12 17:06, 6F
→
12/13 13:40, , 7F
12/13 13:40, 7F
推
12/20 17:14, , 8F
12/20 17:14, 8F
mud 近期熱門文章
9
17
PTT遊戲區 即時熱門文章
7
15