/*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,®s,®s);retcode=regs.x.ax;
if(retcode==0)return 0;
regs.x.ax=7;regs.x.cx=xmi;regs.x.dx=xma;int86(51,®s,®s);
regs.x.ax=8;regs.x.cx=ymi;regs.x.dx=yma;int86(51,®s,®s);
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,®s,®s);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,®s,®s);
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)