自学内容网 自学内容网

13 c++版本的五子棋

前言

呵呵 这大概是 大学里面的 c++ 五子棋了吧 

有一些 面向对象的理解, 但是不多 

这里 具体的实现 就不赘述, 仅仅是 发一下代码 以及 具体的使用 

然后 貌似 放在 win10 上面执行 还有一些问题, 渲染的, 应该很好调整 

 

 

五子棋

#include<Windows.h>
#include<iostream>
#include<ctime>
using namespace std;
#define N 15
void setCursor(int x,int y);

class FIR
{
private:
int tot;
int a[N][N];//状态矩阵
int attacklevel[N][N];//攻击优先级
int defenselevel[N][N];//防御优先级
int max;//攻击优先级+防御优先级的最大值
int maxattack;//最大攻击优先级
int maxdefense;//最大防御优先级
int have41,have42,have32;//人方是否有下一子成五子;电脑方是否有下一子成五子;电脑方是否有下一子成四子;
int r,c,su,sd;//横竖斜捺连续的子数
int power4(int );//4的N次方(计算优先级)
  int spacerow(int ,int ,int );//横向优先级
int spacecolumn(int ,int ,int );//竖向优先级
int spaceslopeup(int ,int ,int ); //斜向优先级
int spaceslopedown(int ,int ,int );//捺向优先级

public:
int steps;//定位光标的”步数“
int x,y;//走子的坐标
FIR()
{
memset(a,0,sizeof(a));
memset(attacklevel,0,sizeof(attacklevel));
memset(defenselevel,0,sizeof(defenselevel));
steps=0;tot=-1;maxattack=0;maxdefense=0;x=0;y=0;
have41=0;have42=0;have32=0;
r=0;c=0;su=0;sd=0;max=0;
}
void setmap();//打印棋桌
int play(int ,int ,int );//下子
int check(int ,int ,int );//检查
void analysis(int );//分析,优先级,最大功防……
void compute(int );//计算走子
void print();//打印状态矩阵

};

int main()
{
FIR fir;
int x,y;
int flag=0,flag2=0;
fir.setmap();
while(true)
{
do{
setCursor(40,fir.steps);
cout<<"player1 :please input x AND y:";
cin>>x>>y;
flag=fir.play(x-1,y-1,1);
if(flag)
{
if(fir.check(x-1,y-1,1))
{flag2=1;
setCursor(32,fir.steps);
cout<<"congratuations you win !"<<endl;
break;
}
}
}while(flag==0);
if(flag2)break;



setCursor(40,fir.steps);
cout<<"computer :";
fir.analysis(2);
fir.compute(2);
fir.play(fir.x,fir.y,2);
if(fir.check(fir.x,fir.y,2))
{
flag2=1;
setCursor(32,fir.steps);
cout<<"you lost please practice."<<endl;
break;
}




/*
do{
setCursor(0,20+fir.steps);
cout<<"player2 :please input x AND y:";
cin>>x>>y;
flag=fir.play(x,y,2);
if(flag)
{
if(fir.check(x-1,y-1))
{flag2=1;
setCursor(0,21+fir.steps);
cout<<"congratuations. you win !"<<endl;
break;
}
}
}while(flag==0);
  */

if(flag2)break;
}
setCursor(40,1+fir.steps);
fir.print();
return 0;
}

void setCursor(int x,int y)//定位光标
{
HANDLE handle;
handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD location;
location.X=x;
location.Y=y;
SetConsoleCursorPosition(handle,location);
}

