请用所学的C语言实现一个命令行下的五子棋游戏。要求有棋盘界面,并实现人人,人机机人的三种对弈模式。

2024-12-27 03:26:23
推荐回答(2个)
回答1:

http://tieba.baidu.com/f?ct=335675392&tn=baiduPostBrowser&sc=4335990148&z=430611008&pn=0&rn=30&lm=0&word=c%2B%2B#4335990148
百度贴吧里面的,我试过可以运行,有人机对战功能~
#include
#include
#include
class five
{
int x;
int y;
int m;
int n;
int num_xy;
int num_mn;
char qipan[20][20];
public:
five(int X = 0,int Y =0,int M = 0,int N = 0,int Num_xy = 0,int Num_mn = 0)
{
x = X;
y = Y;
m = M;
n = N;
num_xy = Num_xy;
num_mn = Num_mn;
for(int i = 0 ; i < 20 ; i ++)
{
for(int j = 0 ;j < 20 ; j ++)
qipan[i][j] = '.';
}

}
int getm()
{
return m;
}
int getn()
{
return n;
}
void getpointxy();
void getpointmn();
void machinemn();
void showqipan();
void print();
bool IsxyWin();
bool IsmnWin();
int dangerlevel(int a,int b);
void attack();
};
void five::getpointxy()
{
cout<<"请分别输入第"<cin >> x >> y;
if(x <= 0 || y <= 0 || x > 20 || y > 20 )
{
cout << "出界,请重新输入:"<getpointxy();
}
else if(qipan[y-1][x-1] != '.' )
{
cout << "请所下的位置已经有棋子,请重新输入:" <getpointxy();
}
else if(num_xy+num_mn >= 400)
{
cout << "棋盘已满,平局!" << endl;
exit(0);
}
else
{
qipan[y-1][x-1] = 'o';
num_xy++;
}

}
void five::getpointmn()
{
cout<<"请分别输入第"<cin >> m >> n;
if(m <= 0 || n <= 0 || m > 20 || n > 20 )
{
cout << "出界,请重新输入:"<getpointmn();
}
else if(qipan[n-1][m-1]!='.')
{
cout << "请所下的位置已经有棋子,请重新输入:" <getpointmn();
}
else if(num_xy+num_mn >= 400)
{
cout << "棋盘已满,平局!" << endl;
exit(0);
}
else
{
qipan[n-1][m-1] = 'x';
num_mn++;
}

}

int five::dangerlevel(int a, int b)
{
int level = 0;
for(int i = 1 ; a+i<=19; i ++)
{
if(qipan[a+i][b] == '.' && a + i <= 19)
{
i = i+1;
break;
}
if(qipan[a +i][b] == 'x' && a + i <= 19)
break;

}
level += (i-1);
for(i = 1 ; a-i>=0; i ++)
{
if(qipan[a-i][b] == '.' && (a-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a-i][b] == 'x' && (a-i) >= 0)
break;
}
level += (i-1);
for(i = 1 ; b+i<=19 ; i ++)
{
if(qipan[a][b + i] == '.' && b + i <= 19)
{
i = i+1;
break;
}
if(qipan[a][b + i] == 'x' && b + i <= 19)
break;
}
level += (i-1);
for( i = 1 ; b-i>=0 ; i ++)
{
if(qipan[a][b - i] == '.' && (b-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a][b - i] == 'x' && (b-i) >= 0)
break;
}
for( i = 1 ; a+i<=19&& b+i<=19; i ++)
{
if(qipan[a + i][b + i] == '.' && b+i <= 19 && a+i <= 19)
{
i = i+1;
break;
}
if(qipan[a + i][b + i] == 'x' && b+i <= 19 && a+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b-i >= 0; i ++)
{
if(qipan[a - i][b - i] == '.' && a-i>= 0 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a - i][b - i] == 'x' && a-i>= 0 && b-i >= 0)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b+i <= 19 ; i ++)
{
if(qipan[a - i][b + i] == '.' && a-i>= 0 && b+i <= 19)
{
i = i+1;
break;
}
if(qipan[a - i][b + i] == 'x' && a-i>= 0 && b+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a + i<= 19 && b-i >= 0 ; i ++)
{
if(qipan[a + i][b - i] == '.' && a + i<= 19 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a + i][b - i] == 'x' && a + i<= 19 && b-i >= 0)
break;
}
level += (i-1);
return level;
}

