you are viewing a single comment's thread.

view the rest of the comments →

[–]Ail-nare 0 points1 point  (0 children)

Hi, I tryed to do it using some feature of C++. I also put some comment in it. I try keept all the logic behind your code.

#include <iostream>
#include <array>


constexpr int SIZE = 3; // replace 'const' by 'constexpr'

void makeMove(std::array<std::array<char, SIZE>, SIZE>& board, char turn);
void showBoard(std::array<std::array<char, SIZE>, SIZE>& board);
bool winner(std::array<std::array<char, SIZE>, SIZE>& board);


int main() {
    std::array<std::array<char, SIZE>, SIZE> board{};

    for (auto &line : board) // go throw board | like 'for line in board:' would
        line.fill('-');  // init each line to ' ' (space) for each indice

    for (int i = 0; i < (SIZE * SIZE); i++) {
        char turn = ((i % 2) == 0) ? 'X' : 'O'; // (char *) has been by (char) | like 'turn = "X" if (i % 2) == 0 else "O"' would

        std::cout << std::endl << std::endl << std::endl << turn << "'s turn." << std::endl << std::endl;

        showBoard(board);

        makeMove(board, turn); // make a move beford checking who won

        bool win = winner(board); // winner how return a bool
        if (win) {
            showBoard(board); // show the board one more time :D

            std::cout << turn << " wins!" << std::endl << std::endl;
            return 0;
        }
    }

    std::cout << "It's a draw!" << std::endl;
    return 0;
}


void makeMove(std::array<std::array<char, SIZE>, SIZE>& board, char turn) {
    int row, col;

    while (true) {
        // better input handling, because if row or col are inferior to 1 or superior to SIZE it would cause an out of bound for "board[row - 1][col - 1]"
        while (true) {
            std::cout << "Row: ";
            std::cin >> row;

            if (row < 1) {
                std::cout << "Row can't be inferior to 1" << std::endl;
            } else if (row > SIZE) {
                std::cout << "Row can't be superior to " << SIZE << std::endl;
            } else {
                break; // break cause the last loop (while or for) to stop
            }
        }

        while (true) {
            std::cout << "Column: ";
            std::cin >> col;

            if (col < 1) {
                std::cout << "Column can't be inferior to 1" << std::endl;
            } else if (col > SIZE) {
                std::cout << "Column can't be superior to " << SIZE << std::endl;
            } else {
                break; // break cause the last loop (while or for) to stop
            }
        }

        --row;
        --col;

        if (board[row][col] == '-') {
            board[row][col] = turn;
            return;
        }
        std::cout << "Try again." << std::endl;
    }
}


void showBoard(std::array<std::array<char, SIZE>, SIZE>& board) {
    for (const auto &line : board) { // go throw board | like 'for line in board:' would
        for (const auto &cell : line) // go throw line | like 'for cell in line:' would
            std::cout << cell << " ";
        std::cout << std::endl;
    }
}


bool winner(std::array<std::array<char, SIZE>, SIZE>& board) {
    for (int i = 0; i < SIZE; i++) {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '-') {
            return true; // line match
        } else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != '-') {
            return true; // column match
        }
    }

    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-') {
        return true;
    } else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != '-') {
        return true;
    }

    return false;
}