zoukankan      html  css  js  c++  java
  • C语言之二维数组棋盘游戏

    #include<iostream>
    
    #include<stdio.h>
    using namespace std;
    int main()
    {
    //-1 没人赢 0:O赢 1:X赢
    int size;
    printf("0表示“甲方”,1表示“乙方”
    
    ");
    printf("请输入矩阵的大小
    ");
    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("抱歉啊,没有人赢哦!
    不服,再战!
    ");
    }
    else
    if (result == 0)
    {
    printf("恭喜!!!甲方赢啦
    ");
    }
    else
    if (result == 1)
    {
    printf("恭喜!!!乙方赢啦
    ");
    }
    printf("请输入矩阵的大小
    ");
    }
    return 0;
    }
    以大多数人努力程度之低,根本轮不到去拼天赋~
  • 相关阅读:
    BlockUI常见问题
    AssemblyInfo文件
    asp.net 如何让虚拟目录里面的webconfig不继承主目录config(转)
    jquery Ajax示例
    jQuery Ajax 实例 全解析 (转)
    如何在ASP.NET服务器控件库中嵌入JavaScript脚本文件(转)
    如何使用ASP.NET2.0的“嵌入的资源”(转)
    BlockUI对话框
    Jquery ajax参数设置
    What's production quality
  • 原文地址:https://www.cnblogs.com/gcter/p/5746743.html
Copyright © 2011-2022 走看看