#include
using namespace std;
int map[17][17] = {0};
void print(int (&map)[17][17]) //打印棋盘和棋子
{
cout << "双人五子棋 gz81 2010年4月10日" << endl ;
cout << "玩家1使用棋子:X 玩家2使用棋子O"<< endl << endl;
for (int i=0; i<17; ++i)
{
for (int j=0; j<17; ++j)
{
if (i==16 && j==16) //最右下角的点
{
cout << "**";
}
else if (i==16)
{
if (j<10)
cout << ' ' << j;
else
cout << j;
}
else if (j==16)
{
if (i<10)
cout << ' ' << i;
else
cout << i;
}
else
{
if (map[i][j] == 0)
cout << " .";
else if (map[i][j] == 1)
cout << " X";
else //else if (map[i][j] == 2)
cout << " O";
}
}
cout << endl;
}
}
void SetStep(int & mStep, int & nStep, int turn/*表第几回检测*/) //设置步进
{
switch (turn)
{
case 0: //左右
mStep = 0;
nStep = -1;
break;
case 1: //上下
mStep = -1;
nStep = 0;
break;
case 2: //斜1
mStep = 1;
nStep = -1;
break;
case 3: //斜2
mStep = -1;
nStep = -1;
break;
}
}
bool Win(int m, int n,int player/*哪个玩家的子 1 2*/)
{
int mTra;
int nTra;
int mStep;
int nStep;
for (int i=0; i<4; ++i) //检测四个方向
{
int cnt = 1; //记录每次检测,自己连续的棋数(包括下那个棋)
SetStep(mStep, nStep, i);
mTra = m;
nTra = n;
for (;;)
{
mTra += mStep;
nTra += nStep; //移动一格
if (mTra<0 || nTra<0 || mTra>15 || nTra>15 || map[mTra][nTra] != player) //如果出界或者不是自己的棋
break;
//else
++cnt;
}
mTra = m;
nTra = n;
for (;;)
{
mTra -= mStep;
nTra -= nStep; //反方向移动一格
if (mTra<0 || nTra<0 || mTra>15 || nTra>15 || map[mTra][nTra] != player)
break;
//else
++cnt;
}
if (cnt >= 5)
return true;
}
return false;
}
int main()
{
print(map);
int chess = 0; //已下的子数
const int totalChess = 16*16; //总子数
int m,n; //玩家输入的行,列坐标
int player = 1;
for (;;)
{
//提示并接受玩家输入要下的棋的坐标
for (;;)
{
cout << endl << "请玩家" << player << "下子(输入棋子的行、列坐标,以空格分开):" << endl;
cin >> m >> n;
if (m>=0 && n>=0 && m<=15 && n<=15 && map[m][n]==0) //如果输入有效
{
map[m][n] = player; //将棋放入棋盘
++chess;
break;
}
}
system("cls"); //清屏
print(map);
if (Win(m, n, player/*哪个玩家的子*/))
{
cout << endl << "玩家" << player << "赢了!" << endl;
system("pause");
return 0;
}
if (chess == totalChess) //和棋
{
cout << "双方和棋!" << endl;
system("pause");
return 0;
}
player = (player==1)? 2: 1; //更换玩家
}
}