2013-04-24 1428 views
1

我有一个1v1的比赛,我更新到3名球员(最终4)和我正在与玩家跟踪一个问题,当用户打2个电脑玩家。添加在一个NSObject的@property作为一个指针@property在另一个NSObject的

我有一个游戏NSObject处理游戏的所有主要代码,也控制着电脑玩家的行为。在过去,我在Game.h(用户和对手)中拥有2个属性,现在我有3个(用户,对手,对手2)。声明如下:

@property (retain) Player *user; 

问题出现在我需要使用控制计算机播放器操作的方法时。作为一个例子,当电脑玩家需要出价时,它必须知道出价和其他有关它的对手的信息。旧的代码片段是:

- (void)computerBiddingObjects { 
     // ... other stuff 
    if (_opponentPoints == 110 && _userPoints >= 90 && _userDidBid != YES) { 
      bid = bidTwenty; 
    } else if (_opponentPoints == 115 && _userPoints >= 90 && _userDidBid != YES) { 
      bid = bidTwentyFive; 
    } 
     // ... other stuff 
} 

为了得到对对方球员的信息,一想到我是添加两个新的属性,以Player.h以便跟踪每个玩家的对手(opponent1和opponent2 )。我的问题是,如果我为每一个球员的属性,如:

_opponent.opponent = _user; 
_opponent.opponent2 = _opponent2; 

我可以指给他们,并/或在两种情况下设置它们的各个属性?将给出上述这些语句是等价的:

_opponent.opponent.didBid = YES; 
_user.didBid = YES; 

而且,我可以访问Player对象的属性,如投标代码片段的新版本?

- (void)computerBiddingObjectsForOpponent:(Player *)opponent { 
     // ... other stuff 
    if (opponent.points == 110 && self.currentBid < bidTwenty && ((opponent.opponent1.points >= 90 && opponent.opponent1.didBid != YES) || (opponent.opponent2.points >= 90 && opponent.opponent2.didBid != YES))) { 
      bid = bidTwenty; 
    } else if (opponent.points == 115 && self.currentBid < bidTwentyFive && ((opponent.opponent1.points >= 90 && opponent.opponent1.didBid != YES) || (opponent.opponent2.points >= 90 && opponent.opponent2.didBid != YES))) { 
      bid = bidTwentyFive; 
    } 
     // ... other stuff 
    opponent.bid = bid. 
} 

我离开基地/偏离轨道吗?有没有更简单的方法来做到这一点?

回答

1

这确实是等同的。

但是......

didBid会被其他球员也被设置(因为他们持同样参照该播放器,覆盖了每个球员设置。

您正在创建保留周期您可能需要手动分手(或将对手属性设置为weak

如果每个玩家都会为每个对手保留一个“策略”对象,并且该策略会引用这样,每个玩家都可以拥有针对其他玩家的不同策略。

+0

所以,只要而不是播放器中的对手对象的对象本身,我想有他们在一个Stategy对象? – 2013-04-25 03:02:53

+0

您对每个球员的战略信息(玩家参考仅供鉴定,并获得玩家当前状态为[只读])。 ('Player'具有'strategies'(每对手)的每一个具有参考另一'Player'和关于该玩家内部'Strategy'信息) – 2013-04-25 03:19:44

相关问题