Fork Copy #include #include #include #include using namespace std; char myTurn; char board[15][15]; int score[15][15]; void printBoard(int board[15][15]) { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (j > 0) { cout << "," << board[i][j]; } else { cout << board[i][j]; } } cout << endl; } } void writeBoard() { FILE* f = fopen("board", "w"); for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (j == 14) { fprintf(f, "%c\n", board[i][j]); } else { fprintf(f, "%c,", board[i][j]); } } } fclose(f); } void readBoard() { FILE* f = fopen("board", "r"); for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (j == 14) { fscanf(f, "%c\n", &board[i][j]); } else { fscanf(f, "%c,", &board[i][j]); } } } fclose(f); } bool checkTurn() { FILE* f = fopen("turn", "r"); char turn; fscanf(f, "%c", &turn); fclose(f); return myTurn == turn; } void swapTurn() { FILE* f = fopen("turn", "w"); fprintf(f, "%c", myTurn == 'x' ? 'o' : 'x'); fclose(f); } void eval(char board[15][15], int score[15][15]) { for (int row = 0; row < 15; row++) { for (int col = 0; col < 15; col++) { score[row][col] = 0; } } for (int row = 0; row < 15; row++) { for (int col = 0; col < 15; col++) { if (board[row][col] != '-') { continue; } int memory[10][2]; int base = 4; for (int i = max(row - 1, 0); i <= min(row + 1, 14); i++) { for (int j = max(col - 1, 0); j <= min(col + 1, 14); j++) { score[row][col] += board[i][j] != '-'; } } //ngang 4 int i, j = col - 1, k = base; while(j > 0 && board[row][j] != '-') { if (board[row][j] == board[row][j - 1]) { score[row][col] += ++k; } else { break; } j--; } //ngang 6 j = col + 1; if (col - 1 >= 0 && col + 1 <= 14 && board[row][col - 1] != board[row][col + 1]) { k = base; } while(j < 14 && board[row][j] != '-') { if (board[row][j] == board[row][j + 1]) { score[row][col] += ++k; } else { break; } j++; } //doc 8 i = row - 1; k = base; while(i > 0 && board[i][col] != '-') { if (board[i][col] == board[i - 1][col]) { score[row][col] += ++k; } else { break; } i--; } //doc 2 i = row + 1; if (row - 1 >= 0 && row + 1 <= 14 && board[row - 1][col] != board[row + 1][col]) { k = base; } k = base; while(i < 14 && board[i][col] != '-') { if (board[i][col] == board[i + 1][col]) { score[row][col] += ++k; } else { break; } i++; } //cheo 7 i = row - 1; j = col - 1; k = base; while(i > 0 && j > 0 && board[i][j] != '-') { if (board[i][j] == board[i - 1][j - 1]) { score[row][col] += ++k; } else { break; } i--; j--; } //cheo 3 i = row + 1; j = col + 1; if (row - 1 >= 0 && col - 1 >= 0 && row + 1 <= 14 && col + 1 <= 14 && board[row - 1][col - 1] != board[row + 1][col + 1]) { k = base; } while(i < 14 && j < 14 && board[i][j] != '-') { if (board[i][j] == board[i + 1][j + 1]) { score[row][col] += ++k; } else { break; } i++; j++; } //cheo 9 i = row - 1; j = col + 1; k = base; while(i > 0 && j < 14 && board[i][j] != '-') { if (board[i][j] == board[i - 1][j + 1]) { score[row][col] += ++k; } else { break; } i--; j++; } //cheo 1 i = row + 1; j = col - 1; if (row - 1 >= 0 && col + 1 <= 14 && row + 1 <= 14 && col - 1 >= 0 && board[row - 1][col + 1] != board[row + 1][col - 1]) { k = base; } while(i < 14 && j > 0 && board[i][j] != '-') { if (board[i][j] == board[i + 1][j - 1]) { score[row][col] += ++k; } else { break; } i++; j--; } // cout << row << " " << col << " " << score[row][col] << endl; } } } void maxScore(int &i, int &j) { int max = 0; for (int row = 0; row < 15; row++) { for (int col = 0; col < 15; col++) { if (score[row][col] > max) { max = score[row][col]; i = row; j = col; } } } if (max == 0) { i = 7; j = 7; while (board[i][j] != '-' && j <= 14) { j++; } } } int main() { int t; cout << "Vui long nhap quan (x/o):"; cin >> myTurn; while (true) { Sleep(3000); if (! checkTurn()) { continue; } // system("cls"); // cout << "Press any key to play:";getch(); readBoard(); eval(board, score); int row, col; maxScore(row, col); board[row][col] = myTurn; cout << row << " " << col << endl; // printBoard(score); writeBoard(); swapTurn(); } }