用opencv和vc++6.0开发的五子棋游戏软件

用opencv和vc++6.0开发的五子棋软件

需要的工具如下

1)opencv1.0

2)vc++ 6.0

// wuziqi.cpp : Defines the entry point for the console application.

//
#include "stdafx.h"
#include
#include
#include
#include
#include "windows.h"
#include "stdio.h"


#define up 'w'
#define left 'a'
#define down 's'
#define right 'd'
#define lz 'p'
#define cls 'q'


#include
#include

int SCORE[15][15]={0};

struct stu  //定义位置记录结构体
{
int x;
int y;
} weizhi;

int player=2;
int Q[15][15]={0}; //定义数组以记录落子情况

void idtoxy(int id,int &x,int &y)
{
x=id%15;
y=id/15;
}
void xytoid(int &id,int &x,int &y)
{
id=x+15*y;
}

int isc(int w,int b)
{ int sc1[4]={35,800,15000,800000};
int sc2[4]={15,400,1800,100000}; 
if(w==0&&b==0) return  7;
if(w+b==5) return  0;
if(w>=1&&b>=1) return  0;
if(w==0) return sc1[b-1];
if(b==0) return sc2[w-1];
}

int s1(int i,int j,int n)
{
if(j-n>=0&&j-n<=10)
return 1;
else return 0;
}
int score_s1(int i,int j,int n)
{
int w=0; int k;
int b=0;
int score=0;
for(k=0;k<5;k++)
{if(Q[i][j+k-n]==2)  //人
w++;
else if(Q[i][j+k-n]==1)
b++;
}
score+=isc(w,b);
return score;
}
int s2(int i,int j,int n)
{
if(i-n>=0&&i-n<=10)
return 1;
else return 0;
}
int score_s2(int i,int j,int n)
{
int w=0; int k;int b=0;
int score=0;
for(k=0;k<5;k++)
{if(Q[i+k-n][j]==2)  //人
w++;
else if(Q[i+k-n][j]==1)
b++;
}
score+=isc(w,b);
return score;
}
int s3(int i,int j,int n)
{
// if(n==0 && i>=0 i<=10  && j>=0 j<=10 )
if( i>=n&& i<=10+n  && j>=n && j<=10+n )
return 1;
return 0;
}
int score_s3(int i,int j,int n)
{
int w=0; int k;int b=0;
int score=0;

for(k=0;k<5;k++)
{if(Q[i+k-n][j+k-n]==2)  //人
w++;
else if(Q[i+k-n][j+k-n]==1)
b++;
}
score+=isc(w,b);
return score;
}
int s4(int i,int j,int n)
{
// if(n==0 && i>=0 i<=10  && j>=0 j<=10 )
if( i>=n&& i<=10+n  && j>=4-n && j<=14-n )
return 1;
return 0;
}

int score_s4(int i,int j,int n)
{
int w=0; int k;int b=0;
int score=0;
for(k=0;k<5;k++)
{if(Q[i+k-n][j-k+n]==2)  //人
w++;
else if(Q[i+k-n][j-k+n]==1)
b++;
}
score+=isc(w,b);
//if(score!=7) printf("x:%d y:%d score:%d",i,j,score);
return score;
}

