目录
0.引言
1.完整代码/制作剖析
2.总结
大一期末基本结束,闲来无事,也便突发奇想,想着能否不用char字符类型去创造一个有符号的简单小游戏。也因为本人非计算机专业,第一学期的c语言lab作业中有一次便是让我们自己做井字棋游戏。当时用了许多函数进行拼接,也用了char字符类型,三子棋游戏也便不难做出。所以我也就为了打发时间,写了一个完全int类型完成的井字棋。
此程序三子棋的结果必然是player输 or 平局,player永远无法赢computer
*将后文出现的代码按顺序拼接即可运行!
1.这是一些头文件 #include是为了调用之后的getch()函数
#include
#include #include #include #include void Function(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9); int transform(int tran);
2.开始主函数,变量的初始化!主函数中运用了大量的数组,主要目的是用来记录和后续的判断
int main() { printf(" Welcome to play tick tack toe\n O represents the AI\n X represents the player\n"); printf("computer_keyboard\n 7 8 9\n 4 5 6\n 1 2 3\n . . .\n . . .\n . . .\n"); printf("You first ,please input\n"); const int size = 3; int cnt = 0; int count1[3][3] = {0}; int count[3][3] = {0}; int a[9] = {0}; int b[size][size]; int q = 0; int w = 0; int toe1 = 0, toe2 = 0, toe3 = 0, toe4 = 0, toe5 = 0, toe6 = 0, toe7 = 0, toe8 = 0, toe9 = 0; int numofo; int numofx; int result = -1; for (q = 0; q < 3; q++) { for (w = 0; w < 3; w++) { b[q][w] = 0; } }
3.player先行,computer后行,其中a[ ]数组用于记录每次下的位置对应的数字
你想下的每一个点对应键盘的123456789,这点与正常数组打印顺序显然是不对应的,所以要做到这一步需要后期在函数中进行数字对应的更改!
//player to 1 AI to 2 no to 0 int i, j; int t = 0; int tran; while (result == -1) { printf("You turn!\n"); int x; do { t = 0; tran=getch(); x=transform(tran); for (i = 0; i < 9; i++) { if (x == a[i]) { printf("error number! input again:\n"); t = 1; } } if (x < 0 || x > 9) { printf("error number! input again:\n"); t = 1; } } while (t == 1);
4.这段代码单纯只是为了用count[ ]和b[ ]数组来分别记录player和computer的下棋位置,a[ ]是记录整体,这样做只是为了后续的检查,和让AI可以选择性的堵塞玩家
//do the smart AI if (x == 1) { count[2][0] = 1; } else if (x == 2) { count[2][1] = 1; } else if (x == 3) { count[2][2] = 1; } else if (x == 4) { count[1][0] = 1; } else if (x == 5) { count[1][1] = 1; } else if (x == 6) { count[1][2] = 1; } else if (x == 7) { count[0][0] = 1; } else if (x == 8) { count[0][1] = 1; } else if (x == 9) { count[0][2] = 1; } a[cnt] = x; cnt++; if (x == 1) { b[2][0] = 1; } else if (x == 2) { b[2][1] = 1; } else if (x == 3) { b[2][2] = 1; } else if (x == 4) { b[1][0] = 1; } else if (x == 5) { b[1][1] = 1; } else if (x == 6) { b[1][2] = 1; } else if (x == 7) { b[0][0] = 1; } else if (x == 8) { b[0][1] = 1; } else if (x == 9) { b[0][2] = 1; }
5.打印出玩家所下的棋子的图像,呈现在电脑屏幕上!这里无脑之处在于用了一个function里面包含九个参数,其实对应的就是最多下的九个棋子(也可以说是九个数字),然后调用进函数,用数组顺序打印棋盘。这样的做法非常无脑,可想而知在五子棋中便不可能适用。这样的好处也是记录下每一次走的位置,以便在下一次新的数字输入时可以保证之前下的棋子也在新打印的棋盘中!
//check function,how to draw graph if (cnt == 1) { toe1 = a[0]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 2) { toe2 = a[1]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 3) { toe3 = a[2]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 4) { toe4 = a[3]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 5) { toe5 = a[4]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 6) { toe6 = a[5]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 7) { toe7 = a[6]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 8) { toe8 = a[7]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 9) { toe9 = a[8]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); }
6.这是一个检查模块,目的就是判断有没有出现一方获胜的情况,整个函数都包含在一个大的while循环中,一旦出现获胜便会跳出循环,否则一直进行循环,所以这个程序的效率非常的低,所以也是为什么说无脑的写法。(效率极低,游戏运行过程中等待computer下可能需要五六秒)
//Check the row for (i = 0; i < 3 && result == -1; i++) { numofo = 0; numofx = 0; for (j = 0; j < 3; j++) { if (b[i][j] == 1) { numofx++; } else if (b[i][j] == 2) { numofo++; } if (numofo == 3) { result = 1; break; } else if (numofx == 3) { result = 2; break; } } } //check the column for (j = 0; j < 3 && result == -1; j++) { numofo = 0; numofx = 0; for (i = 0; i < 3; i++) { if (b[i][j] == 1) { numofx++; } else if (b[i][j] == 2) { numofo++; } if (numofo == 3) { result = 1; break; } else if (numofx == 3) { result = 2; break; } } } //check the diagonal numofo = 0; numofx = 0; for (i = 0; i < 3 && result == -1; i++) { if (b[i][i] == 1) { numofx++; } else if (b[i][i] == 2) { numofo++; } if (numofo == 3) { result = 1; break; } else if (numofx == 3) { result = 2; break; } } numofo = 0; numofx = 0; for (i = 0; i < 3 && result == -1; i++) { if (b[i][2 - i] == 1) { numofx++; } else if (b[i][2 - i] == 2) { numofo++; } if (numofo == 3) { result = 1; break; } else if (numofx == 3) { result = 2; break; } } if (result == 1 || result == 2) { break; } if (cnt == 9) { result = 0; break; } //check over!
7.这里用到一个清屏system("cls"),目的是程序执行时有更好的体验感,之前用的getch()函数可以直接读取键盘,也是为了游戏更好的体验感! 之后就是computer下棋,采用随机数生成,然后进行筛选数组中是否存在已经有过的数字,从而达到目的。
system("cls"); printf("AI turn!\n"); int put = 0; int number; do { put = 0; srand(time(0)); number = rand() % 9 + 1; int g = number; for (i = 0; i < 9; i++) { if (number == a[i]) { put = 1; } }
8.这一大段代码就是我闲来没事穷举出所有情况,而不是让电脑随机产生没出现过的数字,不然显得电脑非常弱智!!所以在进行游戏时,即使玩家先手,最多也只是平局!
//do a smart AI if (cnt == 1 && a[0] != 5) { number = 5; break; } if (cnt == 1 && a[0] == 5) { number = 7; break; } int op0; op0 = number; if (count[0][0] == 1 && count[0][1] == 1) { number = 9; op0 = number; for (i = 0; i < 9; i++) { if (a[i] == 9) { number = g; } } } // int op1; op1 = op0; if (count[0][2] == 1 && count[0][1] == 1) { number = 7; op1 = number; for (i = 0; i < 9; i++) { if (a[i] == 7) { number = op0; op1 = op0; } } } // int op2; op2 = op1; if (count[0][0] == 1 && count[1][0] == 1) { number = 1; op2 = number; for (i = 0; i < 9; i++) { if (a[i] == 1) { number = op1; op2 = op1; } } } // int op3; op3 = op2; if (count[1][0] == 1 && count[2][0] == 1) { number = 7; op3 = number; for (i = 0; i < 9; i++) { if (a[i] == 7) { number = op2; op3 = op2; } } } // int op4; op4 = op3; if (count[2][0] == 1 && count[2][1] == 1) { number = 3; op4 = number; for (i = 0; i < 9; i++) { if (a[i] == 3) { number = op3; op4 = op3; } } } // int op5; op5 = op4; if (count[2][2] == 1 && count[2][1] == 1) { number = 1; op5 = number; for (i = 0; i < 9; i++) { if (a[i] == 1) { number = op4; op5 = op4; } } } // int op6; op6 = op5; if (count[0][2] == 1 && count[1][2] == 1) { number = 3; op6 = number; for (i = 0; i < 9; i++) { if (a[i] == 3) { number = op5; op6 = op5; } } } // int op7; op7 = op6; if (count[2][2] == 1 && count[1][2] == 1) { number = 9; op7 = number; for (i = 0; i < 9; i++) { if (a[i] == 9) { number = op6; op7 = op6; } } } // int op8; op8 = op7; if (count[2][0] == 1 && count[1][1] == 1) { number = 9; op8 = number; for (i = 0; i < 9; i++) { if (a[i] == 9) { number = op7; op8 = op7; } } } // int op9; op9 = op8; if (count[0][2] == 1 && count[1][1] == 1) { number = 1; op9 = number; for (i = 0; i < 9; i++) { if (a[i] == 1) { number = op8; op9 = op8; } } } // int op10; op10 = op9; if (count[0][0] == 1 && count[1][1] == 1) { number = 3; op10 = number; for (i = 0; i < 9; i++) { if (a[i] == 3) { number = op9; op10 = op9; } } } // int op11; op11 = op10; if (count[2][2] == 1 && count[1][1] == 1) { number = 7; op11 = number; for (i = 0; i < 9; i++) { if (a[i] == 7) { number = op10; op11 = op10; } } } // int op12; op12 = op11; if (count[1][1] == 1 && count[1][2] == 1) { number = 4; op12 = number; for (i = 0; i < 9; i++) { if (a[i] == 4) { number = op11; op12 = op11; } } } // int op13; op13 = op12; if (count[1][0] == 1 && count[1][1] == 1) { number = 6; op13 = number; for (i = 0; i < 9; i++) { if (a[i] == 6) { number = op12; op13 = op12; } } } // int op14; op14 = op13; if (count[0][1] == 1 && count[1][1] == 1) { number = 2; op14 = number; for (i = 0; i < 9; i++) { if (a[i] == 2) { number = op13; op14 = op13; } } } // int op15; op15 = op14; if (count[2][1] == 1 && count[1][1] == 1) { number = 8; op15 = number; for (i = 0; i < 9; i++) { if (a[i] == 8) { number = op14; op15 = op14; } } } // int op16; op16 = op15; if (count[2][0] == 1 && count[2][2] == 1) { number = 2; op16 = number; for (i = 0; i < 9; i++) { if (a[i] == 2) { number = op15; op16 = op15; } } } // int op17; op17 = op16; if (count[0][0] == 1 && count[2][0] == 1) { number = 4; op17 = number; for (i = 0; i < 9; i++) { if (a[i] == 4) { number = op16; op17 = op16; } } } // int op18; op18 = op17; if (count[0][0] == 1 && count[0][2] == 1) { number = 8; op18 = number; for (i = 0; i < 9; i++) { if (a[i] == 8) { number = op17; op18 = op17; } } } // int op19; op19 = op18; if (count[0][2] == 1 && count[2][2] == 1) { number = 6; op19 = number; for (i = 0; i < 9; i++) { if (a[i] == 6) { number = op18; op19 = op18; } } } for (i = 0; i < 9; i++) { if (number == a[i]) { put = 1; } } //AI try to win int k; int od0; k = op19; od0 = op19; if (count1[0][0] == 1 && count1[0][1] == 1) { number = 9; k = number; for (i = 0; i < 9; i++) { if (a[i] == 9) { number = k; od0 = k; } } } // int od1 = 0; od1 = od0; if (count1[0][2] == 1 && count1[0][1] == 1) { number = 7; od1 = number; for (i = 0; i < 9; i++) { if (a[i] == 7) { number = od0; od1 = od0; } } } // int od2 = 0; od2 = od1; if (count1[0][0] == 1 && count1[1][0] == 1) { number = 1; od2 = number; for (i = 0; i < 9; i++) { if (a[i] == 1) { number = od1; od2 = od1; } } } // int od3 = 0; od3 = od2; if (count1[1][0] == 1 && count1[2][0] == 1) { number = 7; od3 = number; for (i = 0; i < 9; i++) { if (a[i] == 7) { number = od2; od3 = od2; } } } // int od4 = 0; od4 = od3; if (count1[2][0] == 1 && count1[2][1] == 1) { number = 3; od4 = number; for (i = 0; i < 9; i++) { if (a[i] == 3) { number = od3; od4 = od3; } } } // int od5 = 0; od5 = od4; if (count1[2][2] == 1 && count1[2][1] == 1) { number = 1; od5 = number; for (i = 0; i < 9; i++) { if (a[i] == 1) { number = od4; od5 = od4; } } } // int od6 = 0; od6 = od5; if (count1[0][2] == 1 && count1[1][2] == 1) { number = 3; od6 = number; for (i = 0; i < 9; i++) { if (a[i] == 3) { number = od5; od6 = od5; } } } // int od7 = 0; od7 = od6; if (count1[2][2] == 1 && count1[1][2] == 1) { number = 9; od7 = number; for (i = 0; i < 9; i++) { if (a[i] == 9) { number = od6; od7 = od6; } } } // int od8 = 0; od8 = od7; if (count1[2][0] == 1 && count1[1][1] == 1) { number = 9; od8 = number; for (i = 0; i < 9; i++) { if (a[i] == 9) { number = od7; od8 = od7; } } } // int od9 = 0; od9 = od8; if (count1[0][2] == 1 && count1[1][1] == 1) { number = 1; od9 = number; for (i = 0; i < 9; i++) { if (a[i] == 1) { number = od8; od9 = od8; } } } // int od10 = 0; od10 = od9; if (count1[0][0] == 1 && count1[1][1] == 1) { number = 3; od10 = number; for (i = 0; i < 9; i++) { if (a[i] == 3) { number = od9; od10 = od9; } } } // int od11 = 0; od11 = od10; if (count1[2][2] == 1 && count1[1][1] == 1) { number = 7; od11 = number; for (i = 0; i < 9; i++) { if (a[i] == 7) { number = od10; od11 = od10; } } } // int od12 = 0; od12 = od11; if (count1[1][1] == 1 && count1[1][2] == 1) { number = 4; od12 = number; for (i = 0; i < 9; i++) { if (a[i] == 4) { number = od11; od12 = od11; } } } // int od13 = 0; od13 = od12; if (count1[1][0] == 1 && count1[1][1] == 1) { number = 6; od13 = number; for (i = 0; i < 9; i++) { if (a[i] == 6) { number = od12; od13 = od12; } } } // int od14 = 0; od14 = od13; if (count1[0][1] == 1 && count1[1][1] == 1) { number = 2; od14 = number; for (i = 0; i < 9; i++) { if (a[i] == 2) { number = od13; od14 = od13; } } } // int od15 = 0; od15 = od14; if (count1[2][1] == 1 && count1[1][1] == 1) { number = 8; od15 = number; for (i = 0; i < 9; i++) { if (a[i] == 8) { number = od14; od15 = od14; } } } // int od16 = 0; od16 = od15; if (count1[2][0] == 1 && count1[2][2] == 1) { number = 2; od16 = number; for (i = 0; i < 9; i++) { if (a[i] == 2) { number = od15; od16 = od15; } } } // int od17 = 0; od17 = od16; if (count1[0][0] == 1 && count1[2][0] == 1) { number = 4; od17 = number; for (i = 0; i < 9; i++) { if (a[i] == 4) { number = od16; od17 = od16; } } } // int od18 = 0; od18 = od17; if (count1[0][0] == 1 && count1[0][2] == 1) { number = 8; od18 = number; for (i = 0; i < 9; i++) { if (a[i] == 8) { number = od17; od18 = od17; } } } // int od19 = 0; od19 = od18; if (count1[0][2] == 1 && count1[2][2] == 1) { number = 6; od19 = number; for (i = 0; i < 9; i++) { if (a[i] == 6) { number = od18; od19 = od18; } } } for (i = 0; i < 9; i++) { if (number == a[i]) { put = 1; } } } while (put == 1); if (number == 1) { b[2][0] = 2; } else if (number == 2) { b[2][1] = 2; } else if (number == 3) { b[2][2] = 2; } else if (number == 4) { b[1][0] = 2; } else if (number == 5) { b[1][1] = 2; } else if (number == 6) { b[1][2] = 2; } else if (number == 7) { b[0][0] = 2; } else if (number == 8) { b[0][1] = 2; } else if (number == 9) { b[0][2] = 2; } // if (number == 1) { count1[2][0] = 1; } else if (number == 2) { count1[2][1] = 1; } else if (number == 3) { count1[2][2] = 1; } else if (number == 4) { count1[1][0] = 1; } else if (number == 5) { count1[1][1] = 1; } else if (number == 6) { count1[1][2] = 1; } else if (number == 7) { count1[0][0] = 1; } else if (number == 8) { count1[0][1] = 1; } else if (number == 9) { count1[0][2] = 1; } a[cnt] = number; cnt++; if (cnt == 9) { result = 0; break; }
8.跟之前一样,电脑下棋后的呈现模块!
if (cnt == 1) { toe1 = a[0]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 2) { toe2 = a[1]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 3) { toe3 = a[2]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 4) { toe4 = a[3]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 5) { toe5 = a[4]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 6) { toe6 = a[5]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 7) { toe7 = a[6]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 8) { toe8 = a[7]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); } else if (cnt == 9) { toe9 = a[8]; Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9); }
9.电脑的判断模块
//judge the keyboard //Check the row for (i = 0; i < 3 && result == -1; i++) { numofo = 0; numofx = 0; for (j = 0; j < 3; j++) { if (b[i][j] == 1) { numofx++; } else if (b[i][j] == 2) { numofo++; } if (numofo == 3) { result = 1; break; } else if (numofx == 3) { result = 2; break; } } } //check the column for (j = 0; j < 3 && result == -1; j++) { numofo = 0; numofx = 0; for (i = 0; i < 3; i++) { if (b[i][j] == 1) { numofx++; } else if (b[i][j] == 2) { numofo++; } if (numofo == 3) { result = 1; break; } else if (numofx == 3) { result = 2; break; } } } //check the diagonal numofo = 0; numofx = 0; for (i = 0; i < 3 && result == -1; i++) { if (b[i][i] == 1) { numofx++; } else if (b[i][i] == 2) { numofo++; } if (numofo == 3) { result = 1; break; } else if (numofx == 3) { result = 2; break; } } numofo = 0; numofx = 0; for (i = 0; i < 3 && result == -1; i++) { if (b[i][2 - i] == 1) { numofx++; } else if (b[i][2 - i] == 2) { numofo++; } if (numofo == 3) { result = 1; break; } else if (numofx == 3) { result = 2; break; } } if (result == 1 || result == 2) { break; } //check over }
10 .大循环之后最终情况的判定
if (result == 1) { printf("AI win the game!\n"); } else if (result == 2) { printf("You win the game!\n"); } else if (result == 0) { printf("No one win the game!!"); } system("pause"); printf("time used=%f",(double)clock()/CLOCKS_PER_SEC); return 0; }
11.画图函数Function的主要内容:前面那么多if 是为了对应电脑上的数字键盘和棋盘位置的转换
最后面则是打印棋盘的源头
void Function(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9) { int t1 = -1, t2 = -1, t3 = -1, t4 = -1, t5 = -1, t6 = -1, t7 = -1, t8 = -1, t9 = -1; if (x1 == 7) { t1 = 0; } else if (x1 == 8) { t1 = 1; } else if (x1 == 9) { t1 = 2; } else if (x1 == 4) { t1 = 3; } else if (x1 == 5) { t1 = 4; } else if (x1 == 6) { t1 = 5; } else if (x1 == 1) { t1 = 6; } else if (x1 == 2) { t1 = 7; } else if (x1 == 3) { t1 = 8; } if (x2 == 7) { t2 = 0; } else if (x2 == 8) { t2 = 1; } else if (x2 == 9) { t2 = 2; } else if (x2 == 4) { t2 = 3; } else if (x2 == 5) { t2 = 4; } else if (x2 == 6) { t2 = 5; } else if (x2 == 1) { t2 = 6; } else if (x2 == 2) { t2 = 7; } else if (x2 == 3) { t2 = 8; } if (x3 == 7) { t3 = 0; } else if (x3 == 8) { t3 = 1; } else if (x3 == 9) { t3 = 2; } else if (x3 == 4) { t3 = 3; } else if (x3 == 5) { t3 = 4; } else if (x3 == 6) { t3 = 5; } else if (x3 == 1) { t3 = 6; } else if (x3 == 2) { t3 = 7; } else if (x3 == 3) { t3 = 8; } if (x4 == 7) { t4 = 0; } else if (x4 == 8) { t4 = 1; } else if (x4 == 9) { t4 = 2; } else if (x4 == 4) { t4 = 3; } else if (x4 == 5) { t4 = 4; } else if (x4 == 6) { t4 = 5; } else if (x4 == 1) { t4 = 6; } else if (x4 == 2) { t4 = 7; } else if (x4 == 3) { t4 = 8; } if (x5 == 7) { t5 = 0; } else if (x5 == 8) { t5 = 1; } else if (x5 == 9) { t5 = 2; } else if (x5 == 4) { t5 = 3; } else if (x5 == 5) { t5 = 4; } else if (x5 == 6) { t5 = 5; } else if (x5 == 1) { t5 = 6; } else if (x5 == 2) { t5 = 7; } else if (x5 == 3) { t5 = 8; } if (x6 == 7) { t6 = 0; } else if (x6 == 8) { t6 = 1; } else if (x6 == 9) { t6 = 2; } else if (x6 == 4) { t6 = 3; } else if (x6 == 5) { t6 = 4; } else if (x6 == 6) { t6 = 5; } else if (x6 == 1) { t6 = 6; } else if (x6 == 2) { t6 = 7; } else if (x6 == 3) { t6 = 8; } if (x7 == 7) { t7 = 0; } else if (x7 == 8) { t7 = 1; } else if (x7 == 9) { t7 = 2; } else if (x7 == 4) { t7 = 3; } else if (x7 == 5) { t7 = 4; } else if (x7 == 6) { t7 = 5; } else if (x7 == 1) { t7 = 6; } else if (x7 == 2) { t7 = 7; } else if (x7 == 3) { t7 = 8; } if (x8 == 7) { t8 = 0; } else if (x8 == 8) { t8 = 1; } else if (x8 == 9) { t8 = 2; } else if (x8 == 4) { t8 = 3; } else if (x8 == 5) { t8 = 4; } else if (x8 == 6) { t8 = 5; } else if (x8 == 1) { t8 = 6; } else if (x8 == 2) { t8 = 7; } else if (x8 == 3) { t8 = 8; } if (x9 == 7) { t9 = 0; } else if (x9 == 8) { t9 = 1; } else if (x9 == 9) { t9 = 2; } else if (x9 == 4) { t9 = 3; } else if (x9 == 5) { t9 = 4; } else if (x9 == 6) { t9 = 5; } else if (x9 == 1) { t9 = 6; } else if (x9 == 2) { t9 = 7; } else if (x9 == 3) { t9 = 8; } int bit[9] = {0}; if (t1 != -1) { bit[t1] = 1; } if (t2 != -1) { bit[t2] = 2; } if (t3 != -1) { bit[t3] = 1; } if (t4 != -1) { bit[t4] = 2; } if (t5 != -1) { bit[t5] = 1; } if (t6 != -1) { bit[t6] = 2; } if (t7 != -1) { bit[t7] = 1; } if (t8 != -1) { bit[t8] = 2; } if (bit[t9] != 3) { bit[t9] = 1; } int i; int cnt = 0; for (i = 0; i < 9; i++) { if (bit[i] == 0) { printf(". "); cnt++; } if (bit[i] == 1) { printf("X "); cnt++; } if (bit[i] == 2) { printf("O "); cnt++; } if (cnt % 3 == 0) { printf("\n"); } } }
12.这是对应getch()的一个函数判断!因为getch返回的是这是字符对应的ACILL值
int transform(int tran) { int tot; if(tran==49)tot=1; if(tran==50)tot=2; if(tran==51)tot=3; if(tran==52)tot=4; if(tran==53)tot=5; if(tran==54)tot=6; if(tran==55)tot=7; if(tran==56)tot=8; if(tran==57)tot=9; return tot; }
13.将前面12段代码全部拼接即可运行!
总结:整个程序写下来,确实无脑,啰嗦,繁琐,重复。我也确实用这一千多行的代码打发时间,其实里面有很多模块是可以以函数的形式简化省略的,因为出现了大量的相同模块的重复使用。但是作为一个初学c语言的大一学生而言,让我感受到了这个接近底层,朴实无华的计算机语言的魅力。创造一个相同的物体有很多种方法,不论方法的好坏优劣,当你在解决一个又一个bug和error后,创造出一个达到你心中预期的东西时,这种喜悦感是不言而喻的!