分享
 
 
 

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;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有