#include \"stdio.h\"
#include \"string.h\"
#define N 20
char qipan[N][N];
int flag = 0, n = 1;
char in[2], qizi[] = \"xo.\", player[] = \"玩家\";
void init();
void print();
void test();
int input();
int check();
void main()
{
init();
print();
//test();
while (!flag)
{
if (n % 2 != 0)
{
printf(\"%s1(执%c子,第%d手):\", player, qizi[0], n);
}
else
{
printf(\"%s2(执%c子,第%d手):\", player, qizi[1], n);
}
scanf(\"%s\", in);
if (strcmp(in, \"quit\") == 0)
{
printf(\"玩家退出,游戏终止!\");
break;
}
if (input())
{
if (n % 2 != 0)
{
qipan[in[0] - 97][in[1] - 97] = qizi[0];
}
else
{
qipan[in[0] - 97][in[1] - 97] = qizi[1];
}
n++;
}
print();
if (check() == 1)
{
flag = 1;
printf(\"游戏结束,%s1获胜!\\n\", player);
}
else
{
if (check() == 2)
{
flag = 1;
printf(\"游戏结束,%s2获胜!\\n\", player);
}
}
if (n > N * N)
{
printf(\"无子可下,和棋,游戏终止!\\n\");
flag = 1;
}
}
}
// 初始化
void init()
{
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
qipan[i][j] = qizi[2];
}
}
}
// 输入
int input()
{
if (strlen(in) == 2 && qipan[in[0] - 97][in[1] - 97] == qizi[2]
&& in[0] - 97 >= 0 && in[0] - 97 <= N - 1 && in[1] - 97 >= 0
&& in[1] - 97 <= N - 1)
{
return 1;
}
else
{
return 0;
}
}
// 输出
void print()
{
int i, j;
printf(\"%8c\", \' \');
for (i = 0; i < N; i++)
{
printf(\"%2c\", 97 + i);
}
printf(\"\\n\");
for (i = 0; i < N; i++)
{
printf(\"%8c\", 97 + i);
for (j = 0; j < N; j++)
{
printf(\"%2c\", qipan[i][j]);
}
printf(\"\\n\");
}
}
// 判定输赢
int check()
{
int i, j, r, l, s;
r = in[0] - 97;
l = in[1] - 97;
// 判定行
for (i = 0; i < N - 4; i++)
{
s = 0;
for (j = 0; j < 5; j++)
{
s += qipan[i + j][l];
}
if (s == 5 * qizi[0])
{
return 1;
}
else
{
if (s == 5 * qizi[1])
{
return 2;
}
}
}
// 判定列
for (j = 0; j < N - 4; j++)
{
s = 0;
for (i = 0; i < 5; i++)
{
s += qipan[r][j + i];
}
if (s == 5 * qizi[0])
{
return 1;
}
else
{
if (s == 5 * qizi[1])
{
return 2;
}
}
}
// 判定主对角线方向斜线
if (r - l == 0)
{
for (i = 0; i < N - 4; i++)
{
s = 0;
for (j = 0; j < 5; j++)
{
s += qipan[i + j][i + j];
}
if (s == 5 * qizi[0])
{
return 1;
}
else
{
if (s == 5 * qizi[1])
{
return 2;
}
}
}
}
else
{
if (r - l > 0)
{
for (i = r - l; i < N - 4; i++)
{
s = 0;
for (j = 0; j < 5; j++)
{
s += qipan[i + j][i - (r - l) + j];
}
if (s == 5 * qizi[0])
{
return 1;
}
else
{
if (s == 5 * qizi[1])
{
return 2;
}
}
}
}
else
{
for (i = 0; i < N - 4; i++)
{
s = 0;
for (j = 0; j < 5; j++)
{
s += qipan[i + j][i + (l - r) + j];
}
if (s == 5 * qizi[0])
{
return 1;
}
else
{
if (s == 5 * qizi[1])
{
return 2;
}
}
}
}
}
// 判定副对角线方向斜线
if (r + l == N - 1)
{
for (j = 0; j < N - 4; j++)
{
s = 0;
for (i = 0; i < 5; i++)
{
s += qipan[N - 1 - j - i][j + i];
}
if (s == 5 * qizi[0])
{
return 1;
}
else
{
if (s == 5 * qizi[1])
{
return 2;
}
}
}
}
else
{
if (r + l > N - 1)
{
for (j = r + l - (N - 1); j < N - 4; j++)
{
s = 0;
for (i = 0; i < 5; i++)
{
s += qipan[r + l - j - i][j + i];
}
if (s == 5 * qizi[0])
{
return 1;
}
else
{
if (s == 5 * qizi[1])
{
return 2;
}
}
}
}
else
{
for (j = 0; j < N - 4; j++)
{
s = 0;
for (i = 0; i < 5; i++)
{
s += qipan[r + l - j - i][j + i];
}
if (s == 5 * qizi[0])
{
return 1;
}
else
{
if (s == 5 * qizi[1])
{
return 2;
}
}
}
}
}
// 未形成五子连珠返回0
return 0;
}
//测试副对角线,玩家2获胜
void test() {
//初始化O的位置
qipan[11][8] = qizi[1];
qipan[10][9] = qizi[1];
qipan[9][10] = qizi[1];
qipan[8][11] = qizi[1];
qipan[7][12] = qizi[1];
qipan[7][10] = qizi[1];
qipan[5][8] = qizi[1];
//初始化x的位置
qipan[12][7] = qizi[0];
qipan[12][11] = qizi[0];
qipan[11][9] = qizi[0];
qipan[10][10] = qizi[0];
qipan[10][11] = qizi[0];
qipan[10][12] = qizi[0];
qipan[9][9] = qizi[0];
in[0] = \'h\';
in[1] = \'m\';
print();
if(check() == 1) {
printf(\"玩家一获胜\\n\");
} else if(check() == 2) {
printf(\"玩家二获胜\\n\");
}
}
“五子连珠