Top.Mail.Ru
Ответы

Программа закрывается после введения переменной C++

После ввода переменной col, программа закрывается

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
 #include <iostream> 
using namespace std; 
 
void displayBoard(char** board, int rows, int cols) { 
    for (int i = 0; i < rows; i++) { 
        for (int j = 0; j < cols; j++) { 
            cout << board[i][j] << " "; 
        } 
        cout << endl; 
    } 
} 
 
bool checkWin(char** board, int rows, int cols, char symbol, int row, int col) { 
    int count = 0; 
    for (int j = 0; j < cols; j++) { 
        if (board[row][j] == symbol) { 
            count++; 
            if (count == 4) { 
                return true; 
            } 
        } else { 
            count = 0; 
        } 
    } 
     
    count = 0; 
    for (int i = 0; i < rows; i++) { 
        if (board[i][col] == symbol) { 
            count++; 
            if (count == 4) { 
                return true; 
            } 
        } else { 
            count = 0; 
        } 
    } 
 
    count = 0; 
    for (int i = 0; i < rows; i++) { 
        if (board[i + 1][col + 1] == symbol) { 
            count++; 
            if (count == 4) { 
                return true; 
            } 
        } else { 
            count = 0; 
        } 
    } 
 
    return false; 
} 
 
int main() { 
    int rows, cols; 
    cout << "Enter the number of rows for the game board: "; 
    cin >> rows; 
    cout << "Enter the number of columns for the game board: "; 
    cin >> cols; 
 
    char** board = new char*[rows]; 
    for (int i = 0; i < rows; i++) { 
        board[i] = new char[cols]; 
    } 
 
    for (int i = 0; i < rows; i++) { 
        for (int j = 0; j < cols; j++) { 
            board[i][j] = '.'; 
        } 
    } 
 
    bool player1Turn = true; 
    int moves = 0; 
 
    while (true) { 
        displayBoard(board, rows, cols); 
 
        int col; 
        cout << "Player " << (player1Turn ? "1" : "2") << " turn. Enter column number (0-" << (cols - 1) << "): "; 
        cin >> col; 
 
        if (col < 0 || col >= cols) { 
            cout << "Invalid column number. Please try again." << endl; 
            continue; 
        } 
 
        for (int i = rows - 1; i >= 0; i--) { 
            if (board[i][col] == '.') { 
                board[i][col] = (player1Turn ? '+' : 'x'); 
                if (checkWin(board, rows, cols, (player1Turn ? '+' : 'x'), i, col) == true) { 
                    displayBoard(board, rows, cols); 
                    cout << "Player " << (player1Turn ? "1" : "2") << " wins!" << endl; 
                    break; 
                } 
                player1Turn = !player1Turn; 
                moves++; 
                if (moves == rows * cols) { 
                    cout << "It's a draw!" << endl; 
                    break; 
                } 
                break; 
            } 
            if (i == 0) { 
                cout << "Column is full. Please try again." << endl; 
            } 
        } 
 
        if (moves == rows * cols) { 
            break; 
        } 
    } 
 
    for (int i = 0; i < rows; i++) { 
        delete[] board[i]; 
    } 
    delete[] board; 
 
    return 0; 
} 
По дате
По рейтингу
Аватар пользователя
Мудрец
12
     for (int i = 0; i < rows; i++) {  
        if (board[i + 1][col + 1] == symbol) {  

i + 1 может стать равно rows

Аватар пользователя
Гений

Как вариант выход за пределы массива
попробуй заменить, у меня заработало
for (int i = 0; i < rows; i++) {
на
for (int i = 0; i < rows-1; i++) {


Полный рефакторинг там
https://onlinegdb.com/FTA8H17jh

Аватар пользователя
Оракул

В строке 40 ошибка выхода за границы массива

Аватар пользователя

а на лог ошибки смотрел?

Аватар пользователя
Ученик

Компилируется и запускается все нормально



Видео по теме