• PUDN用户
    了解作者
  • C/C++
    开发工具
  • 3KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 26
    下载次数
  • 2005-08-31 14:24
    上传日期
黑白棋(othello)示例算法,TC实现,提供交互界面功能
OTHELLOCHESS.rar
  • HEBAI7_3.C
    9.6KB
  • www.pudn.com.txt
    218B
内容介绍
/*BLACK AND WHITE CHESS*/ /*Introductions:1.black first; 2.black put 1,white put 2; 3.mapmaxsize 30*30; */ #include <graphics.h> #include <stdio.h> #include <stdlib.h> #include "time.h" #include "conio.h" #include "dos.h" #include "math.h" #define pr printf #define mapmaxsize 30 int chessmapsize=10, M,blacknum=2,whitenum=2; int step[32][32],count=1,i,j; int backcolor=3; int xmin,xmax,ymin,ymax,x_max=639,y_max=479; union REGS regs; Background(float p/*full of stars*/) { float x=-0.1,y=0.1,tmpx,tmpy; int xCord,yCord,nTimes; int xMin=-8,xMax=12,yMin=-6,yMax=8; float xPixel,yPixel; // float p=0.226; /*in (-0.01,0.99)*/ xPixel=640/(xMax-xMin); yPixel=480/(yMax-yMin); // cleardevice(); for(nTimes=0;nTimes<30000;nTimes++) { tmpx=y+p*x+2*x*x/(1+x*x+y*y); tmpy=-x+p*tmpx+2*tmpx*tmpx*(1-p)/(1+tmpx*tmpx); x=tmpx;y=tmpy; xCord=(x-xMin)*xPixel; yCord=(y-yMin)*yPixel; putpixel(xCord,yCord,random(15)); } return(0);} int init(int xmi,int xma,int ymi,int yma) {int retcode; regs.x.ax=0;int86(51,&regs,&regs);retcode=regs.x.ax; if(retcode==0)return 0; regs.x.ax=7;regs.x.cx=xmi;regs.x.dx=xma;int86(51,&regs,&regs); regs.x.ax=8;regs.x.cx=ymi;regs.x.dx=yma;int86(51,&regs,&regs); return retcode;} Hide_mouse() {union REGS iregs;iregs.x.ax=2;int86(0x33,&iregs,&iregs);} Show_mouse() {union REGS regs;regs.x.ax=1;int86(0x33,&regs,&regs);regs.x.ax=2;} int read(int *mx,int *my,int *mbutt) {int xx0=*mx,yy0=*my,buto=0,xnew,ynew; do{ regs.x.ax=3;int86(51,&regs,&regs); xnew=regs.x.cx;ynew=regs.x.dx;*mbutt=regs.x.bx;} while(xnew==xx0&&ynew==yy0&&*mbutt==buto); *mx=xnew;*my=ynew; if(*mbutt){*mx=xnew;*my=ynew;return(-1);} else {*mx=xnew;*my=ynew;return 1;}} void newxy(int *mx,int *my,int *mbutt) {int ch,xx0=*mx,yy0=*my,x,y,xm,ym;ch=read(&xm,&ym,mbutt);*mx=xm;*my=ym;} int over() {char ccc; settextstyle(0,0,2);setcolor(RED); if(blacknum==whitenum) outtextxy(250,330,"No winner"); if(blacknum>whitenum) outtextxy(150,330,"The winner is black chess"); if(blacknum<whitenum) outtextxy(150,330,"The winner is white chess"); Drawbar(150,200,490,260,0); setcolor(15);rectangle(150,200,490,260);settextstyle(0,0,1); outtextxy(170,205," Would you like to have a new game?"); outtextxy(250,230,"Yes");outtextxy(350,230,"No"); setcolor(8);outtextxy(250,230,"Y");outtextxy(350,230,"N"); while(1) {ccc=getch(); if(ccc=='Y'||ccc=='y'||ccc==13) main(); if(ccc=='N'||ccc=='n'||ccc==27) Exit();}} Exit() {char ccc; Hide_mouse();cleardevice();Background(0.4878); moveto(140,50);settextstyle(1,0,5);setcolor(9);outtext("Thanks for playing"); moveto(300,100);settextstyle(1,0,2);setcolor(YELLOW);outtext("By"); moveto(220,135);settextstyle(0,0,2);setcolor(3);outtext(" Hu Guilin"); moveto(270,170);settextstyle(0,0,1);setcolor(15);outtext("7.29.2000"); rectangle(200,200,400,260);outtextxy(215,205,"Are you sure to exit?"); outtextxy(250,230,"Yes");outtextxy(350,230,"No"); setcolor(8);outtextxy(250,230,"Y");outtextxy(350,230,"N"); while(1) {ccc=getch(); if(ccc=='Y'||ccc=='y'||ccc==13) exit(0); if(ccc=='N'||ccc=='n') main();}cleardevice();} Introduction() {char c; cleardevice(); setcolor(YELLOW);settextstyle(0,0,2);outtextxy(200,50,"Introductions"); setcolor(15);settextstyle(0,0,1);outtextxy(125,80,"1.Black put first;"); outtextxy(125,95,"2.If you want to change the map size,click"); outtextxy(135,110,"'Map Size' before playing,then click 'OK';"); outtextxy(125,125,"3.If you find that no place for black or white"); outtextxy(135,140,"chess to put,click 'New Game'."); setcolor(9);outtextxy(180,230,"Press any key to continue."); Background(0.623); c=getch();main();} Drawbar(int x1,int y1,int x2,int y2,int color) {int a; if(y1>y2){a=y1;y1=y2;y2=a;} setcolor(color);setlinestyle(0,0,1);for(a=y1;a<=y2;a++) line(x1,a,x2,a);} Drawcircle(int i,int j,int bw) {int x=125+(j-1)*M+M/2,y=60+(i-1)*M+M/2,r=M/2-3,color; if(bw==1) color=0;if(bw==2) color=15; setcolor(color);setfillstyle(SOLID_FILL,color); circle(x,y,r);floodfill(x,y,color);} void Draw3(int i,int j,int x,int y,int m,int n,int color) {setcolor(color);setlinestyle(0,0,1); moveto(i,j);lineto(x,y); lineto(m,n); lineto(i,j);} Drawlaughter(int x,int y,int feeling) {setlinestyle(0,0,1);setcolor(YELLOW); circle(x,y,18);setfillstyle(SOLID_FILL,YELLOW);floodfill(x,y,YELLOW); setcolor(0);circle(x-8,y-5,1);circle(x+8,y-5,1); if(feeling==1) arc(x,y,210,330,15);if(feeling==0) arc(x,y+18,40,140,15);} Printscore() {int a,b; gotoxy(6,11);pr("%d ",blacknum);gotoxy(74,11);pr("%d ",whitenum); if(count%2==1){Draw3(35,200,55,200,45,183,YELLOW);Draw3(580,200,600,200,590,183,BLACK);} if(count%2==0){Draw3(35,200,55,200,45,183,BLACK);Draw3(580,200,600,200,590,183,YELLOW);} } int Putchess() {int x,y,p=(count%2==1)?1:2,q=(count%2==1)?2:1,a,b,add=0; if(step[i][j]!=0) return 0; else{ y=j-1;while(step[i][y]==q&&y>0)y--; if(step[i][y]==p) {for(a=y+1;a<j;a++){step[i][a]=p;Drawcircle(i,a,p);}add+=j-y-1;}/*left*/ y=j+1;while(step[i][y]==q&&y<=chessmapsize)y++; if(step[i][y]==p) {for(a=j+1;a<y;a++){step[i][a]=p;Drawcircle(i,a,p);}add+=y-j-1;}/*right*/ x=i-1;while(step[x][j]==q&&x>0)x--; if(step[x][j]==p) {for(a=x+1;a<i;a++){step[a][j]=p;Drawcircle(a,j,p);}add+=i-x-1;}/*up*/ x=i+1;while(step[x][j]==q&&x<=chessmapsize)x++; if(step[x][j]==p) {for(a=i+1;a<x;a++){step[a][j]=p;Drawcircle(a,j,p);}add+=x-i-1;}/*down*/ x=i-1;y=j-1;while(step[x][y]==q&&x>0&&y>0){x--;y--;} if(step[x][y]==p) {for(a=x+1,b=y+1;a<i&&b<j;a++,b++) {step[a][b]=p;Drawcircle(a,b,p);}add+=i-x-1;}/*leftup*/ x=i+1;y=j-1;while(step[x][y]==q&&x<=chessmapsize&&y>0){x++;y--;} if(step[x][y]==p) {for(a=x-1,b=y+1;a>i&&b<j;a--,b++) {step[a][b]=p;Drawcircle(a,b,p);}add+=j-y-1;}/*leftdown*/ x=i-1;y=j+1;while(step[x][y]==q&&y<=chessmapsize&&x>0){x--;y++;} if(step[x][y]==p) {for(a=x+1,b=y-1;a<i&&b>j;a++,b--) {step[a][b]=p;Drawcircle(a,b,p);}add+=i-x-1;}/*rightup*/ x=i+1;y=j+1;while(step[x][y]==q&&y<=chessmapsize&&x<=chessmapsize){x++;y++;} if(step[x][y]==p) {for(a=x-1,b=y-1;a>i&&b>j;a--,b--) {step[a][b]=p;Drawcircle(a,b,p);}add+=y-j-1;}/*rightdown*/ if(add!=0) {step[i][j]=p;Drawcircle(i,j,p); if(p==1){blacknum+=add+1;whitenum-=add;} else{whitenum+=add+1;blacknum-=add;} Drawbar(30,100,70,140,0);Drawbar(570,100,610,140,0); if(blacknum>whitenum){Drawlaughter(50,120,1);Drawlaughter(590,120,0);} if(blacknum<whitenum){Drawlaughter(50,120,0);Drawlaughter(590,120,1);} if(blacknum==whitenum){Drawlaughter(50,120,1);Drawlaughter(590,120,1);} count+=1; Printscore(); if(count==chessmapsize*chessmapsize-3) over(); return 1;}}} main() {int gdriver=DETECT,gmode,a,b,x,y,buttons,press=0,k=0,r=0,s=0; char c; union {char c[2];int p;}key; initgraph(&gdriver,&gmode,"..\\bgi"); blacknum=2;whitenum=2;count=1; cleardevice();Background(0.623); for(i=0;i<=chessmapsize;i++)for(j=0;j<=chessmapsize;j++) step[i][j]=0; M=400/chessmapsize; /*the following is to draw the chess map*/ setcolor(9);settextstyle(1,0,3); outtextxy(125,5,"Welcome to the Black&White chess"); Drawbar(125,60,525,460,backcolor);setcolor(6); setlinestyle(0,0,3);rectangle(125,60,525,460);setlinestyle(1,0,1); for(a=0;a<=chessmapsize;a++) line(125,60+M*a,525,60+M*a); for(b=0;b<=chessmapsize;b++) line(125+M*b,60,125+M*b,460); i=j=chessmapsize/2; step[i][j]=1;Drawcircle(i,j,1);i+=1;step[i][j]=2;Drawcircle(i,j,2); j+=1;step[i][j]=1;Drawcircle(i,j,1);i-=1;step[i][j]=2;Drawcircle(i,j,2); settextstyle(0,0,2);setcolor(6);outtextxy(10,80,"BLACK");outtextxy(550,80,"WHITE"); Drawlaughter(50,120,1);Drawlaughter(590,120,1); Drawbar(30,400,100,420,15);Drawbar(30,360,100,380,15); Drawbar(30,320,100,340,15)
评论
    相关推荐
    • VC++黑白棋.rar
      实现黑白棋的VC++源码,可供初学者学习
    • othello.zip
      黑白棋编程法语版(因为这个游戏编程在法国学的嘛),用tkinter做出来的
    • 黑白棋java源码-othello:奥赛罗
      黑白棋java源码黑白棋 AI 竞赛 先决条件 Java 8 Maven 和/或 Eclipse/IntelliJ 开始 Maven 查看代码: git clone https://github.com/davstromb/othello.git 进入这个项目的根目录 通过键入mvn clean install构建...
    • Othello:用java开发黑白棋游戏
      奥赛罗 用java开发黑白棋游戏
    • Othello_AI:AI 玩黑白棋
      一个玩黑白棋的人工智能,它使用 NegaScout 和位板来更有效地搜索游戏树。
    • Othello-JS:黑白棋
      黑白棋-JS 我在 javascript 中实现黑白棋 AI(minimax + alpha-beta pruning)的尝试。
    • 黑白棋代码matlab-othello:带有AI的经典黑白棋游戏
      黑白棋代码matlab 奥赛罗 运行程序 带有 AI 的经典黑白棋游戏 所有 .c 文件都应该先用 mex 编译。 您可以在 MATLAB 命令窗口中执行此操作: mex getAllValid.c mex utility_c.c 然后你可以通过运行来运行主程序: ...
    • 黑白棋java源码-Othello:奥赛罗
      黑白棋java源码jOthelloT(Java 黑白棋锦标赛) 抽象的 入门人工智能本科班经常介绍使用不同搜索算法的不同搜索方法。 在这种情况下,经常教授的算法之一是 minimax 算法,它用于对抗性游戏,您希望在最大化获胜机会...
    • 黑白棋java源码-Othello:旧的黑白棋项目
      黑白棋java源码要玩黑白棋,请执行以下命令: git 克隆 cd奥赛罗 java -jar 黑白棋.jar 您可以访问源代码: git checkout 开发 光盘源
    • Othello-Agent:黑白棋游戏 AI 代理
      黑白棋游戏 AI 代理 游戏状态:游戏板被实现为 BitBoard 以加快搜索速度。 算法:采用 Alpha-Beta 剪枝的 Min-Max 算法。 启发式 - 启发式函数包含来自的游戏策略,并采用加权平均值。 输入- input.txt 将包含: ...