1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
| #include <stdio.h> #include <stdlib.h> #include <time.h>
#define ROW 3 #define COL 3
void display_menu();
void init_board(char arr[ROW][COL], int row, int col);
void display_board(char arr[ROW][COL], int row, int col);
void play_game(char arr[ROW][COL], int row, int col);
void player_move(char arr[ROW][COL], int row, int col);
void computer_move(char arr[ROW][COL], int row, int col);
char is_win(char arr[ROW][COL], int row, int col);
int is_board_full(char arr[ROW][COL], int row, int col);
int main() { char arr[ROW][COL] = {0}; int input = 0;
do { display_menu(); printf("请输入选择:>"); scanf("%d", &input);
switch (input) { case 1: play_game(arr, ROW, COL); break; case 0: printf("退出游戏!\n"); break; default: printf("输入有误,请重新输入!\n"); break; }
} while (input);
return 0; }
void display_menu() { printf("*************************\n"); printf("******** 1. play ********\n"); printf("******** 0. exit ********\n"); printf("*************************\n"); }
void init_board(char arr[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = ' '; } } }
void display_board(char arr[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", arr[i][j]); if (j < col - 1) printf("|"); } printf("\n");
if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } }
void play_game(char arr[ROW][COL], int row, int col) { char rst = 0; srand((unsigned int)time(NULL));
init_board(arr, ROW, COL); display_board(arr, ROW, COL);
while (1) { player_move(arr, ROW, COL); rst = is_win(arr, ROW, COL); if (rst != 'C') { break; }
computer_move(arr, ROW, COL); rst = is_win(arr, ROW, COL); if (rst != 'C') { break; } }
if (rst == 'X') { printf("玩家赢!\n"); printf("\n"); } else if (rst == 'O') { printf("电脑赢!\n"); printf("\n"); } else if (rst == 'Q') { printf("平局!\n"); printf("\n"); } }
void player_move(char arr[ROW][COL], int row, int col) { while (1) { int x = 0; int y = 0; printf("请输入要落子的坐标:>"); scanf("%d%d", &x, &y); printf("\n");
if (x <= 0 || x > row || y <= 0 || y > col) { printf("坐标非法,请重新输入!\n"); display_board(arr, ROW, COL); printf("\n"); } else if (arr[x - 1][y - 1] != ' ') { printf("坐标已被占用,请重新输入!\n"); display_board(arr, ROW, COL); printf("\n"); } else { arr[x - 1][y - 1] = 'X'; display_board(arr, ROW, COL); printf("\n"); break; } } }
void computer_move(char arr[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑落子...\n"); printf("\n");
while (1) { x = rand() % row; y = rand() % col; if (arr[x][y] == ' ') { arr[x][y] = 'O'; sleep(1); display_board(arr, ROW, COL); printf("\n"); break; } } }
char is_win(char arr[ROW][COL], int row, int col) {
char rst = 0;
for (int i = 0; i < row; i++) { if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][0] != ' ') { rst = arr[i][0]; } }
for (int i = 0; i < col; i++) { if (arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i] && arr[0][i] != ' ') { rst = arr[0][i]; } }
if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[2][2] != ' ') { rst = arr[1][1]; }
if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[2][0] != ' ') { rst = arr[1][1]; }
if (is_board_full(arr, row, col) == 1 && rst == 0) { rst = 'C'; }
if (is_board_full(arr, row, col) == 0 && rst == 0) { rst = 'Q'; }
return rst; }
int is_board_full(char arr[ROW][COL], int row, int col) {
int rst = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (arr[i][j] == ' ') rst = 1; } }
return rst; }
|