oc 实现五子棋游戏

思路:第一步,绘制棋盘

        第二步,将每一个交点设置为一个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;
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值