首页 > 代码库 > 中国象棋(IOS)

中国象棋(IOS)

//
//  ViewController.m
//  ChineseChess
//
//  Created by 晚起的蚂蚁 on 2016/11/20.
//  Copyright ? 2016 晚起的蚂蚁. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(assign)CGFloat wigth;
@property(assign)CGFloat height;
@property(retain)NSArray* arr1;
@property(retain)NSArray* arr2;

@property BOOL swop;
@property BOOL next;
@property(retain)NSString* selectedButtonTitle;
@property(assign)NSInteger buttonTag;
@property(assign)float alpha;
@property(retain)UIColor* backgroundColor;//正常棋子的背景颜色
@property(retain)UIColor* selectedBackgroundColor;//被选择的棋子的背景颜色  改为白色
@property(retain)UIColor* selectedButtonTitleColor;//记录被选择棋子的字体颜色
@property(retain)UIColor* red;//红方
@property(retain)UIColor* black;//黑方

@end

@implementation ViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
    
_alpha=0.025;
    
_selectedBackgroundColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    
_backgroundColor=[UIColor colorWithRed:0.5 green:1 blue:0.5 alpha:1];
    
_red=[UIColor colorWithRed:1 green:0 blue:0 alpha:1];
    
_black=[UIColor colorWithRed:0 green:0 blue:0 alpha:1];
    
_wigth=[UIScreen mainScreen].bounds.size.width;
    
_height=[UIScreen mainScreen].bounds.size.height;
    [
self Start];
}
-(
void)Start{
    
_arr1=@[@"",@"",@"",@"",@"",@"",@"",@"",@""];
    
_arr2=@[@"",@"",@"",@"",@"",@"",@"",@"",@""];
    [
self Map];
}
-(
void)Map{
    
self.view.backgroundColor=[UIColor grayColor];
    
UIView* view=[[UIView alloc]initWithFrame:CGRectMake(00_wigth20)];
    view.
backgroundColor=[UIColor whiteColor];
    [
self.view addSubview:view];
    
//棋盘
    
UIView* dibu=[[UIView alloc]initWithFrame:CGRectMake(0_height/4-_wigth/9_wigth_wigth+_wigth*2/9)];
    dibu.
backgroundColor=[UIColor yellowColor];
    [
self.view addSubview:dibu];
    
    
UIView* di=[[UIView alloc]initWithFrame:CGRectMake(_wigth/18_height/4_wigth*8/9_wigth)];
    di.
backgroundColor=[UIColor brownColor];
    [
self.view addSubview:di];
    
    
for (int i=0; i<9; i++) {
        
UIView* view=[[UIView alloc]initWithFrame:CGRectMake(_wigth/18+i*_wigth/9_height/42_wigth*4/9)];
        
if (i==0||i==8) {
            view.
frame=CGRectMake(_wigth/18+i*_wigth/9_height/42_wigth);
        }
        view.
backgroundColor=[UIColor whiteColor];
        [
self.view addSubview:view];
    }
    
for (int i=0; i<10; i++) {
        
UIView* view=[[UIView alloc]initWithFrame:CGRectMake(_wigth/18+i*_wigth/9_height/4+_wigth*5/92_wigth*4/9)];
        view.
backgroundColor=[UIColor whiteColor];
        [
self.view addSubview:view];
    }
    
for (int i=0; i<10; i++) {
        
UIView* view=[[UIView alloc]initWithFrame:CGRectMake(_wigth/18_height/4+i*_wigth/9_wigth*8/92)];
        view.
backgroundColor=[UIColor whiteColor];
        [
self.view addSubview:view];
    }
    
//棋子
    
for (int i=0; i<10; i++) {
        
for (int j=0; j<9; j++) {
            
UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom];
            btn.
frame=CGRectMake(j*_wigth/9_height/4-_wigth/18+i*_wigth/9_wigth/9-4_wigth/9-4);
            btn.
backgroundColor=_backgroundColor;
            btn.
tag=10*i+j+1;
            [
self.view addSubview:btn];
            [btn 
addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn 
setTitle:@"" forState:UIControlStateNormal];
            btn.
titleLabel.font=[UIFont systemFontOfSize:30];
            btn.
layer.masksToBounds=YES;
            btn.
layer.cornerRadius=_wigth/18;
            
if (i<5) {
                [btn 
setTitleColor:_red forState:UIControlStateNormal];
            }
else{
                [btn 
setTitleColor:_black forState:UIControlStateNormal];
            }
            
if (i==0) {
                [btn 
setTitle:_arr1[j] forState:UIControlStateNormal];
            }
else if (i==9){
                [btn 
setTitle:_arr2[j] forState:UIControlStateNormal];
            }
            
else if (i==3&&btn.tag%2==1) {
                [btn 
setTitle:@"" forState:UIControlStateNormal];
            }
else if (i==6&&btn.tag%2==1) {
                [btn 
setTitle:@"" forState:UIControlStateNormal];
            }
else if (btn.tag==72||btn.tag==78||btn.tag==22||btn.tag==28) {
                [btn 
setTitle:@"" forState:UIControlStateNormal];
            }
else{
                btn.
alpha=_alpha;
            }
        }
    }
}
//点击响应事件
-(
void)btnAction:(UIButton*)sender{
    
if (!_swop&&sender.titleLabel.text.length!=0) {
        
if ((!_next&&sender.titleLabel.textColor==_red)||(_next&&sender.titleLabel.textColor==_black))
            [
self saveMessage:sender];
    }
    
else{
        
if (sender.titleLabel.textColor!=_selectedButtonTitleColor) {
            
if ([self swopJudge:sender]) {
                [
self swopMessage:sender];
                
NSLog(@"to->(%ld,%ld)",sender.tag%10,sender.tag/10);
                
_swop=NO;
            }
        }
else{
            ((
UIButton*)[self.view viewWithTag:_buttonTag]).backgroundColor=_backgroundColor;
            
if ((!_next&&sender.titleLabel.textColor==_red)||(_next&&sender.titleLabel.textColor==_black))
                [
self saveMessage:sender];
        }
    }
}

