C++语言五子棋源程序 (利用坐标确定棋子位置)

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

************************************************************** C++语言五子棋源程序

* 南阳理工学院计算机系01612班大鹏课程设计作品

* 运行环境:TC3.0 (vc6.0不支持画图函数,无法画出棋盘和棋子)

* Email:ypsky@mail2.nyist.net

************************************************************* #include

#include

#include

#include

#define backcolor CYAN

#define defaultcolor BLACK

#define linecolor MAGENTA

#define player1_color RED

#define player2_color WHITE

#define error_color RED

#define winner_color RED

const int left=40;

const int top=390;

const int d=30;

const int line_num=9;

const int turn=0;

const int r=d/3;

const int j=10;

int x,y,k=1,step=(line_num+1)*(line_num+1);

union REGS regs1,regs2;

class player1;

class player2;

class qipan{

public:

qipan();

~qipan(){};

void init_qipan();

friend void fall(player1 &num1,player2 &num2,qipan &num);

friend void input(player1 &num1,player2 &num2,qipan &num);

private:

int point[line_num+1][line_num+1];

};

class player1{

public:

player1();

~player1(){};

friend void fall(player1 &num1,player2 &num2,qipan &num);

friend void input(player1 &num1,player2 &num2);

friend int judge_winner(player1 &num1,player2 &num2);

private:

int point1[line_num+1][line_num+1];

};

class player2{

public:

player2();

~player2(){};

friend void fall(player1 &num1,player2 &num2,qipan &num);

friend void input(player1 &num1,player2 &num2,qipan &num);

friend int judge_winner(player1 &num1,player2 &num2);

private:

int point2[line_num+1][line_num+1];

};

void input(player1 &num1,player2 &num2);

void fall(player1 &num1,player2 &num2,qipan &num);

int judge_winner(qipan &num,player1 &num1,player2 &num2);

void inputerror();

void display_winner(int);

void main()

{

int driver=DETECT,mode;

initgraph(&driver,&mode,"e:\tc30\bgi");

qipan num;

player1 num1;

player2 num2;

while(step--)

{

input(num1,num2,num);

fall(num1,num2,num);

if(judge_winner(num1,num2))

{

display_winner(k);

}

}

// getchar();

}

qipan::qipan(void)

