⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hebai7_3.c

📁 黑白棋(othello)示例算法,TC实现,提供交互界面功能
💻 C
字号:
		      /*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);Drawbar(30,440,100,460,15);
 Drawbar(530,440,635,460,15);
 settextstyle(0,0,1);setcolor(4);outtextxy(50,445,"Exit");
 outtextxy(35,365,"New Game");outtextxy(35,325,"Map Size");
 outtextxy(60,405,"OK");outtextxy(530,445,"Introductions");
 Printscore();
 /*the above is to draw the chess map*/
 Show_mouse();
 for(;;)
 {
  Show_mouse();
  key.p=bioskey(1);
  if(key.c[0]==27) Exit();
  newxy(&x,&y,&buttons);
  if(x>30&&x<100&&y>440&&y<460&&buttons==1) Exit();
  if(x>30&&x<100&&y>400&&y<420&&buttons==1&&s==1) main();
  if(x>30&&x<100&&y>360&&y<380&&buttons==1){chessmapsize=10;main();}
  if(x>530&&x<635&&y>440&&y<460&&buttons==1) {Hide_mouse();Introduction();}
  if(x>30&&x<100&&y>320&&y<340&&buttons==1&&press==0)/*map size*/
   {k=1;r=1;s=1;Hide_mouse();delay(2000);}
  if(x>30&&x<100&&y>320&&y<340&&buttons==0&&press==0&&r==1)
  {r=0;Hide_mouse();chessmapsize=10;
   gotoxy(4,17);pr("%d*%d  ",chessmapsize,chessmapsize);
   Drawbar(80,240,100,260,15);Drawbar(80,270,100,290,15);
   Draw3(90,244,85,253,95,253,0);Draw3(90,286,85,277,95,277,0);}
  if(x>80&&x<=100&&y>=240&&y<=260&&buttons==1&&k==1)
   {r=1;Hide_mouse();delay(2000);}
   if(x>80&&x<=100&&y>=270&&y<=290&&buttons==1&&k==1)
   {r=1;Hide_mouse();delay(2000);}
   if(x>80&&x<=100&&y>=240&&y<=260&&buttons==0&&k==1&&r==1)
    {r=0;Hide_mouse();if(chessmapsize<mapmaxsize)chessmapsize+=2;
     gotoxy(4,17);pr("%d*%d  ",chessmapsize,chessmapsize);}
   if(x>80&&x<=100&&y>=270&&y<=290&&buttons==0&&k==1&&r==1)
    {r=0;Hide_mouse();if(chessmapsize>5) chessmapsize-=2;
      gotoxy(4,17);pr("%d*%d  ",chessmapsize,chessmapsize);}
  if(x>125&&x<525&&y>60&&y<460&&buttons==1)
   {press=1;j=(x-125)/M+1;i=(y-60)/M+1;Hide_mouse();
    gotoxy(1,1);pr("%d,%d  ",i,j);Putchess();}
 }/*for(;;) over*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -