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
| #include <stdio.h> #include <stdbool.h> #include <ctype.h> #include <limits.h> #include <assert.h>
#define EMPTY 0 #define BLACK 1 #define WHITE 2
#define FLAG "\x76\x43\x49\x4C\x09\x5A\x4A\x55" \ "\x41\x41\x4E\x54\x11\x46\x5C\x14" \ "\x46\x53\x45\x4E\x5C\x48\x1B\x48" \ "\x52\x1E\x58\x25\x35\x62\x25\x28" \ "\x24\x21\x67\x38\x25\x30"
#define COMPUTER BLACK #define HUMAN WHITE
#define MIN_SCORE -100
char flag[] = FLAG; int board[3][3] = {};
void flag_decode(void) { for (int i = 0; flag[i] != '\0'; i++) { flag[i] ^= (i + 23333); } }
int check(void) { for (int i = 0; i < 3; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != EMPTY) { return board[i][0]; } if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != EMPTY) { return board[0][i]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != EMPTY) { return board[0][0]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != EMPTY) { return board[0][2]; } return false; }
bool full(void) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == EMPTY) { return false; } } } return true; }
void print(void) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { char out = '_'; if (board[i][j] == WHITE) { out = 'X'; } else if (board[i][j] == BLACK) { out = 'O'; } else if (board[i][j] != EMPTY) { out = '?'; } printf("%c", out); } puts(""); } }
int opp(int p) { if (p == BLACK) return WHITE; else if (p == WHITE) return BLACK; assert(0); }
int minimax(int player) { int winColor = check(); if (winColor != EMPTY) { return winColor == player ? 1 : -1; }
int movex = -1, movey = -1; int max_score = MIN_SCORE; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == EMPTY) { board[i][j] = player; int score = -minimax(opp(player)); if (score > max_score) { max_score = score; movex = i; movey = j; } board[i][j] = EMPTY; } } } if (movex == -1) { return 0; } return max_score; }
void ai(int *x, int *y) { int movex = -1, movey = -1; int max_score = MIN_SCORE; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == EMPTY) { board[i][j] = COMPUTER; int score = -minimax(HUMAN); if (score > max_score) { max_score = score; movex = i; movey = j; } board[i][j] = EMPTY; } } } *x = movex; *y = movey; }
int main(void) { setvbuf(stdin, 0, 2, 0); setvbuf(stdout, 0, 2, 0);
bool success = false; char input[128] = {};
puts("Welcome to Tic Tac Toe! Computer first!"); puts("You're 'X' and I'm 'O'!"); while (!success) { int x, y; puts("Now computer goes!"); ai(&x, &y); board[x][y] = BLACK; print(); if (check()) { success = false; break; } if (full()) { break; } puts("Now human goes!");
while (true) { printf("Your turn. Input like (x,y), such as (0,1): "); gets(input); x = input[1] - '0'; y = input[3] - '0'; printf("You wanna put X on (%d,%d)...\n", x, y); if (!(x >= 0 && x < 3 && y >= 0 && y < 3)) { puts("Wrong input! Please try again!"); continue; } if (board[x][y] != EMPTY) { puts("This pos has already been occupied!"); continue; } break; } board[x][y] = WHITE; print(); if (check()) { success = true; break; } if (full()) { break; } } if (success) { puts("What? You win! Here is your flag:"); flag_decode(); puts(flag); } else { puts("You failed! See you next time~"); } return 0; }
|