| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1361 人关注过本帖
标题:vm linux系统下我想输出一个五子棋,但是胜利条件判断不了,想问一下我该修 ...
只看楼主 加入收藏
qq714658686
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-8-14
收藏
 问题点数:0 回复次数:1 
vm linux系统下我想输出一个五子棋,但是胜利条件判断不了,想问一下我该修改哪里
#include <stdio.h>
#include <string.h>
/*


*/


#define N 10            //棋盘的行列数
#define BLACK 'X'        //黑方下棋的字符
#define WHITE 'O'        //白方下棋的字符

//初始化棋盘
void init(char arr[][N],const int n);

//刷新界面
void update(char arr[][N],const int n);

//flag: 1 表示当前下棋者为白方,-1 表示是黑方
int flag = 1;

int win = -1;

int x,y;    //当前下棋的位置
//下棋
int set(char arr[][N]);

void out3Tab(void)
{
    printf("\t\t\t");
}
void out4Tab(void)
{
    printf("\t\t\t\t");
}

//判断胜利者是谁,0是白子胜利,1是黑子胜利
int winner(char arr[][N],const int n){

    if(flag == 1)        //白子胜利的判断
    {
        int i,a=0,b=0,c=0,d=0;
        for(i = 1;i < 5;i++)
            if(x + i < N - 4 && arr[x + i][y] == flag)
                a++;
            else
                break;    //向右判断是否满足五颗
        for(i = 1;i < 5;i++)
            if(x - i >= 4 && arr[x - i][y] == flag)
                a++;
            else
                break;     //向左判断是否满足五颗
        if(a >= 5)
            win == 0;
            return flag;

        for(i = 1;i < 5;i++)
            if(y + i < N - 4 && arr[x][y + i] == flag)
                b++;
            else
                break;     //向上判断是否满足五颗
        for(i = 1;i < 5;i++)
            if(y - i > 4 && arr[x][y - i] == flag)
                b++;
            else
                break;    //向下判断是否满足五颗
        if(b >= 5)
            win == 0;
            return flag;

        for(i = 1;i < 5;i++)
            if(x + i < N - 4 && y + i < N -4 && arr[x+i][y+i] == flag)
                c++;
            else
                break;        //向右上判断是否满足五颗
        for(i = 1;i <5;i++)
            if(x - i > 4 && y - i > 4 && arr[x - i][y - i] == flag)
                c++;
            else
                break;        //左下判断是否满足五颗
        if(c >= 5)
            win == 0;
            return flag;

        for(i = 1;i < 5;i++)
            if(x - i > 4 && y + i < N -4 && arr[x - i][y + i] == flag)
                d++;
            else
                break;        //左上判断
        for(i =1;i < 5;i++)
            if(x + i < N -4 && y - i > 4 && arr[x + i][y - i] == flag)
                d++;
            else
                break;        //右下判断
        if(d >= 5)
            win == 0;
            return flag;

        return 0;
    }

    if(flag == -1)        //黑子胜利的判断
    {
        int i,a=0,b=0,c=0,d=0;
        for(i = 1;i < 5;i++)
            if(x + i < N - 4 && arr[x + i][y] == flag)
                a++;
            else
                break;    //向右判断是否满足五颗
        for(i = 1;i < 5;i++)
            if(x - i >= 4 && arr[x - i][y] == flag)
                a++;
            else
                break;     //向左判断是否满足五颗
        if(a >= 5)
            win == 1;
            return flag;

        for(i = 1;i < 5;i++)
            if(y + i < N - 4 && arr[x][y + i] == flag)
                b++;
            else
                break;     //向上判断是否满足五颗
        for(i == 1;i < 5;i++)
            if(y - i > 4 && arr[x][y - i] == flag)
                b++;
            else
                break;    //向下判断是否满足五颗
        if(b >= 5)
            win == 1;
            return flag;

        for(i = 1;i < 5;i++)
            if(x + i < N - 4 && y + i < N -4 && arr[x+i][y+i] == flag)
                c++;
            else
                break;        //向右上判断是否满足五颗
        for(i = 1;i <5;i++)
            if(x - i > 4 && y - i > 4 && arr[x - i][y - i] == flag)
                c++;
            else
                break;        //左下判断是否满足五颗
        if(c >= 5)
            win == 1;
            return flag;

        for(i = 1;i < 5;i++)
            if(x - i > 4 && y + i < N -4 && arr[x - i][y + i] == flag)
                d++;
            else
                break;        //左上判断是否满足五颗
        for(i =1;i < 5;i++)
            if(x + i < N -4 && y - i > 4 && arr[x + i][y - i] == flag)
                d++;
            else
                break;        //右下判断是否满足五颗
        if(d >= 5)
            win == 1;
            return flag;

        return 0;
    }
            
}

