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) == 1)
{
int board[100][100];
int num_of_O;
int num_of_X;
int result = -1;
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
{
scanf("%d", &board[i][j]);
}

//检查行
for (i = 0; i < size&&result == -1; i++)
{
num_of_O = num_of_X = 0;
for (j = 0; j < size; j++)
{
if (board[i][j] == 1)
{
num_of_X++;
}
else if(board[i][j] == 0)
{
num_of_O++;
}
}
if (num_of_O == size)
{
result = 0;
}
else
if (num_of_X == size)
{
result = 1;
}
}

//检查列
if (result == -1)
{
for (j = 0; j < size&&result == -1; j++)
{
num_of_O = num_of_X = 0;
for (i = 0; i < size; i++)
{
if (board[i][j] == 1)
{
num_of_X++;
}
else
if (board[i][j] == 0)
{
num_of_O++;
}
}
if (num_of_O == size)
{
result = 0;
}
else
if (num_of_X == size)
{
result = 1;
}
}
}
//检查正对角线
num_of_O = num_of_X = 0;
for (i = 0; i < size; i++)
{
if (board[i][i] == 1)
{
num_of_X++;
}
else
if (board[i][i] ==0 )
{
num_of_O++;
}

}

if (num_of_O == size)
{
result = 0;
}
else
if (num_of_X == size)
{
result = 1;
}

//检查反对角线
num_of_O = num_of_X = 0;
for (i = 0; i < size; i++)
{
if (board[i][size - 1 - i] == 1)//反对角线计算方法
{
num_of_X++;
}
else
if (board[i][size - 1 - i] == 0)
{
num_of_O++;
}
}

if (num_of_O == size)
{
result = 0;
}
else
if (num_of_X == size)
{
result = 1;
}

if (result == -1)
{
printf("抱歉啊,没有人赢哦!\n不服,再战!\n");
}
else
if (result == 0)
{
printf("恭喜!!!甲方赢啦\n");
}
else
if (result == 1)
{
printf("恭喜!!!乙方赢啦\n");
}
printf("请输入矩阵的大小\n");
}
return 0;
}

 

posted @ 2016-08-07 17:46  A-inspire  Views(980)  Comments(0Edit  收藏  举报