利用代码实验井字棋,考验对函数传参的应用以及字符型数组的基本使用方法。因为在此之前,都是整型数组用的多,字符型数组的一些输入输出赋值之类的操作运用并不是很熟练,导致代码最开始出现了非常多bug。比如,有一部分map[][]数组被定义为char类型,有一部分被定义为int类型,还有就是赋值的时候本该用单引号,但是错误使用双引号。原来空格也是一个字符,并且char[][]一次只能对一个字符赋值等等等。
以下是代码:
#define _CRT_SECURE_NO_WARNINGS 1 #include#include #include #include //字符要用单引号,字符串才是双引号 void menu() { printf("***************n"); printf("******井字棋******n"); printf("***************n"); printf("1.play 0.exitn"); } void init_map(char map[3][3]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { map[i][j] =' '; } } } void print_map(char map[3][3]) { for (int i = 0; i < 3; i++) { if (i==0) { printf(" %c | %c | %c n",map[0][0],map[0][1],map[0][2]); printf("___|___|___n"); } else if (i == 1) { printf(" %c | %c | %c n", map[1][0], map[1][1], map[1][2]); printf("___|___|___n"); } else { printf(" %c | %c | %c n",map[2][0],map[2][1],map[2][2]); } } } int is_occupy(char map[3][3],int x,int y) { if (x >= 1 && x <= 3 && y >= 1 && y <= 3) { if (map[x - 1][y - 1] == '*' || map[x - 1][y - 1]=='#') { return 1;//落子无效 } return 0; } return 1;//落子无效 } void human_put(char map[3][3]) { int x = 0, y = 0; while (1) { printf("请玩家落子(1,1)--(3,3):n"); scanf("%d %d", &x, &y); if (is_occupy(map, x, y))//判断这个地方是否被落子了 { printf("无效落子!请重新输入n"); } else { map[x - 1][y - 1] = '*'; break; } } } void computer_put(char map[3][3]) { int x = 0, y = 0; while (1) { //printf("请你替计算机落子:n"); //scanf("%d %d", &x, &y); x =1+ rand() % 3; y =1+ rand() % 3; if (!is_occupy(map, x, y))//判断这个地方是否被落子了 { printf("等待计算机落子:n"); Sleep(3000); map[x - 1][y - 1] = '#'; break; } else { //printf("计算机落子无效,在重新输入n"); } } } char judge(char map[3][3]) { //1.先判断是否有人胜出,胜出的情况有八种 if (map[0][0] == map[0][1] && map[0][1] == map[0][2] && map[0][0] != ' ')//第一行 { return map[0][0]; } else if (map[1][0] == map[1][1] && map[1][1] == map[1][2] && map[1][0] != ' ')//第二行 { return map[1][0]; } else if (map[2][0] == map[2][1] && map[2][1] == map[2][2] && map[2][0] != ' ')//第三行 { return map[2][0]; } else if (map[0][0] == map[1][0] && map[1][0] == map[2][0] && map[0][0] != ' ')//第一列 { return map[0][0]; } else if (map[0][1] == map[1][1] && map[1][1] == map[2][1] && map[0][1] != ' ')//第二列 { return map[0][1]; } else if (map[0][2] == map[1][2] && map[1][2] == map[2][2] && map[0][2] != ' ')//第三列 { return map[0][2]; } else if (map[0][0] == map[1][1] && map[1][1] == map[2][2] && map[0][0] != ' ')//左向右斜着 { return map[0][0]; } else if (map[0][2] == map[1][1] && map[1][1] == map[2][0] && map[0][2] != ' ')//右向左斜着 { return map[0][2]; } else if (map[0][0] != ' ' && map[0][1] != ' ' && map[0][2] != ' ' && map[1][0] != ' ' && map[1][1] != ' ' && map[1][2] != ' ' && map[2][0] != ' ' && map[2][1] != ' ' && map[2][2] != ' ')//是否被填满了 { return '-'; } return 'c'; } void game(char map[3][3]) { int x = 0, y = 0; //先打印3*3地图 init_map(map); print_map(map); while (1) { human_put(map); print_map(map); //printf("判断输赢n"); if (judge(map) == '*') { printf("人赢了n"); break; } else if (judge(map) == '#') { printf("电脑赢了n"); break; } else if (judge(map) == '-') { printf("平局n"); break; } else if (judge(map) == 'c') { printf("游戏继续,还未分出胜负n"); } computer_put(map); print_map(map); //printf("判断输赢n"); if (judge(map) == '*') { printf("人赢了n"); break; } else if (judge(map) == '#') { printf("电脑赢了n"); break; } else if (judge(map) == '-') { printf("平局n"); break; } else if (judge(map) == 'c') { printf("游戏继续,还未分出胜负n"); } } } int main() { srand((unsigned int)time(NULL)); char map[3][3] = { 0 }; int input = 0; while (1) { menu(); scanf("%d", &input); if (input == 1) { //开始游戏 printf("开始游戏n"); game(map); printf("游戏结束n"); } else { //退出游戏 printf("退出游戏n"); break; } } system("pause"); return 0; }
这里给的计算机落子的算法是直接乱下,找到能下的地方就下。读者如果愿意可以改良一下算法,可以达到每盘都是平局。还可以设置难度,简单,普通和困难。我这个随机下的差不多就是弱智难度了哈哈哈。也可以再判断完输赢后清屏操作,这样能够使画面更加简洁。其中一个is_win()函数里面一堆if -else if,本人暂时想不到办法改进。如果能用switch()判断就好了;
看到这了,麻烦点个赞吧。毕竟自己辛辛苦苦敲了好久的代码



