To help gather a sense of Objective-C I’m creating a very basic connect 4 game sans Cocoa.
In my program I have three modules:
- “Game”: Contains an array that holds the board information. Object that is created within main, and is responsible for turns. Player and Computer objects live within this module.
- “Player”: Holds a function that inserts a player’s piece into the preferred column (this module exists for the purpose of encapsulation, and only that).
- “Computer”: Holds functions that determine, based on current board setup, where the computer should move, and then places a piece in that location.
Ideally I would like for the Player and Computer classes to be able to edit the same instance of pieceLoc that lives within Game through some sort of inheritance, however I am not sure how to do this.
Here’s a snippet of what I currently am thinking about:
// startGame exists within the "Game" module
char *pieceLoc[42]; // This is a *temporary* global var. I'm still learning about
// the best way to implement this with class and instance
// methods. This is the array that holds the board info.
-(void) startGame {
Player *player =[[Player alloc] init]; // player's symbol = 'X'
Computer *computer =[[Computer alloc] init]; // computer's symbol = 'O'
int i = 0;
while ([game hasPieceWon: 'X'] == NO && [game hasPieceWon: 'O'] == NO) {
//function for player move
if ([game hasPieceWon: 'X'] == NO) {
//function for computer move
}
}
if ([game hasPieceWon: 'X'] == YES)
// Tell the player they've won
else
// Tell the player the computer has won.
}
The functions used for player and computer moves are the functions that would need to somehow obtain access to the array pieceLoc (which will in the future exist as a instance variable, once I learn more about class vs. instance methods). pieceLoc currently exists as a char *, in case I do have to pass it via function parameters.
I feel as if this is a fairly simple problem with how I’m thinking about OOP, but I was unable to find an answer to what I was looking for despite searching for most of the afternoon. From what I’ve gathered my question relates to class composition, but I couldn’t find a good resource about that for Objective-C.
So, again: I am looking for a way to pass a single instance of pieceLoc that lives within “the parent class” Game to two “children classes” where pieceLoc can be directly manipulated without using additional parameters.
If passing the array as a parameter does end up being the more idiomatic thing to do, would I be able to get an example of how passing by reference works in Objective-C?
Thanks for the help!
As you have probably already figured out, Objective-C doesn’t really have “class variables” that you can inherit from a super class.
One way to construct this such that you don’t have to have global variable would be something like this:
You have a Game object that holds references to each of the Players, and to the Board. The Players are either each initialized with a reference to the Board. Your startGame method (in Game) would have code like this:
in the Board class, you’d have methods to query the state of the board, make a move, determine whether a move is legal, and maybe whether or not a player has won (though the game-rule-related logic might go in Game).
In the initWithBoard methods in Player and ComputerPlayer, you’d just retain the board instance, and use it to communicate the moves each player decides to make. The Game would establish the order of moves, and keep track of any global state. When the Player and ComputerPlayer are released at the end of the game, they’d release their reference to the board in their dealloc method.