void five::attack()
{
int num1=1,num2=1,num3=1,num4=1;
int A = 20 , B = 20 ,max = 0;
for(int a = 0 ; a < 19 ; a ++)
{
for(int b = 0 ; b < 19 ; b ++)
{
if(qipan[a][b] == '.')
{
int level = 0;
for(int i = 1 ; a+i<=19; i ++)
{
if(qipan[a+i][b] == '.' && a + i <= 19)
{
i = i+1;
break;
}
if(qipan[a +i][b] == 'o' && a + i <= 19)
break;

}
level += (i-1);
for(i = 1 ; a-i>=0; i ++)
{
if(qipan[a-i][b] == '.' && (a-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a-i][b] == 'o' && (a-i) >= 0)
break;
}
level += (i-1);
for(i = 1 ; b+i<=19 ; i ++)
{
if(qipan[a][b + i] == '.' && b + i <= 19)
{
i = i+1;
break;
}
if(qipan[a][b + i] == 'o' && b + i <= 19)
break;
}
level += (i-1);
for( i = 1 ; b-i>=0 ; i ++)
{
if(qipan[a][b - i] == '.' && (b-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a][b - i] == 'o' && (b-i) >= 0)
break;
}
for( i = 1 ; a+i<=19&& b+i<=19; i ++)
{
if(qipan[a + i][b + i] == '.' && b+i <= 19 && a+i <= 19)
{
i = i+1;
break;
}
if(qipan[a + i][b + i] == 'o' && b+i <= 19 && a+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b-i >= 0; i ++)
{
if(qipan[a - i][b - i] == '.' && a-i>= 0 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a - i][b - i] == 'o' && a-i>= 0 && b-i >= 0)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b+i <= 19 ; i ++)
{
if(qipan[a - i][b + i] == '.' && a-i>= 0 && b+i <= 19)
{
i = i+1;
break;
}
if(qipan[a - i][b + i] == 'o' && a-i>= 0 && b+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a + i<= 19 && b-i >= 0 ; i ++)
{
if(qipan[a + i][b - i] == '.' && a + i<= 19 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a + i][b - i] == 'o' && a + i<= 19 && b-i >= 0)
break;
}
level += (i-1);
if(level >= max)
{
n = a + 1;
m = b + 1;
max = level;
}
}
else
continue;
}
}
qipan[n-1][m-1] = 'x';
}
void five::machinemn()
{

int Num1=1,Num2=1,Num3=1,Num4=1,A11 = 21,A12 = 21,A21 = 21,A22 = 21,A31 = 21,A32 = 21,A41 = 21,A42 = 21;
for(int i = x ; i < x+5&& x+5<20 ; i ++)
{
if(qipan[y-1][i] == '.')
{
A11=i;
break;
}
if(qipan[y-1][i] == 'x')
break;
if(qipan[y-1][i] == 'o')
Num1++;
}
for(i = x-2; i >= 0 ; i --)
{
if(qipan[y-1][i] == '.')
{
A12 = i;
break;
}
if(qipan[y-1][i] == 'x')
{
break;
}
if(qipan[y-1][i] == 'o')
Num1++;
}
////

for( i = y ; i < y+5 && y+5 < 20 ; i ++)
{
if(qipan[i][x-1] == '.')
{
A21 = i;
break;
}
if(qipan[i][x-1] == 'x')
{
break;
}
if(qipan[i][x-1] == 'o')
Num2++;
}
for(i = y-2; i >= 0 ; i --)
{
if(qipan[i][x-1] == '.')
{
A22 = i;
break;
}
if(qipan[i][x-1] == 'x')
{
break;
}
if(qipan[i][x-1] == 'o')
Num2++;
}
////
for(i = 0 ; i < 5 && x+i<20 && y+i<20; i++ )
{
if(qipan[y+i][x+i] == '.')
{
A31 = i;
break;
}
if(qipan[y+i][x+i] == 'x')
{
break;
}
if(qipan[y+i][x+i] =='o')
Num3++;
}
for(i = 0 ; x - i -2>=0 && y-i-2>=0 && i < 5;i++ )
{
if(qipan[y-i-2][x-i-2] == '.')
{
A32 = i;
break;
}
if(qipan[y-i-2][x-i-2] == 'x')
break;
if(qipan[y-i-2][x-i-2] == 'o')
Num3++;
}
////
for(i = 0 ; x + i < 20 && y - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y-i-2][x+i] == '.')
{
A41 = i;
break;
}
if(qipan[y-i-2][x+i] == 'x')
{
break;
}
if(qipan[y-i-2][x+i] == 'o')
Num4++;
}
for(i = 0 ; y + i < 20 && x - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y+i][x-i-2] == '.')
{
A42 = i;
break;
}
if(qipan[y+i][x-i-2] == 'x')
{
break;
}
if(qipan[y+i][x-i-2] == 'o')
Num4++;
}
////
if(num_xy == 0 && num_xy == 0)
{
m = n = 10;
qipan[m-1][n-1] = 'x';
}
else if(num_xy == 1)
attack();
else if(num_xy == 1)
{
m = x+1;
n = x+1;
qipan[m-1][n-1] = 'x';
}
else if(Num1 == 4 && A11 == 21 && A12 != 21) //一排四个右边一头堵住
{
n =1 + y-1;
m = 1 + A12;
qipan[y-1][A12] = 'x';
}
else if(Num1 == 4 && A12 == 21 && A11 != 21) //一排四个左边边一头堵住
{
n =1 + y-1;
m = 1 + A11;
qipan[y-1][A11] = 'x';
}
else if(Num2 == 4 && A21 == 21 && A22 != 21) //一列四个下边一头堵住
{
n =1 + A22;
m = 1 + x-1;
qipan[A22][x-1] = 'x';
}
else if(Num2 == 4 && A22 == 21 && A21 != 21) //一列四个上边一头堵住
{
n =1 + A21;
m = 1 + x-1;
qipan[A21][x-1] = 'x';
}
else if(Num3 == 4 && A31 == 21 && A32 != 21) //一左上右下斜列四个右边一头堵住
{
n =1 + y - A32 - 2;
m = 1 + x - A32 - 2;
qipan[y-A32-2][x-A32-2] = 'x';
}
else if(Num3 == 4 && A32 == 21 && A31 != 21) //一左上右下斜列四个左边一头堵住
{
n =1 + y + A31;
m = 1 + x + A31;
qipan[y+A31][x+A31] = 'x';
}
else if(Num4 == 4 && A41 == 21 && A42 != 21) //一左下右上斜列四个右边一头堵住
{
n =1 + y + A42;
m = 1 + x - A42 - 2;
qipan[y+A42][x-A42-2] = 'x';
}
else if(Num4 == 4 && A42 == 21 && A41 != 21) //一左下右上斜列四个左边一头堵住
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
qipan[y-A41-2][x+A41] = 'x';
}
else if(Num1==3 && A11 != 21 && A12 != 21) //一排3 个两头没堵
{
n =1 + y-1;
m = 1 + A11;
if(dangerlevel(y-1,A11) >= dangerlevel(y-1,A12))
qipan[y-1][A11] = 'x';
else
qipan[y-1][A12] = 'x';

}
else if(Num2==3&& A21 != 21 && A22 != 21) //一列 3 个两头没堵
{
n =1 + A21;
m = 1 + x-1;
if(dangerlevel(A21,x-1) >= dangerlevel(A22,x-1))
qipan[A21][x-1] = 'x';
else
qipan[A22][x-1] = 'x';
}
else if(Num3 == 3&& A31 != 21 && A32 != 21) //一左上右下斜列3 个两头没堵
{
n =1 + y + A31;
m = 1 + x + A31;
if(dangerlevel(y+A31,x+A31) >= dangerlevel(y-A32 - 2,y - A32 -2))
qipan[y+A31][x+A31] = 'x';
else
qipan[y-A32-2][x-A32 -2] = 'x';

}
else if(Num4==3 && A41 != 21 && A42 != 21) //一左下右上斜列3 个两头没堵
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
if(dangerlevel(y-A41-2,x+A41) >= dangerlevel(y-A42-2,x+A42))
qipan[y-A41-2][x+A41] = 'x';
else
qipan[y-A42-2][x+A42] = 'x';
}
else if(Num1 == 2&& A11 != 21 && A12 != 21) //一排2个两头没堵
{
n =1 + y-1;
m = 1 + A11;
qipan[y-1][A11] = 'x';
}

