Title says it all really (almost!). Well I have been reading various books and such and I decided to try and incorporate all the things I have been learning about into one accomplishable task yet hard at the same time (for example planning the logic and designing it ect...)
So I have the board printed out and the "didWin" boolean method however I have come across one error that I can't seem to get around!
It says 'move' undeclared (first use in this function) despite having a
return move;
line in my code after 'move' is assigned?
Also, I was wondering if anyone could help me solve the problem of listing ALL the possible ways of winning?
Thanks!
Arcadiu
P.S Could you also point out things that aren't so good (waste of code) and what is good or could be done better. Thanks!
Code:
#import <Cocoa/Cocoa.h>
@interface Board : NSObject
{
char square1;
char square2;
char square3;
char square4;
char square5;
char square6;
char square7;
char square8;
char square9;
BOOL didWin;
int move;
}
-(void) print;
-(BOOL) didWin;
-(int) getMove;
-(void) setSquare: (int) squareNumber;
@end
@implementation Board
-(void) setSquare: (int) squareNumber
{
switch (move) {
case 1:
square1 = 'X';
break;
case 2:
square2 = 'X';
break;
case 3:
square3 = 'X';
break;
case 4:
square4 = 'X';
break;
case 5:
square5 = 'X';
break;
case 6:
square6 = 'X';
break;
case 7:
square7 = 'X';
break;
case 8:
square8 = 'X';
break;
case 9:
square9 = 'X';
break;
}
}
-(int) getMove
{
NSLog(@"Please enter the square you would like to place an 'X' on!");
scanf("%i", &move);
return move;
}
-(void) print
{
square1 = '1';
square2 = '2';
square3 = '3';
square4 = '4';
square5 = '5';
square6 = '6';
square7 = '7';
square8 = '8';
square9 = '9';
NSLog(@"----|----|----");
NSLog(@" %c | %c | %c ", square1, square2, square3);
NSLog(@"----|----|----");
NSLog(@" %c | %c | %c ", square4, square5, square6);
NSLog(@"----|----|----");
NSLog(@" %c | %c | %c ", square7, square8, square9);
NSLog(@"----|----|----");
}
-(BOOL) didWin
{
if (square1 == 'X' && square2 == 'X' && square3 == 'X')
return didWin = TRUE;
else
return 0;
}
@end
int main()
{
Board *myGame = [[Board alloc] init];
do{
[myGame print];
[myGame getMove];
[myGame setSquare: move]; //<-------BUG HERE
[myGame didWin];
}
while ([myGame didWin] == FALSE);
if ([myGame didWin] == TRUE)
{
NSLog(@"You have won the game!!!");
return 0;
}
return 0;
}