void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: game();//游戏 break; case 0: printf("退出游戏n"); break; default: printf("选择错误n"); break; } } while (input); } int main() { test(); return 0; }menu()打印菜单
void menu() { printf("********************************n"); printf("********* 1. play *********n"); printf("********* 0. exit *********n"); printf("********************************n"); }效果图
game()函数的实现
void game() { int input; printf("请选择您的难度n"); printf("********************************n"); printf("********* 0. 弱智 *********n"); printf("********* 1. 正常 *********n"); printf("**** 其他任意数字. 神仙 ****n"); printf("********************************n"); scanf("%d", &input); char ret = 0; //存放下棋的数据 char board[ROW][COL] = { 0 }; //初始化棋盘为全空格 InitBoard(board, ROW, COL); //打印棋盘 if ((input != 0) && (input != 1)) { int x = rand() % 3;//0~2 int y = rand() % 3;//0~2 board[x][y] = '#'; } //进击的老神仙 DisplayBoard(board, ROW, COL); while (1) { //玩家下棋 player_move(board, ROW, COL); DisplayBoard(board, ROW, COL); //判断输赢 ret = is_win(board, ROW, COL); if (ret != 'C') { break; } //电脑下棋 if (input == 0) { computer_move(board, ROW, COL);//随机下棋 } else if (input ==1) { computer_move1(board, ROW, COL);//智能下棋 } else { computer_move2(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"); } //DisplayBoard(board, ROW, COL); }效果图 初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } }打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { //打印数据 int j = 0; 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 player_move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家下棋n"); while (1) { printf("请输入坐标:>"); 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"); } } }
char is_win(char board[ROW][COL], int row, int col) { int i = 0; //判断行 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && 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 (if_full(board, row, col) == 1) { return 'Q'; } //继续 return 'C'; }简单难度 (电脑随机下)
void computer_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; } } }标准难度(增加进攻和防守,但是考虑不能无限平局,开局随机而不是随机应变)
void computer_move1(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; int z = 1; printf("电脑下棋:>n"); //进攻 for (int i = 0; i < 3; i++) { //行处理 if ((board[i][0] == '#') && (board[i][1] == '#') && (board[i][2] == ' ')) { board[i][2] = '#'; z = 0; return 0; } else if ((board[i][2] == '#') && (board[i][1] == '#') && (board[i][0] == ' ')) { board[i][2] = '#'; z = 0; return 0; } else if ((board[i][2] == '#') && (board[i][0] == '#') && (board[i][1] == ' ')) { board[i][1] = '#'; z = 0; return 0; } //列处理 else if ((board[0][i] == '#') && (board[1][i] == '#') && (board[2][i] == ' ')) { board[2][i] = '#'; z = 0; return 0; } else if ((board[2][i] == '#') && (board[1][i] == '#') && (board[0][i] == ' ')) { board[0][i] = '#'; z = 0; return 0; } else if ((board[2][i] == '#') && (board[0][i] == '#') && (board[1][i] == ' ')) { board[1][i] = '#'; z = 0; return 0; } } //斜型处理 if (z == 1) { if ((board[0][0] == '#') && (board[1][1]) == '#' && (board[2][2] == ' ')) { board[2][2] = '#'; z = 0; return 0; } else if ((board[2][2] == '#') && (board[1][1]) == '#' && (board[0][0] == ' ')) { board[0][0] = '#'; z = 0; return 0; } else if ((board[0][2] == '#') && (board[1][1]) == '#' && (board[2][0] == ' ')) { board[2][0] = '#'; z = 0; return 0; } else if ((board[2][0] == '#') && (board[1][1]) == '#' && (board[0][2] == ' ')) { board[0][2] = '#'; z = 0; return 0; } else if ((((board[0][0] == '#') && (board[2][2] == '#')) || ((board[2][0] == '#') && (board[0][2] == '#'))) && (board[1][1] == ' ')) { board[1][1] = '#'; z = 0; return 0; } }开挂模式(电脑先手,且后面每回合下两子,--其实原理也和随机模式一样,莽夫模式,你大约有1/3的胜率)
void computer_move2(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; } } while (1) { x = rand() % row;//0~2 y = rand() % col;//0~2 if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
注:电脑的先手在game中已经预置了
emmm,我写的真垃圾,以后还是和大家分享知识点的内容吧
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)