else if( Num2 == 2&& A21 != 21 && A22 != 21) //一列2个两头没堵
{
n =1 + A21;
m = 1 + x-1;
qipan[A21][x-1] = 'x';
}
else if(Num3 == 2&& A31 != 21 && A32 != 21) //一左上右下斜列2 个两头没堵
{
n =1 + y + A31;
m = 1 + x + A31;
qipan[y+A31][x+A31] = 'x';
}
else if(Num4==2 && A41 != 21 && A42 != 21) //一左下右上斜列2个两头没堵
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
qipan[y-A41-2][x+A41] = 'x';
}
else
attack();
}

void five::showqipan()
{
int p=0,q=0;
for(int i = 0 ; i <= 20 ; i++)
{

if(p<10)
{
cout << p <<' ';
}
else
cout << p ;
p++;
}
cout << endl;
for(i = 0 ; i < 20 ; i++)
{
if(i>=0)
{
q++;
if(q<10)
cout << q <<' ';
else
cout << q;
}
for(int j = 0 ; j < 20 ; j ++)
cout << qipan[i][j]<<' ' ;

cout << endl;
}
}

void five::print()
{
int p=0,q=0;
for(int i = 0 ; i <= 20 ; i++)
{

if(p<10)
{
cout << p <<' ';
}
else
cout << p ;
p++;
}
cout << endl;
for(i = 0 ; i < 20 ; i++)
{
if(i>=0)
{
q++;
if(q<10)
cout << q <<' ';
else
cout << q;
}
for(int j = 0 ; j < 20 ; j ++)
{
cout << qipan[i][j]<<' ' ;
}

cout << endl;
}
}

