linked list - Handling player on turn with Objective C -
i'm creating simple app has list of characters , list of (4) players, players reference playable character.
i'm stuck on how following:
- make reference current player on turn
- find out next player on turn is
- handling last player return first player on turn.
ideally, able after, first, last before commands on nsmutablearray, of these i'm able lastobject, there no firstobject, afterobject, etc.
i believe can fake before,after,first commands objectatindex; ideally not want rely on numeric references because incorrect -- if mutable, size change.
typically, able following in pseudocode.
global player_on_turn.player = null //player_on_turn pointer player object ; handle next player on turn if (player_on_turn.player = null) error("no player on turn assigned") if (sizeof[playerlist]==0) error("there no players in game") if after player_on_turn = null ; reset player_on_turn.player = first player else ; move next player on turn player_on_turn.player = after player_on_turn.player end if
with in mind, best strategy handle player on turn concept described in 1-2-3 example above.
thanks
it doesn’t matter data structure you’re using - @ level have rely on numerical index (except if using linked lists). , alright. if don’t want use in main game implementation alright, too. create class encapsulates things. first think of operations need support. guess here those:
- (playerobject *) currentplayer; - (void) startnextturn;
if have can write game using primitives , worry how best implement later. can change @ time without breaking actual game.
my recommendation this:
- (playerobject *) currentplayer; { return [players objectatindex: currentplayerindex]; } - (void) startnextturn; { ++currentplayerindex; if (currentplayerindex >= [players count]) { currentplayerindex = 0; } }
using index there ok, if array mutable. if have methods change player
array can take care of necessary changes currentplayerindex
.
having object makes easy write unit tests. global variable suggest in pseudo-code makes impossible write meaningful unit tests.
Comments
Post a Comment