int main(void)
{
    char arr[N][N];
    init(arr,N);
    update(arr,N);
    while(1)
    {
        win = -1;
        set(arr);
        update(arr,N);
        init(arr,N);
        while(win != 0 && win != 1)
        {
            set(arr);
            update(arr,N);
            winner(arr,N);
            init(arr,N);
        }
        
    }
    return 0;
}

int set(char arr[][N])
{
    while(1)
    {
        out3Tab();
        if(flag == 1)
            printf("白方下棋:\n");
        else
            printf("黑方下棋:\n");
            
        out3Tab();
        printf("请输入下棋的位置(x,y):");
        scanf("%d,%d",&x,&y);
        if(arr[x - 1][y - 1] == '+')
            break;
        else
        {
            out3Tab();
            printf("请重新输入!!!!!!!!!!!\n");
        }
            
    }

    if(flag == 1)
        arr[x - 1][y - 1] = WHITE;
    else
        arr[x - 1][y - 1] = BLACK;
   
    flag = -flag;

    return flag;
}


void update(char arr[][N],const int n)
{
    system("clear");
    printf("\n\n\n\n\n");
    int i,j;
    for(i = -1; i < n; i++)
    {
        out4Tab();
        for(j = -1;j < N; j++)
        {
            if(i == -1 && j > -1)
                printf("%-2d",j + 1);
            else if(j == -1 && i > -1)
                printf("%-2d",i + 1);
            else if(i > -1 && j > -1)
                printf("%c ",arr[i][j]);
            else
                printf("  ");
            
        }
        putchar(10);
    }
}

void init(char arr[][N],const int n)
{
    int i,j;
    for(i = 0; i < n; i++)
        for(j = 0;j < N; j++)
            arr[i][j] = '+';

        if(win == 0)
        {
            printf("白子胜利!\n");
            return;

        }
        else if(win == 1)
        {
            printf("黑子胜利!\n");
            return;
        }
}

[此贴子已经被作者于2020-8-14 14:44编辑过]

搜索更多相关主题的帖子: 判断 flag break i++ for 
2020-08-14 14:41
qq714658686
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-8-14
收藏
得分:0 
void update(char arr[][N],const int n)
{
    system("clear");
    printf("\n\n\n\n\n");
    int i,j;
    for(i = -1; i < n; i++)
    {
        out4Tab();
        for(j = -1;j < N; j++)
        {
            if(i == -1 && j > -1)
                printf("%-2d",j + 1);
            else if(j == -1 && i > -1)
                printf("%-2d",i + 1);
            else if(i > -1 && j > -1)
                printf("%c ",arr[i][j]);
            else
                printf("  ");
            
        }
        putchar(10);
    }
}

void init(char arr[][N],const int n)
{
    int i,j;
    for(i = 0; i < n; i++)
        for(j = 0;j < N; j++)
            arr[i][j] = '+';

        if(win == 0)
        {
            printf("白子胜利!\n");
            return;

        }
        else if(win == 1)
        {
            printf("黑子胜利!\n"
2020-08-14 14:42
快速回复:vm linux系统下我想输出一个五子棋,但是胜利条件判断不了,想问一下我 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033949 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved