#include <stdio.h>
#include <graphics.h>
#include <bios.h>
#include <math.h>
#include <stdlib.h>
/*键盘代码*/
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ENTER 0x1c0d
#define ESC 0x011b
#define YES 0x3920 /*空格键*/
#define F1 0x3b00
#define X0 138
#define Y0 66
#define dx 44
#define dy 42
/*全局变量*/
int oldx;
int oldy;
int nowx;
int nowy;
int key;/*键盘*/
int a;/*当前行棋方*/
int b;/*当前使用的棋子*/
int change=1;/*change为奇数红方行棋,偶数绿方走,开始时红方先行*/
int size[14];
void *buf[14];
int size1[7];
int size2[7];
void *buf1[7];
void *buf2[7];
/*记录双方的各种棋子及所处位子;表示:0车,1马,3相,4帅(将),5兵(卒),6炮*/
struct situation
{ int chess_num;
int player;/*记录棋盘上各处状态*/}map[10][9];
/*old()是棋子原来位置;new()为棋子将要落下的位置*/
/*函数声明*/
int choose_chess(int x,int y);
void cover_board(int oldx,int oldy);
void draw_board();
void draw_chess();
void draw_map();
void gb_control();
void image_map();
int judge_win(int nowx,int nowy);
int judge_player();
void make_cover(int ox,int oy);
void move_to(int nowx,int nowy,int ox,int oy);
int step_judge(int nowx,int nowy,int ox,int oy);
void intit();
/*画棋盘*/
void draw_map(void)
{int i,j;
/*背景*/
setbkcolor(9);
setfillstyle(10,4);
floodfill(0,0,4);
/*边框*/
setcolor(11);
rectangle(125,52,504,458);
setfillstyle(1,WHITE);
floodfill((125+504)/2,(57+453)/2,11);
setcolor(6);
rectangle(138,66,490,444);
setfillstyle(1,WHITE);
floodfill((138+490)/2,(66+444)/2,6);
/*制格*/
setcolor(RED);
for(i=1;i<9;i++)
line(138,66+(444-66)*i/9,490,66+(444-66)*i/9);
for(j=1;j<8;j++)
{
line(138+(490-138)*j/8,66,138+(490-138)*j/8,234);
line(138+(490-138)*j/8,276,138+(490-138)*j/8,444);
}
setcolor(RED);
line(138+(490-138)*3/8,66,138+(490-138)*5/8,66+(444-66)*2/9);
line(138+(490-138)*5/8,66,138+(490-138)*3/8,66+(444-66)*2/9);
line(138+(490-138)*3/8,444,138+(490-138)*5/8,66+(444-66)*7/9);
line(138+(490-138)*5/8,444,138+(490-138)*3/8,70+(444-66)*7/9);
/*做河界*/
setcolor(4);
rectangle(138,66+(444-66)*4/9,490,66+(444-66)*5/9);
setfillstyle(1,15);
floodfill((138+490)/2,(66+444)/2,15);
getch();
}
/*画棋子*/
void draw_chess()
{
int i;
char z[2];
char c[5]={'A','B','C','D','E'};
/*a车,b马,c象,d士,e帅,f炮,g兵*/
/*红色底线*/
for(i=0;i<5;i++)
{
z[0]=c[i];
z[1]='\0';
setcolor(5);
circle(138+(490-138)*i/8,66,10);
settextstyle(0,0,0);
outtextxy(133+(490-138)*i/8,61, z);
size1[i]=imagesize(127+i*44,55,149+i*44,77);
buf1[i]=malloc(size1[i]);
getimage(127+i*44,55,149+i*44,77,buf1[i]);
}
for(i=3;i>=0;i--)
{
z[0]=c[i];
z[1]='\0';
setcolor(5);
circle(138+(490-138)*(8-i)/8,66,10);
settextstyle(0,0,0);
outtextxy(133+(490-138)*(8-i)/8,61, z);}
/*兵*/
for(i=0;i<9;i+=2)
{
setcolor(5);
circle(138+(490-138)*i/8,192,10);
settextstyle(0,0,0);
outtextxy(133+(490-138)*i/8,187, "G");
}
size1[5]=imagesize(127,181,149,203);
buf1[5]=malloc(size1[5]);
getimage(127,181,149,203,buf1[5]);
/*炮*/
for(i=1;i<9;i+=6)
{
setcolor(5);
circle(138+350*i/8,150,10);
settextstyle(0,0,0);
outtextxy(133+350*i/8,145,"F");
}
size1[6]=imagesize(171,139,193,161);
buf1[6]=malloc(size1[6]);
getimage(171,139,193,161,buf1[6]);
/*绿色底线*/
for(i=0;i<5;i++)
{
z[0]=c[i];
z[1]='\0';
setcolor(GREEN);
circle(138+(490-138)*i/8,444,10);
settextstyle(0,0,0);
outtextxy(133+(490-138)*i/8,439, z);
size2[i]=imagesize(127+i*44,433,149+i*44,455);
buf2[i]=malloc(size2[i]);
getimage(127+i*44,433,149+i*44,455,buf2[i]); }
for(i=3;i>=0;i--)
{
z[0]=c[i];
z[1]='\0';
setcolor(GREEN);
circle(138+(490-138)*(8-i)/8,444,10);
settextstyle(0,0,0);
outtextxy(133+(490-138)*(8-i)/8,439, z);
}
/*卒*/
for(i=0;i<9;i+=2)
{
setcolor(GREEN);
circle(138+350*i/8,318,10);
settextstyle(0,0,0);
outtextxy(133+350*i/8,313,"G");
}
size2[5]=imagesize(127,307,149,329);
buf2[5]=malloc(size2[5]);
getimage(127,307,149,329,buf2[5]);
/*炮*/
for(i=1;i<9;i+=6)
{
setcolor(GREEN);
circle(138+350*i/8,360,10);
settextstyle(0,0,0);
outtextxy(133+350*i/8,355, "F");
}
size2[6]=imagesize(171,349,193,371);
buf2[6]=malloc(size2[6]);
getimage(171,349,193,371,buf2[6]);
}
/*画光标*/
void draw_board()
{
setcolor(BLUE);
setfillstyle(0,BLUE);
if(nowy==444||nowy==234)
pieslice(nowx+1,nowy-1,20,70,10);
else if(nowx==490)
pieslice(nowx-1,nowy+1,200,250,10);
else
pieslice(nowx+1,nowy+1,290,340,10);}
/*记录空棋盘*/
void image_map()
{
/*左上角0*/
size[0]=imagesize(127,55,149,77);
buf[0]=malloc(size[0]);
getimage(127,55,149,77,buf[0]);
/*右上角1*/
size[1]=imagesize(479,55,501,77);
buf[1]=malloc(size[1]);
getimage(479,55,501,77,buf[1]);
/*左下角2*/
size[2]=imagesize(127,433,149,455);
buf[2]=malloc(size[2]);
getimage(127,433,149,455,buf[2]);
/*右下角3*/
size[3]=imagesize(479,433,501,455);
buf[3]=malloc(size[3]);
getimage(479,433,501,455,buf[3]);
/*T10*/
size[4]=imagesize(171,55,193,77);
buf[4]=malloc(size[4]);
getimage(171,55,193,77,buf[4]);
/*左T8*/
size[5]=imagesize(127,223,149,245);
buf[5]=malloc(size[5]);
getimage(127,223,149,245,buf[5]);
/*右T7*/
size[6]=imagesize(479,223,501,245);
buf[6]=malloc(size[6]);
getimage(479,223,501,245,buf[6]);
/*上T9*/
size[7]=imagesize(171,433,193,455);
buf[7]=malloc(size[7]);
getimage(171,433,193,455,buf[7]);
/*左米上4*/
size[8]=imagesize(259,55,281,77);
buf[8]=malloc(size[8]);
getimage(259,55,281,77,buf[8]);
/*右米上5*/
size[9]=imagesize(347,55,369,77);
buf[9]=malloc(size[9]);
getimage(347,55,369,77,buf[9]);
/*左米下12*/
size[12]=imagesize(259,139,281,161);
buf[12]=malloc(size[12]);
getimage(259,139,281,161,buf[12]);
/*右米下13*/
size[13]=imagesize(347,139,369,161);
buf[13]=malloc(size[13]);
getimage(347,139,369,161,buf[13]);
/*中6*/
size[10]=imagesize(303,97,325,119);
buf[10]=malloc(size[10]);
getimage(303,97,325,119,buf[10]);
/*十11*/
size[11]=imagesize(171,97,193,119);
buf[11]=malloc(size[11]);
getimage(171,97,193,119,buf[11]); }
/*用来擦除并恢复无棋状态*/
void make_cover( int ox,int oy)
{
int n;
if(ox==138&&oy==66) n=0;
else if(ox==490&&oy==66) n=1;
else if(ox==138&&oy==444) n=2;
else if(ox==490&&oy==444) n=3;/*四郊*/
else if((ox==270&&oy==66)||(ox==270&&oy==360)) n=4;/*左上米*/
else if((ox==358&&oy==66)||(ox==358&&oy==360)) n=5;/*右上米*/
else if((ox==270&&oy==150)||(ox==270&&oy==444)) n=12;/*左下米*/
else if((ox==358&&oy==150)||(ox==358&&oy==444)) n=13;/*右下米*/
else if((ox==314&&oy==108)||(ox==314&&oy==402)) n=6;/*中*/
else if(oy>66&&oy<444&&(ox==490)) n=7;/*两侧*/
else if(oy>66&&oy<444&&(ox==138)) n=8;
else if(ox>138&&ox<270&&oy==66) n=9;
else if(ox>358&&ox<490&&oy==66) n=9;
else if (ox>138&&ox<490&&oy==276)n=9;
else if (ox>138&&ox<490&&oy==234) n=10;
else if (ox>138&&ox<270&&oy==444) n=10;
else if(ox>358&&ox<490&&oy==444) n=10;
else n=11;/*十*/
switch(n)
{
case 11:putimage(ox-11,oy-11,buf[11],0);break;
case 8:putimage(ox-11,oy-11,buf[5],0);break;
case 7:putimage(ox-11,oy-11,buf[6],0);break;
case 9:putimage(ox-11,oy-11,buf[4],0);break;
case 10:putimage(ox-11,oy-11,buf[7],0);break;
case 0:putimage(ox-11,oy-11,buf[0],0);break;
case 1:putimage(ox-11,oy-11,buf[1],0);break;
case 2:putimage(ox-11,oy-11,buf[2],0);break;
case 3:putimage(ox-11,oy-11,buf[3],0);break;