C语言之二维数组棋盘游戏

#include<iostream>

#include<stdio.h>
using namespace std;
int main()
{
//-1 没人赢 0:O赢 1:X赢
int size;
printf("0表示“甲方”,1表示“乙方”\n\n");
printf("请输入矩阵的大小\n");
while (scanf("%d", &size) == )
{
int board[][];
int num_of_O;
int num_of_X;
int result = -;
int i, j;
for (i = ; i < size; i++)
for (j = ; j < size; j++)
{
scanf("%d", &board[i][j]);
} //检查行
for (i = ; i < size&&result == -; i++)
{
num_of_O = num_of_X = ;
for (j = ; j < size; j++)
{
if (board[i][j] == )
{
num_of_X++;
}
else if(board[i][j] == )
{
num_of_O++;
}
}
if (num_of_O == size)
{
result = ;
}
else
if (num_of_X == size)
{
result = ;
}
} //检查列
if (result == -)
{
for (j = ; j < size&&result == -; j++)
{
num_of_O = num_of_X = ;
for (i = ; i < size; i++)
{
if (board[i][j] == )
{
num_of_X++;
}
else
if (board[i][j] == )
{
num_of_O++;
}
}
if (num_of_O == size)
{
result = ;
}
else
if (num_of_X == size)
{
result = ;
}
}
}
//检查正对角线
num_of_O = num_of_X = ;
for (i = ; i < size; i++)
{
if (board[i][i] == )
{
num_of_X++;
}
else
if (board[i][i] == )
{
num_of_O++;
} } if (num_of_O == size)
{
result = ;
}
else
if (num_of_X == size)
{
result = ;
} //检查反对角线
num_of_O = num_of_X = ;
for (i = ; i < size; i++)
{
if (board[i][size - - i] == )//反对角线计算方法
{
num_of_X++;
}
else
if (board[i][size - - i] == )
{
num_of_O++;
}
} if (num_of_O == size)
{
result = ;
}
else
if (num_of_X == size)
{
result = ;
} if (result == -)
{
printf("抱歉啊,没有人赢哦!\n不服,再战!\n");
}
else
if (result == )
{
printf("恭喜!!!甲方赢啦\n");
}
else
if (result == )
{
printf("恭喜!!!乙方赢啦\n");
}
printf("请输入矩阵的大小\n");
}
return ;
}
上一篇:linux 调度器配制参数


下一篇:【WinForm】C# 采用POST登录京东