void gotoxy(int x, int y) //建立坐标函数 
{   
COORD c;   
c.X=2*x;   
c.Y=y; 
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c); //修改当前光标的位置
}
void ai(int &mx,int &my)
{
//struct yuanzu na[1000];
int i,j,n; 
int max=0; //int mx;int my;
//水平
int index=0;
for(i=0;i<15;i++)
{
for(j=0;j<15;j++)
{
if(Q[i][j]!=0)
continue;
int sum=0; int hh=0;
for(n=0;n<5;n++)
{
hh=s1(i,j,n)*score_s1(i,j,n); //printf(< sum+=hh;
}
for(n=0;n<5;n++)
{
hh=s2(i,j,n)*score_s2(i,j,n); //printf(< sum+=hh;
}
for(n=0;n<5;n++)  //左斜
{
hh=s3(i,j,n)*score_s3(i,j,n);
sum+=hh;
}
for(n=0;n<5;n++)
{
hh=s4(i,j,n)*score_s4(i,j,n);
sum+=hh;
}
//SCORE[i][j]=sum;
if(sum>max)
{max=sum;// printf("max:%d",max);
mx=i;
my=j;}
}
}
// gotoxy(19,19);
// printf("x:%d y:%d max:%d\n",mx,my,max);
}
/*
void drawqipan() //绘制棋盘及数据初始化
{
int i,j;
system("cls"); //清除屏幕
for(i=0;i<15;i++)
{
for(j=0;j<15;j++)
{
Q[i][j]=0;
printf("十");
}
printf("\n");
}
weizhi.x=0;weizhi.y=0; //程序数据初始化
gotoxy(0,0);
}

void jilu() //记录落子情况
{
Q[weizhi.x][weizhi.y]=2;
if(player)
{
player=0;  //玩家变换
return;
}
player=1;
}

int cluozi(int x,int y) //由电脑落子时调用
{
weizhi.x=x;weizhi.y=y;
gotoxy(weizhi.x,weizhi.y);
if(Q[weizhi.x][weizhi.y]==0) //判断当前位置是否已经落子
{
jilu();
printf("○");
gotoxy(weizhi.x,weizhi.y);
}
return 0;
}*/
/*
void luozi() //玩家落子
{
if(Q[weizhi.x][weizhi.y]==0) //判断当前位置是否已经落子
{
if(player)
{
jilu();
printf("●"); 
gotoxy(weizhi.x,weizhi.y);
}
//else
{ int x,y;
ai(x,y);
gotoxy(x,y);
//jilu();
printf("○");
Q[x][y]=1;
player=1;
} }
}
*/

int checkWin() //检查游戏是否有输赢
{
int p;
int r,c,rr,cc,count=0;
p=player==2?2:1;
for(c=0;c<15;c++)
{
for(r=0;r<15;r++)
{
if(Q[r][c]!=p)
continue;
//检查列
rr=r;cc=c;
while(--cc>=0 &&Q[rr][cc]==p)count++;  cc=c;
while(++cc<20 &&Q[rr][cc]==p)count++;  cc=c;
if(count+1>=5)
return p;
//检查行
count=0;
while(--rr>=0 &&Q[rr][cc]==p)count++;  rr=r;
while(++rr<20 &&Q[rr][cc]==p)count++;  rr=r;
if(count+1>=5)
return p;
//检查反斜边
count=0;
cc--;rr--;
while((cc>=0||rr>=0) &&Q[rr][cc]==p){count++;cc--;rr--;}  rr=r;cc=c;
cc++;rr++;
while((cc<20||rr<20) &&Q[rr][cc]==p){count++;cc++;rr++;}  rr=r;cc=c;
if(count+1>=5)
return p;
count=0;
//检查正斜边
count=0;
cc++;rr--;
while((cc<20||rr>=0) &&Q[rr][cc]==p){count++;cc++;rr--;}  rr=r;cc=c;
cc--;rr++;
while((cc>=0||rr<20) &&Q[rr][cc]==p){count++;cc--;rr++;}  rr=r;cc=c;
if(count+1>=5)
return p;
count=0;
}
}
return 0;
}
/*
void Keypress(char n) //光标位置移动
{
switch(n)
{
case up:if(weizhi.y<=0)weizhi.y=19;else weizhi.y--;gotoxy(weizhi.x,weizhi.y);break;
//向上移动光标
case left:if(weizhi.x<=0)weizhi.x=19;else weizhi.x--;gotoxy(weizhi.x,weizhi.y);break;
//向左移动光标
case right:if(weizhi.x>=19)weizhi.x=0;else weizhi.x++;gotoxy(weizhi.x,weizhi.y);break;
//向右移动光标
case down:if(weizhi.y>=19)weizhi.y=0;else weizhi.y++;gotoxy(weizhi.x,weizhi.y);break;
//向下移动光标
case lz:luozi();break;
//开始落子操作
case cls:drawqipan();break;
//重新开始
}
}*/
int fx=20;
int fy=20;
IplImage* painty;
int num=0;
char nums[10];

//IplImage* src=0;  
void drawai()
{
//else
cvWaitKey(1000); 
player=1;
int x1,y1;
ai(x1,y1);
//gotoxy(x,y);
//jilu();
//printf("○");
CvFont font;    
cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 0, 1);    
sprintf(nums,"%d",num++);
cvCircle(painty, cvPoint(fy+y1*30,fx+x1*30), 15,CV_RGB(255, 0,0) ,CV_FILLED, CV_AA, 0 );  
cvPutText(painty, nums, cvPoint(fy+y1*30-10,fx+x1*30+5), &font, CV_RGB(0,0,0));
cvShowImage( "src", painty ); 
Q[x1][y1]=1;
int winer=checkWin();
if(winer!=0)
{
if(winer==2)
printf("the side of player ● wins\n");
else if(winer==1)
printf("the side of ai ○ wins\n");
}
player=2;
}