bool five::IsxyWin()
{
int num1=1,num2=1,num3=1,num4=1;
for(int i = x ; i < x+5&& x+5<20 ; i ++)
{
if(qipan[y-1][i] != 'o')
break;
if(qipan[y-1][i] == 'o')
num1++;
}
for(i = x-2; i >= 0 ; i --)
{
if(qipan[y-1][i] != 'o')
break;
if(qipan[y-1][i] == 'o')
num1++;
}
for( i = y ; i < y+5 ; i ++)
{
if(qipan[i][x-1] != 'o')
break;
if(qipan[i][x-1] == 'o')
num2++;
}
for(i = y-2; i >= 0 ; i --)
{
if(qipan[i][x-1] != 'o')
break;
if(qipan[i][x-1] == 'o')
num2++;
}
for(i = 0 ; i < 5 && x+i<20 && y+i<20; i++ )
{
if(qipan[y+i][x+i] != 'o')
break;
if(qipan[y+i][x+i] =='o')
num3++;
}
for(i = 0 ; x - i -2>=0 && y-i-2>=0 && i < 5;i++ )
{
if(qipan[y-i-2][x-i-2] != 'o')
break;
if(qipan[y-i-2][x-i-2] == 'o')
num3++;
}
for(i = 0 ; x + i < 20 && y - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y-i-2][x+i] != 'o')
break;
if(qipan[y-i-2][x+i] == 'o')
num4++;
}
for(i = 0 ; y + i < 20 && x - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y+i][x-i-2] != 'o')
break;
if(qipan[y+i][x-i-2] == 'o')
num4++;
}
if(num1>=5||num2>=5||num3>=5||num4>=5)
return 1;
else
return 0;

}
bool five::IsmnWin()
{
int num1=1,num2=1,num3=1,num4=1;
for(int i = m ; i < m+5&& m+5<20 ; i ++)
{
if(qipan[n-1][i] != 'x')
break;
if(qipan[n-1][i] == 'x')
num1++;
}
for(i = m-2; i >= 0 ; i --)
{
if(qipan[n-1][i] != 'x')
break;
if(qipan[n-1][i] == 'x')
num1++;
}
for( i = n ; i < n+5 ; i ++)
{
if(qipan[i][m-1] != 'x')
break;
if(qipan[i][m-1] == 'x')
num2++;
}
for(i = n-2; i >= 0 ; i --)
{
if(qipan[i][m-1] != 'x')
break;
if(qipan[i][m-1] == 'x')
num2++;
}
for(i = 0 ; i < 5 && m+i<20 && n+i<20; i++ )
{
if(qipan[n+i][m+i] != 'x')
break;
if(qipan[n+i][m+i] =='x')
num3++;
}
for(i = 0 ; m - i -2>=0 && n-i-2>=0 && i < 5;i++ )
{
if(qipan[n-i-2][m-i-2] != 'x')
break;
if(qipan[n-i-2][m-i-2] == 'x')
num3++;
}
for(i = 0 ; m + i < 20 && n - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[n-i-2][m+i] != 'x')
break;
if(qipan[n-i-2][m+i] == 'x')
num4++;
}
for(i = 0 ; n + i < 20 && m - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[n+i][m-i-2] != 'x')
break;
if(qipan[n+i][m-i-2] == 'x')
num4++;
}
if(num1>=5||num2>=5||num3>=5||num4>=5)
return 1;
else
return 0;

}

