- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
<分区>
我有以下文件:ex19.c:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "object.h"
#include "ex19.h"
//#include "gamemechanics.h"
int main(int argc, char *argv[])
{
// simple way to setup the randomness
srand(time(NULL));
// make our map to work with
Map *game = NEW(Map, "The Hall of the Minotaur.");
/*printf("You enter the ");
game->location->_(describe)(game->location);
while(process_input(game)) {
}*/
return 0;
}
gamemechanics.c:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "ex19.h"
#include "object.h"
//#include "gamemechanics.h"
int Monster_attack(void *self, int damage)
{
Monster *monster = self;
printf("You attack %s!\n", monster->_(description));
monster->hit_points -= damage;
if(monster->hit_points > 0) {
printf("It is still alive.\n");
return 0;
} else {
printf("It is dead!\n");
return 1;
}
}
int Monster_init(void *self)
{
Monster *monster = self;
monster->hit_points = 10;
return 1;
}
Object MonsterProto = {
.init = Monster_init,
.attack = Monster_attack
};
void *Room_move(void *self, Direction direction)
{
Room *room = self;
Room *next = NULL;
if(direction == NORTH && room->north) {
printf("You go north, into:\n");
next = room->north;
} else if(direction == SOUTH && room->south) {
printf("You go south, into:\n");
next = room->south;
} else if(direction == EAST && room->east) {
printf("You go east, into:\n");
next = room->east;
} else if(direction == WEST && room->west) {
printf("You go west, into:\n");
next = room->west;
} else if(direction == SOUTHWEST && room->southwest) {
printf("You go southwest, into:\n");
next = room->southwest;
} else if(direction == NORTHEAST && room->northeast) {
printf("You go northeast, into: \n");
next = room->northeast;
} else if(direction == NORTHWEST && room->northwest) {
printf("You go northwest, into: \n");
next = room->northwest;
} else if(direction == SOUTHEAST && room->southeast) {
printf("You go southeast, into: \n");
next = room->southeast;
} else {
printf("You can't go that direction.");
next = NULL;
}
if(next) {
next->_(describe)(next);
}
return next;
}
int Room_attack(void *self, int damage)
{
Room *room = self;
Monster *monster = room->bad_guy;
if(monster) {
monster->_(attack)(monster, damage);
return 1;
} else {
printf("You flail in the air at nothing. Idiot.\n");
return 0;
}
}
Object RoomProto = {
.move = Room_move,
.attack = Room_attack
};
void *Map_move(void *self, Direction direction)
{
Map *map = self;
Room *location = map->location;
Room *next = NULL;
next = location->_(move)(location, direction);
if(next) {
map->location = next;
}
return next;
}
int Map_attack(void *self, int damage)
{
Map* map = self;
Room *location = map->location;
return location->_(attack)(location, damage);
}
int Map_init(void *self)
{
Map *map = self;
// make some rooms for a small map
Room *hall = NEW(Room, "The great Hall");
Room *throne = NEW(Room, "The throne room");
Room *arena = NEW(Room, "The arena, with the minotaur");
Room *kitchen = NEW(Room, "Kitchen, you have the knife now");
Room *bathroom = NEW(Room, "The bathroom, place where people shit");
Room *playroom = NEW(Room, "A child's playroom");
Room *dungeon = NEW(Room, "The dungeon with the witch");
Room *jail = NEW(Room, "The jail with crooked criminals");
// put the bad guy in the arena
arena->bad_guy = NEW(Monster, "The evil minotaur");
//ashwini is adding a new monster
dungeon->bad_guy = NEW(Monster,"Witch");
dungeon->bad_guy->hit_points = 20;
// setup the map rooms
hall->north = throne;
throne->west = arena;
throne->east = kitchen;
throne->south = hall;
arena->east = throne;
kitchen->west = throne;
//ashwini's addition to the map
throne->northeast = dungeon;
kitchen->north = dungeon;
dungeon->southwest = throne;
dungeon->south = kitchen;
dungeon->east = jail;
throne->northwest = jail;
arena->north = jail;
jail->southeast = throne;
jail->south = arena;
jail->east = dungeon;
throne->southeast = playroom;
kitchen->south = playroom;
playroom->north = kitchen;
playroom->west = hall;
playroom->northwest = throne;
throne->southwest = bathroom;
arena->south = bathroom;
bathroom->northeast = throne;
bathroom->north = arena;
bathroom->east = hall;
// start the map and the character off in the hall
map->start = hall;
map->location = hall;
return 1;
}
Object MapProto = {
.init = Map_init,
.move = Map_move,
.attack = Map_attack
};
int process_input(Map *game)
{
printf("\n> ");
char ch = getchar();
getchar(); // eat ENTER
int damage = rand() % 4;
switch(ch) {
case -1:
printf("Giving up? You suck.\n");
return 0;
break;
case 'n':
game->_(move)(game, NORTH);
break;
case 'N':
game->_(move)(game,NORTHEAST);
break;
case 's':
game->_(move)(game, SOUTH);
break;
case 'S':
game->_(move)(game,SOUTHWEST);
break;
case 'e':
game->_(move)(game, EAST);
break;
case 'E':
game->_(move)(game,SOUTHEAST);
break;
case 'w':
game->_(move)(game, WEST);
break;
case 'W':
game->_(move)(game,NORTHWEST);
break;
case 'a':
game->_(attack)(game, damage);
break;
case 'l':
printf("You can go:\n");
if(game->location->north) printf("NORTH(n)\n");
if(game->location->south) printf("SOUTH(s)\n");
if(game->location->east) printf("EAST(e)\n");
if(game->location->west) printf("WEST(w)\n");
if(game->location->northeast) printf("NORTHEAST(N)\n");
if(game->location->northwest) printf("NORTHWEST(W)\n");
if(game->location->southeast) printf("SOUTHEAST(E)\n");
if(game->location->southwest) printf("SOUTHWEST(S)\n");
break;
default:
printf("What?: %d\n", ch);
}
return 1;
}
ex19.h
#ifndef _ex19_h
#define _ex19_h
#include "object.h"
struct Monster {
Object proto;
int hit_points;
};
typedef struct Monster Monster;
int Monster_attack(void *self, int damage);
int Monster_init(void *self);
struct Room {
Object proto;
Monster *bad_guy;
struct Room *north;
struct Room *south;
struct Room *east;
struct Room *west;
struct Room *southwest;
struct Room *southeast;
struct Room *northeast;
struct Room *northwest;
};
typedef struct Room Room;
void *Room_move(void *self, Direction direction);
int Room_attack(void *self, int damage);
int Room_init(void *self);
struct Map {
Object proto;
Room *start;
Room *location;
};
typedef struct Map Map;
void *Map_move(void *self, Direction direction);
int Map_attack(void *self, int damage);
int Map_init(void *self);
#endif
对象.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "object.h"
#include <assert.h>
void Object_destroy(void *self)
{
Object *obj = self;
assert(obj != NULL);
if(obj) {
if(obj->description) free(obj->description);
free(obj);
}
}
void Object_describe(void *self)
{
Object *obj = self;
assert(obj!=NULL);
printf("%s.\n", obj->description);
}
int Object_init(void *self)
{
// do nothing really
return 1;
}
void *Object_move(void *self, Direction direction)
{
printf("You can't go that direction.\n");
return NULL;
}
int Object_attack(void *self, int damage)
{
printf("You can't attack that.\n");
return 0;
}
void *Object_new(size_t size, Object proto, char *description)
{
assert(description != NULL);
// setup the default functions in case they aren't set
if(!proto.init) proto.init = Object_init;
if(!proto.describe) proto.describe = Object_describe;
if(!proto.destroy) proto.destroy = Object_destroy;
if(!proto.attack) proto.attack = Object_attack;
if(!proto.move) proto.move = Object_move;
// this seems weird, but we can make a struct of one size,
// then point a different pointer at it to "cast" it
Object *el = calloc(1, size);
assert(el!=NULL);
*el = proto;
// copy the description over
el->description = strdup(description);
// initialize it with whatever init we were given
if(!el->init(el)) {
// looks like it didn't initialize properly
el->destroy(el);
return NULL;
} else {
// all done, we made an object of any type
return el;
}
}
对象.h
#ifndef _object_h //this is if note defined
#define _object_h //checks if there is alread an object_h
//above is usefule if we are including it multiple times, it doesn't reinclude,
//just does it once and keeps refereing back
typedef enum {
NORTH, SOUTH, EAST, WEST, SOUTHWEST, NORTHWEST, SOUTHEAST,NORTHEAST
} Direction;
typedef struct {
char *description;
int (*init)(void *self);
void (*describe)(void *self);
void (*destroy)(void *self);
void *(*move)(void *self, Direction direction);
int (*attack)(void *self, int damage);
} Object;
int Object_init(void *self);
void Object_destroy(void *self);
void Object_describe(void *self);
void *Object_move(void *self, Direction direction);
int Object_attack(void *self, int damage);
void *Object_new(size_t size, Object proto, char *description);
#define NEW(T, N) Object_new(sizeof(T), T##Proto, N)
#define _(N) proto.N
#endif
我正在努力使 ex19.c 使用 gamemechanics.c 中的函数和结构。但是每当我尝试编译时,我都会收到以下错误:
:~/c_learning/ex19_folder$ gcc -c ex19.c gamemechanics.c object.c
In file included from ex19.c:6:0:
ex19.c: In function ‘main’:
ex19.c:17:21: error: ‘MapProto’ undeclared (first use in this function)
Map *game = NEW(Map, "The Hall of the Minotaur.");
^
object.h:26:41: note: in definition of macro ‘NEW’
#define NEW(T, N) Object_new(sizeof(T), T##Proto, N)
^
ex19.c:17:21: note: each undeclared identifier is reported only once for each function it appears in
Map *game = NEW(Map, "The Hall of the Minotaur.");
^
object.h:26:41: note: in definition of macro ‘NEW’
#define NEW(T, N) Object_new(sizeof(T), T##Proto, N)
^
我的问题实际上是如何开始弄清楚到底发生了什么,因为在这一点上,我只是在进行混合和匹配,只是为了看看是什么让它起作用,但这并不能帮助我理解错误的原因正在发生(并且因为它总是失败)。因此,首先为什么我会收到 MapProto 未声明的错误,即使我在 gamemechanics.c 中声明了?我尝试创建 gamemechanics.h 但后来我对依赖项的工作方式感到困惑。这是 ex19 来自 learn c hard way。我可以尝试做一些事情,比如创建一个 gamemechanics.h 来创建函数原型(prototype),但后来我对它应该如何在系统中实现感到困惑。任何建议都会有所帮助。此外,关于如何使这个问题更容易理解的建议也会有所帮助。
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!