Objective-C Student Project: Calculating 8x6 Connect4 wins?

Status
Not open for further replies.

norton50

Distinguished
Aug 11, 2011
3
0
18,510
I am looking for a way to check if there is a win in my objective-c IOS application. Everything works, but I don't have a function to calculate the win possibilities. The positions are stored in a multidimensional array of array[x][y]. If there is no chip, it is valued at =0, red =1, and yellow=2. Does anyone know how to use some sort of method to calculate and check if there are 4 of the same colour in a row either horizontally vertically or diagonally. Skip to line 160 of ViewController.m :hello:

Thanks for any help! :p



Complete code:

ViewController.h
[cpp]
//
// ViewController.h
// Connect4
//
// Created by Matthew Cooke on 12-12-18.
// Copyright (c) 2012 Programming 12. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {





IBOutlet UIButton * button1;
IBOutlet UIButton * button2;
IBOutlet UIButton * button3;
IBOutlet UIButton * button4;
IBOutlet UIButton * button5;
IBOutlet UIButton * button6;
IBOutlet UIButton * button7;
IBOutlet UIButton * button8;



UIImageView *image1;//had to use separate images as an array wouldn't work.
UIImageView *image2;
UIImageView *image3;
UIImageView *image4;
UIImageView *image5;
UIImageView *image6;
UIImageView *image7;
UIImageView *image8;
UIImageView *image9;
UIImageView *image10;
UIImageView *image11;
UIImageView *image12;
UIImageView *image13;
UIImageView *image14;
UIImageView *image15;
UIImageView *image16;
UIImageView *image17;
UIImageView *image18;
UIImageView *image19;
UIImageView *image20;
UIImageView *image21;
UIImageView *image22;
UIImageView *image23;
UIImageView *image24;
UIImageView *image25;
UIImageView *image26;
UIImageView *image27;
UIImageView *image28;
UIImageView *image29;
UIImageView *image30;
UIImageView *image31;
UIImageView *image32;
UIImageView *image33;
UIImageView *image34;
UIImageView *image35;
UIImageView *image36;
UIImageView *image37;
UIImageView *image38;
UIImageView *image39;
UIImageView *image40;
UIImageView *image41;
UIImageView *image42;
UIImageView *image43;
UIImageView *image44;
UIImageView *image45;
UIImageView *image46;
UIImageView *image47;
UIImageView *image48;






}

//@property (nonatomic, retain) IBOutlet UIImageView* imageView;
@property (nonatomic) IBOutlet UIButton * button1;
@property (nonatomic) IBOutlet UIButton * button2;
@property (nonatomic) IBOutlet UIButton * button3;
@property (nonatomic) IBOutlet UIButton * button4;
@property (nonatomic) IBOutlet UIButton * button5;
@property (nonatomic) IBOutlet UIButton * button6;
@property (nonatomic) IBOutlet UIButton * button7;
@property (nonatomic) IBOutlet UIButton * button8;
@property (nonatomic) IBOutlet UIImageView *image1;
@property (nonatomic) IBOutlet UIImageView *image2;
@property (nonatomic) IBOutlet UIImageView *image3;
@property (nonatomic) IBOutlet UIImageView *image4;
@property (nonatomic) IBOutlet UIImageView *image5;
@property (nonatomic) IBOutlet UIImageView *image6;
@property (nonatomic) IBOutlet UIImageView *image7;
@property (nonatomic) IBOutlet UIImageView *image8;
@property (nonatomic) IBOutlet UIImageView *image9;
@property (nonatomic) IBOutlet UIImageView *image10;
@property (nonatomic) IBOutlet UIImageView *image11;
@property (nonatomic) IBOutlet UIImageView *image12;
@property (nonatomic) IBOutlet UIImageView *image13;
@property (nonatomic) IBOutlet UIImageView *image14;
@property (nonatomic) IBOutlet UIImageView *image15;
@property (nonatomic) IBOutlet UIImageView *image16;
@property (nonatomic) IBOutlet UIImageView *image17;
@property (nonatomic) IBOutlet UIImageView *image18;
@property (nonatomic) IBOutlet UIImageView *image19;
@property (nonatomic) IBOutlet UIImageView *image20;
@property (nonatomic) IBOutlet UIImageView *image21;
@property (nonatomic) IBOutlet UIImageView *image22;
@property (nonatomic) IBOutlet UIImageView *image23;
@property (nonatomic) IBOutlet UIImageView *image24;
@property (nonatomic) IBOutlet UIImageView *image25;
@property (nonatomic) IBOutlet UIImageView *image26;
@property (nonatomic) IBOutlet UIImageView *image27;
@property (nonatomic) IBOutlet UIImageView *image28;
@property (nonatomic) IBOutlet UIImageView *image29;
@property (nonatomic) IBOutlet UIImageView *image30;
@property (nonatomic) IBOutlet UIImageView *image31;
@property (nonatomic) IBOutlet UIImageView *image32;
@property (nonatomic) IBOutlet UIImageView *image33;
@property (nonatomic) IBOutlet UIImageView *image34;
@property (nonatomic) IBOutlet UIImageView *image35;
@property (nonatomic) IBOutlet UIImageView *image36;
@property (nonatomic) IBOutlet UIImageView *image37;
@property (nonatomic) IBOutlet UIImageView *image38;
@property (nonatomic) IBOutlet UIImageView *image39;
@property (nonatomic) IBOutlet UIImageView *image40;
@property (nonatomic) IBOutlet UIImageView *image41;
@property (nonatomic) IBOutlet UIImageView *image42;
@property (nonatomic) IBOutlet UIImageView *image43;
@property (nonatomic) IBOutlet UIImageView *image44;
@property (nonatomic) IBOutlet UIImageView *image45;
@property (nonatomic) IBOutlet UIImageView *image46;
@property (nonatomic) IBOutlet UIImageView *image47;
@property (nonatomic) IBOutlet UIImageView *image48;



