私は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;
}
私が最も問題を抱えているのは、2 番目の .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 クラスから何も認識しないということです。コードで何をしたいのかはわかっていますが、そこに到達するのに苦労しています。(そして、すでに非常に長い投稿であるため、問題がなかった後の方法のいくつかを省略しました)
編集:すべての助けをありがとう、私はこれを完全なコードで更新しましたが、あなたは正しいです、それは私が今助けを必要としているものです。参照渡しは、当初思っていたよりも問題が少なくなりました。私が今得ているエラーは、同じ問題の一部であると信じています: 1. 宣言されていない識別された「変数名」(die1/die2/die3 など) の使用。私はそれらを別の方法で定義していますが、それは問題になるべきではないと思いましたか? 私が間違っていたのか、そこに重要な何かが欠けているのかわからない
- 「DiceGame」の目に見える @interface は、セレクター「GetRandomRoll」(およびその他) を宣言しません
NSObjectから継承できるクラスは1つだけだと思いますが、コードを修正してそれを機能させる方法がわかりません