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

************************************************************** 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_num;i++)
{
line(left,top-i*d,left+line_num*d,top-i*d);
}
for(i=0;i<=line_num;i++)
{
line(left+i*d,top,left+i*d,top-line_num*d);
}
for(i=0;i<=line_num;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<=line_num;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<=line_num;i++)
{
for(j=0;j<=line_num;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,&regs1,&regs1)-'0';
if(xx==('q'-'0')||xx==('Q'-'0'))
{ step=0;
return;
}
regs1.h.ah=0;
yy=int86(22,&regs1,&regs1)-'0';

if(yy==('q'-'0')||yy==('Q'-'0'))
{
step=0;
return ;
}
if(xx<0||xx>line_num)
{ inputerror();
continue;
}
if(yy<0||yy>line_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<=line_num;i++)
{
for(j=0;j<=line_num;j++)
point1[i][j]=0;
}
}

player2::player2()
{ int i,j;
for(i=0;i<=line_num;i++)
{
for(j=0;j<=line_num;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<4) {xx0--;m++;}
while(n<9&&xx0<=line_num)
{
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<4){yy0--;m++;}
while(n<9&&yy0<=line_num)
{
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<4){ xx0--; a++; m++;} m=0;
while(yy0<=line_num&&m<4){ yy0++; b++; m++;}
if(a<=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0<=line_num&&yy0>=0&&n<9)
{
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 <4){ xx0++; m++; a++;} m=0;
while(yy0 <4){ yy0--; m++; b++; }
if(a<=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0>=0&&yy0>=0&&n<9)
{
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<4) {xx0--;m++;}
while(n<9&&xx0<=line_num)
{
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<4){yy0--;m++;}
while(n<9&&yy0<=line_num)
{
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<4){ xx0--; a++; m++;} m=0;
while(yy0<=line_num&&m<4){ yy0++; b++; m++;}
if(a<=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0<=line_num&&yy0>=0&&n<9)
{
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 <4){ xx0++; m++; a++;} m=0;
while(yy0 <4){ yy0--; m++; b++; }
if(a<=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0>=0&&yy0>=0&&n<9)
{
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;

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
c++编写的五子棋游戏 // Five.cpp: implementation of the CFive class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "gobang.h" #include "Five.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif BOOL debugqian = false; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CFive::CFive() { } CFive::~CFive() { } BOOL CFive::has_five(int color) { //color = 1表示黑子,color = 2表示白子 //判断是否出现了5个子想连的情况 int m,n,i,j; for(m=1;m<16;m++) for(n=1;n<16;n++) { if(points[m][n]==color) { int p=1,s=1,zx=1,yx=1; for( i=1;i<5;i++) {if(n-i>=0&&points[m][n-i]==color) p++; else break; } for( i=1;i<5;i++) { if(n+i<16&&points[m][n+i]==color) p++; else break; } for( i=1;i<5;i++) { if(m-i>=0&&points[m-i][n]==color) s++; else break; } for( i=1;i<5;i++) { if(m+i<16&&points[m+i][n]==color) s++; else break; } for( i=1;i<5;i++) { if(m-i>=0&&n+i<16&&points[m-i][n+i]==color) zx++; else break; } for( i=1;i<5;i++) { if(n-i>=0&&m+i<16&&points[m+i][n-i]==color) zx++; else break; } for( i=1;i<5;i++) { if(m-i>=0&&n-i>=0&&points[m-i][n-i]==color) yx++; else break; } for( i=1;i<5;i++) { if(m+i<16&&n+i<16&&points[m+i][n+i]==color) yx++; else break; } if(p>=5||s>=5||zx>=5||yx>=5) return true; } } return false; } void CFive::Clear() { int i,j; for(i=1;i<=15;i++) for(j=1;j<=15;j++) { points[i][j] = 0;//先清空 } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值