Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 511925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:16:02+00:00 2026-05-13T07:16:02+00:00

To help gather a sense of Objective-C I’m creating a very basic connect 4

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:16:02+00:00Added an answer on May 13, 2026 at 7:16 am

    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:

    board = [[Board alloc] init];
    player = [[Player alloc] initWithBoard: board];
    computer = [[ComputerPlayer alloc] initWithBoard: board];
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Help! I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to
Help! I have an Axis web service that is being consumed by a C#
HELP! I just setup a virtual host for two sites that have a lot
Please help! I'm really at my wits' end. My program is a little personal
Please help! Background info I have a WPF application which accesses a SQL Server
Please help clarifying : In web.xml I have the following <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored>false</el-ignored>
Help me ..my page index is not working in visual studio. my page load
Help! I am using jQuery to make an AJAX call to fill in a
help me to manage IIS (what i am trying to do is create virtual
Help I am busy making changes to a type library in a Datasnap project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.