C语言实现递归版多子棋的设计(上)https://developer.aliyun.com/article/new#8%EF%BC%89%E6%9C%80%E5%90%8E%E4%B8%B2%E8%81%94%E5%9C%A8%E4%B8%80%E8%B5%B7
8)最后串联在一起
void game() { char c='C'; int result = 1; char arr[Row][Col]; init(arr, Row, Col); menu1(arr, Row, Col); while (c=='C') { playmove(arr, Row, Col); menu1(arr, Row, Col); c = iswin(arr, Row, Col, Times); if (c != 'C')break; computermove(arr, Row, Col); menu1(arr, Row, Col,Times); c = iswin(arr, Row, Col, Times); } if (c == '*')printf("玩家胜利\n"); else if (c == '#')printf("电脑胜利\n"); else printf("平局\n"); }
二.展示源码
#include<time.h> #include<stdlib.h> #include<stdio.h> #define Row 10//改变棋盘行数 #define Col 10//改变棋盘列数 #define Times 5//改变胜利棋子数 void menu(); void game(); void init(char arr[Row][Col], int row, int col); void menu1(char arr[Row][Col], int row, int col); void playmove(char arr[Row][Col], int row, int col); void computermove(char arr[Row][Col], int row, int col); char iswin(char arr[Row][Col], int row, int col,int times); int main() { srand((unsigned int)time(NULL)); int choose; do { menu(); scanf("%d", &choose); switch (choose) { case 0: printf("正在退出\n"); break; case 1:game(); break; default: printf("没有这个输入选项,请重新输入\n"); } } while (choose); return 0; } void menu() { printf("******************\n"); printf("***** 1.开始 *****\n"); printf("***** 0.退出 *****\n"); printf("******************\n"); } void game() { char c='C'; int result = 1; char arr[Row][Col]; init(arr, Row, Col); menu1(arr, Row, Col); while (c=='C') { playmove(arr, Row, Col); menu1(arr, Row, Col); c = iswin(arr, Row, Col, Times); if (c != 'C')break; computermove(arr, Row, Col); menu1(arr, Row, Col,Times); c = iswin(arr, Row, Col, Times); } if (c == '*')printf("玩家胜利\n"); else if (c == '#')printf("电脑胜利\n"); else printf("平局\n"); } void init(char arr[Row][Col], int row, int col) { int i, j; for (i = 0; i < row; i++) for (j = 0; j < col; j++) arr[i][j] = ' '; } void menu1(char arr[Row][Col], int row, int col) { int i, j; for (j = 0; j < row; j++) { for (i = 0; i < col; i++) { printf(" %c ", arr[j][i]); if (i < row - 1) printf("|"); } printf("\n"); if(j<row-1) for (i = 0; i < col; i++) { printf("---"); if (i < col - 1) printf("|"); } printf("\n"); } } void playmove(char arr[Row][Col], int row, int col) { int x, y; while (1) { printf("玩家下棋->"); scanf("%d%d", &x, &y); printf("\n"); if (x < row + 1 && y < col + 1 && arr[x-1][y-1] == ' ') { arr[x-1][y-1] = '*'; break; } else printf("输入错误或者输入的位置已经被下过,请重新下棋\n"); } } void computermove(char arr[Row][Col], int row, int col) { while (1) { int x = rand() % row; int y = rand() % col; if (arr[x][y] == ' ') { arr[x][y] = '#'; break; } } } char iswin(char arr[Row][Col], int row, int col, int times) { int i, flag = 0, j, count = 0, k, count1; for (i = 0; i < row; i++) { for (j = 0, flag = 0; j < col; j++) { if (arr[i][j] == arr[i][count]&&arr[i][count]!=' ') flag++; else { flag = 0; count = j; } if (flag == times)return arr[i][j]; } count = 0; for (i = 0; i < col; i++) { for (j = 0, flag = 0; j < row; j++) { if (arr[j][i] == arr[count][j] && arr[j][i] != ' ') flag++; else { flag = 0; count = j; } if (flag == times)return arr[i][j]; } } } for (i=0; i < row; i++) { flag = 0; j = 0; count = 0; count1 = i; k = i; while (k < row && j < col) { if (arr[k][j] == arr[count1][count]&& arr[count1][count]!=' ') flag++; else { flag = 0; count = j; count1 = k; } if (flag == times)return arr[count1][count]; k++; j++; } } for (i = 0; i < row; i++) { flag = 0; j = 0; count = col-1; count1 = i; k = i; while (k < row && j < col) { if (arr[k][j] == arr[count1][count]&&arr[k][j]!=' ') flag++; else { flag = 0; count = j; count1 = k; } if (flag == times)return arr[count1][count]; k++; j--; } } for (i = 0, j = col - 1; j > 0; j--) { flag = 0; i = 0; count = j; count1 = 0; k = j; while (k < col && i<row) { if (arr[i][k] == arr[count1][count]&& arr[count1][count]!=' ') flag++; else { flag = 0; count = k; count1 = i; } if (flag == times)return arr[count1][count]; k--; i++; } } for (i = 0,j=0; j < col; j++) { flag = 0; i = 0; count = j; count1 = i; k = j; while (i < row && k < col) { if (arr[i][k] == arr[count1][count]&&arr[i][k]!=' ') flag++; else { flag = 0; count = k; count1 = i; } if (flag == times)return arr[count1][count]; k++; i++; } } count = 0; for (i = 0; i < row; i++) for (j = 0; j < col; j++) if (arr[i][j] != ' ')count++; else break; if (count == row * col)return 'Q'; else return 'C'; }
最后收工。
感谢大家的浏览,欢迎点赞收藏