您的位置:首页 > 其它

oc 实现五子棋游戏

2016-06-05 19:23 330 查看
思路:第一步,绘制棋盘

        第二步,将每一个交点设置为一个button 

        第三部,模拟黑白子字走棋,(第一次点击为白子,第二次为黑子)

        第三部,记录黑白子坐标,判断五个连城一线的为获胜方



#import "AppDelegate.h"

@implementation AppDelegate
static int num=0;
-(id)init
{
if (self=[super init])
{
_blackChess=[[NSMutableArray alloc]init];
_whiteChess=[[NSMutableArray alloc]init];
_sumPosArray=[[NSMutableArray alloc]init];
_SubArray=[[NSMutableArray alloc]init];
_sumPosForBlackArray=[[NSMutableArray alloc]init];

}
return self;

}

//创建棋盘
-(void)creatChessBoard
{
for (int i=0; i<8; i++)

{
for (int j=0; j<8; j++)
{     UIView*view1=[[UIView alloc]init];
view1.frame=CGRectMake(40*j, 40*i+20, 39, 39);
if ((i+j)%2)
{
view1.backgroundColor=[UIColor brownColor];
}
else
{
view1.backgroundColor=[UIColor brownColor];
}

[self.window addSubview:view1];

}
}
}

//放置控制按钮
-(void)creatChess
{
for (int i=0; i<9; i++)
{
for (int j=0; j<9; j++)
{
UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.backgroundColor=[UIColor cyanColor];
//在每个交点上创建一个button,
btn.frame=CGRectMake(40*j-10, 40*i+10, 20,20);
btn.alpha=0.1f;//把button的透明度设置为0
btn.tag=100+i+j;
//为每一个button添加一个事件
[btn addTarget:self action:@selector(pressButon:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn];
}
}

}

//放置棋子
-(void)pressButon:(UIButton*)btn
{
if (num%2==0&&btn.tag)
{

UIButton*chessBtn=btn;
chessBtn.backgroundColor=[UIColor blackColor];
chessBtn.alpha=1.0f;
[_blackChess addObject:chessBtn];
[self judgeBlackChess];
btn.tag=0;
num++;

[self.window addSubview:chessBtn];
}
if (num%2&&btn.tag) {
UIButton*chessBtn=btn;
chessBtn.backgroundColor=[UIColor whiteColor];
chessBtn.alpha=1.0f;
[_whiteChess addObject:chessBtn];
[self judgeWhiteChess];
btn.tag=0;
num++;
[self.window addSubview:chessBtn];

}

}

//开始判断白棋
-(void)judgeWhiteChess
{

for (int i=0; i<_whiteChess.count; i++)
{
UIButton*btn=_whiteChess[_whiteChess.count-1];

int xPos=btn.center.x/40;
int yPos=(btn.center.y-20)/40;
int sumPos=xPos+yPos;
[_sumPosArray addObject:[NSNumber numberWithInt:sumPos]];
break;
}
if (_sumPosArray.count>=5)
{
[self subPosArray];
}
}

-(void)subPosArray
{

for (int i=_sumPosArray.count-5; i<_sumPosArray.count-1; i++)
{
[_SubArray addObject:_sumPosArray[i]];

for (int j=_sumPosArray.count-5; j<_sumPosArray.count-i-1 ; j++)
{

if ([_sumPosArray[j] compare:_sumPosArray[j+1]]==NSOrderedDescending)

{

[_sumPosArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];

}

}

}

for (int i=_sumPosArray.count-5; i<=_sumPosArray.count-1; i++)
{
NSLog(@"%d",[_sumPosArray[i]intValue]);

}

for (int i=_sumPosArray.count-4; i<=_sumPosArray.count-1; i++)
{
//[_SubArray addObject:_sumPosArray[i]];
static int count1=0;
static int count2=0;

static int count3=0;

int val=[_sumPosArray[i]intValue]-[_sumPosArray[i-1]intValue];
if (val==1)
{
count1++;
if (count1==4)
{
NSLog(@"白旗赢");
[self winerArlertView];
break;
}
}

else if (val==2)
{
count2++;
if (count2==4) {
NSLog(@"白棋赢");
[self winerArlertView];
break;
}

}

else if(val==0)
{
count3++;
if (count3==4) {
NSLog(@"白棋赢");
[self winerArlertView];
break;
}

}

}
}

//开始判断黑棋
-(void)judgeBlackChess
{

for (int i=0; i<_blackChess.count; i++)
{
UIButton*btn=_blackChess[_blackChess.count-1];

int xPos=btn.center.x/40;
int yPos=(btn.center.y-20)/40;
int sumPos=xPos+yPos;
[_sumPosForBlackArray addObject:[NSNumber numberWithInt:sumPos]];
break;
}
if (_sumPosForBlackArray.count>=5)
{
[self subPosArrayForBalck];
}
}

-(void)subPosArrayForBalck
{

for (int i=_sumPosForBlackArray.count-5; i<_sumPosForBlackArray.count-1; i++)
{
[_SubArray addObject:_sumPosForBlackArray[i]];

for (int j=_sumPosForBlackArray.count-5; j<_sumPosForBlackArray.count-i-1 ; j++)
{

if ([_sumPosForBlackArray[j] compare:_sumPosForBlackArray[j+1]]==NSOrderedDescending)

{

[_sumPosForBlackArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];

}

}

}

for (int i=_sumPosForBlackArray.count-5; i<=_sumPosForBlackArray.count-1; i++)
{
NSLog(@"%d",[_sumPosForBlackArray[i]intValue]);

}

for (int i=_sumPosForBlackArray.count-4; i<=_sumPosForBlackArray.count-1; i++)
{
//[_SubArray addObject:_sumPosArray[i]];
static int count1=0;
static int count2=0;

static int count3=0;

int val=[_sumPosForBlackArray[i]intValue]-[_sumPosForBlackArray[i-1]intValue];
if (val==1)
{
count1++;
if (count1==4)
{
NSLog(@"黑旗赢");
[self winerArlertView];
break;
}
}

else if (val==2)
{
count2++;
if (count2==4) {
NSLog(@"黑棋赢");
[self winerArlertView];
break;
}

}

else if(val==0)
{
count3++;
if (count3==4) {
NSLog(@"黑棋赢");
[self winerArlertView];
break;
}

}

}

}

//赢棋弹出提示
-(void)winerArlertView
{
UIAlertView*alertView=[[UIAlertView alloc]
initWithTitle:@"提示"
message:@"赢棋"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确认",
nil];
[alertView show];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self creatChessBoard];
[self creatChess];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

return YES;
}


运行结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: