中国象棋
感谢zhaoqianhao的优化
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#define red SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); //红色
#define nop SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); //原色
#define white SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); //白色
using namespace std;
const int N=10,M=9;
struct chess{
int flag;//红方 or 空 or 黑方
int opt;//棋子类型
}a[15][15];
string s1,s2,s3,s4;
int sx,sy,ex,ey;//(sx,sy) to (ex,ey)
int op;//移动方
char chess1[15][4]={"帅","士","相","馬","車","炮","兵"};
char chess2[15][4]={"将","仕","象","马","车","砲","卒"};
char num[10][4]={"零","一","二","三","四","五","六","七","八","九"};
void init(){
for(int i=1;i<=M;++i){
a[1][i].flag=-1;
a[1][i].opt=abs(i-5);
a[N][i].flag=1;
a[N][i].opt=abs(i-5);
}
for(int i=1;i<=M;i+=2){
a[4][i].flag=-1;
a[4][i].opt=6;
a[7][i].flag=1;
a[7][i].opt=6;
}
for(int i=2;i<=8;i+=6){
a[3][i].flag=-1;
a[3][i].opt=5;
a[8][i].flag=1;
a[8][i].opt=5;
}
return;
}
void print(){
white
cout<<"吴 零 一 二 三 四 五 六 七 八"<<endl<<endl;
for(int i=1;i<=N*2-1;++i){
if(i&1){
cout<<num[i/2]<<" ";
for(int j=1;j<=M;++j){
if(a[i/2+1][j].flag==-1){
red
cout<<chess1[a[i/2+1][j].opt];
white
}
if(a[i/2+1][j].flag==0){
cout<<"十";
}
if(a[i/2+1][j].flag==1){
nop
cout<<chess2[a[i/2+1][j].opt];
white
}
if(j!=M){
cout<<"一";
}
}
}
else{
if(i==10){
cout<<" ";
cout<<" 楚 河 汉 界 ";
}
else{
cout<<" ";
for(int j=1;j<=M;++j){
cout<<"丨";
if((i==2 || i==4 || i==16 || i==18) && (j>=4 && j<=5)){
cout<<"·";
}
else{
cout<<" ";
}
}
}
}
cout<<endl;
}
if(op==-1){
cout<<"红方行棋:";
}
else{
cout<<"黑方行棋:";
}
return;
}
bool move0(int x1,int y1,int x2,int y2){
int cnt=0;
if(a[x2][y2].opt==0 && a[x2][y2].flag!=0){//对面笑
if(y1!=y2){
return 0;
}
for(int i=min(x1,x2)+1;i<=max(x1,x2)-1;++i){
if(a[i][y1].flag!=0){
++cnt;
}
}
if(cnt!=0){
return 0;
}
}
else{
if(abs(x1-x2)+abs(y1-y2)!=1){
return 0;
}
if(y2<4 || y2>6){
return 0;
}
if(op==-1){
if(x2>3){
return 0;
}
}
else{
if(x2<8){
return 0;
}
}
}
return 1;
}
bool move1(int x1,int y1,int x2,int y2){
if(abs(x1-x2)!=1 || abs(y1-y2)!=1){
return 0;
}
if(y2<4 || y2>6){
return 0;
}
if(op==-1){
if(x2>3){
return 0;
}
}
else{
if(x2<8){
return 0;
}
}
return 1;
}
bool move2(int x1,int y1,int x2,int y2){
if(abs(x1-x2)!=2 || abs(y1-y2)!=2){
return 0;
}
if(a[(x1+x2)/2][(y1+y2)/2].flag!=0){
return 0;
}
if(op==-1){
if(x2>5){
return 0;
}
}
else{
if(x2<6){
return 0;
}
}
return 1;
}
bool move3(int x1,int y1,int x2,int y2){
if(abs(x1-x2)+abs(y1-y2)!=3){
return 0;
}
if(abs(x1-x2)==0 || abs(y1-y2)==0){
return 0;
}
if(abs(x1-x2)==1){
if(y2>y1){
if(a[x1][y1+1].flag!=0){
return 0;
}
}
else{
if(a[x1][y1-1].flag!=0){
return 0;
}
}
}
else{
if(x2>x1){
if(a[x1+1][y1].flag!=0){
return 0;
}
}
else{
if(a[x1-1][y1].flag!=0){
return 0;
}
}
}
return 1;
}
bool move4(int x1,int y1,int x2,int y2){
if(x1!=x2 && y1!=y2){
return 0;
}
int cnt=0;
if(x1==x2){
for(int i=min(y1,y2)+1;i<=max(y1,y2)-1;++i){
if(a[x1][i].flag!=0){
++cnt;
}
}
}
else{
for(int i=min(x1,x2)+1;i<=max(x1,x2)-1;++i){
if(a[i][y1].flag!=0){
++cnt;
}
}
}
if(cnt!=0){
return 0;
}
return 1;
}
bool move5(int x1,int y1,int x2,int y2){
if(x1!=x2 && y1!=y2){
return 0;
}
int cnt=0;
if(a[x2][y2].flag==0){
if(x1==x2){
for(int i=min(y1,y2)+1;i<=max(y1,y2)-1;++i){
if(a[x1][i].flag!=0){
++cnt;
}
}
}
else{
for(int i=min(x1,x2)+1;i<=max(x1,x2)-1;++i){
if(a[i][y1].flag!=0){
++cnt;
}
}
}
if(cnt!=0){
return 0;
}
}
else{
if(x1==x2){
for(int i=min(y1,y2)+1;i<=max(y1,y2)-1;++i){
if(a[x1][i].flag!=0){
++cnt;
}
}
}
else{
for(int i=min(x1,x2)+1;i<=max(x1,x2)-1;++i){
if(a[i][y1].flag!=0){
++cnt;
}
}
}
if(cnt!=1){
return 0;
}
}
return 1;
}
bool move6(int x1,int y1,int x2,int y2){
if(abs(x1-x2)+abs(y1-y2)!=1){
return 0;
}
if(op==-1){
if(x1>5){//过河
if(x2==x1-1){
return 0;
}
}
else{
if(x2!=x1+1){
return 0;
}
}
}
else{
if(x1<6){//过河
if(x2==x1+1){
return 0;
}
}
else{
if(x2!=x1-1){
return 0;
}
}
}
return 1;
}
bool move(int x1,int y1,int x2,int y2){
if(x1<1 || x2<1 || y1<1 || y2<1 || x1>N || x2>N || y1>M || y2>M){
return 0;
}
if(x1==x2 && y1==y2){
return 0;
}
if(a[x1][y1].flag!=op){
return 0;
}
if(a[x1][y1].flag==a[x2][y2].flag){
return 0;
}
if(a[x1][y1].opt==0){//帅 or 将
if(move0(x1,y1,x2,y2)==0){
return 0;
}
}
if(a[x1][y1].opt==1){//士 or 仕
if(move1(x1,y1,x2,y2)==0){
return 0;
}
}
if(a[x1][y1].opt==2){//相 or 象
if(move2(x1,y1,x2,y2)==0){
return 0;
}
}
if(a[x1][y1].opt==3){//馬 or 马
if(move3(x1,y1,x2,y2)==0){
return 0;
}
}
if(a[x1][y1].opt==4){//車 or 车
if(move4(x1,y1,x2,y2)==0){
return 0;
}
}
if(a[x1][y1].opt==5){//炮 or 砲
if(move5(x1,y1,x2,y2)==0){
return 0;
}
}
if(a[x1][y1].opt==6){//兵 or 卒
if(move6(x1,y1,x2,y2)==0){
return 0;
}
}
if(a[x2][y2].opt==0 && a[x2][y2].flag!=0){
if(op==-1){
cout<<"红方赢了"<<endl;
}
else{
cout<<"黑方赢了"<<endl;
}
exit(0);
}
a[x2][y2].flag=a[x1][y1].flag;
a[x2][y2].opt=a[x1][y1].opt;
a[x1][y1].flag=0;
a[x1][y1].opt=0;
return 1;
}
void error(){
cout<<"走棋有误"<<endl;
Sleep(1000);
system("cls");
return;
}
int main(){
init();
op=-1;
while(1){
print();
cin>>s1>>s2>>s3>>s4;
if(s1.size()!=1 || s2.size()!=1 || s3.size()!=1 || s4.size()!=1){
error();
continue;
}
sx=s1[0]-'0';
sy=s2[0]-'0';
ex=s3[0]-'0';
ey=s4[0]-'0';
if(move(sx+1,sy+1,ex+1,ey+1)==0){
error();
continue;
}
if(op==-1){
op=1;
}
else{
op=-1;
}
system("cls");
}
return 0;
}棋钟
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
struct clock{
int h,m,s;
bool flag;
int cnt;
int dh,dm,ds;
}p1,p2;
int op;
int cnts;
char c;
void init(){
//红方
p1.flag=0;
//包干
p1.h=0;//时
p1.m=30;//分
p1.s=0;//秒
//读秒
p1.cnt=3;//次数
p1.dh=0;//时
p1.dm=0;//分
p1.ds=30;//秒
//黑方
p2.flag=0;
//包干
p2.h=0;//时
p2.m=30;//分
p2.s=0;//秒
//读秒
p2.cnt=3;//次数
p2.dh=0;//时
p2.dm=0;//分
p2.ds=30;//秒
return;
}
void print(){
printf("红方\n");
if(p1.flag==0){
printf("读秒:%.2d:%.2d:%.2d %d次\n",p1.dh,p1.dm,p1.ds,p1.cnt);
printf("包干:%.2d:%.2d:%.2d\n",p1.h,p1.m,p1.s);
}
else{
printf("读秒:%.2d:%.2d:%.2d %d次\n",p1.h,p1.m,p1.s,p1.cnt);
printf("包干:%.2d:%.2d:%.2d\n",0,0,0);
}
printf("\n");
printf("黑方\n");
if(p2.flag==0){
printf("读秒:%.2d:%.2d:%.2d %d次\n",p2.dh,p2.dm,p2.ds,p2.cnt);
printf("包干:%.2d:%.2d:%.2d\n",p2.h,p2.m,p2.s);
}
else{
printf("读秒:%.2d:%.2d:%.2d %d次\n",p2.h,p2.m,p2.s,p2.cnt);
printf("包干:%.2d:%.2d:%.2d\n",0,0,0);
}
printf("\n");
if(op==-1){
printf("红方计时中\n");
}
else{
printf("黑方计时中\n");
}
return;
}
bool change(){
if(op==-1){
if(p1.flag==0){
--p1.s;
if(p1.s<0){
--p1.m;
p1.s+=60;
if(p1.m<0){
--p1.h;
p1.m+=60;
if(p1.h<0){
p1.flag=1;
p1.h=p1.dh;
p1.m=p1.dm;
p1.s=p1.ds;
}
}
}
}
if(p1.flag==1){
if(p1.cnt==0){
return 0;
}
--p1.s;
if(p1.s<0){
--p1.m;
p1.s+=60;
if(p1.m<0){
--p1.h;
p1.m+=60;
if(p1.h<0){
--p1.cnt;
p1.h=p1.dh;
p1.m=p1.dm;
p1.s=p1.ds;
}
}
}
if(p1.cnt==0){
return 0;
}
}
}
if(op==1){
if(p2.flag==0){
--p2.s;
if(p2.s<0){
--p2.m;
p2.s+=60;
if(p2.m<0){
--p2.h;
p2.m+=60;
if(p2.h<0){
p2.flag=1;
p2.h=p2.dh;
p2.m=p2.dm;
p2.s=p2.ds;
}
}
}
}
if(p2.flag==1){
if(p2.cnt==0){
return 0;
}
--p2.s;
if(p2.s<0){
--p2.m;
p2.s+=60;
if(p2.m<0){
--p2.h;
p2.m+=60;
if(p2.h<0){
--p2.cnt;
p2.h=p2.dh;
p2.m=p2.dm;
p2.s=p2.ds;
}
}
}
if(p2.cnt==0){
return 0;
}
}
}
return 1;
}
int main(){
init();
op=-1;
printf("敲击键盘开始计时\n");
c=_getch();
system("cls");
print();
while(1){
if(_kbhit()!=0){
c=_getch();
cnts=0;
if(op==-1){
if(p1.flag==1){
p1.h=p1.dh;
p1.m=p1.dm;
p1.s=p1.ds;
}
}
else{
if(p2.flag==1){
p2.h=p2.dh;
p2.m=p2.dm;
p2.s=p2.ds;
}
}
op=-op;
}
Sleep(100);
++cnts;
if(cnts==10){
cnts=0;
if(change()==0){
if(op==-1){
printf("红方超时\n");
}
else{
printf("黑方超时\n");
}
return 0;
}
system("cls");
print();
}
}
return 0;
}