用的是边角权值算法不很完善,请大家多提意见。
/**********************************************/ /* 黑白棋源代码 */ /* TC2.0下运行通过 */ /* 走子策略采用边角权值算法 */ /**********************************************/
# include <stdio.h> # include <dos.h> # include <conio.h> # include <graphics.h> # include <stdlib.h> # include <time.h>
# define BGCOLOR 1 /* 背景颜色*/ # define COLOR_WHITECHESS 0 /*白棋棋子的颜色*/ # define COLOR_BLACKCHESS 15 /*黑棋棋子的颜色*/ # define COLOR_LINE 15 /*棋盘线的颜色*/
# define MINBOXSIZE 30 /*棋盘每一格边长*/ # define MAXSIZE 300 /*棋盘的大小 (1+8+1)*25 */ # define SCCOL 50 /*棋盘左上角的坐标*/ # define SCROW 60
/* 按键码*/ # define VK_LEFT 0x4b00 # define VK_RIGHT 0x4d00 # define VK_DOWN 0x5000 # define VK_UP 0x4800 # define VK_SPACE 0x3920 # define VK_ESC 0x011b
/*初始棋盘布局*/ int gril[10][10]= {{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,1,2,0,0,0,0}, {0,0,0,0,2,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} };
int gril_value[10][10]= {{0,0,0,0,0,0,0,0,0,0}, {0,36,1,19,9,9,19,1,36,0}, {0,1,1,4,4,4,4,1,1,0}, {0,19,4,6,4,4,6,4,19,0}, {0,9,4,4,4,4,4,4,9,0}, {0,9,4,4,4,4,4,4,9,0}, {0,19,4,6,4,4,6,4,19,0}, {0,1,1,4,4,4,4,1,1,0}, {0,36,1,19,9,9,19,1,36,0}, {0,0,0,0,0,0,0,0,0,0}, };
int num_user=2; /*得分统计*/ int num_computer=2;
int gbwzc=5,gbwzr=5; /*光标的初始位置*/ int present_p_v[10][10]; /*当前格的位置及其权值*/
char again='0'; /*用来判断游戏是否需要重新开始*/
/****************用到的汉字*********************/ char ci16H[]={ /* 以下是 '此' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x00,0x00,0x00,0x00,0x80,0x0C,0x50, 0x04,0x58,0x07,0xF0,0x27,0xA0,0x14,0xE0, 0x15,0xC0,0x16,0x82,0x1C,0x86,0x38,0x7E, 0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };
char you16H[]={ /* 以下是 '游' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x00,0x00,0x30,0x0D,0xD0,0x0D,0xBC, 0x21,0xF8,0x17,0x98,0x1F,0xB8,0x1D,0xD0, 0x17,0x5C,0x16,0xBC,0x1A,0xF8,0x19,0x48, 0x08,0x78,0x00,0x10,0x00,0x00,0x00,0x00, };
char xi16H[]={ /* 以下是 '戏' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x00,0x01,0x80,0x00,0x98,0x00,0xD0, 0x06,0x70,0x0E,0x60,0x0F,0xD0,0x36,0xB0, 0x4C,0x30,0x4F,0x70,0x39,0x90,0x00,0x0A, 0x00,0x0C,0x00,0x06,0x00,0x00,0x00,0x00, };
char song16H[]={ /* 以下是 '送' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x20,0x00,0xB0,0x19,0xA0,0x1D,0x60, 0x21,0xE0,0x38,0xC0,0x19,0xF0,0x17,0x80, 0x12,0xC0,0x0B,0x30,0x3F,0x10,0x00,0xFE, 0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00, };
char gei16H[]={ /* 以下是 '给' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x00,0x00,0xC0,0x0C,0xC0,0x0C,0x80, 0x19,0xE0,0x39,0x30,0x3A,0x78,0x15,0xCE, 0x38,0x62,0x1B,0xF0,0x1A,0x60,0x33,0xE0, 0x11,0x20,0x00,0x00,0x00,0x00,0x00,0x00, };
char wo16H[]={ /* 以下是 '我' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x80,0x00,0xC0,0x01,0xD8,0x03,0xC8, 0x0E,0x60,0x13,0xE0,0x27,0x20,0x2E,0x78, 0x1F,0xB0,0x03,0x30,0x0E,0xD0,0x06,0x0A, 0x02,0x0C,0x00,0x06,0x00,0x02,0x00,0x00, };
char ai16H[]={ /* 以下是 '爱' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x20,0x00,0xF0,0x07,0xA0,0x02,0xA0, 0x06,0xA0,0x02,0xFC,0x17,0x8E,0x19,0xF0, 0x17,0xA0,0x13,0xE0,0x04,0xE0,0x05,0xC0, 0x0F,0xA0,0x00,0x1C,0x00,0x00,0x00,0x00, };
char de16H[]={ /* 以下是 '的' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x00,0x00,0x60,0x0C,0xA0,0x04,0xA0, 0x1D,0x40,0x26,0x78,0x2A,0xC4,0x3E,0x04, 0x3E,0x64,0x3A,0xA4,0x1C,0x48,0x14,0x78, 0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00, };
char yao16H[]={ /* 以下是 '姚' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x00,0x00,0x40,0x04,0x60,0x05,0x70, 0x05,0xA8,0x26,0xB0,0x36,0xA0,0x3B,0xD0, 0x3A,0xD0,0x2D,0x60,0x2B,0x44,0x1A,0x3E, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };
char min16H[]={ /* 以下是 '敏' 的 16点阵华文行楷 字模,32 byte */ 0x00,0x00,0x04,0x00,0x0F,0x10,0x1E,0x10, 0x14,0x30,0x0F,0x3C,0x0B,0x70,0x1D,0x50, 0xF6,0xD0,0x6F,0x30,0x32,0x38,0x26,0xEE, 0x04,0x0F,0x00,0x00,0x00,0x00,0x00,0x00, }; /*******************************************************/ char zuo16S[]={ /* 以下是 '左' 的 16点阵宋体 字模,32 byte */ 0x02,0x00,0x03,0x00,0x02,0x00,0xFF,0xFE, 0x02,0x00,0x02,0x00,0x04,0x00,0x04,0x00, 0x0F,0xF8,0x08,0x80,0x10,0x80,0x10,0x80, 0x20,0x80,0x40,0x80,0xBF,0xFE,0x00,0x00, };
char you16S[]={ /* 以下是 '右' 的 16点阵宋体 字模,32 byte */ 0x02,0x00,0x02,0x00,0x02,0x00,0xFF,0xFE, 0x04,0x00,0x04,0x00,0x08,0x00,0x18,0x00, 0x2F,0xF8,0x48,0x08,0x88,0x08,0x08,0x08, 0x08,0x08,0x0F,0xF8,0x08,0x08,0x00,0x00, };
char shang16S[]={ /* 以下是 '上' 的 16点阵宋体 字模,32 byte */ 0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0x00,0x01,0xF8,0x01,0x00, 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0x04,0x7F,0xFE,0x00,0x00, };
char xia16S[]={ /* 以下是 '下' 的 16点阵宋体 字模,32 byte */ 0x00,0x04,0x7F,0xFE,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0xC0,0x01,0x60,0x01,0x30, 0x01,0x20,0x01,0x00,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00, };
char yi16S[]={ /* 以下是 '移' 的 16点阵宋体 字模,32 byte */ 0x0C,0x40,0x70,0x7C,0x10,0x84,0x15,0x08, 0xFE,0x50,0x30,0x20,0x30,0xD0,0x5B,0x20, 0x54,0x7E,0x90,0x84,0x11,0x88,0x12,0x50, 0x10,0x20,0x10,0x40,0x11,0x80,0x16,0x00, };
char luo16S[]={ /* 以下是 '落' 的 16点阵宋体 字模,32 byte */ 0x04,0x40,0x04,0x40,0xFF,0xFE,0x04,0x40, 0x45,0x00,0x21,0xF8,0xA1,0x10,0x4A,0xA0, 0x48,0x40,0x10,0xA0,0x13,0x18,0xEF,0xF6, 0x22,0x10,0x22,0x10,0x23,0xF0,0x22,0x10, };
char zi16S[]={ /* 以下是 '子' 的 16点阵宋体 字模,32 byte */ 0x00,0x00,0x3F,0xF0,0x00,0x20,0x00,0x40, 0x00,0x80,0x01,0x00,0x01,0x00,0x01,0x04, 0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00, };
char tui16S[]={ /* 以下是 '退' 的 16点阵宋体 字模,32 byte */ 0x40,0x00,0x27,0xF0,0x24,0x10,0x07,0xF0, 0x04,0x10,0x04,0x10,0xE7,0xF0,0x24,0x88, 0x24,0x50,0x24,0x20,0x25,0x10,0x26,0x18, 0x24,0x08,0x50,0x00,0x8F,0xFE,0x00,0x00, };
char chu16S[]={ /* 以下是 '出' 的 16点阵宋体 字模,32 byte */ 0x01,0x00,0x01,0x00,0x21,0x04,0x21,0x04, 0x21,0x04,0x21,0x04,0x3F,0xFC,0x21,0x04, 0x01,0x00,0x21,0x04,0x21,0x04,0x21,0x04, 0x21,0x04,0x3F,0xFC,0x20,0x04,0x00,0x00, }; /*******************************************************/
/*********************************************************************/ /*功能:初始化图形模式 */ /*调用TC中initgraph(&gdrive,&gmode,"E:\\Turboc\\")函数 */ /*********************************************************************/ void init(int gdrive,int gmode) { int gd = DETECT, gm = 0; /* 和gd=VGA,gm=VGAHI是同样效果 */ registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行*/ initgraph(&gd, &gm, ""); }
/*********************************************/ /*在(a,b)处用color色显示大小为font的字符串txt*/ /*********************************************/ void txt(int a,int b,char *txt,int font,int color) { setcolor(color); settextstyle(0,0,font);/*设置图形模式下字符显示底字体、方向和大小*/ outtextxy(a,b,txt); /*图形模式下在x,y坐标位置显示一行字符串*/ }
/*******************************************************/ /* */ /* */ /* */ /*******************************************************/ void drawmat(char *mat,int matsize,int x,int y,int color) /*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/ { int i, j, k, n; n = (matsize - 1) / 8 + 1; for(j = 0; j < matsize; j++) for(i = 0; i < n; i++) for(k = 0;k < 8; k++) if(mat[j * n + i] & (0x80 >> k)) /*测试为1的位则显示*/ putpixel(x + i * 8 + k, y + j, color); }
void show_ym() { drawmat(ci16H, 16, 110,414, 1); drawmat(you16H,16, 130,414, 1); drawmat(xi16H, 16, 150,414, 1); drawmat(song16H,16,170,414, 1); drawmat(gei16H, 16,190,414, 1); drawmat(wo16H, 16, 210,414, 1); drawmat(ai16H, 16, 230,414, 1); drawmat(de16H, 16, 250,414, 1); drawmat(yao16H, 16,270,414, 1); drawmat(min16H, 16,290,414, 1); txt(315,417 ,"*****",1,1);
}
/**************************/ /*功能:在图形模式下的清屏*/ /*将屏幕用黑色填充 */ /**************************/ void cls() { setfillstyle(SOLID_FILL,0); /*用黑色进行实填充*/ setcolor(0); /*设置前景颜色 */ bar(0,0,640,480); /*0,0为屏幕左上角,屏幕宽为640,长为480*/ }
/*************************************/ /*功能:在图形模式下的高级清屏 */ /*它能填充指定颜色color的指定大小的矩*/ /*形框从(a,b)到(c,d) */ /*************************************/ void clscr(int a,int b,int c,int d,int color) { setfillstyle(SOLID_FILL,color); setcolor(color); bar(a,b,c,d); }
/***********************************************************/ /*功能:最小棋子的绘制 */ /*所在方格的所在格的坐标为(fc,fr) (1,1)为可落子的最左上格 */ /*棋子的半径为radius */ /*棋子的颜色为color */ /*棋子中心坐标为(a,b) */ /***********************************************************/ void minbox(int fc,int fr,int color) { int a=0,b=0; int radius; int color1=color; a=(fc*MINBOXSIZE+SCCOL)+MINBOXSIZE/2; b=(fr*MINBOXSIZE+SCROW)+MINBOXSIZE/2; radius=MINBOXSIZE/2-5;
setcolor(color1); setfillstyle(1,color1); pieslice(a, b,0,360,radius);
}
/**********************************/ /*功能:绘制棋盘的可见的最外框 */ /*非可见外框的最左上坐标为(ac,ar) */ /* 每一格棋的宽度为aw */ /**********************************/ void outerdraw(int ac,int br,int aw,int color) { int a,b; a=ac+aw; b=br+aw;
setcolor(color); rectangle(a,b,a+8*MINBOXSIZE, b+8*MINBOXSIZE); rectangle(a-3,b-3,a+8*MINBOXSIZE+3, b+8*MINBOXSIZE+3); rectangle(a-4,b-4,a+8*MINBOXSIZE+4, b+8*MINBOXSIZE+4); rectangle(a-5,b-5,a+8*MINBOXSIZE+5, b+8*MINBOXSIZE+5); }
/*************************************/ /*功能:绘制棋盘中间的纵横线 */ /*非可见外框的最左上坐标为(cc,cr) */ /*每一格棋的宽度为cw */ /*线的颜色为color */ /*先画七根纵线,后画七根横线 */ /*************************************/ void linedraw(int cc,int cr,int cw,int color) { int m=0; int x1,y1,x2,y2; for(m=1;m<8;m++) { x1=(cc+cw)+m*cw; y1=(cr+cw); x2=x1; y2=y1+8*cw;
setcolor(color); line(x1,y1,x2,y2); } for(m=1;m<8;m++) {
x1=(cc+cw); y1=(cr+cw)+m*cw; x2=x1+8*cw; y2=y1;
setcolor(color); line(x1,y1,x2,y2); } }
/********************/ /*整个屏幕外框的绘制*/ /********************/ void win(int a,int b,int c,int d,int bgcolor,char *text,int font,int textcolor) { clscr(a,b,c,d,7); clscr(a+3,b+24,c-3,d-3,bgcolor); clscr(a+3,b+3,c-3,b+20,9);
setcolor(15); line(a,b,c,b); line(a,b,a,d); line(a+2,b+21,c-2,b+21); line(c-2,b+2,c-2,b+21); line(a+2,d-2,c-2,d-2); line(c-2,b+23,c-2,d-2); txt(a+5,b+8,text,font,textcolor);/*调用文字显示函数*/
setcolor(0); line(a,d,c,d); line(c,b,c,d); line(a+3,b+2,c-3,b+2); line(a+2,b+2,a+2,b+20); line(a+2,b+24,a+2,d-2); line(a+2,b+23,c-3,b+23); } /*************************************************************/ /*功能:绘出所在格的提示光标 */ /*(gc,gr)表示当前光标的所在格,(1,1)为的最左上格 */ /*************************************************************/ void gbdw(int gc,int gr,int color) { int x1,y1; x1=(SCCOL+MINBOXSIZE)+(gc-1)*MINBOXSIZE; y1=(SCROW+MINBOXSIZE)+(gr-1)*MINBOXSIZE;
setcolor(color); line(x1+7,y1+7,x1+MINBOXSIZE-7,y1+MINBOXSIZE-7); line(x1+7,y1+MINBOXSIZE-7,x1+MINBOXSIZE-7,y1+7);
}