Andrew Robinson
Andrew Robinson

Reputation: 3424

Adding an @property in an NSObject as a pointer to an @property in another NSObject

I have a 1v1 game that I am updating to 3 players (eventually 4) and I am running into an issue with player tracking when the user is playing against 2 computer players.

I have a Game NSObject that handles all of the major code for the gameplay, and also controls the actions of the computer players. In the past, I have been good with 2 properties in Game.h (User and Opponent), and now I have 3 (User, Opponent, Opponent2). Declared like the following:

@property (retain) Player *user;

The problem comes in when I need to use a method that controls computer player actions. As an example, when the computer player needs to bid, it must know the bids and some other info about it's opponents. A snippet of the old code is:

- (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
}

In order to get information for the opposing players, a thought I had was to add 2 new properties to Player.h in order to track each player's opponents (opponent1 and opponent2). My question is, if I set the properties for each Player as in:

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

Can I refer to them and/or set their individual properties in both contexts? Would these statements be equivalent given the above:

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

Also, could I access the Player objects properties like this new version of the bidding code snippet?

- (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.
}

Am I way off base/off track? Is there an easier way to do this?

Upvotes: 1

Views: 64

Answers (1)

Dan Shelly
Dan Shelly

Reputation: 6011

This is indeed equivalent.

But ...

The didBid will be set by the other players as well (as they hold the same reference to that player, overwriting what each player has set.

You are creating a retain cycle you might need to break manually (or set the opponents properties as weak).

It might be best if each player will keep a "Strategy" object for each opponent, and that strategy will have a reference to the player. This way, each player may have different strategy against any other player.

Upvotes: 1

Related Questions