void onMouse(int event,int x,int y,int flags,void* param )
{
    // printf("( %d, %d) ",x,y);
 if( event == CV_EVENT_LBUTTONDOWN )  
    {  
        //CvPoint pt = cvPoint(x,y);  
        //char temp[16];  
        //sprintf(temp,"(%d,%d)",pt.x,pt.y);  
        //cvPutText(painty,temp, pt, &font, cvScalar(255, 255, 255, 0));  
//printf("The Event is : %d ",event);
//printf("The flags is : %d ",flags);
//printf("The param is : %d\n",param);
int x1,y1;
//luozi();
//ai(x1,y1);
//weizhi.y=y1;
//weizhi.x=x1;
weizhi.y=(x+15-fy)/30;
weizhi.x=(y+15-fx)/30;
//printf("%d %d\n",weizhi.x,weizhi.y);
if(Q[weizhi.x][weizhi.y]==0) //判断当前位置是否已经落子
{
if(player==2)
{
//jilu();
Q[weizhi.x][weizhi.y]=2;
CvFont font;    
sprintf(nums,"%d",num++);
cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 0, 1);   
cvCircle( painty, cvPoint(fy+weizhi.y*30, fx+weizhi.x*30), 15,CV_RGB(0, 0,0) ,CV_FILLED, CV_AA, 0 ); 
cvPutText(painty, nums, cvPoint(fy+weizhi.y*30-10, fx+weizhi.x*30+5), &font, CV_RGB(0,0,255));

cvShowImage( "src", painty ); 
printf("==="); 
int winer=checkWin();
if(winer!=0)
{
if(winer==2)
printf("the side of player ● wins\n");
else if(winer==1)
printf("the side of ai ○ wins\n");
}//gotoxy(weizhi.x,weizhi.y);
}
drawai();
}
    }   
}

/*void on_mouse( int event, int x, int y, int flags, void* ustc)  
{  
    CvFont font;  
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);  
          
    if( event == CV_EVENT_LBUTTONDOWN )  
    {  
        CvPoint pt = cvPoint(x,y);  
        char temp[16];  
        sprintf(temp,"(%d,%d)",pt.x,pt.y);  
        cvPutText(painty,temp, pt, &font, cvScalar(255, 255, 255, 0));  
        cvCircle( painty, pt, 5,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
       
    }   
}  
  */
int main2()  
{  
painty=cvCreateImage( cvSize(fx+450,fy+450),IPL_DEPTH_8U, 3 );;               //vital
//cvZero(painty);
//if(type==1)
cvNot(painty,painty);
cvNamedWindow("src",1); 
cvSetMouseCallback( "src", onMouse, 0 ); 
for(int i=0;i<15;i++)
{cvLine(painty, cvPoint(fx+i*30,fy+0), cvPoint(fx+i*30,fy+420), cvScalar(0,0,0), 1,8, 0 );
cvLine(painty, cvPoint(fx+0,fy+i*30), cvPoint(fx+420,fy+i*30),cvScalar(0,0,0), 1,8, 0 );
}
cvShowImage( "src", painty );  
//cvShowImage("src",painty); 
cvWaitKey(0); 
    //src=cvLoadImage("lena.jpg",1);  
    cvDestroyAllWindows();  
    cvReleaseImage(&painty);  
  
    return 0;  
}  
main()
{
main2();
/* char press;
int winer=0;
system("color 2f");
l: drawqipan();
press=getch();
while(1)
{
Keypress(press);
if(press=='p')
winer=checkWin();
if(winer!=0)
gotoxy(0,15);
if(winer==2)
printf("the side of player ● wins\n");
else if(winer==1)
printf("the side of ai ○ wins\n");
else
{
// if(player==0)
// {
// //cluozi(1,2); //此处落子由电脑控制
// }
press=getch();
continue;
}
printf("do you want to play again?(y/n):");
p: press=getch();
if(press=='n')
break;
else if(press=='y')
{
winer=0;
goto l;
}
else
goto p;
}
*/

}

完整代码运行效果如下

用opencv和vc++6.0开发的五子棋游戏软件_第1张图片

你可能感兴趣的:(c++,编程开发,游戏,c++,opencv)