版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/ main.c/ 象棋/ 車马相仕帅仕相马車/ 十十十十十十十十十/ 十炮十十十十十炮十/ 兵十兵十兵十兵十兵/ 十十十十十十十十十/ 楚河汉界/ 十十十十十十十十十/ 卒十卒十卒十卒十卒/ 十炮十十十十十炮十/ 十十十十十十十十十/ 車马象士将士象马車/ Created by tarena121 on 15/8/12./ Copyright (c) 2015年 Tarena. All rights reserved./#include <stdio.h>#include <stdbool.h>#include <math.h>#include <s
2、tdlib.h>#define R(piece) "03331m"#piece"0330m"/红色棋子#define B(piece) "03330m"#piece"0330m"/黑色棋子#define CROSS "03333m十0330m"/定义外部变量,棋盘坐标char* array119;int xi,yi;/要移动的棋子int xj,yj;/移动的目标位置bool isStandard = 1;/是否符合规则,初始值1,符合bool gameOverSign = 1;/游戏是否结
3、束,0结束bool restart = 0;/生成棋盘void chessboardBuilding();/打印棋盘void printChessboard();/判断是红棋还是黑棋,红旗返回1,黑棋返回-1,否则返回0int redOrBlack(int x,int y);/红棋移动void redMove();/黑棋移动void blackMove();/每种棋子的规则void rulesOfAllKindsOfChessPieces();/判断游戏结束void isGameOver();/*主函数*int main() /生成棋盘 chessboardBuilding(); /打印棋盘
4、printChessboard(); /开始下棋 int turn = -1; while (gameOverSign) isStandard = 1; turn *= (-1);/双方交替下棋 switch (turn) case 1: redMove(); turn = (restart) ? (turn*-1) : turn; break; case -1: blackMove(); turn = (restart) ? (turn*-1) : turn; break; isGameOver(); printf("游戏结束!n"); /主函数结束/*自定义函数*/生成
5、棋盘void chessboardBuilding() for (int i = 0; i < 11; i +) for (int j = 0; j < 9 ; j +) arrayij = CROSS; printf("n"); array50 = array51 = array54 = array57 = array58 = "" array52 = B(楚); array53 = B(河); array55 = B(汉); array56 = B(界); /布置红棋 array00 = array08 = R(車); array01 =
6、 array07 = R(马); array02 = array06 = R(相); array03 = array05 = R(仕); array04 = R(帅); array21 = array27 = R(炮); array30 = array32 = array34 = array36 = array38 = R(兵); /布置黑棋 array100 = array108 = B(車); array101 = array107 = B(马); array102 = array106 = B(相); array103 = array105 = B(仕); array104 = B(将)
7、; array81 = array87 = B(炮); array70 = array72 = array74 = array76 = array78 = B(卒);/打印棋盘void printChessboard() /显示 printf(" 03343;30m中国象棋欢迎您0330mnn"); for (int i = 0; i < 11; i +) for (int j = 0; j < 9; j +) printf("%s",arrayij); printf("n"); /判断是红棋还是黑棋,红旗返回1,黑棋返回
8、-1,否则返回0int redOrBlack(int x,int y) if (arrayxy = R(車) | arrayxy = R(马) | arrayxy = R(相) | arrayxy = R(仕) | arrayxy = R(帅) | arrayxy = R(炮) | arrayxy = R(兵) return 1; else if (arrayxy = B(車) | arrayxy = B(马) | arrayxy = B(象) | arrayxy = B(仕) | arrayxy = B(将) | arrayxy = B(炮) | arrayxy = B(卒) return
9、-1; else return 0;/红棋移动void redMove() if (restart) printf("违反游戏规则,请重新输入n"); restart = 0; printf("红棋请输入你要移动的棋子:n"); scanf("%d %d",&xi,&yi); printf("红棋请输入你要放置的位置:n"); scanf("%d %d",&xj,&yj); rulesOfAllKindsOfChessPieces(); printChessboa
10、rd();/黑棋移动void blackMove() if (restart) printf("违反游戏规则,请重新输入n"); restart = 0; printf("黑棋请输入你要移动的棋子:n"); scanf("%d %d",&xi,&yi); printf("黑棋请输入你要放置的位置:n"); scanf("%d %d",&xj,&yj); rulesOfAllKindsOfChessPieces(); printChessboard();/判断游戏结
11、束void isGameOver() bool sign_r = 0; bool sign_b = 0; for (int i = 0; i < 11; i +) for (int j = 0; j < 9; j +) if (arrayij = R(帅) sign_r = 1; else if (arrayij = B(将) sign_b = 1; if (sign_r = 0)|(sign_b = 0) gameOverSign = 0; /每种棋子的规则void rulesOfAllKindsOfChessPieces()/R(車)- if (arrayxiyi = R(車)
12、 if (yi = yj)/列坐标不变,同列移动 for (int i = xi+1; i < xj; i +) if (i = 5) continue;/如果行等于5,跳过 if (arrayiyi != CROSS) isStandard = 0;/如果初始位置和目标位置之间有棋子,则不符合规则 for (int i = xi-1; i > xj; i -) if (i = 5) continue;/如果行等于5,跳过 if (arrayxiyi != CROSS) isStandard = 0; else if (xi = xj)/行坐标不变,同行移动 for (int i
13、= yi+1; i < yj; i +) if (arrayxii != CROSS) isStandard = 0; for (int i = yi-1; i > yj; i -) if (arrayxii != CROSS) isStandard = 0; if (xi = xj | yi = yj)&& isStandard && (redOrBlack(xj, yj) != 1)/如果棋子直行、没有犯规且落点不是红棋,可以移动 arrayxiyi = CROSS; arrayxjyj = R(車); else restart = 1; /B(
14、車)- else if (arrayxiyi = B(車) if (yi = yj)/列坐标不变,同列移动 for (int i = xi+1; i < xj; i +) if (i = 5) continue;/如果行等于5,跳过 if (arrayiyi != CROSS) isStandard = 0;/如果初始位置和目标位置之间有棋子,则不符合规则 for (int i = xi-1; i > xj; i -) if (i = 5) continue;/如果行等于5,跳过 if (arrayiyi != CROSS) isStandard = 0; else if (xi
15、= xj)/行坐标不变,同行移动 for (int i = yi+1; i < yj; i +) if (arrayxii != CROSS) isStandard = 0; for (int i = yi-1; i > yj; i -) if (arrayxii != CROSS) isStandard = 0; if (xi = xj | yi = yj)&& isStandard && redOrBlack(xj, yj) != -1)/如果棋子直行、没有犯规且落点不是红棋,可以移动 arrayxiyi = CROSS; arrayxjyj =
16、 B(車); else restart = 1; /R(马)- else if (arrayxiyi = R(马) if (redOrBlack(xj, yj) != 1) && (xj = xi-2 && yj = yi-1 &&redOrBlack(xi-1, yi) = 0) | (xj = xi-2 && yj = yi+1 &&redOrBlack(xi-1, yi) = 0) | (xj = xi-1 && yj = yi-2 &&redOrBlack(xi, yi-1)
17、 = 0) | (xj = xi-1 && yj = yi+2 &&redOrBlack(xi, yi+1) = 0) | (xj = xi+1 && yj = yi-2 &&redOrBlack(xi, yi-1) = 0) | (xj = xi+1 && yj = yi+2 &&redOrBlack(xi, yi+1) = 0) | (xj = xi+2 && yj = yi-1 &&redOrBlack(xi+1, yi) = 0) | (xj = xi+2 &
18、amp;& yj = yi+1 &&redOrBlack(xi+1, yi) = 0) arrayxiyi = CROSS; arrayxjyj = R(马); else restart = 1; /B(马)- else if (arrayxiyi = B(马) if (redOrBlack(xj, yj) != -1) && (xj = xi-2 && yj = yi-1 &&redOrBlack(xi-1, yi) = 0) | (xj = xi-2 && yj = yi+1 &&red
19、OrBlack(xi-1, yi) = 0) | (xj = xi-1 && yj = yi-2 &&redOrBlack(xi, yi-1) = 0) | (xj = xi-1 && yj = yi+2 &&redOrBlack(xi, yi+1) = 0) | (xj = xi+1 && yj = yi-2 &&redOrBlack(xi, yi-1) = 0) | (xj = xi+1 && yj = yi+2 &&redOrBlack(xi, yi+1) =
20、0) | (xj = xi+2 && yj = yi-1 &&redOrBlack(xi+1, yi) = 0) | (xj = xi+2 && yj = yi+1 &&redOrBlack(xi+1, yi) = 0) arrayxiyi = CROSS; arrayxjyj = B(马); else restart = 1; /R(炮)- else if (arrayxiyi = R(炮) int count = 0;/起始位置间棋子的个数 if (yi = yj)/列坐标不变,同列移动 for (int i = xi+1;
21、i < xj; i +) if (i = 5) continue;/如果行等于5,跳过 if (redOrBlack(i, yi) != 0) count+; for (int i = xi-1; i > xj; i -) if (i = 5) continue;/如果行等于5,跳过 if (redOrBlack(i, yi) != 0) count+; else if (xi = xj)/行坐标不变,同行移动 for (int i = yi+1; i < yj; i +) if (redOrBlack(xi, i) != 0) count+; for (int i = yi
22、-1; i > yj; i -) if (redOrBlack(xi, i) != 0) count+; if (xi = xj | yi = yj)&& (count <= 1) && redOrBlack(xj, yj) != 1)/如果棋子直行、没有犯规且落点不是红棋,可以移动 arrayxiyi = CROSS; arrayxjyj = R(炮); else restart = 1; /B(炮)- else if (arrayxiyi = B(炮) int count = 0;/起始位置间棋子的个数 if (yi = yj)/列坐标不变,同列
23、移动 for (int i = xi+1; i < xj; i +) if (i = 5) continue;/如果行等于5,跳过 if (redOrBlack(i, yi) != 0) count+; for (int i = xi-1; i > xj; i -) if (i = 5) continue;/如果行等于5,跳过 if (redOrBlack(i, yi) != 0) count+; else if (xi = xj)/行坐标不变,同行移动 for (int i = yi+1; i < yj; i +) if (redOrBlack(xi, i) != 0) c
24、ount+; for (int i = yi-1; i > yj; i -) if (redOrBlack(xi, i) != 0) count+; if (xi = xj | yi = yj)&& (count <= 1) && redOrBlack(xj, yj) != -1)/如果棋子直行、没有犯规且落点不是红棋,可以移动 arrayxiyi = CROSS; arrayxjyj = B(炮); else restart = 1; /R(兵)- else if (arrayxiyi = R(兵) if (xi > xj) isStanda
25、rd = 0;/如果倒退,则不符合规范 if (xi = 3) if (xj != xi+1) | (yi != yj) isStandard = 0;/第3行时只能前进一步 if (xi = 4) if (xj != xi+2) | (yi != yj) isStandard = 0;/第4行时只能前进两步 if (xi > 4) if (xj = xi+1 && yi =yj)| (xj = xi && yi =yj+1)|(xj = xi && yi =yj-1) else isStandard = 0; if (xi = xj |
26、yi = yj)&& isStandard && redOrBlack(xj, yj) != 1)/ arrayxiyi = CROSS; arrayxjyj = R (兵); else restart = 1; /B(卒)- else if (arrayxiyi = B(卒) if (xi < xj) isStandard = 0;/如果倒退,则不符合规范 if (xi = 7) if (xj != xi-1) | (yi != yj) isStandard = 0;/第3行时只能前进一步 if (xi = 6) if (xj != xi-2) | (y
27、i != yj) isStandard = 0;/第4行时只能前进两步 if (xi < 4) if (xj = xi-1 && yi =yj)| (xj = xi && yi =yj+1)|(xj = xi && yi =yj-1) else isStandard = 0; if (isStandard && redOrBlack(xj, yj) != -1)/ arrayxiyi = CROSS; arrayxjyj = R (卒); else restart = 1; /R(相)- else if (arrayxiyi
28、 = R(相) if (xj <= 4)&&(redOrBlack(xj, yj) != 1) && (xj = xi-2 && yj = yi-2 &&redOrBlack(xi-1, yi-1) = 0) | (xj = xi-2 && yj = yi+2 &&redOrBlack(xi-1, yi+1) = 0) | (xj = xi+2 && yj = yi-2 &&redOrBlack(xi+1, yi-1) = 0) | (xj = xi+2 &am
29、p;& yj = yi+2 &&redOrBlack(xi+1, yi+1) = 0) arrayxiyi = CROSS; arrayxjyj = R(相); else restart = 1; /B(象)- else if (arrayxiyi = B(象) if (xj >= 6)&&(redOrBlack(xj, yj) != -1) && (xj = xi-2 && yj = yi-2 &&redOrBlack(xi-1, yi-1) = 0) | (xj = xi-2 &&
30、 yj = yi+2 &&redOrBlack(xi-1, yi+1) = 0) | (xj = xi+2 && yj = yi-2 &&redOrBlack(xi+1, yi-1) = 0) | (xj = xi+2 && yj = yi+2 &&redOrBlack(xi+1, yi+1) = 0) arrayxiyi = CROSS; arrayxjyj = B(象); else restart = 1; /R(仕)- else if (arrayxiyi = R(仕) if (xj <= 2)&&(redOrBlack(xj, yj) != 1) && (xj = xi-1 && yj = yi-1 ) | (xj = xi-1 && yj = yi+1 )
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 行政总厨个人述职报告
- 糖尿病护理方案
- 3.3.1盐类的水解酸碱性高二上学期化学人教版(2019)选择性必修1
- 足跟痛的诊断与治疗
- 保护牙齿小班安全教案反思
- 荷塘月色说课稿
- 安踏企业五年战略规划
- 生物物理学实验室安全操作
- 机场租赁合同
- 健身中心土地租赁协议
- 风险分级管控清单(完整版)
- 医院服务-PPT课件
- 《护士执业证书注销注册申请表》(新)
- 检维修交付生产手续(参考模板)
- SYB创业培训全课件(ppt)
- 危险化学品储存、经营企业专业检查表(长输管线)
- GB∕T 24694-2021 玻璃容器 白酒瓶质量要求
- (完整版)五年级数学思维拓展课程整体设计
- 下肢动脉硬化闭塞症临床路径
- 精装修验房最全表格
- 实例两点透视ppt课件
评论
0/150
提交评论