天生棋局(C语言)

廉价感情. 提交于 2020-02-27 23:40:25

源码如下:

生成一个 10*10 的棋局,要求,初始化为零。随机置入 10 颗棋子,棋子处置为 1,并打印。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{

    int chess[10][10] = {0};
    srand(time(NULL));  //注意!

//方法一
    int t=10;
    while(t--)
    {
        int i = rand()%10;
        int j = rand()%10;
        if(chess[i][j] == 0)
        {
            chess[i][j] = 1;
        }
        else
            t += 1;     //注意!
    }


#if 0
//方法二
//    int count = 0;
//    while(1)    //死循环
//    {
//        int i = rand()%10;
//        int j = rand()%10;
//        if(chess[i][j] == 0)
//        {
//            chess[i][j] = 1;
//            count++;
//            if(count == 10)
//                break;
//        }
//    }
#endif

#if 0
//方法三
//    int count = 0;
//    while(count < 10)
//    {
//        int i = rand()%10;
//        int j = rand()%10;
//        if(chess[i][j] == 1)
//            continue;
//        chess[i][j] = 1;
//        count++;
//    }
#endif

    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            printf("%2d",chess[i][j]);
        }
        putchar(10);
    }

    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!