- (IBAction)contentModeChanged:(UISegmentedControl*)segmentedControl;

@end
[/cpp]

ViewController.m
[cpp]//
// ViewController.m
// Connect4
//
// Created by Block B-F on 12-12-18.
// Copyright (c) 2012 Programming 12. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController;

@synthesize image1;
@synthesize image2;
@synthesize image3;
@synthesize image4;
@synthesize image5;
@synthesize image6;
@synthesize image7;
@synthesize image8;
@synthesize image9;
@synthesize image10;
@synthesize image11;
@synthesize image12;
@synthesize image13;
@synthesize image14;
@synthesize image15;
@synthesize image16;
@synthesize image17;
@synthesize image18;
@synthesize image19;
@synthesize image20;
@synthesize image21;
@synthesize image22;
@synthesize image23;
@synthesize image24;
@synthesize image25;
@synthesize image26;
@synthesize image27;
@synthesize image28;
@synthesize image29;
@synthesize image30;
@synthesize image31;
@synthesize image32;
@synthesize image33;
@synthesize image34;
@synthesize image35;
@synthesize image36;
@synthesize image37;
@synthesize image38;
@synthesize image39;
@synthesize image40;
@synthesize image41;
@synthesize image42;
@synthesize image43;
@synthesize image44;
@synthesize image45;
@synthesize image46;
@synthesize image47;
@synthesize image48;






//@synthesize imageView;

@synthesize button1, button2, button3, button4, button5, button6, button7, button8;







int x1 = 0; //unactivated
int x2 = 0;
int x3 = 0;
int x4 = 0;
int x5 = 0;
int x6 = 0;
int x7 = 0;
int x8 = 0;
int x9 = 0;
int x10 = 0;
int x11 = 0;
int x12 = 0;
int x13 = 0;
int x14 = 0;
int x15 = 0;
int x16 = 0;
int x17 = 0;
int x18 = 0;
int x19 = 0;
int x20 = 0;
int x21 = 0;
int x22 = 0;
int x23 = 0;
int x24 = 0;
int x25 = 0;
int x26 = 0;
int x27 = 0;
int x28 = 0;
int x29 = 0;
int x30 = 0;
int x31 = 0;
int x32 = 0;
int x33 = 0;
int x34 = 0;
int x35 = 0;
int x36 = 0;
int x37 = 0;
int x38 = 0;
int x39 = 0;
int x40 = 0;
int x41 = 0;
int x42 = 0;
int x43 = 0;
int x44 = 0;
int x45 = 0;
int x46 = 0;
int x47 = 0;
int x48 = 0;





int i = -1;
int row1 = 0;
int row2 = 0;
int row3 = 0;
int row4 = 0;
int row5 = 0;
int row6 = 0;
int row7 = 0;
int row8 = 0;
int x = 0;
int winArray[9][7];
bool win = 0;

UIImage * red;
UIImage * yellow;





-(int)checkWin{

for (int c=1; c<=8; c++){

for (int r=1; r<=6; r++){

//....*******HELP*******HELP*******HELP*******HELP********HELP********HELP********HELP********HELP********HELP********HELP

}

}




return win;
}










