码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-08-07 18:47:20      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

 

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

标签:

原文地址:http://www.cnblogs.com/gcter/p/5746743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!