void main()
{
five a;
int temp;
cout << "欢迎体验本游戏!这是本游戏的棋盘:" << endl;
a.showqipan();
cout << "双人游戏请按1,人机游戏请按2,退出请按3:";
cin >> temp;
if(temp == 3)
exit(1);
if(temp == 1)
{
cout << "您选择了双人游戏,下面游戏开始:" << endl;
for(int i = 0 ; ; i ++)
{
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "白棋胜!" << endl;
break;
}
a.getpointmn();
a.print();
if(a.IsmnWin())
{
cout << "黑棋胜!" << endl;
break;
}
}
}
if(temp == 2)
{
int temp1=0;
cout << "您选择了人机游戏,您是白棋('o'),电脑是黑棋('x'),下面游戏开始:" << endl;
cout << "您先下请按1,电脑先下请按2:";
cin >> temp1;
if(temp1 == 1)
{
for(int i = 0 ; ; i ++)
{
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "你赢了!恭喜!" << endl;
break;
}
a.machinemn();
a.print();
cout << "电脑走(" << a.getm() << ',' << a.getn() << ')' << endl;
if(a.IsmnWin())
{
cout << "哈哈,你输了!" << endl;
break;
}
}
}
if(temp1 == 2)
{
for(int i = 0 ; ; i ++)
{
a.machinemn();
a.print();
if(a.IsmnWin())
{
cout << "哈哈,你输了!" << endl;
break;
}
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "你赢了!恭喜!" << endl;
break;
}
}
}
}
cout << "Press any key to EXIT\n";
getch();
}

回答2:

有点难度了啊