0

I'm trying to create a simple commandline tic-tac-toe game using an NSMutableArray.

Created a class called "Board" with the method "getPosition" (I'm assuming this is the best way to get a user input) I'm asking for position, then casting from int to NSUInteger)

#import "Board.h"

@implementation Board

-(void)getPosition;
{
    int enteredPosition;
    scanf("%i", &enteredPosition);
    NSUInteger nsEnteredPosition = (NSUInteger ) enteredPosition;
    NSLog(@"Position = %lu", (unsigned long)nsEnteredPosition);
}



#import <Foundation/Foundation.h>
#import "Board.h"


int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSString *currentPlayer;
        NSMutableArray *gameBoard=[[NSMutableArray alloc] initWithCapacity:9];

        for(int i; i<=2; i++)
        {
            if(i %2)
            {
                currentPlayer=@"X";
            }
            else
            {
                currentPlayer=@"O";
            }

        NSLog(@"Player %@, select an open spot 1 - 9 on the board", currentPlayer);
        Board *currentPosition = [[Board alloc] init];
        [currentPosition getPosition];
        [gameBoard insertObject:currentPlayer atIndex:currentPosition]; //this is where i have a problem
        }

As I understand it atIndex requires an NSUInteger parameter, but I'm receiving the error message:

"Incompatible pointer to integer conversion sending 'Board *_strong" to parameter of type 'NSUInteger' (aka 'unassigned long')

Kara
  • 6,115
  • 16
  • 50
  • 57
zamanfu
  • 1
  • 1

1 Answers1

1

You're using currentPosition as your index which is a Board object. Perhaps [currentPosition getPosition] is supposed to return an NSUInteger. If so, try rewriting the last portion of your code like this:

Board *theBoard = [[Board alloc] init];
NSUInteger currentPosition = [theBoard getPosition];
[gameBoard insertObject:currentPlayer atIndex:currentPosition]; //this is where i have a problem
Casey Fleser
  • 5,707
  • 1
  • 32
  • 43
  • Thanks @somegeekintn, however when I change the code to that, I receive the following error: "Initializing 'NSUInteger" (aka 'unsigned long') with and expression of incompatible type void. Is it because I've defined method in Board class to return (void)? I tried changing it but doesn't seem to help. – zamanfu Nov 19 '14 at 20:28
  • Perhaps. It might be helpful to amend the question showing the source for the Board class as well. – Casey Fleser Nov 19 '14 at 20:31
  • Nevermind. I think I fix it now. I needed to change the getPosition method type from (void) to (int) both my class header and implementation files. – zamanfu Nov 19 '14 at 20:36