電腦下象棋是如何編程的
電腦下象棋是如何編程的
象棋程序是讓人跟電腦下棋的,下面學(xué)習(xí)啦小編給你介紹電腦下象棋是如何編程的,歡迎閱讀。
電腦下象棋是如何編程的
棋類(lèi)游戲一般都能建立起數(shù)學(xué)模型,電腦做的只是判斷這一步棋的下一步的每種可能走法,以及每種走法的下一步……以此類(lèi)推,也就是說(shuō)電腦能判斷下一步怎么走,才能對(duì)自己在下下一步以致以后的多少步內(nèi)都對(duì)自己有利。隨著電腦預(yù)測(cè)的步數(shù)的增加,計(jì)算量成指數(shù)級(jí)增加,計(jì)算時(shí)間也會(huì)更長(zhǎng),這樣,除了棋類(lèi)程序的算法因素以外,考驗(yàn)的就是計(jì)算機(jī)的CPU計(jì)算速度了~所以要在電腦的“預(yù)測(cè)能力”和等待時(shí)間之間取一個(gè)折中的步數(shù),比如20步。1997年IBM的“深藍(lán)”戰(zhàn)敗世界棋王卡西帕羅夫,就是因?yàn)樗挠?jì)算機(jī)的計(jì)算能力是當(dāng)時(shí)世界頂尖的。
中國(guó)象棋源代碼-C語(yǔ)言小程序
*--------------------che;#include"dos.h";#include"stdio.h&qu;/*----------------------;#defineRED7;#defineBLACK14;#definetrue1;#definefalse0;#defineSELECT0;#defineMOVE*--------------------chess.c----------------------*/#include "dos.h"#include "stdio.h"/*----------------------------------------------------*/#define RED 7#define BLACK 14#define true 1#define false 0#define SELECT 0#define MOVE 1#define RED_UP 0x1100#define RED_DOWN 0x1f00#define RED_LEFT 0x1e00#define RED_RIGHT 0x2000#define RED_DO 0x3900#define RED_UNDO 0x1000#define BLACK_UP 0x4800#define BLACK_DOWN 0x5000#define BLACK_LEFT 0x4b00#define BLACK_RIGHT 0x4d00#define BLACK_DO 0x1c00#define BLACK_UNDO 0x2b00#define ESCAPE 0x0100#define RED_JU 1#define RED_MA 2#define RED_XIANG 3#define RED_SHI 4#define RED_JIANG 5#define RED_PAO 6#define RED_BIN 7#define BLACK_JU 8#define BLACK_MA 9#define BLACK_XIANG 10#define BLACK_SHI 11#define BLACK_JIANG 12#define BLACK_PAO 13#define BLACK_BIN 14/*----------------------------------------------------*/int firsttime=1;int savemode;char page_new=0,page_old=0;int finish=false,turn=BLACK,winner=0;int key;int redstate=SELECT,blackstate=SELECT;int board[10][9];/*----------------------------------------------------*/char *chessfile[15]={"","bmp\rju.wfb", "bmp\rma.wfb", "bmp\rxiang.wfb","bmp\rshi.wfb","bmp\rjiang.wfb","bmp\rpao.wfb","bmp\rbin.wfb","bmp\bju.wfb", "bmp\bma.wfb", "bmp\bxiang.wfb","bmp\bshi.wfb","bmp\bjiang.wfb","bmp\bpao.wfb","bmp\bbin.wfb"};char *boardfile[10][9]={{"bmp\11.wfb","bmp\1t.wfb","bmp\1t.wfb","bmp\14.wfb","bmp\15.wfb","bmp\16.wfb","bmp\1t.wfb","bmp\1t.wfb","bmp\19.wfb"},{"bmp\21.wfb","bmp\2c.wfb","bmp\2c.wfb","bmp\24.wfb","bmp\25.wfb","bmp\26.wfb","bmp\2c.wfb","bmp\2c.wfb","bmp\29.wfb"},{"bmp\21.wfb","bmp\3a.wfb","bmp\3t.wfb","bmp\34.wfb","bmp\3t.wfb","bmp\36.wfb","bmp\3t.wfb","bmp\3a.wfb","bmp\29.wfb"},{"bmp\41.wfb","bmp\4t.wfb","bmp\4a.wfb","bmp\4t.wfb","bmp\4a.wfb","bmp\4t.wfb","bmp\4a.wfb","bmp\4t.wfb","bmp\49.wfb"},{"bmp\51.wfb","bmp\52.wfb","bmp\5t.wfb","bmp\54.wfb","bmp\5t.wfb","bmp\56.wfb","bmp\5t.wfb","bmp\58.wfb","bmp\59.wfb"},{"bmp\61.wfb","bmp\62.wfb","bmp\6t.wfb","bmp\64.wfb","bmp\6t.wfb","bmp\66.wfb","bmp\6t.wfb","bmp\68.wfb","bmp\69.wfb"},{"bmp\71.wfb","bmp\7t.wfb","bmp\7a.wfb","bmp\7t.wfb","bmp\7a.wfb","bmp\7t.wfb","bmp\7a.wfb","bmp\7t.wfb","bmp\79.wfb"},{"bmp\81.wfb","bmp\8a.wfb","bmp\8t.wfb","bmp\84.wfb","bmp\85.wfb","bmp\86.wfb","bmp\8t.wfb","bmp\8a.wfb","bmp\89.wfb"},{"bmp\91.wfb","bmp\9t.wfb","bmp\9t.wfb","bmp\9t.wfb","bmp\95.wfb","bmp\9t.wfb","bmp\9t.wfb","bmp\9t.wfb","bmp\99.wfb"},{"bmp\101.wfb","bmp\102.wfb","bmp\102.wfb","bmp\104.wfb","bmp\105.wfb","bmp\106.wfb","bmp\108.wfb","bmp\108.wfb","bmp\109.wfb"}};char cursor[14][14]={0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,255,255,0,0,1,1,1,0,255,255,255,255,255,255,255,255,255,0,0,1,1,0,255,255,0,255,255,255,255,255,255,255,0,0,1,0,255,0,1,1,0,255,255,255,255,255,255,255,0,0,0,1,1,1,1,0,255,255,255,255,255,0,1,0,1,1,1,1,1,1,0,255,255,255,0,1,1,1,1,1,1,1,1,1,1,0,255,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1};struct pos{int x;int y;}position[10][9],redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blackoldpos; /*----------------------------------------------------*/selectpage(register char page) /*換頁(yè)函數(shù)*/{union REGS r;r.x.ax=0x4f05;r.x.bx=0;r.x.dx=page; /*選擇頁(yè)面*/int86(0x10,&r,&r);}unsigned char set_SVGA_mode(int vmode) /*設(shè)置SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f02;r.x.bx=vmode;int86(0x10,&r,&r);return(r.h.ah);}unsigned int get_SVGA_mode() /*獲取當(dāng)前SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f03;int86(0x10,&r,&r);return(r.x.bx);}drawbmp(int start_x,int start_y,char filename[]){char buffer[640];int i,j,k,n,r,g,b,width,length;long position;FILE *fp;if((fp=fopen(filename,"rb"))==NULL){printf("Error! Can't open file!");getch();return;}fseek(fp,28,SEEK_SET);fread(&i,2,1,fp);if(i!=8) /*檢查是否為256色位圖*/{puts("Error!Can't find bitmap!");fclose(fp);getch();exit(0);}fseek(fp,18,SEEK_SET);fread(&width,4,1,fp);fread(&length,4,1,fp);if(firsttime){fseek(fp,54,SEEK_SET);for(i=0;i<256;i++) /*按照該圖片的DAC色表設(shè)置色彩寄存器*/{b=fgetc(fp);g=fgetc(fp);r=fgetc(fp); /*獲取R、G、B分量*/outportb(0x3c8,i);outportb(0x3c9,r>>2); /*右移是要轉(zhuǎn)化為VGA的6位寄存器形式*/ outportb(0x3c9,g>>2);outportb(0x3c9,b>>2);fgetc(fp);}}elsefseek(fp,300,SEEK_SET);k=(width%4)?(4-width%4):0; /*寬度修正值*/for(j=length-1+start_x;j>=start_x;j--){fread(buffer,width,1,fp);for(i=start_y,n=0;i
看了“電腦下象棋是如何編程的 ”的人還看了: