Writing Gobang in C language

1, Build chessboard First of all, a two-dimen...
1, Build chessboard
2, Judge position out of range and wrong input
3, Judge the outcome
5, Source code

1, Build chessboard

First of all, a two-dimensional array can be used to build the chessboard. The subscript represents the position and the content represents the black, white or empty space. When the array content is 1, the position is white, when the array is 0, the position is white, and the empty output+

int w[11][11], flag = 0; int a, b; for (int k = 0; k < 11; k++) printf("The first%d column\t", k); printf("\n"); for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (w[i][j] == 0) printf("black\t"); else if (w[i][j] == 1) printf("white\t"); else printf("+\t"); } printf("The first%d That's ok\n\n", i); }

2, Judge position out of range and wrong input

When the entered chessboard coordinate is out of range or there is already a chessboard at this location, enter the chessboard coordinate again. Among them, flag is used as a mark to distinguish black hand and white hand.

if (flag == 0) { printf("Black chess\n"); scanf("%d %d", &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf("This location is out of range, please re-enter:"); scanf("%d %d", &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf("There are already pieces in this location. Please re-enter:"); scanf("%d %d", &a, &b); } flag = 1; w[a][b] = 0; } else { printf("White chess\n"); scanf("%d %d", &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf("This location is out of range, please re-enter:"); scanf("%d %d", &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf("There are already pieces in this location. Please re-enter:"); scanf("%d %d", &a, &b); }

3, Judge the outcome

In Gobang, if there are five pieces in a row or a row or an oblique row, the winner will be judged. When a piece is dropped, five pieces are diagonally arranged in front of and behind the piece, and more than five consecutive pieces of the same color will win.

int A = 1, B = 1, jieguo = 0, C = 1, D = 1; int x = 1, y = 1, m = 1, n = 1; if (w[a][b] == 1) { for (int i = 1; i < 5; i++) { if (w[a][b + i] == 1) A++; else break; } for (int i = 1; i < 5; i++) { if (w[a][b - i] == 1) A++; else break; } for (int i = 1; i < 5; i++) { if (w[a+i][b] == 1) C++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b] == 1) C++; else break; } if (A > 4 || C > 4) jieguo = 1;//White victory mark else { for (int i = 1; i < 5; i++) { if (w[a+i][b + i] == 1) x++; else break; } for (int i = 1; i < 5; i++) { if (w[a-i][b - i] == 1) x++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b-i] == 1) y++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b+i] == 1) y++; else break; } if (x > 4 || y > 4) jieguo = 1; } } if (w[a][b] == 0) { for (int i = 1; i < 5; i++) { if (w[a][b + i] == 0) B++; else break; } for (int i = 1; i < 5; i++) { if (w[a][b - i] == 0) B++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b] == 0) D++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b] == 0) D++; else break; } if (B > 4 || D > 4) jieguo = 2;//Black victory mark else { for (int i = 1; i < 5; i++) { if (w[a + i][b + i] == 0) m++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b - i] == 0) m++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b - i] == 0) n++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b + i] == 0) n++; else break; } if (m > 4 || n > 4) jieguo = 2; } }

Program completion

4, Presentation results

5, Source code

int main() { int w[11][11], flag = 0; int a, b; while (1) { if (flag == 0) { printf("Black chess\n"); scanf("%d %d", &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf("This location is out of range, please re-enter:"); scanf("%d %d", &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf("There are already pieces in this location. Please re-enter:"); scanf("%d %d", &a, &b); } flag = 1; w[a][b] = 0; } else { printf("White chess\n"); scanf("%d %d", &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf("This location is out of range, please re-enter:"); scanf("%d %d", &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf("There are already pieces in this location. Please re-enter:"); scanf("%d %d", &a, &b); } flag = 0; w[a][b] = 1; } int A = 1, B = 1, jieguo = 0, C = 1, D = 1; int x = 1, y = 1, m = 1, n = 1; if (w[a][b] == 1) { for (int i = 1; i < 5; i++) { if (w[a][b + i] == 1) A++; else break; } for (int i = 1; i < 5; i++) { if (w[a][b - i] == 1) A++; else break; } for (int i = 1; i < 5; i++) { if (w[a+i][b] == 1) C++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b] == 1) C++; else break; } if (A > 4 || C > 4) jieguo = 1;//White victory mark else { for (int i = 1; i < 5; i++) { if (w[a+i][b + i] == 1) x++; else break; } for (int i = 1; i < 5; i++) { if (w[a-i][b - i] == 1) x++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b-i] == 1) y++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b+i] == 1) y++; else break; } if (x > 4 || y > 4) jieguo = 1; } } if (w[a][b] == 0) { for (int i = 1; i < 5; i++) { if (w[a][b + i] == 0) B++; else break; } for (int i = 1; i < 5; i++) { if (w[a][b - i] == 0) B++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b] == 0) D++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b] == 0) D++; else break; } if (B > 4 || D > 4) jieguo = 2;//Black victory mark else { for (int i = 1; i < 5; i++) { if (w[a + i][b + i] == 0) m++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b - i] == 0) m++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b - i] == 0) n++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b + i] == 0) n++; else break; } if (m > 4 || n > 4) jieguo = 2; } } for (int k = 0; k < 11; k++) printf("The first%d column\t", k); printf("\n"); for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (w[i][j] == 0) printf("black\t"); else if (w[i][j] == 1) printf("white\t"); else printf("+\t"); } printf("The first%d That's ok\n\n", i); } if (jieguo == 1) { printf("Victory in white chess"); break; } if (jieguo == 2) { printf("Black victory"); break; } } return 0; }

weixin_46265516 Published 1 original article, praised 0 and visited 1 Private letter follow

4 February 2020, 12:21 | Views: 6832

Add new comment

For adding a comment, please log in
or create account

0 comments