分享一个五子棋的程序,以及一个bug大佬看看怎么回事
本帖最后由 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;
char chess = {'0','@'};
int main(){
char *CB;
CB = &chessBoard;
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);
} // 打印棋盘
printf("");
}
printf("\n");
}
}
int addBlack(int row_in_fun,int col_in_fun,int count){
int flag;//接收judge的返回值,判断是否结束比赛
if(chessBoard == '*'){ //判断该区域是否已经落了子
chessBoard = '@';
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 == '*'){
chessBoard = '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);
} // 打印棋盘
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 == chess && row >= MIN){
count_N++;
}
return count_N;
}
int judge_S(int row,int col,int turn){
int count_S = 0;
while(chessBoard == chess && row <= MAX){
count_S++;
}
return count_S;
}
int judge_E(int row,int col,int turn){
int count_E = 0;
while(chessBoard == chess && col <= MAX){
count_E++;
}
return count_E;
}
int judge_W(int row,int col,int turn){
int count_W = 0;
while(chessBoard == chess && col >= MIN){
count_W++;
}
return count_W;
}
int judge_NW(int row,int col,int turn){
int count_NW = 0;
while(chessBoard == chess && (row <= MAX && col >= MIN)){
count_NW++;
}
return count_NW;
}
int judge_NE(int row,int col,int turn){
int count_NE = 0;
while(chessBoard == chess && (row <= MAX && col <= MAX)){
count_NE++;
}
return count_NE;
}
int judge_SE(int row,int col,int turn){
int count_SE = 0;
while(chessBoard == chess && (row >= MIN && col <= MAX)){
count_SE++;
}
return count_SE;
}
int judge_SW(int row,int col,int turn){
int count_SW = 0;
while(chessBoard == chess && (row >= MIN && col >= MIN)){
count_SW++;
}
return count_SW;
}
请黑棋落子!请输入要落子的坐标(行 列):1 5
黑棋获胜!
0 1 2 3 4 5 6 7 8 910111213
1 0 @ @ @ @ * * * * * * * *
2 * 0 * * * * * * * * * * *
3 * * 0 * * * * * * * * * *
4 * * * 0 * * * * * * * * *
5 * * * * * * * * * * * * *
6 * * * * * * * * * * * * *
7 * * * * * * * * * * * * *
8 * * * * * * * * * * * * *
9 * * * * * * * * * * * * *
10 * * * * * * * * * * * * *
11 * * * * * * * * * * * * *
12 * * * * * * * * * * * * *
13 * * * * * * * * * * * * *
对了‘0’是表示白棋,‘@’是表示黑棋
最后有个这样的结果,大佬们看看咋办呐 {:10_266:} 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;
char chess = {'0', '@'};
int main(){
char *CB;
CB = &chessBoard;
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);
} // 打印棋盘
printf("");
}
printf("\n");
}
}
int addBlack(int row_in_fun, int col_in_fun, int count){
int flag;//接收judge的返回值,判断是否结束比赛
if(chessBoard == '*'){ //判断该区域是否已经落了子
chessBoard = '@';
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 == '*'){
chessBoard = '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);
} // 打印棋盘
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 == chess
&& chessBoard == chess
&& chessBoard == chess
&& chessBoard == chess
&& chessBoard == chess)
{
return 1;
}
}
}
for(int x = MIN; x <= MAX; ++x) // 每一列
{
for(int y = MIN; y <= MAX - 4; ++y)
{
if(chessBoard == chess
&& chessBoard == chess
&& chessBoard == chess
&& chessBoard == chess
&& chessBoard == chess)
{
return 1;
}
}
}
int tmp;
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 = chessBoard;
}
for(int y = MIN; y <= MAX; ++y) // 验证
{
for(int x = MIN; x <= MAX - 4; ++x)
{
if(tmp == chess
&& tmp == chess
&& tmp == chess
&& tmp == chess
&& tmp == chess)
{
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 = chessBoard;
}
for(int y = MIN; y <= MAX; ++y) // 验证
{
for(int x = MIN; x <= MAX - 4; ++x)
{
if(tmp == chess
&& tmp == chess
&& tmp == chess
&& tmp == chess
&& tmp == chess)
{
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 = chessBoard;
}
for(int y = MIN; y <= MAX; ++y) // 验证
{
for(int x = MIN; x <= MAX - 4; ++x)
{
if(tmp == chess
&& tmp == chess
&& tmp == chess
&& tmp == chess
&& tmp == chess)
{
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 = chessBoard;
}
for(int y = MIN; y <= MAX; ++y) // 验证
{
for(int x = MIN; x <= MAX - 4; ++x)
{
if(tmp == chess
&& tmp == chess
&& tmp == chess
&& tmp == chess
&& tmp == chess)
{
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 == chess && row >= MIN){
// count_N++;
// }
// return count_N;
//}
//int judge_S(int row, int col, int turn){
// int count_S = 0;
// while(chessBoard == chess && row <= MAX){
// count_S++;
// }
// return count_S;
//}
//int judge_E(int row, int col, int turn){
// int count_E = 0;
// while(chessBoard == chess && col <= MAX){
// count_E++;
// }
// return count_E;
//}
//int judge_W(int row, int col, int turn){
// int count_W = 0;
// while(chessBoard == chess && col >= MIN){
// count_W++;
// }
// return count_W;
//}
//int judge_NW(int row, int col, int turn){
// int count_NW = 0;
// while(chessBoard == chess && (row <= MAX && col >= MIN)){
// count_NW++;
// }
// return count_NW;
//}
//int judge_NE(int row, int col, int turn){
// int count_NE = 0;
// while(chessBoard == chess && (row <= MAX && col <= MAX)){
// count_NE++;
// }
// return count_NE;
//}
//int judge_SE(int row, int col, int turn){
// int count_SE = 0;
// while(chessBoard == chess && (row >= MIN && col <= MAX)){
// count_SE++;
// }
// return count_SE;
//}
//int judge_SW(int row, int col, int turn){
// int count_SW = 0;
// while(chessBoard == chess && (row >= MIN && col >= MIN)){
// count_SW++;
// }
// return count_SW;
//}
emm,大佬能否说说我的逻辑错误是怎样的?还有就是大佬这个是搜索整个棋盘的,这样是不是有点费时呢?
cookies945 发表于 2019-1-30 09:51
emm,大佬能否说说我的逻辑错误是怎样的?还有就是大佬这个是搜索整个棋盘的,这样是不是有点费时呢?
五子棋的规则是什么?
在每条横线,每条竖线,还有斜线
只要有连续的5个棋子,就结束游戏,达成这一目的方获胜
你的代码逻辑是
在每条横线,每条竖线,还有斜线上只要有5个相同的棋子就行了 等一等,我再看一看你的代码 我知道问题了,你在判断的时候多加了一次
#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;
char chess = {'0', '@'};
int main(){
//char tmp = {
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, '@', '0', '@', '@', '@', '@', '0', 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 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;
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);
} // 打印棋盘
printf("");
}
printf("\n");
}
}
int addBlack(int row_in_fun, int col_in_fun, int count){
int flag;//接收judge的返回值,判断是否结束比赛
if(chessBoard == '*'){ //判断该区域是否已经落了子
chessBoard = '@';
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 == '*'){
chessBoard = '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);
} // 打印棋盘
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 == chess && row >= MIN){
count_N++;
}
return count_N;
}
int judge_S(int row, int col, int turn){
int count_S = 0;
while(chessBoard[++row] == chess && row <= MAX){
count_S++;
}
return count_S;
}
int judge_E(int row, int col, int turn){
int count_E = 0;
while(chessBoard == chess && col <= MAX){
count_E++;
}
return count_E;
}
int judge_W(int row, int col, int turn){
int count_W = 0;
while(chessBoard[--col] == chess && col >= MIN){
count_W++;
}
return count_W;
}
int judge_NW(int row, int col, int turn){
int count_NW = 0;
while(chessBoard == chess && (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 && (row <= MAX && col <= MAX)){
count_NE++;
}
return count_NE;
}
int judge_SE(int row, int col, int turn){
int count_SE = 0;
while(chessBoard == chess && (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 && (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 == chess && row >= MIN){
count_N++;
}
return count_N;
}
int judge_S(int row, int col, int turn){
int count_S = 0;
while(chessBoard[++row] == chess && row <= MAX){
count_S++;
}
return count_S;
}
int judge_E(int row, int col, int turn){
int count_E = 0;
while(chessBoard == chess && col <= MAX){
count_E++;
}
return count_E;
}
int judge_W(int row, int col, int turn){
int count_W = 0;
while(chessBoard[--col] == chess && col >= MIN){
count_W++;
}
return count_W;
}
int judge_NW(int row, int col, int turn){
int count_NW = 0;
while(chessBoard == chess && (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 && (row <= MAX && col <= MAX)){
count_NE++;
}
return count_NE;
}
int judge_SE(int row, int col, int turn){
int count_SE = 0;
while(chessBoard == chess && (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 && (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 == chess && row >= MIN){
count_N++;
}
return count_N;
}
int judge_S(int row,int col,int turn){
int count_S = 0;
while(chessBoard == chess && row <= MAX){
count_S++;
}
return count_S;
}
int judge_E(int row,int col,int turn){
int count_E = 0;
while(chessBoard == chess && col <= MAX){
count_E++;
}
return count_E;
}
int judge_W(int row,int col,int turn){
int count_W = 0;
while(chessBoard == chess && col >= MIN){
count_W++;
}
return count_W;
}
int judge_NW(int row,int col,int turn){
int count_NW = 0;
while(chessBoard == chess && (row <= MAX && col >= MIN)){
count_NW++;
}
return count_NW;
}
int judge_NE(int row,int col,int turn){
int count_NE = 0;
while(chessBoard == chess && (row <= MAX && col <= MAX)){
count_NE++;
}
return count_NE;
}
int judge_SE(int row,int col,int turn){
int count_SE = 0;
while(chessBoard == chess && (row >= MIN && col <= MAX)){
count_SE++;
}
return count_SE;
}
int judge_SW(int row,int col,int turn){
int count_SW = 0;
while(chessBoard == chess && (row >= MIN && col >= MIN)){
count_SW++;
}
return count_SW;
} 人造人 发表于 2019-1-30 13:15
我知道问题了,你在判断的时候多加了一次
能在注释里告诉我哪里多判断了一次么,我代码太乱了{:10_266:}
我也不知道是哪里的判断多了一次
{:10_266:} cookies945 发表于 2019-1-30 14:51
能在注释里告诉我哪里多判断了一次么,我代码太乱了
我也不知道是哪里的判断多了一次
{:10_2 ...
int judge_N(int row, int col, int turn){
int count_N = 0;
while(chessBoard == chess && row >= MIN){
count_N++;
}
return count_N;
}
int judge_S(int row, int col, int turn){
int count_S = 0;
while(chessBoard[++row] == chess && 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 == chess && row >= MIN){
count_N++;
}
return count_N;
}
int judge_S(int row, int col, int turn){
int count_S = 0;
while(chessBoard[++row] == chess && row <= MAX){
count_S++;
}
return count_S;
}
int judge_E(int row, int col, int turn){
int count_E = 0;
while(chessBoard == chess && col <= MAX){
count_E++;
}
return count_E;
}
int judge_W(int row, int col, int turn){
int count_W = 0;
while(chessBoard[--col] == chess && col >= MIN){
count_W++;
}
return count_W;
}
int judge_NW(int row, int col, int turn){
int count_NW = 0;
while(chessBoard == chess && (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 && (row <= MAX && col <= MAX)){
count_NE++;
}
return count_NE;
}
int judge_SE(int row, int col, int turn){
int count_SE = 0;
while(chessBoard == chess && (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 && (row >= MIN && col >= MIN)){
count_SW++;
}
return count_SW;
}
while(chessBoard[row--] == chess && row >= MIN){
while(chessBoard[++row] == chess && row <= MAX){ 人造人 发表于 2019-1-30 15:18
while(chessBoard == chess && row >= MIN){
while(chessBoard[++row] == chess
算法了解的很到位,代码看得很仔细! 本帖最后由 cookies945 于 2019-1-31 11:01 编辑
人造人 发表于 2019-1-30 15:18
while(chessBoard == chess && row >= MIN){
while(chessBoard[++row] == chess
emm 还是有点小问题呀
斜着判断还是会出错
0 1 2 3 4 5 6 7 8 910111213
1 * * * * 0 * * * * * * * @
2 * * * 0 * * * * * * * * @
3 * * 0 * * * * * * * * * @
4 * 0 * * * * * * * * * * @
5 0 * * * * * * * * * * * *
6 * * * * * * * * * * * * *
7 * * * * * * * * * * * * *
8 * * * * * * * * * * * * *
9 * * * * * * * * * * * * *
10 * * * * * * * * * * * * *
11 * * * * * * * * * * * * *
12 * * * * * * * * * * * * *
13 * * * * * * * * * * * * *
请黑棋落子!请输入要落子的坐标(行 列):
这里应该白棋获胜了
cookies945 发表于 2019-1-31 10:34
emm 还是有点小问题呀
斜着判断还是会出错
0 1 2 3 4 5 6 7 8 910111213
...
是++ -- 运算符导致的
其他judge函数我没看,如果有问题,照着下面这样改
int judge_NW(int row, int col, int turn){
int count_NW = 0;
/*while(chessBoard == chess && (row <= MAX && col >= MIN)){
count_NW++;
}*/
while(chessBoard == chess && (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 && (row <= MAX && col <= MAX)){
count_NE++;
}*/
++row;
++col;
while(chessBoard == chess && (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 == chess && (row >= MIN && col <= MAX)){
count_SE++;
}*/
while(chessBoard == chess && (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 && (row >= MIN && col >= MIN)){
count_SW++;
}*/
--row;
--col;
while(chessBoard == chess && (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;
char chess = {'0', '@'};
int main(){
//char tmp = {
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, '@', '0', '@', '@', '@', '@', '0', 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// {0, 0, 0, 0, 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;
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);
} // 打印棋盘
printf("");
}
printf("\n");
}
}
int addBlack(int row_in_fun, int col_in_fun, int count){
int flag;//接收judge的返回值,判断是否结束比赛
if(chessBoard == '*'){ //判断该区域是否已经落了子
chessBoard = '@';
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 == '*'){
chessBoard = '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);
} // 打印棋盘
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 == chess && row >= MIN){
count_N++;
}
return count_N;
}
int judge_S(int row, int col, int turn){
int count_S = 0;
while(chessBoard[++row] == chess && row <= MAX){
count_S++;
}
return count_S;
}
int judge_E(int row, int col, int turn){
int count_E = 0;
while(chessBoard == chess && col <= MAX){
count_E++;
}
return count_E;
}
int judge_W(int row, int col, int turn){
int count_W = 0;
while(chessBoard[--col] == chess && col >= MIN){
count_W++;
}
return count_W;
}
int judge_NW(int row, int col, int turn){
int count_NW = 0;
/*while(chessBoard == chess && (row <= MAX && col >= MIN)){
count_NW++;
}*/
while(chessBoard == chess && (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 && (row <= MAX && col <= MAX)){
count_NE++;
}*/
++row;
++col;
while(chessBoard == chess && (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 == chess && (row >= MIN && col <= MAX)){
count_SE++;
}*/
while(chessBoard == chess && (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 && (row >= MIN && col >= MIN)){
count_SW++;
}*/
--row;
--col;
while(chessBoard == chess && (row >= MIN && col >= MIN)){
--row;
--col;
count_SW++;
}
return count_SW;
}
还有,要学会调试,这样的问题只是用眼睛盯着代码看是看不出来的,当然有那么一些高手例外,既然你我都不是高手,那就乖乖调试吧
都是大佬!小弟膜拜{:10_257:} 人造人 发表于 2019-1-31 16:00
还有,要学会调试,这样的问题只是用眼睛盯着代码看是看不出来的,当然有那么一些高手例外,既然你我都不是 ...
嗯受教了
页:
[1]