//储存数据
-(
void)saveMessage:(UIButton*)sender{
    
_selectedButtonTitle= sender.titleLabel.text;
    
_selectedButtonTitleColor= sender.titleLabel.textColor;
    
_buttonTag=sender.tag;
     sender.
backgroundColor=_selectedBackgroundColor;
    
_swop=YES;
    
//NSLog(@"tag=%ld ==%@ ==%@",_buttonTag,_selectedButtonTitle,_selectedButtonTitleColor);
    
NSLog(@"from(%ld,%ld)",_buttonTag%10,_buttonTag/10);
}
//交换数据
-(
void)swopMessage:(UIButton*)sender{
    [sender 
setTitle:_selectedButtonTitle forState:UIControlStateNormal];
    sender.
titleLabel.text=_selectedButtonTitle;
    [sender 
setTitleColor:_selectedButtonTitleColor forState:UIControlStateNormal];
    sender.
alpha=1;
    
    
UIButton* button=(UIButton*)[self.view viewWithTag:_buttonTag];
    [button 
setTitle:@"" forState:UIControlStateNormal];
    ((
UIButton*)[self.view viewWithTag:_buttonTag]).titleLabel.text=@"";
    button.
backgroundColor=_backgroundColor;
    button.
alpha=_alpha;
    button.
titleLabel.textColor=[UIColor yellowColor];
    
_next=!_next;
}
//判断是否能交换
-(
BOOL)swopJudge:(UIButton*)sender{
    
NSInteger buttonTagNumber1=_buttonTag/10;//上下移动
    
NSInteger buttonTagNumber2=_buttonTag%10;//左右移动
    
NSInteger senderTagNumber1=sender.tag/10;//上下移动
    
NSInteger senderTagNumber2=sender.tag%10;//左右移动
    
    
NSString* buttonTitle=((UIButton*)[self.view viewWithTag:_buttonTag]).titleLabel.text;
    
//红帅
    
if ([buttonTitle isEqualToString:@""]){
        
if (sender.tag/10<=2&&sender.tag/10>=0&&sender.tag%10<=6&&sender.tag%10>=4){
            
if (labs(sender.tag/10-_buttonTag/10)==0&&labs(sender.tag%10-_buttonTag%10)==1){
                
return YES;
            }
else if (labs(sender.tag/10-_buttonTag/10)==1&&labs(sender.tag%10-_buttonTag%10)==0){
                
return YES;
            }
        }
    }
    
//黑将
    
if ([buttonTitle isEqualToString:@""]){
        
if (sender.tag/10<=9&&sender.tag/10>=7&&sender.tag%10<=6&&sender.tag%10>=4){
            
if (labs(sender.tag/10-_buttonTag/10)==0&&labs(sender.tag%10-_buttonTag%10)==1){
                
return YES;
            }
else if (labs(sender.tag/10-_buttonTag/10)==1&&labs(sender.tag%10-_buttonTag%10)==0){
                
return YES;
            }
        }
    }
    
//红像
    
if ([buttonTitle isEqualToString:@""]) {
        
if (labs(sender.tag/10-_buttonTag/10)==2&&labs(sender.tag%10-_buttonTag%10)==2&&sender.tag<50) {
            
if (((UIButton*)[self.view viewWithTag:(sender.tag+_buttonTag)/2]).titleLabel.text.length==0) {
                
return YES;
            }
else{
                
NSLog(@"遇到障碍--%@",((UIButton*)[self.view viewWithTag:(sender.tag+_buttonTag)/2]).titleLabel.text );
            }
        }
    }
    
//黑象
    
if ([buttonTitle isEqualToString:@""]) {
        
if (labs(sender.tag/10-_buttonTag/10)==2&&labs(sender.tag%10-_buttonTag%10)==2&&sender.tag>50) {
            
if (((UIButton*)[self.view viewWithTag:(sender.tag+_buttonTag)/2]).titleLabel.text.length==0) {
                
return YES;
            }
else{
                
NSLog(@"遇到障碍--%@",((UIButton*)[self.view viewWithTag:(sender.tag+_buttonTag)/2]).titleLabel.text );
            }
        }
    }
    
//红仕
    
if ([buttonTitle isEqualToString:@""]) {
        
if (sender.tag==4||sender.tag==6||sender.tag==15||sender.tag==24||sender.tag==26) {
            
if (labs(sender.tag/10-_buttonTag/10)==1||labs(sender.tag%10-_buttonTag%10)==1) {
                
return YES;
            }
        }
    }
    
//黑士
    
if ([buttonTitle isEqualToString:@""]) {
        
if (sender.tag==74||sender.tag==76||sender.tag==85||sender.tag==94||sender.tag==96) {
            
if (labs(sender.tag/10-_buttonTag/10)==1||labs(sender.tag%10-_buttonTag%10)==1) {
                
return YES;
            }
        }
    }
    
//红兵
    
if ([buttonTitle isEqualToString:@""]) {
        
if (_buttonTag<50) {
            
if (_buttonTag/10==sender.tag/10-1&&_buttonTag%10==sender.tag%10)
                
return YES;
        }
else{
            
if (_buttonTag/10==sender.tag/10-1||_buttonTag%10==sender.tag%10+1||_buttonTag%10==sender.tag%10-1)
                
return YES;
        }
    }
    
//黑卒
    
if ([buttonTitle isEqualToString:@""]) {
        
if (_buttonTag>50) {
            
if (_buttonTag/10==sender.tag/10+1&&_buttonTag%10==sender.tag%10)
                
return YES;
        }
else{
            
if (_buttonTag/10==sender.tag/10+1||_buttonTag%10==sender.tag%10+1||_buttonTag%10==sender.tag%10-1)
                
return YES;
        }
    }
    
//
    
if ([buttonTitle isEqualToString:@""]) {
        
if ((labs(sender.tag/10-_buttonTag/10)==2&&labs(sender.tag%10-_buttonTag%10)==1)||(labs(sender.tag/10-_buttonTag/10)==1&&labs(sender.tag%10-_buttonTag%10)==2)) {
            
if (labs(sender.tag/10-_buttonTag/10)==2&&labs(sender.tag%10-_buttonTag%10)==1) {
                
if (sender.tag/10>_buttonTag/10) {
                    
if (((UIButton*)[self.view viewWithTag:(_buttonTag/10+1)*10+_buttonTag%10]).titleLabel.text.length==0) {
                        
return YES;
                    }
else{
                        
NSLog(@"遇到障碍--%@",((UIButton*)[self.view viewWithTag:(_buttonTag/10+1)*10+_buttonTag%10]).titleLabel.text );
                    }
                }
else{
                    
if (((UIButton*)[self.view viewWithTag:(_buttonTag/10-1)*10+_buttonTag%10]).titleLabel.text.length==0) {
                        
return YES;
                    }
else{
                        
NSLog(@"遇到障碍--%@",((UIButton*)[self.view viewWithTag:(_buttonTag/10-1)*10+_buttonTag%10]).titleLabel.text );
                    }

                }
            }
else{
                
if (sender.tag%10>_buttonTag%10) {
                    
if (((UIButton*)[self.view viewWithTag:_buttonTag/10*10+_buttonTag%10+1]).titleLabel.text.length==0) {
                        
return YES;
                    }
else{
                        
NSLog(@"遇到障碍--%@",((UIButton*)[self.view viewWithTag:_buttonTag/10*10+_buttonTag%10+1]).titleLabel.text );
                    }
                }
else{
                    
if (((UIButton*)[self.view viewWithTag:_buttonTag/10*10+_buttonTag%10-1]).titleLabel.text.length==0) {
                        
return YES;
                    }
else{
                        
NSLog(@"遇到障碍--%@",((UIButton*)[self.view viewWithTag:_buttonTag/10*10+_buttonTag%10-1]).titleLabel.text );
                    }
                    
                }
            }
        }
    }
    
//
    
if ([buttonTitle isEqualToString:@""]) {
        
if (_buttonTag/10==sender.tag/10||_buttonTag%10==sender.tag%10) {
            
//左右移动
            
if (_buttonTag/10==sender.tag/10) {
                
if (_buttonTag%10<sender.tag%10) {
                    
for (NSInteger i=1; i<sender.tag%10-_buttonTag%10; i++) {
                        buttonTagNumber2++;
                        
UIButton* button=(UIButton*)[self.view viewWithTag:buttonTagNumber2+buttonTagNumber1*10];
                        
if (button.titleLabel.text.length!=0){
                            
NSLog(@"遇到障碍--%ld%@",buttonTagNumber2+buttonTagNumber1*10,button.titleLabel.text);
                            
return NO;
                        }
                    }
                }
else{
                    
for (NSInteger i=1; i<_buttonTag%10-sender.tag%10; i++) {
                        senderTagNumber2++;
                        
UIButton* button=(UIButton*)[self.view viewWithTag:senderTagNumber2+senderTagNumber1*10];
                        
if (button.titleLabel.text.length!=0){
                            
NSLog(@"遇到障碍--%ld%@",senderTagNumber2+senderTagNumber1*10,button.titleLabel.text);
                            
return NO;
                        }
                    }
                }
                
//上下移动
            }
else{
                
if (_buttonTag/10<sender.tag/10) {
                    
for (NSInteger i=1; i<sender.tag/10-_buttonTag/10; i++) {
                        buttonTagNumber1++;
                        
UIButton* button=(UIButton*)[self.view viewWithTag:buttonTagNumber2+buttonTagNumber1*10];
                        
if (button.titleLabel.text.length!=0){
                            
NSLog(@"遇到障碍--%ld%@",buttonTagNumber2+buttonTagNumber1*10,button.titleLabel.text);
                            
return NO;
                        }
                    }
                }
else{
                    
for (NSInteger i=1; i<_buttonTag/10-sender.tag/10; i++) {
                        senderTagNumber1++;
                        
UIButton* button=(UIButton*)[self.view viewWithTag:senderTagNumber2+senderTagNumber1*10];
                        
if (button.titleLabel.text.length!=0){
                            
NSLog(@"遇到障碍--%ld%@",senderTagNumber2+senderTagNumber1*10,button.titleLabel.text);
                            
return NO;
                        }
                    }
                }
            }
            
return YES;
        }
    }
    
//
    
if ([buttonTitle isEqualToString:@""]) {
        
NSInteger number=0;
        
if (_buttonTag/10==sender.tag/10||_buttonTag%10==sender.tag%10) {
            
//左右移动
            
if (_buttonTag/10==sender.tag/10) {
                
if (_buttonTag%10<sender.tag%10) {
                    
for (NSInteger i=0; i<sender.tag%10-_buttonTag%10; i++) {
                        buttonTagNumber2++;
                        
UIButton* button=(UIButton*)[self.view viewWithTag:buttonTagNumber2+buttonTagNumber1*10];
                        
if (button.titleLabel.text.length!=0){
                            
NSLog(@"遇到障碍--%ld%@",buttonTagNumber2+buttonTagNumber1*10,button.titleLabel.text);
                            number++;
                        }
                        
if ((number==2&&i==sender.tag%10-_buttonTag%10-1)||(number==0&&i==sender.tag%10-_buttonTag%10-1)) {
                            
return YES;
                        }
                    }
                }
else{
                    
for (NSInteger i=0; i<_buttonTag%10-sender.tag%10; i++) {
                        buttonTagNumber2--;
                        
UIButton* button=(UIButton*)[self.view viewWithTag:buttonTagNumber2+senderTagNumber1*10];
                        
if (button.titleLabel.text.length!=0){
                            
NSLog(@"遇到障碍--%ld%@",buttonTagNumber2+buttonTagNumber1*10,button.titleLabel.text);
                            number++;
                        }
                        
if ((number==2&&i==_buttonTag%10-sender.tag%10-1)||(number==0&&i==_buttonTag%10-sender.tag%10-1)) {
                            
return YES;
                        }
                    }
                }
            
//上下移动
            }
else{
                
if (_buttonTag/10<sender.tag/10) {
                    
for (NSInteger i=0; i<sender.tag/10-_buttonTag/10; i++) {
                        buttonTagNumber1++;
                        
UIButton* button=(UIButton*)[self.view viewWithTag:buttonTagNumber2+buttonTagNumber1*10];
                        
if (button.titleLabel.text.length!=0){
                            
NSLog(@"遇到障碍--%ld%@",buttonTagNumber2+buttonTagNumber1*10,button.titleLabel.text);
                            number++;
                        }
                        
if ((number==2&&i==sender.tag/10-_buttonTag/10-1)||(number==0&&i==sender.tag/10-_buttonTag/10-1)) {
                            
return YES;
                        }
                    }
                }
else{
                    
for (NSInteger i=0; i<_buttonTag/10-sender.tag/10; i++) {
                        buttonTagNumber1--;
                        
UIButton* button=(UIButton*)[self.view viewWithTag:senderTagNumber2+buttonTagNumber1*10];
                        
if (button.titleLabel.text.length!=0){
                            
NSLog(@"遇到障碍--%ld%@",buttonTagNumber2+buttonTagNumber1*10,button.titleLabel.text);
                            number++;
                        }
                        
if ((number==2&&i==_buttonTag/10-sender.tag/10-1)||(number==0&&i==_buttonTag/10-sender.tag/10-1)) {
                            
return YES;
                        }
                    }
                }
            }
        }
    }
    
return NO;
}
-(
void)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning];
}
@end
技术分享
技术分享

中国象棋(IOS)