Submission

Status:

--------------------

Subtask/Task Score:

0/100

Score: 0

User: yourmom

Problemset: Othello

Language: cpp

Time: 0.004 second

Submitted On: 2026-10-08 20:36:15

#include <iostream>
#include <vector>
#include <string>

using namespace std;

// ฟังก์ชันสำหรับพิมพ์บอร์ดล่าสุด
void printBoard(const vector<vector<char>>& board) {
    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 8; ++j) {
            cout << board[i][j];
        }
        cout << "\n";
    }
}

// ฟังก์ชันสำหรับตรวจสอบและประกาศผลผู้ชนะ
void printWinner(const vector<vector<char>>& board) {
    int blackCount = 0;
    int whiteCount = 0;
    
    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 8; ++j) {
            if (board[i][j] == 'B') blackCount++;
            else if (board[i][j] == 'W') whiteCount++;
        }
    }
    
    if (blackCount > whiteCount) {
        cout << "Black wins" << "\n";
    } else if (whiteCount > blackCount) {
        cout << "White wins"<<"\n";
    } else {
        cout << "Draw" << "\n";
    }
}

// ฟังก์ชันสำหรับเช็คว่าบอร์ดเต็มหรือยัง
bool isBoardFull(const vector<vector<char>>& board) {
    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 8; ++j) {
            if (board[i][j] == '_') return false;
        }
    }
    return true;
}

// ฟังก์ชันสำหรับลงหมากและเปลี่ยนสีหมากที่ถูกล้อม (Flip)
void makeMove(vector<vector<char>>& board, int r, int c, char color) {
    board[r][c] = color;
    char opponent = (color == 'B') ? 'W' : 'B';
    
    // ทิศทางรอบตัว 8 ทิศทาง (dr, dc)
    int dr[] = {-1, -1, -1,  0, 0,  1, 1, 1};
    int dc[] = {-1,  0,  1, -1, 1, -1, 0, 1};
    
    // ตรวจสอบและเปลี่ยนสีทั้ง 8 ทิศทาง
    for (int d = 0; d < 8; ++d) {
        int currR = r + dr[d];
        int currC = c + dc[d];
        vector<pair<int, int>> cellsToFlip;
        
        // เดินหน้าไปตราบใดที่ยังเจอหมากฝั่งตรงข้าม
        while (currR >= 0 && currR < 8 && currC >= 0 && currC < 8 && board[currR][currC] == opponent) {
            cellsToFlip.push_back({currR, currC});
            currR += dr[d];
            currC += dc[d];
        }
        
        // ถ้าทิศทางนั้นจบลงด้วยการเจอหมากสีตัวเอง แปลว่าล้อมสำเร็จ ให้เปลี่ยนสีหมากที่บันทึกไว้
        if (currR >= 0 && currR < 8 && currC >= 0 && currC < 8 && board[currR][currC] == color) {
            for (const auto& cell : cellsToFlip) {
                board[cell.first][cell.second] = color;
            }
        }
    }
}

int main() {
    vector<vector<char>> board(8, vector<char>(8));
    
    // 1. รับค่าโครงสร้างบอร์ดเริ่มต้น 8 บรรทัด บรรทัดละ 8 ตัวอักษร
    for (int i = 0; i < 8; ++i) {
        string row;
        cin >> row;
        for (int j = 0; j < 8; ++j) {
            board[i][j] = row[j];
        }
    }
    
    int turn = 0; // 0 = Black ('B'), 1 = White ('W')
    int r, c;
    
    // 2. วนรับพิกัดเพื่อเดินหมาก
    while (true) {
        // เงื่อนไขจบเกม: ถ้าบอร์ดเต็มแล้วให้หยุดทันทีก่อนรับค่าใหม่
        if (isBoardFull(board)) {
            break;
        }
        
        if (!(cin >> r >> c)) {
            break; // จบการทำงานหากหมดข้อมูลเข้า (EOF)
        }
        
        // เงื่อนไขจบเกม: ผู้เล่นใส่ -1 -1 เพื่อบังคับจบเกม
        if (r == -1 && c == -1) {
            break;
        }
        
        // กำหนดสีตามเทิร์นปัจจุบัน
        char currentColor = (turn % 2 == 0) ? 'B' : 'W';
        
        // ทำการลงหมากและพลิกสี
        makeMove(board, r, c, currentColor);
        
        turn++; // เปลี่ยนเทิร์นถัดไป
    }
    
    // 3. แสดงผลลัพธ์บอร์ดสุดท้ายและผู้ชนะ
    printBoard(board);
    printWinner(board);
    
    return 0;
}