鱼C论坛

 找回密码
 立即注册
查看: 1244|回复: 14

[已解决]分享一个五子棋的程序,以及一个bug大佬看看怎么回事

[复制链接]
发表于 2019-1-28 14:45:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 cookies945 于 2019-1-28 14:46 编辑
#include <stdio.h>
#define MAX 13
#define MIN 1

int addBlack(int row_in_fun,int col_in_fun,int count);
int addWhite(int row_in_fun,int col_in_fun,int count);
void cleanChessBoard(char* CB,int n);
int judge(int row_in_fun,int col_in_fun,int trun);
void printChessBoard(void);
void choose(void);
int judge_N(int row,int col,int turn);
int judge_S(int row,int col,int turn);
int judge_E(int row,int col,int turn);
int judge_W(int row,int col,int turn);
int judge_NW(int row,int col,int turn);
int judge_NE(int row,int col,int turn);
int judge_SE(int row,int col,int turn);
int judge_SW(int row,int col,int turn);

char chessBoard[14][14];
char chess[2] = {'0','@'};
int main(){        
        char *CB;
        CB = &chessBoard[0][0];
        cleanChessBoard(CB,14);
        choose();
        return 0;
}
void cleanChessBoard(char* CB,int n){
        int i,j; 
        for(i = 0;i < n;i++){  // n 为行 
                for(j = 0;j < n;j++){ // j 为列 
        
                        if(i == 0){
                                printf("%2d",j); // 第一行打印棋盘上面的数字坐标 
                                
                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d",i);
                        }
                        else{
                                printf(" ");
                                *(CB + i*n + j) = '*';       //未落子的区域打印 * 表明未落子;
                                printf("%c",chessBoard[i][j]);   
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
int addBlack(int row_in_fun,int col_in_fun,int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){   //判断该区域是否已经落了子 
                chessBoard[row_in_fun][col_in_fun] = '@'; 
                flag = judge(row_in_fun,col_in_fun,count);
                if(flag){
                        printf("黑棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!"); 
                printf("重新输入坐标:");
                int row,col;
                scanf("%d %d",&row,&col);
                addBlack(row,col,count);
        }
        return 0;
}
int addWhite(int row_in_fun,int col_in_fun,int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){
                chessBoard[row_in_fun][col_in_fun] = '0';
                flag = judge(row_in_fun,col_in_fun,count);
                if(flag){
                        printf("白棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!"); 
                printf("重新输入坐标:");
                int row,col;
                scanf("%d %d",&row,&col);
                addWhite(row,col,count);
        }
        return 0;
}
void printChessBoard(void){
        int i,j;
        for(i = 0;i < 14;i++){  // n 为行 
                for(j = 0;j < 14;j++){ // j 为列 
        
                        if(i == 0){
                                printf("%2d",j); // 第一行打印棋盘上面的数字坐标 
                                
                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d",i);
                        }
                        else{
                                printf(" ");              
                                printf("%c",chessBoard[i][j]);   
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
void choose(void){
        int count = 0;//判断是谁下棋 
        int row,col;
        int end;//结束判断 
        do{
                if(count % 2 == 0){
                        printf("请白棋落子!"); 
                }
                else{
                        printf("请黑棋落子!"); 
                }
                printf("请输入要落子的坐标(行 列):");
                scanf("%d %d",&row,&col);
                if(count % 2 == 0){
                        end = addWhite(row,col,count);
                        count++;
                }
                else{
                        end = addBlack(row,col,count);
                        count++;
                }
                printChessBoard();
                if(end){
                        break;
                }
        }while(1);
}
int judge(int row,int col,int turn){
        int N = 0,S = 0,E = 0,W = 0,NW = 0,NE = 0,SW = 0,SE = 0;
        turn = turn % 2;
        N = judge_N(row,col,turn);
        S = judge_S(row,col,turn);
        W = judge_W(row,col,turn);
        E = judge_E(row,col,turn);
        SW = judge_SW(row,col,turn);
        SE = judge_SE(row,col,turn);
        NW = judge_NW(row,col,turn);
        NE = judge_NE(row,col,turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >=5){
                return 1;
        }
        return 0;
}
int judge_N(int row,int col,int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row,int col,int turn){
        int count_S = 0;
        while(chessBoard[row++][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row,int col,int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row,int col,int turn){
        int count_W = 0;
        while(chessBoard[row][col--] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row,int col,int turn){
        int count_NW = 0;
        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row,int col,int turn){
        int count_NE = 0;
        while(chessBoard[row++][col++] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row,int col,int turn){
        int count_SE = 0;
        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row,int col,int turn){
        int count_SW = 0;
        while(chessBoard[row--][col--] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }
        return count_SW;
}
请黑棋落子!请输入要落子的坐标(行 列):1 5
黑棋获胜!
0   1   2   3   4   5   6   7   8   9  10  11  12  13
1   0   @   @   @   @   *   *   *   *   *   *   *   *
2   *   0   *   *   *   *   *   *   *   *   *   *   *
3   *   *   0   *   *   *   *   *   *   *   *   *   *
4   *   *   *   0   *   *   *   *   *   *   *   *   *
5   *   *   *   *   *   *   *   *   *   *   *   *   *
6   *   *   *   *   *   *   *   *   *   *   *   *   *
7   *   *   *   *   *   *   *   *   *   *   *   *   *
8   *   *   *   *   *   *   *   *   *   *   *   *   *
9   *   *   *   *   *   *   *   *   *   *   *   *   *
10   *   *   *   *   *   *   *   *   *   *   *   *   *
11   *   *   *   *   *   *   *   *   *   *   *   *   *
12   *   *   *   *   *   *   *   *   *   *   *   *   *
13   *   *   *   *   *   *   *   *   *   *   *   *   *

对了‘0’是表示白棋,‘@’是表示黑棋
最后有个这样的结果,大佬们看看咋办呐
最佳答案
2019-1-30 13:15:15
我知道问题了,你在判断的时候多加了一次
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 13
#define MIN 1

int addBlack(int row_in_fun, int col_in_fun, int count);
int addWhite(int row_in_fun, int col_in_fun, int count);
void cleanChessBoard(char* CB, int n);
int judge(int row_in_fun, int col_in_fun, int trun);
void printChessBoard(void);
void choose(void);
int judge_N(int row, int col, int turn);
int judge_S(int row, int col, int turn);
int judge_E(int row, int col, int turn);
int judge_W(int row, int col, int turn);
int judge_NW(int row, int col, int turn);
int judge_NE(int row, int col, int turn);
int judge_SE(int row, int col, int turn);
int judge_SW(int row, int col, int turn);

char chessBoard[14][14];
char chess[2] = {'0', '@'};

int main(){
        //char tmp[14][14] = {
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, '@', '0', '@', '@', '@', '@', '0', 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        //};
        //
        //memcpy(chessBoard, tmp, sizeof(tmp));
        //printf("%d\n", judge(1, 2, 1));
        //exit(1);
        
        
        char *CB;
        CB = &chessBoard[0][0];
        cleanChessBoard(CB, 14);
        choose();
        return 0;
}
void cleanChessBoard(char* CB, int n){
        int i, j;
        for(i = 0; i < n; i++){  // n 为行 
                for(j = 0; j < n; j++){ // j 为列 

                        if(i == 0){
                                printf("%2d", j); // 第一行打印棋盘上面的数字坐标 

                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d", i);
                        }
                        else{
                                printf(" ");
                                *(CB + i * n + j) = '*';       //未落子的区域打印 * 表明未落子;
                                printf("%c", chessBoard[i][j]);
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
int addBlack(int row_in_fun, int col_in_fun, int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){   //判断该区域是否已经落了子 
                chessBoard[row_in_fun][col_in_fun] = '@';
                flag = judge(row_in_fun, col_in_fun, count);
                if(flag){
                        printf("黑棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!");
                printf("重新输入坐标:");
                int row, col;
                scanf("%d %d", &row, &col);
                addBlack(row, col, count);
        }
        return 0;
}
int addWhite(int row_in_fun, int col_in_fun, int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){
                chessBoard[row_in_fun][col_in_fun] = '0';
                flag = judge(row_in_fun, col_in_fun, count);
                if(flag){
                        printf("白棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!");
                printf("重新输入坐标:");
                int row, col;
                scanf("%d %d", &row, &col);
                addWhite(row, col, count);
        }
        return 0;
}
void printChessBoard(void){
        int i, j;
        for(i = 0; i < 14; i++){  // n 为行 
                for(j = 0; j < 14; j++){ // j 为列 

                        if(i == 0){
                                printf("%2d", j); // 第一行打印棋盘上面的数字坐标 

                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d", i);
                        }
                        else{
                                printf(" ");
                                printf("%c", chessBoard[i][j]);
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
void choose(void){
        int count = 0;//判断是谁下棋 
        int row, col;
        int end;//结束判断 
        do{
                if(count % 2 == 0){
                        printf("请白棋落子!");
                }
                else{
                        printf("请黑棋落子!");
                }
                printf("请输入要落子的坐标(行 列):");
                scanf("%d %d", &row, &col);
                if(count % 2 == 0){
                        end = addWhite(row, col, count);
                        count++;
                }
                else{
                        end = addBlack(row, col, count);
                        count++;
                }
                printChessBoard();
                if(end){
                        break;
                }
        }
        while(1);
}

int judge(int row, int col, int turn){
        int N = 0, S = 0, E = 0, W = 0, NW = 0, NE = 0, SW = 0, SE = 0;
        turn = turn % 2;
        N = judge_N(row, col, turn);
        S = judge_S(row, col, turn);
        W = judge_W(row, col, turn);
        E = judge_E(row, col, turn);
        SW = judge_SW(row, col, turn);
        SE = judge_SE(row, col, turn);
        NW = judge_NW(row, col, turn);
        NE = judge_NE(row, col, turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >= 5){
                return 1;
        }
        return 0;
}
int judge_N(int row, int col, int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row, int col, int turn){
        int count_S = 0;
        while(chessBoard[++row][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row, int col, int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row, int col, int turn){
        int count_W = 0;
        while(chessBoard[row][--col] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row, int col, int turn){
        int count_NW = 0;
        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row, int col, int turn){
        int count_NE = 0;
        while(chessBoard[++row][++col] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row, int col, int turn){
        int count_SE = 0;
        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row, int col, int turn){
        int count_SW = 0;
        while(chessBoard[--row][--col] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }
        return count_SW;
}

int judge(int row, int col, int turn){
        int N = 0, S = 0, E = 0, W = 0, NW = 0, NE = 0, SW = 0, SE = 0;
        turn = turn % 2;
        N = judge_N(row, col, turn);
        S = judge_S(row, col, turn);
        W = judge_W(row, col, turn);
        E = judge_E(row, col, turn);
        SW = judge_SW(row, col, turn);
        SE = judge_SE(row, col, turn);
        NW = judge_NW(row, col, turn);
        NE = judge_NE(row, col, turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >= 5){
                return 1;
        }
        return 0;
}
int judge_N(int row, int col, int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row, int col, int turn){
        int count_S = 0;
        while(chessBoard[++row][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row, int col, int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row, int col, int turn){
        int count_W = 0;
        while(chessBoard[row][--col] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row, int col, int turn){
        int count_NW = 0;
        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row, int col, int turn){
        int count_NE = 0;
        while(chessBoard[++row][++col] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row, int col, int turn){
        int count_SE = 0;
        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row, int col, int turn){
        int count_SW = 0;
        while(chessBoard[--row][--col] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }
        return count_SW;
}
int judge(int row,int col,int turn){
        int N = 0,S = 0,E = 0,W = 0,NW = 0,NE = 0,SW = 0,SE = 0;
        turn = turn % 2;
        N = judge_N(row,col,turn);
        S = judge_S(row,col,turn);
        W = judge_W(row,col,turn);
        E = judge_E(row,col,turn);
        SW = judge_SW(row,col,turn);
        SE = judge_SE(row,col,turn);
        NW = judge_NW(row,col,turn);
        NE = judge_NE(row,col,turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >=5){
                return 1;
        }
        return 0;
}
int judge_N(int row,int col,int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row,int col,int turn){
        int count_S = 0;
        while(chessBoard[row++][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row,int col,int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row,int col,int turn){
        int count_W = 0;
        while(chessBoard[row][col--] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row,int col,int turn){
        int count_NW = 0;
        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row,int col,int turn){
        int count_NE = 0;
        while(chessBoard[row++][col++] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row,int col,int turn){
        int count_SE = 0;
        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row,int col,int turn){
        int count_SW = 0;
        while(chessBoard[row--][col--] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }
        return count_SW;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-1-29 21:26:32 | 显示全部楼层
judge函数逻辑错误
#include <stdio.h>
#include <string.h>

#define MAX 13
#define MIN 1

int addBlack(int row_in_fun, int col_in_fun, int count);
int addWhite(int row_in_fun, int col_in_fun, int count);
void cleanChessBoard(char* CB, int n);
//int judge(int row_in_fun, int col_in_fun, int trun);
int judge(int turn);
void printChessBoard(void);
void choose(void);
//int judge_N(int row, int col, int turn);
//int judge_S(int row, int col, int turn);
//int judge_E(int row, int col, int turn);
//int judge_W(int row, int col, int turn);
//int judge_NW(int row, int col, int turn);
//int judge_NE(int row, int col, int turn);
//int judge_SE(int row, int col, int turn);
//int judge_SW(int row, int col, int turn);

char chessBoard[14][14];
char chess[2] = {'0', '@'};

int main(){
        char *CB;
        CB = &chessBoard[0][0];
        cleanChessBoard(CB, 14);
        choose();
        return 0;
}
void cleanChessBoard(char* CB, int n){
        int i, j;
        for(i = 0; i < n; i++){  // n 为行 
                for(j = 0; j < n; j++){ // j 为列 

                        if(i == 0){
                                printf("%2d", j); // 第一行打印棋盘上面的数字坐标 

                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d", i);
                        }
                        else{
                                printf(" ");
                                *(CB + i * n + j) = '*';       //未落子的区域打印 * 表明未落子;
                                printf("%c", chessBoard[i][j]);
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
int addBlack(int row_in_fun, int col_in_fun, int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){   //判断该区域是否已经落了子 
                chessBoard[row_in_fun][col_in_fun] = '@';
                flag = judge(count % 2);
                if(flag){
                        printf("黑棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!");
                printf("重新输入坐标:");
                int row, col;
                scanf("%d %d", &row, &col);
                addBlack(row, col, count);
        }
        return 0;
}
int addWhite(int row_in_fun, int col_in_fun, int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){
                chessBoard[row_in_fun][col_in_fun] = '0';
                flag = judge(count % 2);
                if(flag){
                        printf("白棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!");
                printf("重新输入坐标:");
                int row, col;
                scanf("%d %d", &row, &col);
                addWhite(row, col, count);
        }
        return 0;
}
void printChessBoard(void){
        int i, j;
        for(i = 0; i < 14; i++){  // n 为行 
                for(j = 0; j < 14; j++){ // j 为列 

                        if(i == 0){
                                printf("%2d", j); // 第一行打印棋盘上面的数字坐标 

                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d", i);
                        }
                        else{
                                printf(" ");
                                printf("%c", chessBoard[i][j]);
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
void choose(void){
        int count = 0;//判断是谁下棋 
        int row, col;
        int end;//结束判断 
        do{
                if(count % 2 == 0){
                        printf("请白棋落子!");
                }
                else{
                        printf("请黑棋落子!");
                }
                printf("请输入要落子的坐标(行 列):");
                scanf("%d %d", &row, &col);
                if(count % 2 == 0){
                        end = addWhite(row, col, count);
                        count++;
                }
                else{
                        end = addBlack(row, col, count);
                        count++;
                }
                printChessBoard();
                if(end){
                        break;
                }
        }
        while(1);
}

int judge(int turn)
{
        for(int y = MIN; y <= MAX; ++y)                // 每一行
        {
                for(int x = MIN; x <= MAX - 4; ++x)
                {
                        if(chessBoard[y][x] == chess[turn]
                                && chessBoard[y][x + 1] == chess[turn]
                                && chessBoard[y][x + 2] == chess[turn]
                                && chessBoard[y][x + 3] == chess[turn]
                                && chessBoard[y][x + 4] == chess[turn])
                        {
                                return 1;
                        }
                }
        }

        for(int x = MIN; x <= MAX; ++x)                // 每一列
        {
                for(int y = MIN; y <= MAX - 4; ++y)
                {
                        if(chessBoard[y][x] == chess[turn]
                                && chessBoard[y + 1][x] == chess[turn]
                                && chessBoard[y + 2][x] == chess[turn]
                                && chessBoard[y + 3][x] == chess[turn]
                                && chessBoard[y + 4][x] == chess[turn])
                        {
                                return 1;
                        }
                }
        }

        int tmp[MAX + 1][MAX + 1];
        memset(tmp, 0, sizeof(tmp));
        for(int y = MIN; y <= MAX; ++y)                // 左下角到右上角的斜线的一半
        {
                int index = 1;
                for(int a = y, x = MIN; a >= 1; --a, ++x)
                        tmp[y][index++] = chessBoard[a][x];
        }
        for(int y = MIN; y <= MAX; ++y)                // 验证
        {
                for(int x = MIN; x <= MAX - 4; ++x)
                {
                        if(tmp[y][x] == chess[turn]
                                && tmp[y][x + 1] == chess[turn]
                                && tmp[y][x + 2] == chess[turn]
                                && tmp[y][x + 3] == chess[turn]
                                && tmp[y][x + 4] == chess[turn])
                        {
                                return 1;
                        }
                }
        }
        memset(tmp, 0, sizeof(tmp));
        for(int y = MIN; y <= MAX; ++y)                // 左下角到右上角的斜线的另一半
        {
                int index = 1;
                for(int a = y, x = MAX; a <= MAX; ++a, --x)
                        tmp[y][index++] = chessBoard[a][x];
        }
        for(int y = MIN; y <= MAX; ++y)                // 验证
        {
                for(int x = MIN; x <= MAX - 4; ++x)
                {
                        if(tmp[y][x] == chess[turn]
                                && tmp[y][x + 1] == chess[turn]
                                && tmp[y][x + 2] == chess[turn]
                                && tmp[y][x + 3] == chess[turn]
                                && tmp[y][x + 4] == chess[turn])
                        {
                                return 1;
                        }
                }
        }

        memset(tmp, 0, sizeof(tmp));
        for(int y = MIN; y <= MAX; ++y)                // 左上角到右下角的斜线的一半
        {
                int index = 1;
                for(int a = y, x = MIN; a <= MAX; ++a, ++x)
                        tmp[y][index++] = chessBoard[a][x];
        }
        for(int y = MIN; y <= MAX; ++y)                // 验证
        {
                for(int x = MIN; x <= MAX - 4; ++x)
                {
                        if(tmp[y][x] == chess[turn]
                                && tmp[y][x + 1] == chess[turn]
                                && tmp[y][x + 2] == chess[turn]
                                && tmp[y][x + 3] == chess[turn]
                                && tmp[y][x + 4] == chess[turn])
                        {
                                return 1;
                        }
                }
        }
        memset(tmp, 0, sizeof(tmp));
        for(int y = MIN; y <= MAX; ++y)                // 左上角到右下角的斜线的另一半
        {
                int index = 1;
                for(int a = y, x = MAX; a >= 1; --a, --x)
                        tmp[y][index++] = chessBoard[a][x];
        }
        for(int y = MIN; y <= MAX; ++y)                // 验证
        {
                for(int x = MIN; x <= MAX - 4; ++x)
                {
                        if(tmp[y][x] == chess[turn]
                                && tmp[y][x + 1] == chess[turn]
                                && tmp[y][x + 2] == chess[turn]
                                && tmp[y][x + 3] == chess[turn]
                                && tmp[y][x + 4] == chess[turn])
                        {
                                return 1;
                        }
                }
        }
        return 0;
}

//int judge(int row, int col, int turn){
//        int N = 0, S = 0, E = 0, W = 0, NW = 0, NE = 0, SW = 0, SE = 0;
//        turn = turn % 2;
//        N = judge_N(row, col, turn);
//        S = judge_S(row, col, turn);
//        W = judge_W(row, col, turn);
//        E = judge_E(row, col, turn);
//        SW = judge_SW(row, col, turn);
//        SE = judge_SE(row, col, turn);
//        NW = judge_NW(row, col, turn);
//        NE = judge_NE(row, col, turn);
//        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >= 5){
//                return 1;
//        }
//        return 0;
//}
//int judge_N(int row, int col, int turn){
//        int count_N = 0;
//        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
//                count_N++;
//        }
//        return count_N;
//}
//int judge_S(int row, int col, int turn){
//        int count_S = 0;
//        while(chessBoard[row++][col] == chess[turn] && row <= MAX){
//                count_S++;
//        }
//        return count_S;
//}
//int judge_E(int row, int col, int turn){
//        int count_E = 0;
//        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
//                count_E++;
//        }
//        return count_E;
//}
//int judge_W(int row, int col, int turn){
//        int count_W = 0;
//        while(chessBoard[row][col--] == chess[turn] && col >= MIN){
//                count_W++;
//        }
//        return count_W;
//}
//int judge_NW(int row, int col, int turn){
//        int count_NW = 0;
//        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
//                count_NW++;
//        }
//        return count_NW;
//}
//int judge_NE(int row, int col, int turn){
//        int count_NE = 0;
//        while(chessBoard[row++][col++] == chess[turn] && (row <= MAX && col <= MAX)){
//                count_NE++;
//        }
//        return count_NE;
//}
//int judge_SE(int row, int col, int turn){
//        int count_SE = 0;
//        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
//                count_SE++;
//        }
//        return count_SE;
//}
//int judge_SW(int row, int col, int turn){
//        int count_SW = 0;
//        while(chessBoard[row--][col--] == chess[turn] && (row >= MIN && col >= MIN)){
//                count_SW++;
//        }
//        return count_SW;
//}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-30 09:51:39 | 显示全部楼层
emm,大佬能否说说我的逻辑错误是怎样的?还有就是大佬这个是搜索整个棋盘的,这样是不是有点费时呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-30 12:46:19 | 显示全部楼层
cookies945 发表于 2019-1-30 09:51
emm,大佬能否说说我的逻辑错误是怎样的?还有就是大佬这个是搜索整个棋盘的,这样是不是有点费时呢?

五子棋的规则是什么?
在每条横线,每条竖线,还有斜线
只要有连续的5个棋子,就结束游戏,达成这一目的方获胜

你的代码逻辑是
在每条横线,每条竖线,还有斜线上只要有5个相同的棋子就行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-30 13:03:15 | 显示全部楼层
等一等,我再看一看你的代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-30 13:15:15 | 显示全部楼层    本楼为最佳答案   
我知道问题了,你在判断的时候多加了一次
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 13
#define MIN 1

int addBlack(int row_in_fun, int col_in_fun, int count);
int addWhite(int row_in_fun, int col_in_fun, int count);
void cleanChessBoard(char* CB, int n);
int judge(int row_in_fun, int col_in_fun, int trun);
void printChessBoard(void);
void choose(void);
int judge_N(int row, int col, int turn);
int judge_S(int row, int col, int turn);
int judge_E(int row, int col, int turn);
int judge_W(int row, int col, int turn);
int judge_NW(int row, int col, int turn);
int judge_NE(int row, int col, int turn);
int judge_SE(int row, int col, int turn);
int judge_SW(int row, int col, int turn);

char chessBoard[14][14];
char chess[2] = {'0', '@'};

int main(){
        //char tmp[14][14] = {
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, '@', '0', '@', '@', '@', '@', '0', 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        //};
        //
        //memcpy(chessBoard, tmp, sizeof(tmp));
        //printf("%d\n", judge(1, 2, 1));
        //exit(1);
        
        
        char *CB;
        CB = &chessBoard[0][0];
        cleanChessBoard(CB, 14);
        choose();
        return 0;
}
void cleanChessBoard(char* CB, int n){
        int i, j;
        for(i = 0; i < n; i++){  // n 为行 
                for(j = 0; j < n; j++){ // j 为列 

                        if(i == 0){
                                printf("%2d", j); // 第一行打印棋盘上面的数字坐标 

                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d", i);
                        }
                        else{
                                printf(" ");
                                *(CB + i * n + j) = '*';       //未落子的区域打印 * 表明未落子;
                                printf("%c", chessBoard[i][j]);
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
int addBlack(int row_in_fun, int col_in_fun, int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){   //判断该区域是否已经落了子 
                chessBoard[row_in_fun][col_in_fun] = '@';
                flag = judge(row_in_fun, col_in_fun, count);
                if(flag){
                        printf("黑棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!");
                printf("重新输入坐标:");
                int row, col;
                scanf("%d %d", &row, &col);
                addBlack(row, col, count);
        }
        return 0;
}
int addWhite(int row_in_fun, int col_in_fun, int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){
                chessBoard[row_in_fun][col_in_fun] = '0';
                flag = judge(row_in_fun, col_in_fun, count);
                if(flag){
                        printf("白棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!");
                printf("重新输入坐标:");
                int row, col;
                scanf("%d %d", &row, &col);
                addWhite(row, col, count);
        }
        return 0;
}
void printChessBoard(void){
        int i, j;
        for(i = 0; i < 14; i++){  // n 为行 
                for(j = 0; j < 14; j++){ // j 为列 

                        if(i == 0){
                                printf("%2d", j); // 第一行打印棋盘上面的数字坐标 

                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d", i);
                        }
                        else{
                                printf(" ");
                                printf("%c", chessBoard[i][j]);
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
void choose(void){
        int count = 0;//判断是谁下棋 
        int row, col;
        int end;//结束判断 
        do{
                if(count % 2 == 0){
                        printf("请白棋落子!");
                }
                else{
                        printf("请黑棋落子!");
                }
                printf("请输入要落子的坐标(行 列):");
                scanf("%d %d", &row, &col);
                if(count % 2 == 0){
                        end = addWhite(row, col, count);
                        count++;
                }
                else{
                        end = addBlack(row, col, count);
                        count++;
                }
                printChessBoard();
                if(end){
                        break;
                }
        }
        while(1);
}

int judge(int row, int col, int turn){
        int N = 0, S = 0, E = 0, W = 0, NW = 0, NE = 0, SW = 0, SE = 0;
        turn = turn % 2;
        N = judge_N(row, col, turn);
        S = judge_S(row, col, turn);
        W = judge_W(row, col, turn);
        E = judge_E(row, col, turn);
        SW = judge_SW(row, col, turn);
        SE = judge_SE(row, col, turn);
        NW = judge_NW(row, col, turn);
        NE = judge_NE(row, col, turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >= 5){
                return 1;
        }
        return 0;
}
int judge_N(int row, int col, int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row, int col, int turn){
        int count_S = 0;
        while(chessBoard[++row][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row, int col, int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row, int col, int turn){
        int count_W = 0;
        while(chessBoard[row][--col] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row, int col, int turn){
        int count_NW = 0;
        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row, int col, int turn){
        int count_NE = 0;
        while(chessBoard[++row][++col] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row, int col, int turn){
        int count_SE = 0;
        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row, int col, int turn){
        int count_SW = 0;
        while(chessBoard[--row][--col] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }
        return count_SW;
}

int judge(int row, int col, int turn){
        int N = 0, S = 0, E = 0, W = 0, NW = 0, NE = 0, SW = 0, SE = 0;
        turn = turn % 2;
        N = judge_N(row, col, turn);
        S = judge_S(row, col, turn);
        W = judge_W(row, col, turn);
        E = judge_E(row, col, turn);
        SW = judge_SW(row, col, turn);
        SE = judge_SE(row, col, turn);
        NW = judge_NW(row, col, turn);
        NE = judge_NE(row, col, turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >= 5){
                return 1;
        }
        return 0;
}
int judge_N(int row, int col, int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row, int col, int turn){
        int count_S = 0;
        while(chessBoard[++row][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row, int col, int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row, int col, int turn){
        int count_W = 0;
        while(chessBoard[row][--col] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row, int col, int turn){
        int count_NW = 0;
        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row, int col, int turn){
        int count_NE = 0;
        while(chessBoard[++row][++col] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row, int col, int turn){
        int count_SE = 0;
        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row, int col, int turn){
        int count_SW = 0;
        while(chessBoard[--row][--col] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }
        return count_SW;
}
int judge(int row,int col,int turn){
        int N = 0,S = 0,E = 0,W = 0,NW = 0,NE = 0,SW = 0,SE = 0;
        turn = turn % 2;
        N = judge_N(row,col,turn);
        S = judge_S(row,col,turn);
        W = judge_W(row,col,turn);
        E = judge_E(row,col,turn);
        SW = judge_SW(row,col,turn);
        SE = judge_SE(row,col,turn);
        NW = judge_NW(row,col,turn);
        NE = judge_NE(row,col,turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >=5){
                return 1;
        }
        return 0;
}
int judge_N(int row,int col,int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row,int col,int turn){
        int count_S = 0;
        while(chessBoard[row++][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row,int col,int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row,int col,int turn){
        int count_W = 0;
        while(chessBoard[row][col--] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row,int col,int turn){
        int count_NW = 0;
        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row,int col,int turn){
        int count_NE = 0;
        while(chessBoard[row++][col++] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row,int col,int turn){
        int count_SE = 0;
        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row,int col,int turn){
        int count_SW = 0;
        while(chessBoard[row--][col--] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }
        return count_SW;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-30 14:51:48 | 显示全部楼层
人造人 发表于 2019-1-30 13:15
我知道问题了,你在判断的时候多加了一次

能在注释里告诉我哪里多判断了一次么,我代码太乱了
我也不知道是哪里的判断多了一次
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-30 15:17:41 | 显示全部楼层
cookies945 发表于 2019-1-30 14:51
能在注释里告诉我哪里多判断了一次么,我代码太乱了
我也不知道是哪里的判断多了一次
{:10_2 ...
int judge_N(int row, int col, int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row, int col, int turn){
        int count_S = 0;
        while(chessBoard[++row][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}




int judge(int row, int col, int turn){
        int N = 0, S = 0, E = 0, W = 0, NW = 0, NE = 0, SW = 0, SE = 0;
        turn = turn % 2;
        N = judge_N(row, col, turn);
        S = judge_S(row, col, turn);
        W = judge_W(row, col, turn);
        E = judge_E(row, col, turn);
        SW = judge_SW(row, col, turn);
        SE = judge_SE(row, col, turn);
        NW = judge_NW(row, col, turn);
        NE = judge_NE(row, col, turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >= 5){
                return 1;
        }
        return 0;
}
int judge_N(int row, int col, int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row, int col, int turn){
        int count_S = 0;
        while(chessBoard[++row][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row, int col, int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row, int col, int turn){
        int count_W = 0;
        while(chessBoard[row][--col] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row, int col, int turn){
        int count_NW = 0;
        while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row, int col, int turn){
        int count_NE = 0;
        while(chessBoard[++row][++col] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row, int col, int turn){
        int count_SE = 0;
        while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row, int col, int turn){
        int count_SW = 0;
        while(chessBoard[--row][--col] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }
        return count_SW;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-30 15:18:29 | 显示全部楼层
while(chessBoard[row--][col] == chess[turn] && row >= MIN){
while(chessBoard[++row][col] == chess[turn] && row <= MAX){
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-1-30 18:35:07 | 显示全部楼层
人造人 发表于 2019-1-30 15:18
while(chessBoard[row--][col] == chess[turn] && row >= MIN){
while(chessBoard[++row][col] == chess[t ...

算法了解的很到位,代码看得很仔细!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-31 10:34:55 | 显示全部楼层
本帖最后由 cookies945 于 2019-1-31 11:01 编辑
人造人 发表于 2019-1-30 15:18
while(chessBoard[row--][col] == chess[turn] && row >= MIN){
while(chessBoard[++row][col] == chess[t ...


emm 还是有点小问题呀
斜着判断还是会出错
0   1   2   3   4   5   6   7   8   9  10  11  12  13
1   *   *   *   *   0   *   *   *   *   *   *   *   @
2   *   *   *   0   *   *   *   *   *   *   *   *   @
3   *   *   0   *   *   *   *   *   *   *   *   *   @
4   *   0   *   *   *   *   *   *   *   *   *   *   @
5   0   *   *   *   *   *   *   *   *   *   *   *   *
6   *   *   *   *   *   *   *   *   *   *   *   *   *
7   *   *   *   *   *   *   *   *   *   *   *   *   *
8   *   *   *   *   *   *   *   *   *   *   *   *   *
9   *   *   *   *   *   *   *   *   *   *   *   *   *
10   *   *   *   *   *   *   *   *   *   *   *   *   *
11   *   *   *   *   *   *   *   *   *   *   *   *   *
12   *   *   *   *   *   *   *   *   *   *   *   *   *
13   *   *   *   *   *   *   *   *   *   *   *   *   *
请黑棋落子!请输入要落子的坐标(行 列):
这里应该白棋获胜了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-31 15:57:23 | 显示全部楼层
cookies945 发表于 2019-1-31 10:34
emm 还是有点小问题呀
斜着判断还是会出错
0   1   2   3   4   5   6   7   8   9  10  11  12  13
...

是++ -- 运算符导致的
其他judge函数我没看,如果有问题,照着下面这样改
int judge_NW(int row, int col, int turn){
        int count_NW = 0;
        /*while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }*/
        while(chessBoard[row][col] == chess[turn] && (row <= MAX && col >= MIN)){
                ++row;
                --col;
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row, int col, int turn){
        int count_NE = 0;
        /*while(chessBoard[++row][++col] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }*/

        ++row;
        ++col;
        while(chessBoard[row][col] == chess[turn] && (row <= MAX && col <= MAX)){
                ++row;
                ++col;
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row, int col, int turn){
        int count_SE = 0;
        /*while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }*/

        while(chessBoard[row][col] == chess[turn] && (row >= MIN && col <= MAX)){
                --row;
                ++col;
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row, int col, int turn){
        int count_SW = 0;
        /*while(chessBoard[--row][--col] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }*/

        --row;
        --col;
        while(chessBoard[row][col] == chess[turn] && (row >= MIN && col >= MIN)){
                --row;
                --col;
                count_SW++;
        }
        return count_SW;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 13
#define MIN 1

int addBlack(int row_in_fun, int col_in_fun, int count);
int addWhite(int row_in_fun, int col_in_fun, int count);
void cleanChessBoard(char* CB, int n);
int judge(int row_in_fun, int col_in_fun, int trun);
void printChessBoard(void);
void choose(void);
int judge_N(int row, int col, int turn);
int judge_S(int row, int col, int turn);
int judge_E(int row, int col, int turn);
int judge_W(int row, int col, int turn);
int judge_NW(int row, int col, int turn);
int judge_NE(int row, int col, int turn);
int judge_SE(int row, int col, int turn);
int judge_SW(int row, int col, int turn);

char chessBoard[14][14];
char chess[2] = {'0', '@'};

int main(){
        //char tmp[14][14] = {
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, '@', '0', '@', '@', '@', '@', '0', 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        //        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        //};
        //
        //memcpy(chessBoard, tmp, sizeof(tmp));
        //printf("%d\n", judge(1, 2, 1));
        //exit(1);


        char *CB;
        CB = &chessBoard[0][0];
        cleanChessBoard(CB, 14);
        choose();
        return 0;
}
void cleanChessBoard(char* CB, int n){
        int i, j;
        for(i = 0; i < n; i++){  // n 为行 
                for(j = 0; j < n; j++){ // j 为列 

                        if(i == 0){
                                printf("%2d", j); // 第一行打印棋盘上面的数字坐标 

                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d", i);
                        }
                        else{
                                printf(" ");
                                *(CB + i * n + j) = '*';       //未落子的区域打印 * 表明未落子;
                                printf("%c", chessBoard[i][j]);
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
int addBlack(int row_in_fun, int col_in_fun, int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){   //判断该区域是否已经落了子 
                chessBoard[row_in_fun][col_in_fun] = '@';
                flag = judge(row_in_fun, col_in_fun, count);
                if(flag){
                        printf("黑棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!");
                printf("重新输入坐标:");
                int row, col;
                scanf("%d %d", &row, &col);
                addBlack(row, col, count);
        }
        return 0;
}
int addWhite(int row_in_fun, int col_in_fun, int count){
        int flag;//接收judge的返回值,判断是否结束比赛 
        if(chessBoard[row_in_fun][col_in_fun] == '*'){
                chessBoard[row_in_fun][col_in_fun] = '0';
                flag = judge(row_in_fun, col_in_fun, count);
                if(flag){
                        printf("白棋获胜!\n");
                        return 1;
                }
        }
        else{
                printf("这里有棋子了!");
                printf("重新输入坐标:");
                int row, col;
                scanf("%d %d", &row, &col);
                addWhite(row, col, count);
        }
        return 0;
}
void printChessBoard(void){
        int i, j;
        for(i = 0; i < 14; i++){  // n 为行 
                for(j = 0; j < 14; j++){ // j 为列 

                        if(i == 0){
                                printf("%2d", j); // 第一行打印棋盘上面的数字坐标 

                        }
                        else if(j == 0 && i != 0){ //每一行的开头打印列标 
                                printf("%2d", i);
                        }
                        else{
                                printf(" ");
                                printf("%c", chessBoard[i][j]);
                        } // 打印棋盘 
                        printf("  ");
                }
                printf("\n");
        }
}
void choose(void){
        int count = 0;//判断是谁下棋 
        int row, col;
        int end;//结束判断 
        do{
                if(count % 2 == 0){
                        printf("请白棋落子!");
                }
                else{
                        printf("请黑棋落子!");
                }
                printf("请输入要落子的坐标(行 列):");
                scanf("%d %d", &row, &col);
                if(count % 2 == 0){
                        end = addWhite(row, col, count);
                        count++;
                }
                else{
                        end = addBlack(row, col, count);
                        count++;
                }
                printChessBoard();
                if(end){
                        break;
                }
        }
        while(1);
}

int judge(int row, int col, int turn){
        int N = 0, S = 0, E = 0, W = 0, NW = 0, NE = 0, SW = 0, SE = 0;
        turn = turn % 2;
        N = judge_N(row, col, turn);
        S = judge_S(row, col, turn);
        W = judge_W(row, col, turn);
        E = judge_E(row, col, turn);
        SW = judge_SW(row, col, turn);
        SE = judge_SE(row, col, turn);
        NW = judge_NW(row, col, turn);
        NE = judge_NE(row, col, turn);
        if(N + S >= 5 || W + E >= 5 || SW + NW >= 5 || NE + SE >= 5){
                return 1;
        }
        return 0;
}
int judge_N(int row, int col, int turn){
        int count_N = 0;
        while(chessBoard[row--][col] == chess[turn] && row >= MIN){
                count_N++;
        }
        return count_N;
}
int judge_S(int row, int col, int turn){
        int count_S = 0;
        while(chessBoard[++row][col] == chess[turn] && row <= MAX){
                count_S++;
        }
        return count_S;
}
int judge_E(int row, int col, int turn){
        int count_E = 0;
        while(chessBoard[row][col++] == chess[turn] && col <= MAX){
                count_E++;
        }
        return count_E;
}
int judge_W(int row, int col, int turn){
        int count_W = 0;
        while(chessBoard[row][--col] == chess[turn] && col >= MIN){
                count_W++;
        }
        return count_W;
}
int judge_NW(int row, int col, int turn){
        int count_NW = 0;
        /*while(chessBoard[row++][col--] == chess[turn] && (row <= MAX && col >= MIN)){
                count_NW++;
        }*/
        while(chessBoard[row][col] == chess[turn] && (row <= MAX && col >= MIN)){
                ++row;
                --col;
                count_NW++;
        }
        return count_NW;
}
int judge_NE(int row, int col, int turn){
        int count_NE = 0;
        /*while(chessBoard[++row][++col] == chess[turn] && (row <= MAX && col <= MAX)){
                count_NE++;
        }*/

        ++row;
        ++col;
        while(chessBoard[row][col] == chess[turn] && (row <= MAX && col <= MAX)){
                ++row;
                ++col;
                count_NE++;
        }
        return count_NE;
}
int judge_SE(int row, int col, int turn){
        int count_SE = 0;
        /*while(chessBoard[row--][col++] == chess[turn] && (row >= MIN && col <= MAX)){
                count_SE++;
        }*/

        while(chessBoard[row][col] == chess[turn] && (row >= MIN && col <= MAX)){
                --row;
                ++col;
                count_SE++;
        }
        return count_SE;
}
int judge_SW(int row, int col, int turn){
        int count_SW = 0;
        /*while(chessBoard[--row][--col] == chess[turn] && (row >= MIN && col >= MIN)){
                count_SW++;
        }*/

        --row;
        --col;
        while(chessBoard[row][col] == chess[turn] && (row >= MIN && col >= MIN)){
                --row;
                --col;
                count_SW++;
        }
        return count_SW;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-31 16:00:17 | 显示全部楼层
还有,要学会调试,这样的问题只是用眼睛盯着代码看是看不出来的,当然有那么一些高手例外,既然你我都不是高手,那就乖乖调试吧

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-31 16:32:13 | 显示全部楼层
都是大佬!小弟膜拜
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-2-2 19:30:11 | 显示全部楼层
人造人 发表于 2019-1-31 16:00
还有,要学会调试,这样的问题只是用眼睛盯着代码看是看不出来的,当然有那么一些高手例外,既然你我都不是 ...

嗯受教了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 06:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表