首页 > 代码库 > 中国象棋(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(0, 0, _wigth, 20)];
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/4, 2, _wigth*4/9)];
if (i==0||i==8) {
view.frame=CGRectMake(_wigth/18+i*_wigth/9, _height/4, 2, _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/9, 2, _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/9, 2)];
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];
}
// 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(0, 0, _wigth, 20)];
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/4, 2, _wigth*4/9)];
if (i==0||i==8) {
view.frame=CGRectMake(_wigth/18+i*_wigth/9, _height/4, 2, _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/9, 2, _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/9, 2)];
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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。