//┏  ┓  ┗  ┛ ┣  ┫  ┳  ┻  ╋  ⊙  ●
void FIR::setmap()//画出“棋盘”
{
cout<<endl<<"  FIVE IN A ROW ";
int i,j;
setCursor(2,5);
cout<<"┏";
for(i=1;i<=N-2;i++)
cout<<"┳";
cout<<"┓";

for(i=1;i<=N-2;i++)
{setCursor(2,i+5);
cout<<"┣";
for(j=0;j<N-2;j++)
cout<<"╋";
cout<<"┫"<<endl;
}
cout<<"  ┗";
for(i=0;i<N-2;i++)
cout<<"┻";
cout<<"┛";


for(i=1;i<=N;i++)
{
setCursor(0,i+4);
cout<<i;
setCursor(i*2,3);
if(i>=10)cout<<i/10;
else cout<<i;
setCursor(i*2,4);
if(i>=10)cout<<i%10;
}
}

int FIR::play(int x,int y,int k)//落子
{
if(x<0 || y<0 || x>=N || y>=N || a[x][y]!=0)
{
setCursor(35,1+steps);
cout<<"your input is error, please input int again."<<endl;
steps=steps+2;
return 0;
}
else
{
setCursor(2*(y+1),x+5);
if(k==1)
{a[x][y]=1;
cout<<"⊙";
}
else if(k==2)
{a[x][y]=2;
cout<<"●";
}
else cout<<"k is error."<<endl;
steps++;
return 1;
}
}

int FIR::check(int x,int y,int k)//检查是否有五子
{
spacerow(x,y,k);spacecolumn(x,y,k);spaceslopeup(x,y,k);spaceslopedown(x,y,k);
if(r>4||c>4||su>4||sd>4)
return 1;
else return 0;
}

int FIR::power4(int n)
{
int sum=4;
if(n==0)
sum=1;
else
for(int i=1;i<n;i++)
sum*=4;
return sum;
}

int FIR::spacerow(int x,int y,int k)//行的优先级
{
tot=1;
int _x=x,_y=y-1,_y2;
while(_y>=0 && a[_x][_y]==k )
{
tot++;
_y--;
}
_y2=_y;
_x=x;_y=y+1;
while(_y<N && a[_x][_y]==k)
{
tot++;
_y++;
}
r=tot;
if(_y2>=0 && _y<N)
{
if(a[_x][_y2]==2/k && a[_x][_y]==2/k)
tot=0;
else if(a[_x][_y2]==0 && a[_x][_y]==0)
tot=power4(tot);
else tot=power4(tot-1);
}
else tot=power4(tot-1);

return tot;
}

int FIR::spacecolumn(int x,int y,int k)//列的优先级
{
tot=1;
int _x=x-1,_y=y,_x2;
while(_x>=0 && a[_x][_y]==k )
{
tot++;
_x--;
}
_x2=_x;
_x=x+1;_y=y;
while(_x<N && a[_x][_y]==k)
{
tot++;
_x++;
}
c=tot;
if(_x2>=0 && _x<N)
{
if(a[_x2][_y]==2/k && a[_x][_y]==2/k)
tot=0;
else if(a[_x2][_y]==0 && a[_x][_y]==0)
tot=power4(tot);
else tot=power4(tot-1);
}
else tot=power4(tot-1);

return tot;
}

int FIR::spaceslopeup(int x,int y,int k)//斜上的优先级
{
tot=1;
int _x=x+1,_y=y-1,_x2,_y2;
while(_x<N && _y>=0 && a[_x][_y]==k )
{
tot++;
_x++;
_y--;
}
_x2=_x;_y2=y;
_x=x-1;_y=y+1;
while(_x>=0 && _y<N && a[_x][_y]==k)
{
tot++;
_x--;
_y++;
}
su=tot;
if(_x2<N && _y2>=0 && _x>=0 &&_y<N)
{
if(a[_x2][_y2]==2/k && a[_x][_y]==2/k)
tot=0;
else if(a[_x2][_y2]==0 && a[_x][_y]==0)
tot=power4(tot);
else tot=power4(tot-1);
}
else tot=power4(tot-1);

return tot;
}