{ int j,i;

char ch[2]="0";

setbkcolor(backcolor);

setcolor(linecolor);

for(i=0;i

{

line(left,top-i*d,left+line_num*d,top-i*d);

}

for(i=0;i

{

line(left+i*d,top,left+i*d,top-line_num*d);

}

for(i=0;i

{ if(*ch=='9'+1) *ch='a';

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+i*d-2,top+r+3,ch);

(*ch)=(*ch)+1;

}

*ch='0';

for(i=0;i

{if(*ch=='9'+1) *ch='a';

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left-r-10,top-d*i-3,ch);

(*ch)=(*ch)+1;

}

setcolor(defaultcolor);

for(i=0;i

{

for(j=0;j

point[i][j]=0;

}

}

void fall(player1 &num1,player2 &num2,qipan &num)

{

int flag=k%2;

if(flag)

{ setcolor(player2_color);

num2.point2[x][y]=1;

num.point[x][y]=2;

circle(left+d*x,top-d*y,r);

setfillstyle(1,player2_color);

floodfill(left+d*x,top-d*y,player2_color);

}

else

{ num1.point1[x][y]=1;

num.point[x][y]=1;

setcolor(player1_color);

circle(left+d*x,top-d*y,r);

setfillstyle(1,player1_color);

floodfill(left+d*x,top-d*y,player1_color);

}

setcolor(defaultcolor);

}

void input(player1 &num1,player2 &num2,qipan &num)

{ char xx,yy;

k++;

while(1)

{

regs1.h.ah=0;

xx=int86(22,®s1,®s1)-'0';

if(xx==('q'-'0')||xx==('Q'-'0'))

{ step=0;

return;

}

regs1.h.ah=0;

yy=int86(22,®s1,®s1)-'0';

if(yy==('q'-'0')||yy==('Q'-'0'))

{

step=0;

return ;

}

if(xxline_num)

{ inputerror();

continue;

}

if(yyline_num)

{inputerror();

continue;

}

if(num.point[xx][yy]==0)

{

break;

}

else

{

inputerror();

continue;

}

}

x=(int)xx;

y=(int)yy;

setcolor(backcolor);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+d*line_num/3,top+d*2,"Input error");

setcolor(defaultcolor);

}

player1::player1()

{

int i,j;

for(i=0;i

{

for(j=0;j

point1[i][j]=0;

}

}

player2::player2()

{ int i,j;

for(i=0;i

{

for(j=0;j

point2[i][j]=0;

}

}

void inputerror(void)

{ setcolor(error_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

outtextxy(left+d*line_num/3,top+d*2,"Input error");

setcolor(defaultcolor);

}

int judge_winner(player1 &num1,player2 &num2)

{

int count=0,m=0,n=0,a=0,b=0,xx0,yy0;

int flag=k%2;

xx0=x; yy0=y;

if(!flag)

{ //left right

while(xx0=1&&m

while(n

{

if(num1.point1[xx0][y]==1)

{

count++;

if(count==5) return 1;

}

else

{

count=0;

}

n++;

xx0++;

}

//up down

count=0; xx0=x; m=0; n=0;

while(yy0=1&&m

while(n

{

if(num1.point1[x][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

n++;

yy0++;

}

//left up ----- right down

xx0=x;

yy0=y;

m=0;

n=0;

count=0;

while(xx0=1&&m

while(yy0

if(a

{

xx0=x-a; yy0=y+a;

}

else

{

xx0=x-b; yy0=y+b;

}

while(xx0=0&&n

{

if(num1.point1[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

xx0++;

yy0--;

n++;

}

//right up left down

count=0;

a=0;

b=0;

n=0;

m=0;

xx0=x;

yy0=y;

while(xx0

while(yy0

if(a

{

xx0=x+a;

yy0=y+a;

}

else

{

xx0=x+b;

yy0=y+b;

}

while(xx0=0&&yy0=0&&n

{

if(num1.point1[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

count=0;

xx0--;

yy0--;

n++;

}

//no winer

return 0;

}

else

{

//left right

while(xx0=1&&m

while(n

{

if(num1.point1[xx0][y]==1)

{

count++;

if(count==5) return 1;

}

else

{

count=0;

}

n++;

xx0++;

}

//up down

count=0; xx0=x; m=0; n=0;

while(yy0=1&&m

while(n

{

if(num2.point2[x][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

n++;

yy0++;

}

//left up ----- right down

xx0=x;

yy0=y;

m=0;

n=0;

count=0;

while(xx0=1&&m

while(yy0

if(a

{

xx0=x-a; yy0=y+a;

}

else

{

xx0=x-b; yy0=y+b;

}

while(xx0=0&&n

{

if(num2.point2[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

{

count=0;

}

xx0++;

yy0--;

n++;

}

//right up left down

count=0;

a=0;

b=0;

n=0;

m=0;

xx0=x;

yy0=y;

while(xx0

while(yy0

if(a

{

xx0=x+a;

yy0=y+a;

}

else

{

xx0=x+b;

yy0=y+b;

}

while(xx0=0&&yy0=0&&n

{

if(num2.point2[xx0][yy0]==1)

{

count++;

if(count==5)

return 1;

}

else

count=0;

xx0--;

yy0--;

n++;

}

//no winer

return 0;

}

}

void display_winner(int k)

{

int flag=k%2;

if(!flag)

{ setcolor(winner_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(left+d*2,top+40,"Red is winner");

setcolor(defaultcolor);

step=0;

getchar();

}

else

{ setcolor(winner_color);

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(left+2*d,top+40,"White is winner");

setcolor(defaultcolor);

step=0;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航