我已经在这里多次询问过这个关于我正在尝试制作的这个红润游戏的问题.我正在研究基于文本的冒险游戏.首先我用Java创建它,因为这就是我在学习游戏所用的类.现在我正在尝试学习需要objective-c的iOS开发.在参加Lynda Essentials课程之后,我对目标c感到非常满意(当然,以前的Java经验帮助过).无论如何我正在研究这个游戏,我遇到的问题似乎与目标c非常不同.
在Java中,当我有多个类时,他们只需要在同一个目录中,以便我在其他类中使用它们.在Objective-C中不是这种情况......如果我想在B类中使用A类,我必须导入头文件.对于这个游戏,我有两个自定义类,一个Location类和一个Exit类.Location类需要知道它有什么退出(所以如果我想使用它我必须导入Exit.h)并且退出需要知道它连接到哪个位置(所以我必须导入Location.h).似乎我不能这样做,因为有一种称为循环引用(或类似的东西).但是,如果我不这样做,那么我会收到"预期的类型"错误.所以我不知道该怎么做.我将在下面显示代码.
Exit.h
#import <Foundation/Foundation.h>
#import "Location.h"
#define NORTH 0
#define SOUTH 1
#define EAST 2
#define WEST 3
@interface Exit : NSObject
@property NSString * dirName;
@property NSString * dirShortName;
@property int direction;
@property Location * connection;
-(id)initWithConnection:(Location *) loc andDirection:(int) dir;
@end
Run Code Online (Sandbox Code Playgroud)
Exit.m
#import "Exit.h"
@implementation Exit
@synthesize dirName;
@synthesize dirShortName;
@synthesize direction;
@synthesize connection;
-(id)initWithConnection:(Location *)loc andDirection:(int)dir {
self = [super init];
if(self) {
direction = dir;
switch(direction) {
case 0:
dirName = @"North";
dirShortName = …Run Code Online (Sandbox Code Playgroud) 我有一个包含大量不同案例的枚举.想象它就像石头剪刀一样.
enum Play {
case Rock, Paper, Scissors
}
Run Code Online (Sandbox Code Playgroud)
现在我正在创建一个具有Play属性和方法的类,该方法将另一个Play参数作为"攻击者",我需要比较所有可能的结果.我能想到的唯一方法就是嵌套开关,但由于我的枚举超过3个案例,因此它成为一个很有可能出错的大功能.
func determineEffectiveness(withAttacker attacker: Play) {
switch attacker.type {
case .Rock {
switch self.type {
case .Rock {
return 0
}
case .Paper {
return 1
}
case .Scissors {
return -1
}
}
}
case .Paper {
// ...etc
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一种丑陋的暴力方法,但我很难想出更好的解决方案.
更新:
按照惯例......在我提出问题之后,我想出了一个更好的解决方案.我将把这个解决方案作为答案但我不会接受它,直到我得到别人的答案,因为可能有更好的方法.
我认为这可能是PostgreSQL的错误,但是我在这里发布了它,以防万一我丢失了一些东西。当我的WHERE子句有一个NOT IN ()子句时,null在列表中包含该子句将不再是真实的。下面是我的问题的一个愚蠢的例子。
=# select 1 where 1 not in (1);
?column?
----------
(0 rows)
=# select 1 where 1 not in (2);
?column?
----------
1
(1 row)
=# select 1 where 1 not in (null);
?column?
----------
(0 rows)
=# select 1 where 1 not in (null, 2);
?column?
----------
(0 rows)
=# select 1 where 1 not in (2, null);
?column?
----------
(0 rows)
=# select 1 where 1 not in (2, 3);
?column? …Run Code Online (Sandbox Code Playgroud) 所以我遇到的问题是我想根据条件测试数组的每个部分,如果数组的所有部分都返回false我想做某事但是如果其中任何一个返回true我想做其他事情.例如
String[] arr = new String[8];
for(String s : arr) {
if(s.equalsIgnoreCase("example") {
// Do this
} else {
// do that
}
}
Run Code Online (Sandbox Code Playgroud)
上面的问题是,它会对所有8个数组执行此操作或执行此操作.我想要的是它测试所有这些,如果它们都是假的,那么如果它们中的任何一个都是真的那么就这样做.
当我创建自定义类时,我希望能够在构建类的实例后跳过代码的alloc init部分.与它的完成方式类似:
NSString * ex = [NSString stringWithFormat...];
Run Code Online (Sandbox Code Playgroud)
基本上我已经使用自定义初始化方法设置了类来设置我的基本变量.然而,当我在前端并实际制作这些小动物时,我不得不说:
[[Monster alloc] initWithAttack:50 andDefense:45];
Run Code Online (Sandbox Code Playgroud)
而且我宁愿能说
[Monster monsterWithAttack:50 andDefense:45];
Run Code Online (Sandbox Code Playgroud)
我知道删除alloc部分是一个简单的愚蠢的事情,但它使代码更具可读性,所以我更喜欢这样做.我最初尝试改变我的方法
-(id)initWithAttack:(int) a andDefense:(int) d
Run Code Online (Sandbox Code Playgroud)
至
-(id)monsterWithAttack:(int) a andDefense:(int) d
Run Code Online (Sandbox Code Playgroud)
然后改变我的self = [super init],self = [[super alloc] init];但显然不起作用!有任何想法吗?
我正在为iphone制作RPG游戏.我想要做的是有一个库存,只允许您持有特定数量的特定类型的项目.我想这样做我只是扩展NSMutableArray并添加限制.我无法弄清楚这样做的最好方法.这是我头脑中的想法......
Backpack.h
@interface Backpack : NSMutableArray {
Class * arrayClass;
NSMutableArray * array;
int limit;
}
-(id) initWithClass:(Class) type andLimit:(int) num;
@end
Run Code Online (Sandbox Code Playgroud)
Backpack.m
@implementation Backpack
-(id)initWithClass:(Class) type andLimit:(int) num {
arrayClass = type;
limit = num;
array = [NSMutableArray new];
return self;
}
-(void)insertObject:(id) object atIndex:(int) index {
if([object isKindOfClass:arrayClass] && index < limit) {
// Insert it
} else {
// Throw Exception
}
}
@end
Run Code Online (Sandbox Code Playgroud)