int FIR::spaceslopedown(int x,int y,int k)//斜下的优先级
{
tot=1;
int _x=x-1,_y=y-1,_x2,_y2;
while(_x<N && _y>=0 && a[_x][_y]==k )
{
tot++;
_x--;
_y--;
}
_x2=_x;_y2=y;
_x=x+1;_y=y+1;
while(_x>=0 && _y<N && a[_x][_y]==k)
{
tot++;
_x++;
_y++;
}
sd=tot;
if(_x2>=0 && _y2>=0 && _x<N &&_y<N)
{
if(a[_x2][_y2]==2/k && a[_x][_y]==2/k)
tot=0;
else if(a[_x2][_y2]==0 && a[_x][_y]==0)
tot=power4(tot);
else tot=power4(tot-1);
}
else tot=power4(tot-1);

return tot;
}


void FIR::analysis(int k)//分析每个空格的优先级(横,竖,正斜,反斜 优先级之和),并计算最高的优先级
{
maxattack=0;maxdefense=0;
int att[4],def[4];
for(int i=0;i<N;i++)
{
for(int j=0;j<N ;j++)
{
if(a[i][j]==0)
{
attacklevel[i][j]=spacerow(i,j,k)+spacecolumn(i,j,k)+spaceslopeup(i,j,k)+spaceslopedown(i,j,k);
att[0]=r;att[1]=c;att[2]=su;att[3]=sd;
if(att[0]==5 || att[1]==5 || att[2]==5 || att[3]==5)have42=1;
if(att[0]==4 || att[1]==4 || att[2]==4 || att[3]==4)have32=1;
if(maxattack<attacklevel[i][j])maxattack=attacklevel[i][j];
defenselevel[i][j]=spacerow(i,j,2/k)+spacecolumn(i,j,2/k)+spaceslopeup(i,j,2/k)+spaceslopedown(i,j,2/k);
def[0]=r;def[1]=c;def[2]=su;def[3]=sd;
if(maxdefense<defenselevel[i][j])maxdefense=defenselevel[i][j];
if(def[0]==5 || def[1]==5 || def[2]==5 || def[3]==5)have41=1;
}
}cout<<endl;
}
}

void FIR::compute(int k)//计算……,有多个优先级最高且相同的随机取一个
{
int i,j,block=256;
srand((int)time(NULL));
setCursor(50,steps);

if(maxdefense>=block)
{
if(have42==1)
{
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(attacklevel[i][j]==maxattack)
{
cout<<i+1<<" "<<j+1<<endl;
x=i;y=j;
memset(attacklevel,0,sizeof(attacklevel));
memset(defenselevel,0,sizeof(defenselevel));
return ;
}

}

else if(have41==1)
{
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(defenselevel[i][j]>=block)
if(attacklevel[i][j]+defenselevel[i][j]>max)
max=attacklevel[i][j]+defenselevel[i][j];

for(i=0;i<N;i++)
for(j=0;j<N;j++)
{if(defenselevel[i][j]>=block)
if(attacklevel[i][j]+defenselevel[i][j]==max)
{
cout<<i+1<<" "<<j+1<<endl;
x=i;y=j;
max=0;
memset(attacklevel,0,sizeof(attacklevel));
memset(defenselevel,0,sizeof(defenselevel));
return ;
}
}
}
/*
else if(have31==1)
{
for(i=0;i<N;i++)
for(j=0;j<N;j++)
//if(attacklevel[i][j]>=block)
if(attacklevel[i][j]+defenselevel[i][j]>max)
max=attacklevel[i][j]+defenselevel[i][j];

for(i=0;i<N;i++)
for(j=0;j<N;j++)
{//if(attacklevel[i][j]>=block)
if(attacklevel[i][j]+defenselevel[i][j]==max)
{
cout<<i+1<<" "<<j+1<<endl;
x=i;y=j;
max=0;
memset(attacklevel,0,sizeof(attacklevel));
memset(defenselevel,0,sizeof(defenselevel));
return ;
}
}
}
*/
else
{
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(defenselevel[i][j]>=block)
if(attacklevel[i][j]+defenselevel[i][j]>max)
max=attacklevel[i][j]+defenselevel[i][j];

for(i=0;i<N;i++)
for(j=0;j<N;j++)
{if(defenselevel[i][j]>=block)
if(attacklevel[i][j]+defenselevel[i][j]==max)
{
cout<<i+1<<" "<<j+1<<endl;
x=i;y=j;
max=0;
memset(attacklevel,0,sizeof(attacklevel));
memset(defenselevel,0,sizeof(defenselevel));
return ;
}
}
}
}


else if(maxattack>maxdefense)
{
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(attacklevel[i][j]==maxattack)
if(attacklevel[i][j]+defenselevel[i][j]>max)
max=attacklevel[i][j]+defenselevel[i][j];

for(i=0;i<N;i++)
for(j=0;j<N;j++)
{if(attacklevel[i][j]==maxattack)
if(attacklevel[i][j]+defenselevel[i][j]==max)
{
cout<<i+1<<" "<<j+1<<endl;
x=i;y=j;
max=0;
memset(attacklevel,0,sizeof(attacklevel));
memset(defenselevel,0,sizeof(defenselevel));
return ;
}
}
}
else
{
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(defenselevel[i][j]==maxdefense)
if(attacklevel[i][j]+defenselevel[i][j]>max)
max=attacklevel[i][j]+defenselevel[i][j];

for(i=0;i<N;i++)
for(j=0;j<N;j++)
{if(defenselevel[i][j]==maxdefense)
if(attacklevel[i][j]+defenselevel[i][j]==max)
{
cout<<i+1<<" "<<j+1<<endl;
x=i;y=j;
max=0;
memset(attacklevel,0,sizeof(attacklevel));
memset(defenselevel,0,sizeof(defenselevel));
return ;
}
}
}
}

