中国象棋(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];
}
中国象棋(IOS)的更多相关文章
- [洛谷P2051] [AHOI2009]中国象棋
洛谷题目链接:[AHOI2009]中国象棋 题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,请问有多少种放置方法 ...
- [AHOI2009]中国象棋 BZOJ1801 dp
题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,请问有多少种放置方法.大家肯定很清楚,在中国象棋中炮的行走方式是 ...
- 洛谷 P2051 [AHOI2009]中国象棋 状态压缩思想DP
P2051 [AHOI2009]中国象棋 题意: 给定一个n*m的空棋盘,问合法放置任意多个炮有多少种情况.合法放置的意思是棋子炮不会相互打到. 思路: 这道题我们可以发现因为炮是隔一个棋子可以打出去 ...
- BZOJ 1801中国象棋 DP
1801: [Ahoi2009]chess 中国象棋 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1426 Solved: 826[Submit][ ...
- C#中国象棋+游戏大厅 服务器 + 客户端源码
来源:www.ajerp.com/bbs C#中国象棋+游戏大厅 服务器 + 客户端源码 源码开源 C#版中国象棋(附游戏大厅) 基于前人大虾的修改版 主要用委托实现 服务器支持在线人数,大厅桌数的设 ...
- 中国象棋引擎的C#源代码
以前写的中国象棋引擎的C#源程序,可在VS2010中编译运行,由于个人精力有限,难以完成后续的开发工作,如果谁感兴趣,请关注微信公众号(“申龙斌的程序人生”,ID:slbGTD),发送后台消息“象棋引 ...
- 1.2 中国象棋将帅问题进一步讨论与扩展:如何用1个变量实现N重循环?[chinese chess]
[题目] 假设在中国象棋中只剩下将帅两个棋子,国人都知道基本规则:将帅不能出九宫格,只能上下左右移动,不能斜向移动,同时将帅不能照面.问在这样条件下,所有可能将帅位置.要求在代码中只能使用一个字节存储 ...
- 基于HTML5实现的中国象棋游戏
棋类游戏在桌面游戏中已经非常成熟,中国象棋的版本也非常多.今天这款基于HTML5技术的中国象棋游戏非常有特色,我们不仅可以选择中国象棋的游戏难度,而且可以切换棋盘的样式.程序写累了,喝上一杯咖啡,和电 ...
- BZOJ 1801: [Ahoi2009]chess 中国象棋( dp )
dp(i, j, k)表示考虑了前i行, 放了0个炮的有j列, 放了1个炮的有k列. 时间复杂度O(NM^2) -------------------------------------------- ...
随机推荐
- Python:Anaconda安装虚拟环境到指定路径
1 曾经的困扰 有段时间,想使用基于不同python版本的anaconda,就直接从官网下载了两个不同的anaconda版本进行安装.刚开始的时候,还觉得也没啥问题.用了一小段时间,在安装其他的第三方 ...
- Fuzz安全狗注入绕过
安全狗版本为: apache 4.0 网站为: php+mysql 系统: win 2003 这里只要是fuzz /*!union 跟 select*/ 之间的内容: /*!union<FUZZ ...
- js函数验证方式:验证是否是数字,支持小数,负数
验证 datatype="/^\d+(\.\d+)?$/" validatform验证是否是数字 支持小数点 datatype="d" 貌似支持小数 js函数验 ...
- 【node】记录项目的开始与完成——pipeline_kafka流式数据库管理项目
前言: 我始终坚信的一点是,学习的效果80%来自总结,甚至全部都是.总结的好处就是让你能翻出你的过往,指出其中的不足,看到未来的改进方法,好的总结更能让知识产生飞跃,所以在工作之余,部署项目之际,总结 ...
- 更改zendstudio花括号匹配显示的方法
- 如何让局域网内的其他电脑访问本机的mysql
测试电脑:本机ip是125.216.250.67 其他电脑 125.216.250.68 第一步:先用其他电脑ping本机,看能否ping通 如上图即为成功ping通了 第二步:开放mysql的全向 ...
- Redis初体验
简介 Redis是一个速度非常快的非关系型数据库,它不仅性能强劲,而且还具有复制特性以及为解决问题而生的独一无二的数据模型.作为键值型数据库,Redis支持5中数据类型:字符串,列表,集 ...
- openCV使用
三方框架----> oprnCV使用的步骤 第一步:从官网下载framework添加到工程 1.打开 http://opencv.org/ 2. 3. 4. 3执行完之后 新打开的网页会倒计时为 ...
- H3C交换机删除VLAN与其绑定端口配置
在系统视图下,执行 undo int vlan 2 undo vlan 2 可以删除vlan2的配置信息. 执行 undo vlan all 可以删除所有的vlan信息. 在vlan2视图下,执行: ...
- CCS学习(三)
边框样式 边框线 dorder-style (top 上: bottom 下: left 左: right 右) 样式:none | hidden | dotted | dashed | sol ...