1 #define _CRT_SECURE_NO_WARNINGS
2 #include25 { 26 printf("| %c | %c | %c | ",broad[row][0],broad[row][1],broad[row][2]); 27 if (row != ROW-1) span> 28 { 29 printf(" ---|---|--- "); 30 } 31 } 32 } 33
3 #include
4 #include
5
6 #define ROW 3
7 #define COL 3
8
9 char g_broad[ROW][COL]; 10
11 void Init(char broad [ROW][COL]) 12 { 13 for (int row = 0;row < ROW;++row) 14 {< /span> 15 for (int span> col = 0;colcol) 16 { 17 broad[row][col] = ' '; 18 } 19 } 20 } 21
22 void Print(char broad[ROW][COL]) 23 { 24 for(< span style="color: #0000ff">int row=0;rowrow)
34 void Playermove(char broad[RO W][COL]) 35 { 36 printf("The player plays chess! "); 37 while(1) 38 { 39 int row = 0; 40 int col = 0; 41 printf(" Please enter a set of coordinates: "); 42 scanf("%d %d", &row, &col); 43 span> if (row <0 || row >= ROW || col <0 || col >= COL) 44 { 45 printf("The coordinates you entered are illegal! Please re-enter: "); 46 continue; 47 } 48 if (broad[row][col] != ' ' ) 49 { span> 50 printf("This position has been There are chess pieces, please re-enter: "); 51 continue; 52 } 53 broad[row][col] = 'x'; 54 break; 55 } 56 } 57
58 void Computermove(char broad[ROW][COL]) 59 { 60 printf("Play chess on the computer! "); 61 while (1) 62 { 63 span> int row = rand() % ROW; 64 int col = rand() % COL; 65 if (broad[row][col] != ' ') 66 span> { 67 //The location is occupied, regenerate
68 continue; 69 } 70 broad[row][col] = 'o'; 71 break; 72 } 73
74 } 75
76 int Isfull(char broad[ROW][COL]) 77 { 78 for (int row = 0;rowrow)
79 { 80 for (int col = 0;colcol) 81 { 82 if (broad[row][col] == ' ') 83 span> { 84 return 0; 85 } 86 } 87 } 88 return 1; 89 span> } 90
91 char Checkgame(char broad [ROW][COL]) 92 { 93 //Check all lines first
94 for (int row = 0;rowrow)
95 { 96 if (broad[row][0] == broad[row][1< /span>] 97 && broad[row][0] == broad[row][2] 98 && broad[row][0] != ' ') 99 {100 return broad[row][0]; 101 } 102 } 103 //Check all columns
104 for (int col = < span style="color: #800080">0;colcol) 105 {106 if< /span> (br oad[0][col] == broad[1][col] 107 && broad[0][col] == broad[2][col] 108 && broad != ' ') 109 { 110 return broad[0 ][col]; 111 } 112 } 113 //Check the diagonal line
114 if (broad[0][0] = = broad[1][1] < /span>115 && broad[0][0] == broad[2][2] 116 && broad[0][0] != ' '< span style="color: #000000">) 117 {118 return broad[0][0]; 119 } 120 if (broad[2][< span style="color: #800080">0] == broad[1][1 ] 121 && broad[ 2][0] == broad[0][2] 122 && broad[2][0] != ' ') 123 {124 return broad[2][0]; 125 } 126 //Determine whether or not to draw
127 if< span style="color: #000000"> (Isfull(broad)) 128 { span>129 return ' q'; 130 } 131 return ' ' ; 132 } 133< /span>
134 void Game() 135 { 136 //1. Initialize the board first
137 Init(g_broad); 138 char winner = ' ' ; 139 while (1) 140 {141 // 2. Print the chessboard (this time is an empty chessboard)
142 Print( g_broad); 143 //3. Players play chess and check if the game is over
144 Playermove(g_broad) ; 145 //winner ==x player wins; o computer wins; q draws; ''the victory is undivided
146 winner = Checkgame(g_br oad); 147 if (winner != ' ') 148 {149 break; 150 } < /span>151 //4. Computer Play chess and check if the game is over
152 Computermove(g_broad); 153 winner = Checkgame(g_broad); 154 if (winner != ' ') 155 {156 break; 157 } 158 //Loop to step 2.
159
160 } 161 //Print the map before the game ends
162 Print(g_broad); 163 if (winner == 'x') 164 {165 span> printf("The player wins! "); 166 } 167 else if (winner == 'o') 168 {169 printf("The computer wins! "); 170 } 171 else< /span> if (winner == ' q') 172< /span> {173 printf("< /span>Draw! " ); 174 } 175 else
176 {177 printf("Program error! "); 178 } 179
180 } 181
182 int Menu() 183 { 184 printf("============= "); 185 printf("1. Start the game "); 186 printf("0. End the game "); 187 printf("============== "< /span>); 188 printf(" Please enter your choice: "); 189 int choice = 0; 190 scanf("%d",&choice); 1 91 return choice; 192 } 193
194 int main() 195 { 196 / /Set a random seed here
197 srand((unsigned int)time(0)); 198 while (1 ) 199 {200 int choice = Menu(); 201 if (choice == 1) 202 {203 Game(); 204 } 205 else
< span style="color: #008080">206 {207 printf(< span style="color: #800000">"Goodbye! "); 208 break; 209< /span> } 210 } 211 return 0< span style="color: #000000">; 212 }
1 #define _CRT_SECURE_NO_WARNINGS
2 #includeCOL 3
3 #include< stdlib.h>
4 #include
5
6 #define ROW 3
7 < span style="color: #0000ff">#define
8
9 char g_broad[ROW][COL]; 10
11 void Init (char broad[ROW][COL]) 12 { 13 for (int row = 0;rowrow) 14 { 15 for (int col = 0;col
col) 16 { 17 broad[row][col] = ' '; 18 } 19 } 20 } 21
22 void Print(char broad[ROW][COL]) 23 { 24 for(int row=0;rowrow) 25 { 26 printf("| %c | %c | %c | ",broad[row][0],broad[row][1],broad[row][2]); 27 if (row != ROW - 1) 28 { 29 printf(" ---|---|--- "); 30 } 31 } 32 } 33
34 void Playermove(char broad[ROW][COL]) 35 { 36 printf("玩家下棋! "); 37 while(1 span>) 38 { 39 int row = 0; 40 int col = 0; 41 printf("请输入一组坐标: "); 42 scanf("%d %d", &row, &col); 43 if (row < 0 || row >= ROW || col < 0 || col >= COL) 44 { 45 printf("您输入的坐标非法!请重新输入: "); 46 continue; 47 } 48 if (broad[row][col] != ‘ ‘) 49 { 50 printf("该位置已经有棋子,请重新输入: "); 51 continue; 52 } 53 broad[row][col] = ‘x‘; 54 break; 55 } 56 } 57
58 void Computermove(char broad[ROW][COL]) 59 { 60 printf("电脑下棋! "); 61 while (1) 62 { 63 int row = rand() % ROW; 64 int col = rand() % COL; 65 if (broad[row][col] != ‘ ‘) 66 { 67 //位置被占用,重新生成
68 continue; 69 } 70 broad[row][col] = ‘o‘; 71 break; 72 } 73
74 } 75
76 int Isfull(char broad[ROW][COL]) 77 { 78 for (int row = 0;row < ROW;++row) 79 { 80 for (int col = 0;col < COL;++col) 81 { 82 if (broad[row][col] == ‘ ‘) 83 { 84 return 0; 85 } 86 } 87 } 88 return 1; 89 } 90
91 char Checkgame(char broad[ROW][COL]) 92 { 93 //先检查所有的行
94 for (int row = 0;row < ROW;++row) 95 { 96 if (broad[row][0] == broad[row][1] 97 && broad[row][0] == broad[row][2] 98 && broad[row][0] != ‘ ‘) 99 { 100 return broad[row][0]; 101 } 102 } 103 //检查所有列
104 for (int col = 0;col < COL;++col) 105 { 106 if (br oad[0][col] == broad[1][col] 107 && broad[0][col] == broad[2][col] 108 && broad != ‘ ‘) 109 { 110 return broad[0][col]; 111 } 112 } 113 //检查对角线
114 if (broad[0][0] == broad[1][1] 115 && broad[0][0] == broad[2][2] 116 && broad[0][0] != ‘ ‘) 117 { 118 return broad[0][0]; 119 } 120 if (broad[2][0] == broad[1][1] 121 && broad[2][0] == broad[0][2] 122 && broad[2][0] != ‘ ‘) 123 { 124 return broad[2][0]; 125 } 126 //判定是否和棋
127 if (Isfull(broad)) 128 { 129 return ‘q‘; 130 } 131 return ‘ ‘; 132 } 133
134 void Game() 135 { 136 //1.先对棋盘进行初始化
137 Init(g_broad); 138 char winner = ‘ ‘; 139 while (1) 140 { 141 //2.打印棋盘(此时是一个空的棋盘)
142 Print(g_broad); 143 //3.玩家下棋,检查游戏是否结束
144 Playermove(g_broad); 145 //winner==x 玩家胜利;o电脑胜利;q和棋 ;‘ ‘胜负未分
146 winner = Checkgame(g_br oad); 147 if (winner != ‘ ‘) 148 { 149 break; 150 } 151 //4.电脑下棋,检查游戏是否结束
152 Computermove(g_broad); 153 winner = Checkgame(g_broad); 154 if (winner != ‘ ‘) 155 { 156 break; 157 } 158 //循环第2步。
159
160 } 161 //在游戏结束之前打印地图
162 Print(g_broad); 163 if (winner == ‘x‘) 164 { 165 printf("玩家胜利! "); 166 } 167 else if (winner == ‘o‘) 168 { 169 printf("电脑胜利! "); 170 } 171 else if (winner == ‘q‘) 172 { 173 printf("和棋! "); 174 } 175 else
176 { 177 printf("程序出错! "); 178 } 179
180 } 181
182 int Menu() 183 { 184 printf("============== "); 185 printf("1.开始游戏 "); 186 printf("0.结束游戏 "); 187 printf("============== "); 188 printf("请输入您的选择: "); 189 int choice = 0; 190 scanf("%d",&choice); 1 91 return choice; 192 } 193
194 int main() 195 { 196 //这里设置随机种子
197 srand((unsigned int)time(0)); 198 while (1) 199 { 200 int choice = Menu(); 201 if (choice == 1) 202 { 203 Game(); 204 } 205 else
206 { 207 printf("Goodbye! "); 208 break; 209 } 210 } 211 return 0; 212 }