|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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’是表示白棋,‘@’是表示黑棋
最后有个这样的结果,大佬们看看咋办呐
我知道问题了,你在判断的时候多加了一次 #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;
}
|
|