我对Objective C非常陌生,并且正在从事一个项目,并且我正在努力解决传递引用问题,这在我的代码中可能很明显。如果有人可以就我的代码为我分解它,我将不胜感激。
@interface Player : NSObject
{
}
//@property
@property (nonatomic, readwrite) NSString* name;
@property (nonatomic, readwrite) NSInteger *humanPoints;
@property (nonatomic, readwrite) NSInteger *computerPoints;
@property (nonatomic, readwrite) BOOL isWin;
@property (nonatomic, readwrite) BOOL isLoss;
@property (nonatomic, readwrite) BOOL isPoints;
@property (nonatomic, readwrite) NSInteger rollAgain;
//Methods
-(id) init;
-(void) RollFirstDie: (NSInteger) firstDie andSecondDie: (NSInteger) secondDie andThirdDie: (NSInteger) thirdDie;
-(NSInteger) GetRandomRoll;
-(BOOL) DoRoll;
@end
用它的.m
#import "Player.h"
@implementation Player
@synthesize name;
@synthesize humanPoints;
@synthesize computerPoints;
@synthesize isWin;
@synthesize isLoss;
@synthesize isPoints;
@synthesize rollAgain;
-(id) init
{
self = [super init];
if (self!=NULL)
{
name = @"human";
humanPoints = 0;
computerPoints = 0;
}
return self;
}
-(void)RollFirstDie: (NSInteger) firstDie andSecondDie: (NSInteger) secondDie andThirdDie: (NSInteger) thirdDie
{
firstDie = arc4random()% 6+1;
secondDie = arc4random()% 6+1;
thirdDie = arc4random()% 6+1;
}
-(NSInteger) GetRandomRoll
{
die1 = firstDie;
die2 = secondDie;
die3 = thirdDie;
return die1, die2, die3;
}
-(BOOL) DoRoll
{
NSLog(@"Human Roll Dice?[Y/N] ");
scanf(" %ld", &rollAgain);
return rollAgain;
}
我遇到的最大麻烦是第二个.h/.m:
#import <Foundation/Foundation.h>
#import "Player.h"
@interface DiceGame : NSObject
{
NSInteger die1;
NSInteger die2;
NSInteger die3;
//Player* human
//Player* cpu
BOOL rollAgain;
}
//Methods
-(id) init;
-(void) Play;
-(void) CheckRollConditions;
-(void) ResetDice;
-(void) DisplayWinner;
-(void) TakeTurn;
@end
.m
#import "DiceGame.h"
@implementation DiceGame
-(id) init
{
self = [super init];
if (self!=NULL)
{
die1 = 0;
die2 = 0;
die3 = 0;
}
return self;
}
-(void) Play
{
[self TakeTurn];
if (name isEqual @"human")
{
[self RollFirstDie: firstDie andSecondDie: secondDie andThirdDie: thirdDie];
[self CheckRollConditions];
if (rollAgain == 1)
{
[self RollFirstDie: firstDie andSecondDie: secondDie andThirdDie: thirdDie]
[self CheckRollConditions];
}
name = @"computer";
}
[self ResetDice ];
while (computerPoints == 0)
{
[self RollFirstDie: firstDie andSecondDie: secondDie andThirdDie: thirdDie]
[self CheckRollConditions];
while (computerPoints == humanPoints)
{
[self RollFirstDie: firstDie andSecondDie: secondDie andThirdDie: thirdDie]
[self CheckRollConditions];
}
}
[self DisplayWinner ];
[self GetRandomRoll];
}
-(void) CheckRollConditions
{
//Three of a Kind
if ((die1 == die2) && (die1 == die3))
{
NSLog(@"You got 3 of a kind, you win!");
}
//Two of a Kind
else if ((die1 == die2) || (die2 == die3) || (die1 == die3))
{
if (die1 == die2)
{
if (Player.name isEqual @"human")
{
humanPoints = die3;
}
else
{
computerPoints = die3;
}
}
else if (die1 == die3)
{)
if (name isEqual @"human")
{
humanPoints = die2;
}
else
{
computerPoints = die2;
}
}
else
{
if (name isEqual @"human")
{
humanPoints = die1;
}
else
{
computerPoints = die1;
}
}
if (name isEqual @"human")
{
NSLog(@"You got a pair, your score is: %ld", humanPoints);
}
else
{
NSLog(@"Computer got a pair, their score is: %ld", computerPoints);
}
}
//Lose Sequence
else if (((die1 != die2) && (die1 != die3) && (die2 != die3)) && ((die1 <= 3) && (die2 <= 3) && (die3 <= 3)))
{
NSLog(@"Game Over, %@ Lose!", name);
}
//Win Sequence
else if (((die1 != die2) && (die1 != die3) && (die2 != die3)) && ((die1 > 3) && (die2 > 3) && (die3 > 3)) && ((die1 <= 6) && (die2 <=6) && (die3 <= 6)))
{
NSLog(@"Congratulations! %@ Wins!", name);
}
//No win or lose
else
{
while (name isEqual @"human")
{
NSLog(@"You did not get a sequence or a pair");
NSLog(@"Human, roll again?[y/n]");
scanf("%ld", &rollAgain);break;
}
}
}
-(void) ResetDice
{
die1 = 0;
die2 = 0;
die3 = 0;
}
-(void) DisplayWinner
{
if (computerPoints > humanPoints)
{
NSLog(@"Computer had a higher score");
}
else
{
NSLog(@"Human had a high score");
}
}
-(void) TakeTurn
{
if ([name isEqual: @"human"])
{
[self RollFirstDie: firstDie andSecondDie: secondDie andThirdDie: thirdDie]
NSLog(@"Rolling...");
NSLog(@"You rolled: ");
NSLog(@"%ld, %ld, %ld", die1, die2, die3);
}
else
{
{
[self RollFirstDie: firstDie andSecondDie: secondDie andThirdDie: thirdDie]
NSLog(@"Rolling...");
NSLog(@"Computer rolled: ");
NSLog(@"%ld, %ld, %ld", die1, die2, die3);
}
}
}
@end
主要:
#import <Foundation/Foundation.h>
#import "DiceGame.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
DiceGame* roll = [[DiceGame alloc] init];
[roll Play];
}
return 0;
}
所以发生的事情是 DiceGame.m 无法识别 Player 类中的任何内容。我知道我想要代码做什么,但我正在努力实现它。(因为这已经是一篇很长的帖子,所以我省略了一些后来我没有遇到问题的方法)
编辑:谢谢你们的帮助,我已经用完整的代码更新了这个,但是你是对的,它没有编译,这就是我现在需要的帮助。Pass By Reference 问题并没有我最初想象的那么严重。我相信我现在遇到的错误是同一问题的一部分: 1. 使用未声明的已识别“variableName”(die1/die2/die3 等)。我让它们在一个单独的方法中定义,但我认为这不应该是一个问题?不知道我是错的还是我错过了一些重要的东西
- 'DiceGame' 没有可见的@interface 声明选择器'GetRandomRoll'(和其他)
我认为只有一个类可以从 NSObject 继承,但我似乎不知道如何修复我的代码以使其工作