时间:2023-02-02 10:13:31 | 栏目:C代码 | 点击:次
int main() { srand((unsigned int)time(NULL));//时间-设置随机数的生成器(后续介绍) test(); return 0; }
void menu() { printf("**************************\n"); printf("******** 1.begin *******\n"); printf("******** 0.exit *******\n"); printf("**************************\n"); } void test() { int input = 0; do { menu(); printf("请选择:"); scanf("%d", &input); switch (input) { case 1: printf("三子棋\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入错误\n"); break; } } while (input); }
运行效果
void game() { char board[ROW][COL] = { 0 };// 初始化数组,存放棋子状态(空/玩家已下棋*/电脑已下棋#) InitBoard(board, ROW, COL);// 初始化棋盘,令棋盘为空,所有位置为可下棋状态 DisplayBoard(board, ROW, COL); // 打印棋盘 char ret = 0;// 与获胜判断有关,后续介绍 while (1)// 下棋循环 { player_move(board, ROW, COL);//玩家下棋函数 DisplayBoard(board, ROW, COL);//打印棋盘 ret = is_win(board, ROW, COL);//获胜判断 if (ret != 'C') { break; } compuer_move(board, ROW, COL);//电脑下棋函数 DisplayBoard(board, ROW, COL);//同上 ret = is_win(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else { printf("平局\n"); } }
行列数的宏定义:(位于头文件中)
#define ROW 3//行 #define COL 3//列
void InitBoard(char board[ROW][COL], int row, int col)//游戏开始时初始化空棋盘 { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } } }
头文件声明:
void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col)//打印棋盘 { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j<col-1) printf("|");//打印列分隔符 } printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---");//打印行分隔符 if (j < col - 1) printf("|"); } } printf("\n"); } }
头文件声明:
void DisplayBoard(char board[ROW][COL], int row, int col);
效果:
void player_move(char board[ROW][COL], int row, int col)//玩家下棋代码 { while (1) { printf("玩家下棋:>(请输入棋盘坐标,例如:1 1)"); int x = 0; int y = 0; scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col)// 判断坐标在棋盘内 { if (board[x-1][y-1] == ' ')// 判断坐标处坐标是否为空 { board[x-1][y-1] = '*'; break; } else { printf("被占用\n"); } } else { printf("坐标非法,请重新输入\n"); } } }
void compuer_move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑下棋:>\n"); while (1) { x = rand() % ROW;//0~2,由时间戳寻找到的伪随机数 y = rand() % COL;//0~2 if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
时间戳可在主函数中加入
srand((unsigned int)time(NULL));//时间-设置随机数的生成器
二者的函数声明(头文件)
void player_move(char board[ROW][COL], int row, int col); void compuer_move(char board[ROW][COL], int row, int col);
效果
char is_win(char board[ROW][COL], int row, int col)//获胜条件判断 { int i = 0; //三行 for (i = 0; i < row - 1; i++) { if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][1] != ' ') { return board[i][1];//即那方获胜返回哪一方的棋子 } } //三列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') { return board[1][i]; } } //对角线的判断 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') { return board[1][1]; } //判断平局 if (1 == is_full(board, row, col)) { return 'Q'; } //继续 return 'C'; }
对应test.c中判断胜负的代码块
int main() { srand((unsigned int)time(NULL));//时间-设置随机数的生成器(后续介绍) test(); return 0; }
void menu() { printf("**************************\n"); printf("******** 1.begin *******\n"); printf("******** 0.exit *******\n"); printf("**************************\n"); } void test() { int input = 0; do { menu(); printf("请选择:"); scanf("%d", &input); switch (input) { case 1: printf("三子棋\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入错误\n"); break; } } while (input); }
运行效果
void game() { char board[ROW][COL] = { 0 };// 初始化数组,存放棋子状态(空/玩家已下棋*/电脑已下棋#) InitBoard(board, ROW, COL);// 初始化棋盘,令棋盘为空,所有位置为可下棋状态 DisplayBoard(board, ROW, COL); // 打印棋盘 char ret = 0;// 与获胜判断有关,后续介绍 while (1)// 下棋循环 { player_move(board, ROW, COL);//玩家下棋函数 DisplayBoard(board, ROW, COL);//打印棋盘 ret = is_win(board, ROW, COL);//获胜判断 if (ret != 'C') { break; } compuer_move(board, ROW, COL);//电脑下棋函数 DisplayBoard(board, ROW, COL);//同上 ret = is_win(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else { printf("平局\n"); } }
行列数的宏定义:(位于头文件中)
#define ROW 3//行 #define COL 3//列
void InitBoard(char board[ROW][COL], int row, int col)//游戏开始时初始化空棋盘 { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } } }
头文件声明:
void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col)//打印棋盘 { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j<col-1) printf("|");//打印列分隔符 } printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---");//打印行分隔符 if (j < col - 1) printf("|"); } } printf("\n"); } }
头文件声明:
void DisplayBoard(char board[ROW][COL], int row, int col);
效果:
void player_move(char board[ROW][COL], int row, int col)//玩家下棋代码 { while (1) { printf("玩家下棋:>(请输入棋盘坐标,例如:1 1)"); int x = 0; int y = 0; scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col)// 判断坐标在棋盘内 { if (board[x-1][y-1] == ' ')// 判断坐标处坐标是否为空 { board[x-1][y-1] = '*'; break; } else { printf("被占用\n"); } } else { printf("坐标非法,请重新输入\n"); } } }
void compuer_move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑下棋:>\n"); while (1) { x = rand() % ROW;//0~2,由时间戳寻找到的伪随机数 y = rand() % COL;//0~2 if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
时间戳可在主函数中加入
srand((unsigned int)time(NULL));//时间-设置随机数的生成器
二者的函数声明(头文件)
void player_move(char board[ROW][COL], int row, int col); void compuer_move(char board[ROW][COL], int row, int col);
效果
char is_win(char board[ROW][COL], int row, int col)//获胜条件判断 { int i = 0; //三行 for (i = 0; i < row - 1; i++) { if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][1] != ' ') { return board[i][1];//即那方获胜返回哪一方的棋子 } } //三列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') { return board[1][i]; } } //对角线的判断 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') { return board[1][1]; } //判断平局 if (1 == is_full(board, row, col)) { return 'Q'; } //继续 return 'C'; }
对应test.c中判断胜负的代码块