-(int)whatImage{
i++;


if(x==1){// skip to the ------ line.
if (i%2==1){
[image1 setImage:[UIImage imageNamed: @"red.png"]];
[image1 setHidden: NO];
////[self.view addSubview: image1];
////[self.view bringSubviewToFront: image1];
x1=1;//red's

}
else if (i%2==0){
[image1 setImage:[UIImage imageNamed: @"yellow.png"]];
[image1 setHidden: NO];
// //[self.view addSubview: image1];
// //[self.view bringSubviewToFront: image1];
x1=2;//yellow's

}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[1][1] = x1;
}

else if(x==2){
if (i%2==1){
[image2 setImage:[UIImage imageNamed: @"red.png"]];
[image2 setHidden: NO];
////[self.view addSubview: image2];
//[self.view bringSubviewToFront: image2];
x2=1;

}
else if (i%2==0){
[image2 setImage:[UIImage imageNamed: @"yellow.png"]];
[image2 setHidden: NO];
////[self.view addSubview: image2];
//[self.view bringSubviewToFront: image2];
x2=2;

}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[2][1] = x2;
}
else if(x==3){
if (i%2==1){
[image3 setImage:[UIImage imageNamed: @"red.png"]];
[image3 setHidden: NO];
////[self.view addSubview: image3];
//[self.view bringSubviewToFront: image3];
x3=1;

}
else if (i%2==0){
[image3 setImage:[UIImage imageNamed: @"yellow.png"]];
[image3 setHidden: NO];
////[self.view addSubview: image3];
//[self.view bringSubviewToFront: image3];
x3=2;

}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[3][1] = x3;
}
else if(x==4){
if (i%2==1){
[image4 setImage:[UIImage imageNamed: @"red.png"]];
[image4 setHidden: NO];
////[self.view addSubview: image4];
//[self.view bringSubviewToFront: image4];
x4=1;

}
else if (i%2==0){
[image4 setImage:[UIImage imageNamed: @"yellow.png"]];
[image4 setHidden: NO];
////[self.view addSubview: image4];
//[self.view bringSubviewToFront: image4];
x4=2;

}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[4][1] = x4;
}

else if(x==5){
if (i%2==1){
[image5 setImage:[UIImage imageNamed: @"red.png"]];
[image5 setHidden: NO];
////[self.view addSubview: image5];
//[self.view bringSubviewToFront: image5];
x5=1;
winArray[5][1] = 1;
}
else if (i%2==0){
[image5 setImage:[UIImage imageNamed: @"yellow.png"]];
[image5 setHidden: NO];
////[self.view addSubview: image5];
//[self.view bringSubviewToFront: image5];
x5=2;
winArray[5][1] = 2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[5][1] = x5;
}
else if(x==6){
if (i%2==1){
[image6 setImage:[UIImage imageNamed: @"red.png"]];
[image6 setHidden: NO];
////[self.view addSubview: image6];
//[self.view bringSubviewToFront: image6];
x6=1;

}
else if (i%2==0){
[image6 setImage:[UIImage imageNamed: @"yellow.png"]];
[image6 setHidden: NO];
////[self.view addSubview: image6];
//[self.view bringSubviewToFront: image6];
x6=2;
;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[6][1] = x6;
}

else if(x==7){
if (i%2==1){
[image7 setImage:[UIImage imageNamed: @"red.png"]];
[image7 setHidden: NO];
////[self.view addSubview: image7];
//[self.view bringSubviewToFront: image7];
x7=1;
winArray[7][1] = 1;
}
else if (i%2==0){
[image7 setImage:[UIImage imageNamed: @"yellow.png"]];
[image7 setHidden: NO];
////[self.view addSubview: image7];
//[self.view bringSubviewToFront: image7];
x7=2;
winArray[7][1] = 1;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];

}
winArray[7][1] = x7;
}
else if(x==8){
if (i%2==1){
[image8 setImage:[UIImage imageNamed: @"red.png"]];
[image8 setHidden: NO];
////[self.view addSubview: image8];
//[self.view bringSubviewToFront: image8];
x8=1;
}
else if (i%2==0){
[image8 setImage:[UIImage imageNamed: @"yellow.png"]];
[image8 setHidden: NO];
////[self.view addSubview: image8];
//[self.view bringSubviewToFront: image8];
x8=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[8][1] = x8;
}


else if(x==9){
if (i%2==1){
[image9 setImage:[UIImage imageNamed: @"red.png"]];
[image9 setHidden: NO];
////[self.view addSubview: image9];
//[self.view bringSubviewToFront: image9];
x9=1;
}
else if (i%2==0){
[image9 setImage:[UIImage imageNamed: @"yellow.png"]];
[image9 setHidden: NO];
////[self.view addSubview: image9];
//[self.view bringSubviewToFront: image9];
x9=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[1][2] = x9;
}

else if(x==10){
if (i%2==1){
[image10 setImage:[UIImage imageNamed: @"red.png"]];
[image10 setHidden: NO];
////[self.view addSubview: image10];
//[self.view bringSubviewToFront: image10];
x10=1;
}
else if (i%2==0){
[image10 setImage:[UIImage imageNamed: @"yellow.png"]];
[image10 setHidden: NO];
////[self.view addSubview: image10];
//[self.view bringSubviewToFront: image10];
x10=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[2][2] = x10;
}

else if(x==11){
if (i%2==1){
[image11 setImage:[UIImage imageNamed: @"red.png"]];
[image11 setHidden: NO];
////[self.view addSubview: image11];
//[self.view bringSubviewToFront: image11];
x11=1;
}
else if (i%2==0){
[image11 setImage:[UIImage imageNamed: @"yellow.png"]];
[image11 setHidden: NO];
////[self.view addSubview: image11];
//[self.view bringSubviewToFront: image11];
x11=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[3][2] = x11;
}

else if(x==12){
if (i%2==1){
[image12 setImage:[UIImage imageNamed: @"red.png"]];
[image12 setHidden: NO];
////[self.view addSubview: image12];
//[self.view bringSubviewToFront: image12];
x12=1;
}
else if (i%2==0){
[image12 setImage:[UIImage imageNamed: @"yellow.png"]];
[image12 setHidden: NO];
////[self.view addSubview: image12];
//[self.view bringSubviewToFront: image12];
x12=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[4][2] = x12;
}

else if(x==13){
if (i%2==1){
[image13 setImage:[UIImage imageNamed: @"red.png"]];
[image13 setHidden: NO];
////[self.view addSubview: image13];
//[self.view bringSubviewToFront: image13];
x13=1;
}
else if (i%2==0){
[image13 setImage:[UIImage imageNamed: @"yellow.png"]];
[image13 setHidden: NO];
////[self.view addSubview: image13];
//[self.view bringSubviewToFront: image13];
x13=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[5][2] = x13;
}

else if(x==14){
if (i%2==1){
[image14 setImage:[UIImage imageNamed: @"red.png"]];
[image14 setHidden: NO];
////[self.view addSubview: image14];
//[self.view bringSubviewToFront: image14];
x14=1;
}
else if (i%2==0){
[image14 setImage:[UIImage imageNamed: @"yellow.png"]];
[image14 setHidden: NO];
////[self.view addSubview: image14];
//[self.view bringSubviewToFront: image14];
x14=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[6][2] = x14;
}

else if(x==15){
if (i%2==1){
[image15 setImage:[UIImage imageNamed: @"red.png"]];
[image15 setHidden: NO];
////[self.view addSubview: image15];
//[self.view bringSubviewToFront: image15];
x15=1;
}
else if (i%2==0){
[image15 setImage:[UIImage imageNamed: @"yellow.png"]];
[image15 setHidden: NO];
////[self.view addSubview: image15];
//[self.view bringSubviewToFront: image15];
x15=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[7][2] = x15;
}

else if(x==16){
if (i%2==1){
[image16 setImage:[UIImage imageNamed: @"red.png"]];
[image16 setHidden: NO];
////[self.view addSubview: image16];
//[self.view bringSubviewToFront: image16];
x16=1;
}
else if (i%2==0){
[image16 setImage:[UIImage imageNamed: @"yellow.png"]];
[image16 setHidden: NO];
////[self.view addSubview: image16];
//[self.view bringSubviewToFront: image16];
x16=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[8][2] = x16;
}

else if(x==17){
if (i%2==1){
[image17 setImage:[UIImage imageNamed: @"red.png"]];
[image17 setHidden: NO];
////[self.view addSubview: image17];
//[self.view bringSubviewToFront: image17];
x17=1;
}
else if (i%2==0){
[image17 setImage:[UIImage imageNamed: @"yellow.png"]];
[image17 setHidden: NO];
////[self.view addSubview: image17];
//[self.view bringSubviewToFront: image17];
x17=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[1][3] = x17;
}

else if(x==18){
if (i%2==1){
[image18 setImage:[UIImage imageNamed: @"red.png"]];
[image18 setHidden: NO];
////[self.view addSubview: image18];
//[self.view bringSubviewToFront: image18];
x18=1;
}
else if (i%2==0){
[image18 setImage:[UIImage imageNamed: @"yellow.png"]];
[image18 setHidden: NO];
////[self.view addSubview: image18];
//[self.view bringSubviewToFront: image18];
x18=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[2][3] = x18;
}

else if(x==19){
if (i%2==1){
[image19 setImage:[UIImage imageNamed: @"red.png"]];
[image19 setHidden: NO];
////[self.view addSubview: image19];
//[self.view bringSubviewToFront: image19];
x19=1;
}
else if (i%2==0){
[image19 setImage:[UIImage imageNamed: @"yellow.png"]];
[image19 setHidden: NO];
////[self.view addSubview: image19];
//[self.view bringSubviewToFront: image19];
x19=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[3][3] = x19;
}

else if(x==20){
if (i%2==1){
[image20 setImage:[UIImage imageNamed: @"red.png"]];
[image20 setHidden: NO];
////[self.view addSubview: image20];
//[self.view bringSubviewToFront: image20];
x20 =1;
}
else if (i%2==0){
[image20 setImage:[UIImage imageNamed: @"yellow.png"]];
[image20 setHidden: NO];
////[self.view addSubview: image20];
//[self.view bringSubviewToFront: image20];
x20 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[4][3] = x20;
}

else if(x==21){
if (i%2==1){
[image21 setImage:[UIImage imageNamed: @"red.png"]];
[image21 setHidden: NO];
////[self.view addSubview: image21];
//[self.view bringSubviewToFront: image21];
x21 =1;
}
else if (i%2==0){
[image21 setImage:[UIImage imageNamed: @"yellow.png"]];
[image21 setHidden: NO];
//[self.view addSubview: image21];
//[self.view bringSubviewToFront: image21];
x21=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[5][3] = x21;
}

else if(x==22){
if (i%2==1){
[image22 setImage:[UIImage imageNamed: @"red.png"]];
[image22 setHidden: NO];
//[self.view addSubview: image22];
//[self.view bringSubviewToFront: image22];
x22 =1;
}
else if (i%2==0){
[image22 setImage:[UIImage imageNamed: @"yellow.png"]];
[image22 setHidden: NO];
//[self.view addSubview: image22];
//[self.view bringSubviewToFront: image22];
x22 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[6][3] = x22;
}

else if(x==23){
if (i%2==1){
[image23 setImage:[UIImage imageNamed: @"red.png"]];
[image23 setHidden: NO];
//[self.view addSubview: image23];
//[self.view bringSubviewToFront: image23];
x23 =1;
}
else if (i%2==0){
[image23 setImage:[UIImage imageNamed: @"yellow.png"]];
[image23 setHidden: NO];
//[self.view addSubview: image23];
//[self.view bringSubviewToFront: image23];
x23 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[7][3] = x23;
}

else if(x==24){
if (i%2==1){
[image24 setImage:[UIImage imageNamed: @"red.png"]];
[image24 setHidden: NO];
//[self.view addSubview: image24];
//[self.view bringSubviewToFront: image24];
x24 =1;
}
else if (i%2==0){
[image24 setImage:[UIImage imageNamed: @"yellow.png"]];
[image24 setHidden: NO];
//[self.view addSubview: image24];
//[self.view bringSubviewToFront: image24];
x24 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[8][3] = x24;
}

else if(x==25){
if (i%2==1){
[image25 setImage:[UIImage imageNamed: @"red.png"]];
[image25 setHidden: NO];
//[self.view addSubview: image25];
//[self.view bringSubviewToFront: image25];
x25 =1;
}
else if (i%2==0){
[image25 setImage:[UIImage imageNamed: @"yellow.png"]];
[image25 setHidden: NO];
//[self.view addSubview: image25];
//[self.view bringSubviewToFront: image25];
x25 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[1][4] = x25;
}

else if(x==26){
if (i%2==1){
[image26 setImage:[UIImage imageNamed: @"red.png"]];
[image26 setHidden: NO];
//[self.view addSubview: image26];
//[self.view bringSubviewToFront: image26];
x26 =1;
}
else if (i%2==0){
[image26 setImage:[UIImage imageNamed: @"yellow.png"]];
[image26 setHidden: NO];
//[self.view addSubview: image26];
//[self.view bringSubviewToFront: image26];
x26 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[2][4] = x26;
}

else if(x==27){
if (i%2==1){
[image27 setImage:[UIImage imageNamed: @"red.png"]];
[image27 setHidden: NO];
//[self.view addSubview: image27];
//[self.view bringSubviewToFront: image27];
x27 =1;
}
else if (i%2==0){
[image27 setImage:[UIImage imageNamed: @"yellow.png"]];
[image27 setHidden: NO];
//[self.view addSubview: image27];
//[self.view bringSubviewToFront: image27];
x27=2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[3][4] = x27;
}

else if(x==28){
if (i%2==1){
[image28 setImage:[UIImage imageNamed: @"red.png"]];
[image28 setHidden: NO];
//[self.view addSubview: image28];
//[self.view bringSubviewToFront: image28];
x28 =1;
}
else if (i%2==0){
[image28 setImage:[UIImage imageNamed: @"yellow.png"]];
[image28 setHidden: NO];
//[self.view addSubview: image28];
//[self.view bringSubviewToFront: image28];
x28 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[4][4] = x28;
}

else if(x==29){
if (i%2==1){
[image29 setImage:[UIImage imageNamed: @"red.png"]];
[image29 setHidden: NO];
//[self.view addSubview: image29];
//[self.view bringSubviewToFront: image29];
x29 =1;
}
else if (i%2==0){
[image29 setImage:[UIImage imageNamed: @"yellow.png"]];
[image29 setHidden: NO];
//[self.view addSubview: image29];
//[self.view bringSubviewToFront: image29];
x29 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[5][4] = x29;
}

else if(x==30){
if (i%2==1){
[image30 setImage:[UIImage imageNamed: @"red.png"]];
[image30 setHidden: NO];
//[self.view addSubview: image30];
//[self.view bringSubviewToFront: image30];
x30 =1;
}
else if (i%2==0){
[image30 setImage:[UIImage imageNamed: @"yellow.png"]];
[image30 setHidden: NO];
//[self.view addSubview: image30];
//[self.view bringSubviewToFront: image30];
x30 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[6][4] = x30;
}

else if(x==31){
if (i%2==1){
[image31 setImage:[UIImage imageNamed: @"red.png"]];
[image31 setHidden: NO];
//[self.view addSubview: image31];
//[self.view bringSubviewToFront: image31];
x31 =1;
}
else if (i%2==0){
[image31 setImage:[UIImage imageNamed: @"yellow.png"]];
[image31 setHidden: NO];
//[self.view addSubview: image31];
//[self.view bringSubviewToFront: image31];
x31 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[7][4] = x31;
}

else if(x==32){
if (i%2==1){
[image32 setImage:[UIImage imageNamed: @"red.png"]];
[image32 setHidden: NO];
//[self.view addSubview: image32];
//[self.view bringSubviewToFront: image32];
x32 =1;
}
else if (i%2==0){
[image32 setImage:[UIImage imageNamed: @"yellow.png"]];
[image32 setHidden: NO];
//[self.view addSubview: image32];
//[self.view bringSubviewToFront: image32];
x32 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[8][4] = x32;
}

else if(x==33){
if (i%2==1){
[image33 setImage:[UIImage imageNamed: @"red.png"]];
[image33 setHidden: NO];
//[self.view addSubview: image33];
//[self.view bringSubviewToFront: image33];
x33 =1;
}
else if (i%2==0){
[image33 setImage:[UIImage imageNamed: @"yellow.png"]];
[image33 setHidden: NO];
//[self.view addSubview: image33];
//[self.view bringSubviewToFront: image33];
x33 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[1][5] = x33;
}

else if(x==34){
if (i%2==1){
[image34 setImage:[UIImage imageNamed: @"red.png"]];
[image34 setHidden: NO];
//[self.view addSubview: image34];
//[self.view bringSubviewToFront: image34];
x34 =1;
}
else if (i%2==0){
[image34 setImage:[UIImage imageNamed: @"yellow.png"]];
[image34 setHidden: NO];
//[self.view addSubview: image34];
//[self.view bringSubviewToFront: image34];
x34 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[2][5] = x34;
}

else if(x==35){
if (i%2==1){
[image35 setImage:[UIImage imageNamed: @"red.png"]];
[image35 setHidden: NO];
//[self.view addSubview: image35];
//[self.view bringSubviewToFront: image35];
x35 =1;
}
else if (i%2==0){
[image35 setImage:[UIImage imageNamed: @"yellow.png"]];
[image35 setHidden: NO];
//[self.view addSubview: image35];
//[self.view bringSubviewToFront: image35];
x35 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[3][5] = x35;
}


else if(x==36){
if (i%2==1){
[image36 setImage:[UIImage imageNamed: @"red.png"]];
[image36 setHidden: NO];
//[self.view addSubview: image36];
//[self.view bringSubviewToFront: image36];
x36 =1;
}
else if (i%2==0){
[image36 setImage:[UIImage imageNamed: @"yellow.png"]];
[image36 setHidden: NO];
//[self.view addSubview: image36];
//[self.view bringSubviewToFront: image36];
x36 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[4][5] = x37;
}

else if(x==37){
if (i%2==1){
[image37 setImage:[UIImage imageNamed: @"red.png"]];
[image37 setHidden: NO];
//[self.view addSubview: image37];
//[self.view bringSubviewToFront: image37];
x37 =1;
}
else if (i%2==0){
[image37 setImage:[UIImage imageNamed: @"yellow.png"]];
[image37 setHidden: NO];
//[self.view addSubview: image37];
//[self.view bringSubviewToFront: image37];
x37 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[5][5] = x37;
}

else if(x==38){
if (i%2==1){
[image38 setImage:[UIImage imageNamed: @"red.png"]];
[image38 setHidden: NO];
//[self.view addSubview: image38];
//[self.view bringSubviewToFront: image38];
x38 =1;
}
else if (i%2==0){
[image38 setImage:[UIImage imageNamed: @"yellow.png"]];
[image38 setHidden: NO];
//[self.view addSubview: image38];
//[self.view bringSubviewToFront: image38];
x38 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[6][5] = x38;
}

else if(x==39){
if (i%2==1){
[image39 setImage:[UIImage imageNamed: @"red.png"]];
[image39 setHidden: NO];
//[self.view addSubview: image39];
//[self.view bringSubviewToFront: image39];
x39 =1;
}
else if (i%2==0){
[image39 setImage:[UIImage imageNamed: @"yellow.png"]];
[image39 setHidden: NO];
//[self.view addSubview: image39];
//[self.view bringSubviewToFront: image39];
x39 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[7][5] = x39;
}

else if(x==40){
if (i%2==1){
[image40 setImage:[UIImage imageNamed: @"red.png"]];
[image40 setHidden: NO];
//[self.view addSubview: image40];
//[self.view bringSubviewToFront: image40];
x40 =1;
}
else if (i%2==0){
[image40 setImage:[UIImage imageNamed: @"yellow.png"]];
[image40 setHidden: NO];
//[self.view addSubview: image40];
//[self.view bringSubviewToFront: image40];
x40 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[8][5] = x40;
}

else if(x==41){
if (i%2==1){
[image41 setImage:[UIImage imageNamed: @"red.png"]];
[image41 setHidden: NO];
//[self.view addSubview: image41];
//[self.view bringSubviewToFront: image41];
x41 =1;
}
else if (i%2==0){
[image41 setImage:[UIImage imageNamed: @"yellow.png"]];
[image41 setHidden: NO];
//[self.view addSubview: image41];
//[self.view bringSubviewToFront: image41];
x41 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[1][6] = x41;
}

else if(x==42){
if (i%2==1){
[image42 setImage:[UIImage imageNamed: @"red.png"]];
[image42 setHidden: NO];
//[self.view addSubview: image42];
//[self.view bringSubviewToFront: image42];
x42 =1;
}
else if (i%2==0){
[image42 setImage:[UIImage imageNamed: @"yellow.png"]];
[image42 setHidden: NO];
//[self.view addSubview: image42];
//[self.view bringSubviewToFront: image42];
x42 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[2][6] = x42;
}

else if(x==43){
if (i%2==1){
[image43 setImage:[UIImage imageNamed: @"red.png"]];
[image43 setHidden: NO];
//[self.view addSubview: image43];
//[self.view bringSubviewToFront: image43];
x43 =1;
}
else if (i%2==0){
[image43 setImage:[UIImage imageNamed: @"yellow.png"]];
[image43 setHidden: NO];
//[self.view addSubview: image43];
//[self.view bringSubviewToFront: image43];
x43 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[3][6] = x43;
}

else if(x==44){
if (i%2==1){
[image44 setImage:[UIImage imageNamed: @"red.png"]];
[image44 setHidden: NO];
//[self.view addSubview: image44];
//[self.view bringSubviewToFront: image44];
x44 =1;
}
else if (i%2==0){
[image44 setImage:[UIImage imageNamed: @"yellow.png"]];
[image44 setHidden: NO];
//[self.view addSubview: image44];
//[self.view bringSubviewToFront: image44];
x44 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[4][6] = x44;
}

else if(x==45){
if (i%2==1){
[image45 setImage:[UIImage imageNamed: @"red.png"]];
[image45 setHidden: NO];
//[self.view addSubview: image45];
//[self.view bringSubviewToFront: image45];
x45 =1;
}
else if (i%2==0){
[image45 setImage:[UIImage imageNamed: @"yellow.png"]];
[image45 setHidden: NO];
//[self.view addSubview: image45];
//[self.view bringSubviewToFront: image45];
x45 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[5][6] = x45;
}

else if(x==46){
if (i%2==1){
[image46 setImage:[UIImage imageNamed: @"red.png"]];
[image46 setHidden: NO];
//[self.view addSubview: image46];
//[self.view bringSubviewToFront: image46];
x46 =1;
}
else if (i%2==0){
[image46 setImage:[UIImage imageNamed: @"yellow.png"]];
[image46 setHidden: NO];
//[self.view addSubview: image46];
//[self.view bringSubviewToFront: image46];
x46 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[6][6] = x46;
}

else if(x==47){
if (i%2==1){
[image47 setImage:[UIImage imageNamed: @"red.png"]];
[image47 setHidden: NO];
//[self.view addSubview: image47];
//[self.view bringSubviewToFront: image47];
x47 =1;
}
else if (i%2==0){
[image47 setImage:[UIImage imageNamed: @"yellow.png"]];
[image47 setHidden: NO];
//[self.view addSubview: image47];
//[self.view bringSubviewToFront: image47];
x47 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[7][6] = x47;
}



else if(x==48){
if (i%2==1){
[image48 setImage:[UIImage imageNamed: @"red.png"]];
[image48 setHidden: NO];
//[self.view addSubview: image48];
//[self.view bringSubviewToFront: image48];
x48 =1;
}
else if (i%2==0){
[image48 setImage:[UIImage imageNamed: @"yellow.png"]];
[image48 setHidden: NO];
//[self.view addSubview: image48];
//[self.view bringSubviewToFront: image48];
x48 =2;
}
else{
NSLog(@"Error");
[super didReceiveMemoryWarning];
}
winArray[8][6] = x48;
}




return x;
}// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-(void)game{

}


- (IBAction) showImage1{

row1++;

x = (row1-1)*8+1;

if (row1 == 1&& i<49){
x=1;
[self whatImage];
}


else if (row1 >= 2&& i<49&&row1<7){


x = (row1-1)*8+1;

[self whatImage];
}






}



- (IBAction) showImage2{
row2++;
if (row2 == 1&& i<49){
x=2;
[self whatImage];
}


else if (row2 >= 2&& i<49&&row2<7){


x = (row2-1)*8+2;

[self whatImage];
}

}

- (IBAction) showImage3{
row3++;
if (row3 == 1&& i<49){
x=3;
[self whatImage];
}


else if (row3 >= 2&& i<49&&row3<7){


x = (row3-1)*8+3;
[self whatImage];

}

}
- (IBAction) showImage4{
row4++;
if (row4 == 1&& i<49){
x=4;
[self whatImage];
}


else if (row4 >= 2&& i<49&&row4<7){


x = (row4-1)*8+4;
[self whatImage];

}

}

- (IBAction) showImage5{
row5++;
if (row5 == 1&& i<49){
x=5;
[self whatImage];
}


else if (row5 >= 2&& i<49&&row5<7){


x = (row5-1)*8+5;

[self whatImage];
}

}
- (IBAction) showImage6{
row6++;
if (row6 == 1&& i<49){
x=6;
[self whatImage];
}


else if (row6 >= 2&& i<49&&row6<7){


x = (row6-1)*8+6;
[self whatImage];

}

}
- (IBAction) showImage7{
row7++;
if (row7 == 1&& i<49){
x=7;
[self whatImage];
}


else if (row7 >= 2&& i<49&&row7<7){


x = (row7-1)*8+7;

[self whatImage];
}

}

- (IBAction) showImage8{
row8++;
if (row8 == 1&& i<49){
x=8;
[self whatImage];
}


else if (row8 >= 2&& i<49&&row8<7){


x = (row8-1)*8+8;
[self whatImage];

}

}
















- (void)viewDidLoad
{


i++;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


}


- (void)viewDidUnload
{

[super viewDidUnload];
}





- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
[/cpp]
 

gamerk316

Distinguished
Jul 8, 2008
325
0
19,060
Well, the way I would do it would be, after a move is made, check in every direction for chips of the same color, and if the colors match, continue in that direction until either four chips are the same color (Win), or a different color is seen (Not a Win). Repeat for each direction.
 

randomizer

Distinguished
Please, please, please refactor this. There is more copy and paste here than I've seen in some job applicants' programming test submissions. Almost all of that code is identical. I suggest you look at why an array won't work and sort that out. You would be able to remove an enormous amount of code duplication if you can dump all of those image fields.

I know this doesn't in any way answer your question, but you are going to save yourself future headaches if you clean this up now. With that much code duplication you are making this application easy to break and hard to maintain.
 

Scott_D_Bowen

Honorable
Nov 28, 2012
50
0
10,590
It's funny because the variables could've been declared as:
int w8h6; to int w1h1; (all inclusive) but they are all x1 to x48 even though it's for a 2D array (at least interpretively).

It looks like the result of what happens when someone either sleeps through, or just doesn't pay enough attention during their programming classes.

If there is a university teaching people to do this Then
we have a reason to use our nuclear stockpile for good!
Else
maybe there is a God after all...
 

gamerk316

Distinguished
Jul 8, 2008
325
0
19,060
^^ Because you can't teach a person program structure; they either know it, or they don't.

Still, having to do the same exact thing 48 times over is generally a hint its time to use some sort of array/collection.
 
Status
Not open for further replies.