void FIR::print()//打印最后的数字结果
{
for(int i=0;i<N;i++)
{
setCursor(40,1+steps+i);
for(int j=0;j<N;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}



/*
int row(int ,int ,int );
int column(int ,int ,int );
int slopeup(int ,int ,int );
int slopedown(int ,int ,int );



int FIR::row(int x,int y,int k)//判断行
{
tot=1;
int _x=x,_y=y-1;
while(_y>=0 && a[_x][_y]==k)
{
tot++;
_y--;
}
_x=x,_y=y+1;
while(_y<N && a[_x][_y]==k)
{
tot++;
_y++;
}
return tot;
}

int FIR::column(int x,int y,int k)//判断列
{
tot=1;
int _x=x-1,_y=y;
while(_x>=0 && a[_x][_y]==k)
{
tot++;
_x--;
}
_x=x+1,_y=y;
while(_x<N && a[_x][_y]==k)
{
tot++;
_x++;
}
return tot;
}

int FIR::slopeup(int x,int y,int k)//判断正斜
{
tot=1;
int _x=x-1,_y=y+1;
while( _x>=0 && _y<N && a[_x][_y]==k)
{
tot++;
_x--;
_y++;
}
_x=x+1,_y=y-1;
while(_x<N && _y>=0  && a[_x][_y]==k)
{
tot++;
_x++;
_y--;
}
return tot;
}

int FIR::slopedown(int x,int y,int k)//判断反斜
{
tot=1;
int _x=x-1,_y=y-1;
while(_x>=0 && _y>=0 && a[_x][_y]==k)
{
tot++;
_x--;
_y--;
}
_x=x+1,_y=y+1;
while(_x<N  && _y<N && a[_x][_y]==k)
{
tot++;
_x++;
_y++;
}
return tot;
}
*/







 

 

程序截图

这个在 win10 上面执行 貌似 渲染是存在问题的, 需要调整 

1c9d19f2273d41ad9f35c83b156e227c.png

 

 

正常执行

9580ed69246744348b4c0d08a18379f6.png

 

 

 

 

 


原文地址:https://blog.csdn.net/u011039332/article/details/130783568

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!