五子棋游戏人机版:
电脑实现了阻挡对方连续的棋子
#include<iostream>
#include<iomanip>
using namespace std;
#include<stdlib.h>
#include<time.h>
/*class PerPlayer
{
public:
int m;
int n;
int person();
};
PerPlayer pp;
class CompPlayer
{
private:
int k;
int r;
public:
int computer();
};
CompPlayer cp;
*/
const int X = 21; //棋盘行数
const int Y = 21; //棋盘列数
char p[X][Y]; //定义棋盘
int m=0;
int n=0;
void display() //输出棋盘
{
for(int j=0; j<Y; j++)
cout<<setw(3)<<setfill(' ')<<j;
cout<<endl;
for(int i=1; i<Y; i++)
{
cout<<setw(3)<<setfill(' ')<<i;//setw(x)设置输出字符位宽
for(int j=1;j<X;j++)
cout<<setw(3)<<setfill(' ')<<p[i][j];//setfill是设置填充填充字符
cout<<endl;
}
}
void reclear()
{
system("cls");
display();
}
int person()
{
cout<<"请输入落子位置:\n"<<"请输入落子的行数:";
cin>>m;
cout<<"请输入落子的列数:";
cin>>n;
if(m>=X||m<=0||n<=0||n>=Y)
{
cout<<"超出棋盘范围,请重新输入正确坐标!\n";
person();
return 0;//防止一步多次清屏
}
else if((p[m][n]==80)||p[m][n]==67)
{
cout<<"该点已有棋子,请重新选取落子点!\n";
person();
return 0;
}
else
p[m][n]=80;
reclear();
return 1;
}
int computer()
{
int k=0;
int r=0;
for(k=3;k<X-2;k++)
{
for(r=3;r<Y-2;r++)
{
if(p[k][r]==80&&p[k-1][r-1]==p[k][r]&&p[k+1][r+1]==p[k][r])
{
if(p[k-2][r-2]=='-'||p[k+2][r+2]=='-')
{
if(p[k-2][r-2]=='-')
{
p[k-2][r-2]=67;
reclear();
return 0;
}
else
{
p[k+2][r+2]=67;
reclear();
return 0;
}
}
else if(p[k-2][r-2]==80||p[k+2][r+2]==80)
{
if(p[k+2][r+2]==80)
{
p[k+3][r+3]=67;
reclear();
return 0;
}
else
{
p[k-3][r-3]=67;
reclear();
return 0;
}
}
}
else if(p[k][r]!='-'&&p[k+1][r-1]==p[k][r]&&p[k-1][r+1]==p[k][r])
{
if(p[k+2][r-2]=='-'||p[k-2][r+2]=='-')
{
if(p[k+2][r-2]=='-')
{
p[k+2][r-2]=67;
reclear();
return 0;
}
else
{
p[k-2][r+2]=67;
reclear();
return 0;
}
}
else if(p[k+2][r-2]==80||p[k-2][r+2]==80)
{
if(p[k+2][r-2]==80)
{
p[k+3][r-3]=67;
reclear();
return 0;
}
else
{
p[k+3][r-3]=67;
reclear();
return 0;
}
}
}
}
}
for(k=1;k<X;k++)
{
for(r=3;r<Y-2;r++)
{
if(p[k][r]!='-'&&p[k][r-1]==p[k][r]&&p[k][r+1]==p[k][r])
{
if(p[k][r-2]=='-'||p[k][r+2]=='-')
{
if(p[k][r-2]=='-')
{
p[k][r-2]=67;
reclear();
return 0;
}
else
{
p[k][r+2]=67;
reclear();
return 0;
}
}
else if(p[k][r-2]==80||p[k][r+2]==80)
{
if(p[k][r+2]==80)
{
p[k][r+3]=67;
reclear();
return 0;
}
else
{
p[k][r-3]=67;
reclear();
return 0;
}
}
}
}
}
for(k=3;k<X-2;k++)
{
for(r=1;r<Y;r++)
{
if(p[k][r]!='-'&&p[k-1][r]==p[k][r]&&p[k+1][r]==p[k][r])
{
if(p[k-2][r]=='-'||p[k+2][r]=='-')
{
if(p[k-2][r]=='-')
{
p[k-2][r]=67;
reclear();
return 0;
}
else
{
p[k+2][r]=67;
reclear();
return 0;
}
}
else if(p[k-2][r]==80 ||p[k+2][r]==80)
{
if(p[k+2][r]==80)
{
p[k+3][r]=67;
reclear();
return 0;
}
else
{
p[k-3][r]=67;
reclear();
return 0;
}
}
}
}
}
while(1)
{
srand((int)time(0));
int a=(rand()%8);
switch(a)
{
case 0:if(p[m-1][n]=='-')
{
p[m-1][n]=67;
reclear();
return 0;
}
case 1:if(p[m][n-1]=='-')
{
p[m][n-1]=67;
reclear();
return 0;
}
case 2:if(p[m-1][n-1]=='-')
{
p[m-1][n-1]=67;
reclear();
return 0;
}
case 3:if(p[m+1][n+1]=='-')
{
p[m+1][n+1]=67;
reclear();
return 0;
}
case 4:if(p[m+1][n]=='-')
{
p[m+1][n]=67;
reclear();
return 0;
}
case 5:if(p[m][n+1]=='-')
{
p[m][n+1]=67;
reclear();
return 0;
}
case 6:if(p[m+1][n-1]=='-')
{
p[m+1][n-1]=67;
reclear();
return 0;
}
case 7:if(p[m-1][n+1]=='-')
{
p[m-1][n+1]=67;
reclear();
return 0;
}
}
}
}
int evalue()
{
int k=0,r=0;
//斜线判断
for(k=3;k<X-2;k++)
{
for(r=3;r<Y-2;r++)
{
if(p[k][r]!='-'&&p[k-2][r-2]==p[k][r]&&p[k-1][r-1]==p[k][r]&&p[k+1][r+1]==p[k][r]&&p[k+2][r+2]==p[k][r])
return 1;
else if(p[k][r]!='-'&&p[k+2][r-2]==p[k][r]&&p[k+1][r-1]==p[k][r]&&p[k-1][r+1]==p[k][r]&&p[k-2][r+2]==p[k][r])
return 1;
}
}
//横线判断
for(k=1;k<X;k++)
for(r=3;r<Y-2;r++)
if(p[k][r]!='-'&&p[k][r-2]==p[k][r]&&p[k][r-1]==p[k][r]&&p[k][r+1]==p[k][r]&&p[k][r+2]==p[k][r])
return 1;
//竖线判断
for(k=3;k<X-2;k++)
for(r=1;r<Y;r++)
if(p[k][r]!='-'&&p[k-2][r]==p[k][r]&&p[k-1][r]==p[k][r]&&p[k+1][r]==p[k][r]&&p[k+2][r]==p[k][r])
return 1;
return 0;
}
int main()
{
memset(p,'-',441); //初始化为‘-’//在一段内存块中填充某个给定的值
cout<<"五子棋人机大战开始\n"
<<"五子棋棋谱如下:\n";
display();
while(1)
{
person();
if(evalue())
{
cout<<"你胜利啦!\n";
break;
}
computer();
if(evalue())
{
cout<<"失败,电脑胜出!\n";
break;
}
}
return 0;
}