1、黑白棋C语言: 某黑白棋代码/othello by vilinov#include #include #include #include #define NONE 0#define BLACK 1#define WHITE 2#define DUMMY 3#define f(x,y) (x)*9+yvoiddraw();voidMoveGenerator(int deep);/*intgetnum();voidturn(int deep,int num);/*voidback(int deep);/*intAI();voidhelp();intcheck(int num);intCheckWa
2、y(int num);intSetLevel();voidEndGenerator(int deep);intHappyEnding(int deep);intBadEnding(int deep,int alpha,int beta);intEnding(int deep,int alpha,int beta);intsearch(int deep);intAlphaBeta(int deep,int alpha,int beta);intPVS(int deep,int alpha,int beta);intMAX_DEPTH=10;intEND_DEPTH=14;intcolour=BL
3、ACK;intcomputer=WHITE;intblack=2,white=2;intnote;charboard91= 3,3,3,3,3,3,3,3,3, 3,0,0,0,0,0,0,0,0, 3,0,0,0,0,0,0,0,0, 3,0,0,0,0,0,0,0,0, 3,0,0,0,2,1,0,0,0, 3,0,0,0,1,2,0,0,0, 3,0,0,0,0,0,0,0,0, 3,0,0,0,0,0,0,0,0, 3,0,0,0,0,0,0,0,0, 3,3,3,3,3,3,3,3,3,3;intdirection8=-10,-9,-8,-1,1,8,9,10;intvalue91=
4、 0,0,0,0,0,0,0,0,0, 0, 40,-8,4,3,3,4,-8, 40, 0,-8,-10,2,1,1,2,-10,-8, 0, 4, 2,3,1,1,3, 2, 4, 0, 3, 1,1,2,2,1, 1, 3, 0, 3, 1,1,2,2,1, 1, 3, 0, 4, 2,3,1,1,3, 2, 4, 0,-8,-10,2,1,1,2,-10,-8, 0, 40,-8,4,3,3,4,-8, 40, 0,0,0,0,0,0,0,0,0;intMoveList2030;intTurnList2010;/0-7 各方向上的吃子个数。8 num。9 总吃子个数intspace20
5、;/ENDing searchintflag20;intBestScore;intBestMove;intmove61=0,/这个零是“捣乱”的。说笑,为了一点点的效率提升/*C4*/31,32,58,59,39,42,48,51,/*C3*/30,33,57,60,/*A1*/10,17,73,80,/*A3*/12,15,75,78,28,35,55,62,/*A4*/13,14,76,77,37,44,46,53,/*B3*/21,24,66,69,29,34,56,61,/*B4*/22,23,67,68,38,43,47,52,/*A2*/11,16,74,79,19,26,64,7
6、1,/*B2*/20,25,65,70;intway;/check()&CheckWay()&turn()&back()intnow;/CheckWay()intcount;/CheckWay()inttemp;/MoveGenerator()&turn()&back()&AI()&G()clock_ttime1,time2;voidNEW() colour=BLACK; computer=WHITE; black=2,white=2; int x,y; for(x=0;x91;x+) boardx=3; for(x=1;x9;x+) for(y=1;y9;y+) boardf(x,y)=0;
7、 boardf(4,4)=2; boardf(5,5)=2; boardf(4,5)=1; boardf(5,4)=1;voiddraw() int x,y; printf( 12345678); for(x=1;x9;x+) printf(n%c,(char)x-1+a); for(y=1;y0;temp-) if(check(movetemp) MoveListdeep +MoveListdeep0 =movetemp; return;intcheck(int num) if(boardnum) return 0; for(way=7;way=0;way-) if(CheckWay(num
8、) return 1; return 0;intCheckWay(int num) count=0; for(now=num+directionway; boardnow&boardnow!=DUMMY;now+=directionway ) if(boardnow=colour) return count; count+; return 0;voidturn(int deep,int num) TurnListdeep9=0; for(way=7;way=0;way-) temp=CheckWay(num); TurnListdeepway=temp; TurnListdeep9+=temp
9、; for(now=num+directionway;temp0;temp-,now+=directionway) boardnow=colour; TurnListdeep8=num; boardnum=colour; if(colour=WHITE) white+=TurnListdeep9+1; black-=TurnListdeep9; else white-=TurnListdeep9; black+=TurnListdeep9+1; return;voidback(int deep) int temp2=3-colour,num=TurnListdeep8; for(way=7;w
10、ay=0;way-) for(temp=TurnListdeepway,now=num+directionway;temp0;temp-,now+=directionway) board now =temp2; board num =NONE; if(colour=WHITE) white-=TurnListdeep9+1; black+=TurnListdeep9; else white+=TurnListdeep9; black-=TurnListdeep9+1; return;intgetnum() char s30; int lenS; int num; for(;) if(colou
11、r=computer) return AI(); printf(); scanf(%s,s); lenS=strlen(s); if(lenS=2) if(s0=a&s0=1&s1=8) num=f(s0-a+1,s1-1+1); if(check(num) return num; if(lenS=1) if(s0=q) exit(2); if(s0=h) help(); if(s0=c) computer=3-computer; if(s0=s) computer=1-computer; if(s0=d) draw(); if(s0=n) return 0; if(s0=l) SetLeve
12、l(); if(lenS=4&!strcmp(exit,s) exit(2); if(lenS=4&!strcmp(help,s) help(); if(lenS=4&!strcmp(draw,s) draw(); if(lenS=3&!strcmp(new,s) return 0; if(lenS=5&!strcmp(level,s) SetLevel(); intSetLevel() char c; printf(当前中盘搜索深度%d层,终盘搜索深度%d层n,MAX_DEPTH,END_DEPTH); while(1) printf(设置中盘搜索深度(1-12):); scanf(%d,&
13、MAX_DEPTH); if (feof(stdin) | ferror(stdin) printf(n输入错误n); exit(2); while ( (c = getchar() != n & c != EOF ) ; if(MAX_DEPTH12) continue; break; ; while(1) printf(设置尾盘搜索深度(1-16):); scanf(%d,&END_DEPTH); if (feof(stdin) | ferror(stdin) printf(n输入错误n); exit(2); while ( (c = getchar() != n & c != EOF )
14、 ; if(END_DEPTH16) continue; break; ; return 0;voidhelp() printf(-HELP-n); printf(h,help 帮助n); printf(a1.h8 棋子地点n); printf(q,exit 退出n); printf(d,draw 画出棋盘n); printf(n,new 重新开始n); printf(l,level 设置等级n); printf(c 让步n); printf(s 双人下棋(再按一次则返回人机)n); printf(-END-n); return;intAI() space0=64-white-black; m
15、emset(flag,0,sizeof(flag); if(space0END_DEPTH) return search(MAX_DEPTH); space0=0; for(temp=60;temp0;temp-) if(!board movetemp ) space +space0 =movetemp; return HappyEnding(space0);/o(_)ointG() int val=0; for(temp=10;temp0;temp-) if(check(spacetemp) MoveListdeep +MoveListdeep0 =spacetemp; return;int
16、HappyEnding(int deep) int score; note+; EndGenerator(deep); /the first note turn(deep,MoveListdeep MoveListdeep0 ); BestScore=-Ending(deep-1,-65,65); BestMove=MoveListdeep MoveListdeep0 ; back(deep); MoveListdeep0-; /first note end for(;MoveListdeep00;MoveListdeep0-) turn(deep,MoveListdeep MoveListd
17、eep0 ); score=-BadEnding(deep-1,-BestScore-1,-BestScore); if(scoreBestScore) BestScore=-Ending(deep-1,-65,-score); BestMove=MoveListdeep MoveListdeep0 ; back(deep); return BestMove;intBadEnding(int deep,int alpha,int beta) int current=-65,score; note+; if(!deep) if(colour=WHITE) return black-white;
18、else return white-black; colour=3-colour; EndGenerator(deep); if(!MoveListdeep0)/no move if(flagdeep)/game over if(colour=WHITE) colour=3-colour; return white-black; else colour=3-colour; return black-white; /让步英语怎么说? flagdeep=1; score=-BadEnding(deep,-beta,-alpha); flagdeep=0; colour=3-colour; retu
19、rn score; for(;MoveListdeep00;MoveListdeep0-) turn(deep,MoveListdeep MoveListdeep0 ); score=-BadEnding(deep-1,-beta,-alpha); back(deep); if(scorecurrent) current=score; if(score=beta) break; colour=3-colour; return current;intEnding(int deep,int alpha,int beta) int best,score; note+; if(!deep) if(co
20、lour=WHITE) return black-white; else return white-black; colour=3-colour; EndGenerator(deep); if(!MoveListdeep0)/no move if(flagdeep)/game over if(colour=WHITE) colour=3-colour; return white-black; else colour=3-colour; return black-white; /让步 flagdeep=1; score=-Ending(deep,-beta,-alpha); flagdeep=0
21、; colour=3-colour; return score; /first note turn(deep,MoveListdeep MoveListdeep0 ); best=-Ending(deep-1,-beta,-alpha); back(deep); MoveListdeep0-; /note end for(;MoveListdeep00;MoveListdeep0-) if(bestalpha) alpha=best; turn(deep,MoveListdeep MoveListdeep0 ); score=-BadEnding(deep-1,-alpha-1,-alpha)
22、; if(scorealpha&scorebest) best=score; back(deep); colour=3-colour; return best;/eNDing eND/searchintsearch(int deep) int score; note+; MoveGenerator(deep); /the first note turn(deep,MoveListdeep MoveListdeep0 ); BestScore=-PVS(deep-1,-32767,32767); BestMove=MoveListdeep MoveListdeep0 ; back(deep);
23、MoveListdeep0-; /first note end for(;MoveListdeep00;MoveListdeep0-) turn(deep,MoveListdeep MoveListdeep0 ); score=-AlphaBeta(deep-1,-BestScore-1,-BestScore); if(scoreBestScore) BestScore=-PVS(deep-1,-32767,-score); BestMove=MoveListdeep MoveListdeep0 ; back(deep); return BestMove;intAlphaBeta(int de
24、ep,int alpha,int beta) int current=-32767,score; note+; if(!deep) if(colour=WHITE) return -G(); else return G(); colour=3-colour; MoveGenerator(deep); if(!MoveListdeep0)/no move if(flagdeep)/game over if(colour=WHITE) colour=3-colour; return G(); else colour=3-colour; return -G(); /让步英语怎么说? flagdeep
25、=1; score=-AlphaBeta(deep,-beta,-alpha); flagdeep=0; colour=3-colour; return score; for(;MoveListdeep00;MoveListdeep0-) turn(deep,MoveListdeep MoveListdeep0 ); score=-AlphaBeta(deep-1,-beta,-alpha); back(deep); if(scorecurrent) current=score; if(score=beta) break; colour=3-colour; return current;intPVS(int deep,int alpha,int beta) int best,
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1