#include "strategic_player.h" using namespace std; void StrategicPlayer::selectLineLocation(Board& board, int& row, int& col) { int rows = board.getRows(); int cols = board.getCols(); // Step 1: Try to complete a box for (int r = 1; r < 2 * rows - 2; r += 2) { // Iterate over box centers (odd rows) for (int c = 1; c < 2 * cols - 2; c += 2) { // Iterate over box centers (odd cols) // Check adjacent lines for an opportunity to complete a box if (board.isLineValid(r - 1, c) && // Top line board.get(r + 1, c) != ' ' && // Bottom line board.get(r, c - 1) != ' ' && // Left line board.get(r, c + 1) != ' ') { // Right line row = r - 1; col = c; return; } if (board.isLineValid(r + 1, c) && // Bottom line board.get(r - 1, c) != ' ' && // Top line board.get(r, c - 1) != ' ' && // Left line board.get(r, c + 1) != ' ') { // Right line row = r + 1; col = c; return; } if (board.isLineValid(r, c - 1) && // Left line board.get(r - 1, c) != ' ' && // Top line board.get(r + 1, c) != ' ' && // Bottom line board.get(r, c + 1) != ' ') { // Right line row = r; col = c - 1; return; } if (board.isLineValid(r, c + 1) && // Right line board.get(r - 1, c) != ' ' && // Top line board.get(r + 1, c) != ' ' && // Bottom line board.get(r, c - 1) != ' ') { // Left line row = r; col = c + 1; return; } } } // Step 2: Avoid moves that leave a box with one line remaining for (int r = 0; r < 2 * rows - 1; ++r) { // Iterate over all valid rows for (int c = 0; c < 2 * cols - 1; ++c) { // Iterate over all valid cols if (board.isLineValid(r, c)) { // Simulate placing the line board.placeLine(r, c, name); // Check if this move leaves a box with only one line remaining bool createsOpportunity = false; for (int nr = 1; nr < 2 * rows - 2; nr += 2) { // Iterate over box centers for (int nc = 1; nc < 2 * cols - 2; nc += 2) { if (board.get(nr, nc) == ' ') { int adjacentLines = 0; if (nr > 0 && board.get(nr - 1, nc) != ' ') adjacentLines++; // Top line if (nr < 2 * rows - 2 && board.get(nr + 1, nc) != ' ') adjacentLines++; // Bottom line if (nc > 0 && board.get(nr, nc - 1) != ' ') adjacentLines++; // Left line if (nc < 2 * cols - 2 && board.get(nr, nc + 1) != ' ') adjacentLines++; // Right line if (adjacentLines == 3) { // Opponent can complete this box createsOpportunity = true; break; } } } if (createsOpportunity) break; } board.set(r, c, ' '); // Undo the simulated move if (!createsOpportunity) { row = r; col = c; return; } } } } // Step 3: Fallback to the first valid move for (int r = 0; r < 2 * rows - 1; ++r) { for (int c = 0; c < 2 * cols - 1; ++c) { if (board.isLineValid(r, c)) { row = r; col = c; return; } } } }