目录
1.效果图
2.主要代码
#include <graphics.h>
#include <time.h>
#define CHESIZE // 棋盘尺寸,不能随意调整
#define RESETX
#define RESETY // 重置原点
typedef enum
{
si, jun, shi, lv, tuan,
ying, lian, pai, ban, gong,
fei, chao, zha, qi, lei, bian,
xian, sheng, shen
}CHESSID;
typedef enum
{
comatt, preatt, noatt
}ATTSTYLE;
typedef enum
{
blue, red, white
}TEAM;
typedef enum
{
alchoose, unchoose
}CHOOSESTATE;
typedef enum
{
unknow, empty, exist
}STATE;
typedef struct
{
int x;
int y;
}COOR;
typedef struct
{
CHESSID id;
int power;
TEAM team;
char *image;
int scoopc;
}CHESS;
typedef struct
{
COOR crdld;
CHESS chess;
STATE state;
}AREA;
typedef struct
{
int i;
int j;
CHOOSESTATE state;
}CHOOSE;
IMAGE image;
AREA area[][6];
CHESS datachess[];
CHOOSE choose;
MOUSEMSG mmsg;
TEAM user;
int lockchessboard =;
int i;
int j;
char *str[]={"工","班","排","连","营","团","旅","师","军","司","棋","炸","变","雷","飞","超","升","神","仙"};
void init();
void initchessbute();
void initvalue();
void drawboard();
void randomarr(int *);
void judge();
void getpreij();
int checkij();
void open();
int whemove();
void move();
int judgeunknow();
ATTSTYLE wheattack();
void kill();
void killself();
void perishtogether();
void getteam();
void userchange();
void judgebunko();
void choosearea();
void cancelchoose();
void change();
void bluewin();
void redwin();
void gamehelp();
void quit();
void peace();
void surrender();
void resetchessboard();
ATTSTYLE judgegong();
ATTSTYLE judgecom();
ATTSTYLE judgezha();
void main()
{
init();
while (true)
{
mmsg = GetMouseMsg();
getpreij();
if (mmsg.uMsg == WM_LBUTTONDOWN)
{
judge();
}
else if (mmsg.uMsg == WM_RBUTTONDOWN
&& choose.state==alchoose)
{
cancelchoose();
}
else if (mmsg.uMsg == WM_MBUTTONDOWN
&& choose.state == alchoose
&& area[choose.i][choose.j].chess.id != zha)
{
killself();
cancelchoose();
userchange();
judgebunko();
}
}
}
void init()
{
initgraph(, 480);
setorigin(RESETX, RESETY);
setaspectratio(, -1);
drawboard();
initvalue();
}
void drawboard()
{
int i;
setlinecolor(WHITE);
for (i=0; i1<7; i1++)
{
line(i*CHESIZE, 0, i1*CHESIZE, CHESIZE*6);
}
for (i=0; i1<7; i1++)
{
line(, i1*CHESIZE, CHESIZE*6, i1*CHESIZE);
}
setlinecolor(WHITE);
setfillcolor(RED);
rectangle(-, -10, CHESIZE*6+10, CHESIZE*6+10);
floodfill(-, -1, WHITE);
rectangle(*CHESIZE, CHESIZE, 9*CHESIZE, 6*CHESIZE);
line(*CHESIZE, 5*CHESIZE, 9*CHESIZE, 5*CHESIZE);
line(*CHESIZE, 4*CHESIZE, 9*CHESIZE, 4*CHESIZE);
line(*CHESIZE, 3*CHESIZE, 9*CHESIZE, 3*CHESIZE);
line(*CHESIZE, 2*CHESIZE, 9*CHESIZE, 2*CHESIZE);
setaspectratio(, 1);
settextstyle(, 18, "黑体");
settextcolor(RED);
outtextxy(*CHESIZE+2, -6*CHESIZE+2, "帮助");
settextcolor(BROWN);
outtextxy(*CHESIZE+2, -5*CHESIZE+2, "投降");
settextcolor(GREEN);
outtextxy(*CHESIZE+2, -4*CHESIZE+2, "和棋");
settextcolor(YELLOW);
outtextxy(*CHESIZE+2, -3*CHESIZE+2, "重置");
settextcolor(CYAN);
outtextxy(*CHESIZE+2, -2*CHESIZE+2, "退出");
settextcolor(LIGHTMAGENTA);
settextstyle(, 20, "黑体");
outtextxy(CHESIZE, -CHESIZE*, "两国军旗");
setaspectratio(, -1);
}
void initchessbute()
{
datachess[].id = gong;
datachess[].power = 1;
datachess[].image = str[0];
datachess[].scoopc = 0;
datachess[].id = ban;
datachess[].power = 2;
datachess[].image = str[1];
datachess[].scoopc = 0;
datachess[].id = pai;
datachess[].power = 3;
datachess[].image = str[2];
datachess[].scoopc = 0;
datachess[].id = lian;
datachess[].power = 4;
datachess[].image = str[3];
datachess[].scoopc = 0;
datachess[].id = ying;
datachess[].power = 5;
datachess[].image = str[4];
datachess[].scoopc = 0;
datachess[].id = tuan;
datachess[].power = 6;
datachess[].image = str[5];
datachess[].scoopc = 0;
datachess[].id = lv;
datachess[].power = 7;
datachess[].image = str[6];
datachess[].scoopc = 0;
datachess[].id = shi;
datachess[].power = 8;
datachess[].image = str[7];
datachess[].scoopc = 0;
datachess[].id = jun;
datachess[].power = 9;
datachess[].image = str[8];
datachess[].scoopc = 0;
datachess[].id = si;
datachess[].power = 10;
datachess[].image = str[9];
datachess[].scoopc = 0;
datachess[].id = qi;
datachess[].power = 100;
datachess[].image = str[10];
datachess[].scoopc = 0;
datachess[].id = zha;
datachess[].power = 99;
datachess[].image = str[11];
datachess[].scoopc = 0;
datachess[].id = bian;
datachess[].power = 0;
datachess[].image = str[12];
datachess[].scoopc = 0;
datachess[].id = lei;
datachess[].power = 98;
datachess[].image = str[13];
datachess[].scoopc = 0;
datachess[].id = fei;
datachess[].power = 9;
datachess[].image = str[14];
datachess[].scoopc = 0;
datachess[].id = chao;
datachess[].power = 11;
datachess[].image = str[15];
datachess[].scoopc = 0;
datachess[].id = sheng;
datachess[].power = 10;
datachess[].image = str[16];
datachess[].scoopc = 0;
datachess[].id = shen;
datachess[].power = 11;
datachess[].image = str[17];
datachess[].scoopc = 0;
datachess[].id = xian;
datachess[].power = 11;
datachess[].image = str[18];
datachess[].scoopc = 0;
}
void initvalue()
{
CHESS chess[];
int random[];
int count;
int i, j1;
initchessbute();
randomarr(random);
for (i=0; i1<=11; i1++)
{
chess[i] = datachess[i1];
chess[i].team = red;
}
chess[i] = datachess[11];
chess[i].team = red;
chess[i+1] = datachess[0];
chess[i+1].team = red;
for (i=0; i1<=11; i1++)
{
chess[i+14] = datachess[i1];
chess[i+14].team = blue;
}
chess[i+14] = datachess[11];
chess[i+14].team = blue;
chess[i+15] = datachess[0];
chess[i+15].team = blue;
for (i=0; i1<4; i1++)
{
chess[i+28] = datachess[12];
chess[i+28].team = white;
chess[i+32] = datachess[13];
chess[i+32].team = white;
}
setfillcolor(YELLOW);
for (count=, i1=0; i1<6; i1++)
{
for (j=0; j1<6; j1++, count++)
{
area[i][j1].chess = chess[random[count]];
area[i][j1].crdld.x = i1 * CHESIZE + 1;
area[i][j1].crdld.y = j1 * CHESIZE + 1;
area[i][j1].state = unknow;
floodfill(area[i][j1].crdld.x, area[i1][j1].crdld.y, WHITE);
}
}
user = red;
choose.state = unchoose;
}
void randomarr(int random[])
{
int i, j1;
int flag =;
srand(time(NULL));
random[] = rand() % 36 ;
for (i=1; i1<36; i1++)
{
while ()
{
random[i] = rand() % 36 ;
for (j=0; j1<i1; j1++)
{
if (random[j] == random[i1])
{
flag =;
break;
}
}
if (flag)
{
flag =;
}
else
{
break;
}
}
}
}
void judge()
{
ATTSTYLE attstyle;
getpreij();
if (checkij())
{
if (area[i][j].state==unknow && choose.state==unchoose)
{
open();
userchange();
}
else if(area[i][j].state == empty)
{
if (choose.state == alchoose)
{
if (whemove())
{
move();
cancelchoose();
userchange();
}
}
}
else
{
if (choose.state == unchoose)
{
if (area[i][j].chess.team==user && area[i][j].chess.id!=qi)
{
choosearea();
}
}
else
{
if (area[i][j].state!=unknow)
{
attstyle = wheattack();
if (attstyle == comatt)
{
kill();
cancelchoose();
userchange();
}
else if (attstyle == preatt)
{
perishtogether();
cancelchoose();
userchange();
}
else
{
;
}
}
}
}
if (!judgeunknow())
{
judgebunko();
}
}
}
int judgeunknow()
{
int i, i2;
int num =;
for (i=0; i1<6; i1++)
{
for (i=0; i2<6; i2++)
{
if (area[i][i2].state == unknow)
{
num++;
}
}
}
return num;
}
void choosearea()
{
choose.i = i;
choose.j = j;
choose.state = alchoose;
setlinecolor(GREEN);
rectangle(choose.i*CHESIZE, choose.j*CHESIZE, choose.i*CHESIZE+CHESIZE, choose.j*CHESIZE+CHESIZE);
}
void cancelchoose()
{
setlinecolor(WHITE);
rectangle(choose.i*CHESIZE, choose.j*CHESIZE, choose.i*CHESIZE+CHESIZE, choose.j*CHESIZE+CHESIZE);
choose.state = unchoose;
}
void getpreij()
{
i = (mmsg.x-RESETX) / CHESIZE;
j = -(mmsg.y-RESETY) / CHESIZE;
}
int checkij()
{
if ((i== || i==8) && j==5)
{
gamehelp();
return;
}
else if ((i== || i==8) && j==4)
{
if (!lockchessboard)
{
surrender();
}
return;
}
else if ((i== || i==8) && j==3)
{
if (!lockchessboard)
{
peace();
}
return;
}
else if ((i== || i==8) && j==2)
{
resetchessboard();
lockchessboard =;
return;
}
else if ((i== || i==8) && j==1)
{
quit();
return;
}
else
{
if (!lockchessboard)
{
if ((i>= && i<=5 && j>=0 && j<=5 && (mmsg.x-RESETX)>0 && -(mmsg.y-RESETY)>0))
{
return;
}
else
{
return;
}
}
else
{
return;
}
}
}
void open()
{
setfillcolor(BLACK);
floodfill(area[i][j].crdld.x, area[i][j].crdld.y, WHITE);
setaspectratio(, 1);
if (area[i][j].chess.team == blue)
{
settextcolor(BLUE);
}
else if (area[i][j].chess.team == red)
{
settextcolor(RED);
}
else
{
settextcolor(MAGENTA);
}
settextstyle(, 18, "黑体");
outtextxy(area[i][j].crdld.x, -area[i][j].crdld.y-CHESIZE+, area[i][j].chess.image);
area[i][j].state = exist;
setaspectratio(, -1);
}
int whemove()
{
if (area[choose.i][choose.j].chess.id==fei || area[choose.i][choose.j].chess.id==sheng
|| area[choose.i][choose.j].chess.id==shen)
{
if (choose.i==i && abs(choose.j-j)<= || choose.j==j && abs(choose.i-i)<=5)
{
return;
}
else
{
return;
}
}
else if (area[choose.i][choose.j].chess.id == xian)
{
return;
}
else
{
if (choose.i==i && abs(choose.j-j)== || choose.j==j && abs(choose.i-i)==1)
{
return;
}
else
{
return;
}
}
}
void move()
{
setfillcolor(BLACK);
floodfill(area[choose.i][choose.j].crdld.x, area[choose.i][choose.j].crdld.y, GREEN);
setaspectratio(, 1);
if (area[choose.i][choose.j].chess.id==gong && area[choose.i][choose.j].chess.scoopc>)
{
if (area[choose.i][choose.j].chess.team == blue)
{
settextcolor(LIGHTBLUE);
}
else
{
settextcolor(LIGHTRED);
}
}
else
{
if (user == blue)
{
settextcolor(BLUE);
}
else
{
settextcolor(RED);
}
}
settextstyle(, 18, "黑体");
outtextxy(area[i][j].crdld.x, -area[i][j].crdld.y-CHESIZE+, area[choose.i][choose.j].chess.image);
area[choose.i][choose.j].state = empty;
area[i][j].state = exist;
area[i][j].chess = area[choose.i][choose.j].chess;
setaspectratio(, -1);
}
ATTSTYLE wheattack()
{
if (whemove())
{
if (area[choose.i][choose.j].chess.id == gong)
{
return judgegong();
}
else if (area[choose.i][choose.j].chess.id == zha)
{
return judgezha();
}
else
{
return judgecom();
}
}
else
{
return noatt;
}
}
ATTSTYLE judgegong()
{
if (area[i][j].chess.team != white)
{
if (area[choose.i][choose.j].chess.team != area[i][j].chess.team)
{
if (area[i][j].chess.id==gong || area[i][j].chess.id==zha)
{
return preatt;
}
else if (area[i][j].chess.id == qi)
{
if (area[choose.i][choose.j].chess.scoopc ==)
{
return noatt;
}
else if (area[choose.i][choose.j].chess.scoopc ==)
{
area[choose.i][choose.j].chess = datachess[];
getteam();
return comatt;
}
else if (area[choose.i][choose.j].chess.scoopc ==)
{
area[choose.i][choose.j].chess = datachess[];
getteam();
return comatt;
}
else if (area[choose.i][choose.j].chess.scoopc ==)
{
area[choose.i][choose.j].chess = datachess[];
getteam();
return comatt;
}
else
{
area[choose.i][choose.j].chess = datachess[];
getteam();
return comatt;
}
}
else
{
return noatt;
}
}
else
{
return noatt;
}
}
else
{
if (area[i][j].chess.id == lei)
{
area[choose.i][choose.j].chess.scoopc++;
return comatt;
}
else
{
change();
return comatt;
}
}
}
ATTSTYLE judgezha()
{
if (area[choose.i][choose.j].chess.team != area[i][j].chess.team)
{
if (area[i][j].chess.id != qi)
{
return preatt;
}
else
{
return noatt;
}
}
else
{
return noatt;
}
}
ATTSTYLE judgecom()
{
if (area[i][j].chess.team != white)
{
if (area[choose.i][choose.j].chess.team != area[i][j].chess.team)
{
if (area[choose.i][choose.j].chess.power==area[i][j].chess.power || area[i][j].chess.id==zha)
{
return preatt;
}
else if (area[choose.i][choose.j].chess.power > area[i][j].chess.power)
{
return comatt;
}
else
{
return noatt;
}
}
else
{
return noatt;
}
}
else
{
if (area[i][j].chess.id == lei)
{
return noatt;
}
else
{
change();
return comatt;
}
}
}
void change()
{
int x;
x = rand() %;
if (x ==)
{
area[choose.i][choose.j].chess = datachess[];
getteam();
}
else
{
x = rand() %;
if (x ==)
{
x = rand() %;
if (x ==)
{
area[choose.i][choose.j].chess = datachess[];
}
else
{
area[choose.i][choose.j].chess = datachess[];
}
getteam();
}
else
{
x = rand() %;
area[choose.i][choose.j].chess = datachess[x];
getteam();
}
}
}
void getteam()
{
if (user == blue)
{
area[choose.i][choose.j].chess.team = blue;
}
else
{
area[choose.i][choose.j].chess.team = red;
}
}
void kill()
{
move();
}
void killself()
{
setfillcolor(BLACK);
floodfill(area[choose.i][choose.j].crdld.x, area[choose.i][choose.j].crdld.y, GREEN);
area[choose.i][choose.j].state = empty;
}
void perishtogether()
{
setfillcolor(BLACK);
cancelchoose();
floodfill(area[choose.i][choose.j].crdld.x, area[choose.i][choose.j].crdld.y, WHITE);
floodfill(area[i][j].crdld.x, area[i][j].crdld.y, WHITE);
area[choose.i][choose.j].state = empty;
area[i][j].state = empty;
}
void userchange()
{
if (user == blue)
{
user = red;
setfillcolor(RED);
floodfill(-, -1, WHITE);
}
else
{
user = blue;
setfillcolor(BLUE);
floodfill(-, -1, WHITE);
}
}
void judgebunko()
{
int i, j1;
int num = 0, num2 = 0;
for (i=0; i1<6; i1++)
{
for (j=0; j1<6; j1++)
{
if (area[i][j1].state != empty)
{
if (area[i][j1].chess.team==red && area[i1][j1].chess.id!=qi)
{
num++;
}
else if(area[i][j1].chess.team==blue && area[i1][j1].chess.id!=qi)
{
num++;
}
}
}
}
if (num==0 && num2!=0)
{
bluewin();
}
if (num==0 && num1!=0)
{
redwin();
}
if (num==0 && num2==0)
{
peace();
}
}
void bluewin()
{
setaspectratio(, 1);
settextcolor(BLUE);
settextstyle(, 20, "黑体");
outtextxy(CHESIZE, -CHESIZE*, "蓝方胜利");
setaspectratio(, -1);
setfillcolor(BLUE);
floodfill(-, -1, WHITE);
lockchessboard =;
}
void redwin()
{
setaspectratio(, 1);
settextcolor(RED);
settextstyle(, 20, "黑体");
outtextxy(CHESIZE, -CHESIZE*, "红方胜利");
setaspectratio(, -1);
setfillcolor(RED);
floodfill(-, -1, WHITE);
lockchessboard =;
}
void peace()
{
setaspectratio(, 1);
settextcolor(GREEN);
settextstyle(, 20, "黑体");
outtextxy(CHESIZE, -CHESIZE*, "握手言和");
setaspectratio(, -1);
setfillcolor(GREEN);
floodfill(-, -1, WHITE);
lockchessboard =;
}
void surrender()
{
if (user == blue)
{
redwin();
}
else
{
bluewin();
}
}
void resetchessboard()
{
cleardevice();
init();
}
void gamehelp()
{
getimage(&image, -, -10, 500, 350);
cleardevice();
setorigin(, 0);
setaspectratio(, 1);
settextcolor(RED);
settextstyle(, 0, "黑体");
outtextxy(-, 0, "注:单击鼠标左键回到游戏界面");
settextcolor(WHITE);
settextstyle(, 0, "黑体");
outtextxy(, 5, "游戏说明");
settextstyle(, 0, "宋体");
outtextxy(, 35, "棋盘大小:6*6; 棋子总数:36; 敌对双方:红,蓝");
outtextxy(, 60, "棋子类别:红棋(红方操作,14个) 蓝棋(蓝方操作,14个) 紫棋(功能棋,8个)");
outtextxy(, 85, "红棋(蓝棋)类型:司令,军长,师长,旅长,团长,营长,连长,班长,军旗,工兵*2,炸弹*2.");
outtextxy(, 100, "紫棋类型:地雷*4,变身棋*4. 注:'*'后面表示该棋的数量,没注则只有一个");
outtextxy(, 125, "规则说明:1.司令最大,工兵最小,大的吃小的,一样就同归于尽,");
outtextxy(textwidth("规则说明:."), 140, "炸弹能炸紫棋和敌方除军旗外所有的棋(炸弹也会消失)." );
outtextxy(textwidth("规则说明:"),, "2.工兵可挖地雷,挖完后可扛对方棋变身(挖的雷越多,变成的人物越厉害).");
outtextxy(textwidth("规则说明:"),, "3.人物棋可吃变,吃后能变成工兵~军长中的一种,有一定几率变成隐藏BOSS.");
outtextxy(textwidth("规则说明:"),, "4.人物棋可自杀(算一次操作).");
outtextxy(textwidth("规则说明:"),, "5.执棋方进行完一次有效操作后,就换对方执棋(边框颜色表当前执棋方).");
outtextxy(textwidth("规则说明:"),, "6.一方棋子(军旗除外)全被消灭,就算输; 同时全部没有,则和棋.");
outtextxy(, 240, "执棋方能进行的操作:操作1:打开棋子(算一次操作).");
outtextxy(textwidth("执棋方能进行的操作:"),, "操作2:攻击.");
outtextxy(textwidth("执棋方能进行的操作:"),, "操作3:移动.");
outtextxy(textwidth("执棋方能进行的操作:"),, "操作4:工兵(已挖雷)扛旗.");
outtextxy(textwidth("执棋方能进行的操作:"),, "操作5:吃变身卡.");
outtextxy(textwidth("执棋方能进行的操作:"),, "操作6:自杀.");
outtextxy(, 340, "实施游戏操作说明(鼠标操作):实施操作1:选择要打开棋子所在的区域,单击.");
outtextxy(textwidth("实施游戏操作说明(鼠标操作):"),, "实施操作2~5:单击选中主动方(棋子边框会变绿)");
outtextxy(textwidth("实施游戏操作说明(鼠标操作):实施操作~5:"), 370, "再单击选中被动方.");
outtextxy(textwidth("实施游戏操作说明(鼠标操作):"),, "实施操作6:选中己方棋子,单机鼠标的中键.");
settextcolor(RED);
outtextxy(textwidth("实施游戏操作说明(鼠标操作):"),,"注:要进行其他操作,必先撤销当前选定(单击右键撤销)");
settextcolor(WHITE);
setlinecolor(WHITE);
line(-, 420, 570, 420);
outtextxy(, 425, "人物棋等级一览(等高杀等小):工1 班2 连3 营4 团5 旅6 师7");
outtextxy(textwidth("人物棋等级一览(等高杀等小):"),, "军8 飞8 司9 升9 神10 仙10");
outtextxy(, 455, "注:'飞' '升' '神' '仙' 都为工兵挖雷后扛旗所变,'飞''升''神'能直线飞,'仙'能满天飞");
while (true)
{
mmsg = GetMouseMsg();
if (mmsg.uMsg == WM_LBUTTONDOWN)
{
break;
}
}
cleardevice();
setorigin(RESETX, RESETY);
setaspectratio(, -1);
putimage(-, -10, &image);
}
void quit()
{
closegraph();
}