Демка

mail@pastecode.io avatar
unknown
c_cpp
a year ago
5.6 kB
3
Indexable
Never
#include <iostream>
#include <fstream>
#include <limits>
#include <random>
#include <string>
using namespace std;

const char PLAYER_ONE = 'X';
const char PLAYER_TWO = 'O';
const char EMPTY = '-';

void drawBoard(const int fieldSize, char** board) {
    for (int i = 0; i < fieldSize; ++i) {
        for (int j = 0; j < fieldSize; ++j) {
            cout << board[i][j] << ' ';
        }
        cout << endl;
    }
}

bool checkWin(const int fieldSize, char** board, char player) {
    for (int i = 0; i < fieldSize; ++i) {
        bool rowWin = true;
        bool colWin = true;
        for (int j = 0; j < fieldSize; ++j) {
            rowWin &= (board[i][j] == player);
            colWin &= (board[j][i] == player);
        }
        if (rowWin || colWin) {
            return true;
        }
    }

    bool diagonalWin = true;
    bool reverseDiagonalWin = true;
    for (int i = 0; i < fieldSize; ++i) {
        diagonalWin &= (board[i][i] == player);
        reverseDiagonalWin &= (board[i][fieldSize - i - 1] == player);
    }

    return diagonalWin || reverseDiagonalWin;
}

void computerMove(const int fieldSize, char** board, char player) {
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> distrib(0, fieldSize - 1);

    int row = distrib(gen);
    int col = distrib(gen);

    while (board[row][col] != EMPTY) {
        row = distrib(gen);
        col = distrib(gen);
    }

    cout << "Computer move: row " << row + 1 << ", column " << col + 1 << endl;
    board[row][col] = player;
}

void printResults(const int result[], int numGames) {
    int playerOneWins = 0;
    int playerTwoWins = 0;
    int draws = 0;

    ofstream file("results.txt", ios::app); // Open the file in append mode

    for (int i = 0; i < numGames; ++i) {
        if (result[i] == 1) {
            ++playerOneWins;
            file << "Player " << i + 1 << ":1 Computer\n";
        }
        else if (result[i] == -1) {
            ++playerTwoWins;
            file << "Player " << i + 1 << ":0 Computer\n";
        }
        else {
            ++draws;
            file << "Player " << i + 1 << ":0 Player\n";
        }
    }

    cout << "Player one wins: " << playerOneWins << endl;
    cout << "Player two wins: " << playerTwoWins << endl;
    cout << "Draws: " << draws << endl;
}


int main() {
    string playerName;
    cout << "Enter your name: ";
    getline(cin, playerName);

    int fieldSize;
    cout << "Enter the size of the field: ";
    if (!(cin >> fieldSize)) {
        cout << "Error. Field size can only be specified as a number!" << endl;
        return 1;
    }

    ofstream file("results.txt", ios::app); // Open the file in append mode

    while (true) {
        char** board = new char* [fieldSize];
        for (int i = 0; i < fieldSize; ++i) {
            board[i] = new char[fieldSize];
            for (int j = 0; j < fieldSize; ++j) {
                board[i][j] = EMPTY;
            }
        }

        bool playerOneTurn = true;
        int moves = fieldSize * fieldSize;

        while (moves > 0) {
            drawBoard(fieldSize, board);

            if (playerOneTurn) {
                int row, col;
                cout << playerName << ", enter your move in the format 'row column': ";
                cin >> row >> col;

                row -= 1; // subtract 1 from row
                col -= 1; // subtract 1 from column

                while (cin.fail() || row < 0 || row >= fieldSize || col < 0
                    || col >= fieldSize || board[row][col] != EMPTY) {
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(), '\n');
                    cout << "Invalid input. Please enter 'row column': ";
                    cin >> row >> col;
                    row -= 1; // subtract 1 from row
                    col -= 1; // subtract 1 from column
                }

                board[row][col] = PLAYER_ONE;
                if (checkWin(fieldSize, board, PLAYER_ONE)) {
                    drawBoard(fieldSize, board);
                    cout << "You won, " << playerName << "!" << endl;
                    file << "Result: " << playerName << " won.\n";
                    break;
                }

                playerOneTurn = false;
            }
            else {
                computerMove(fieldSize, board, PLAYER_TWO);
                if (checkWin(fieldSize, board, PLAYER_TWO)) {
                    drawBoard(fieldSize, board);
                    cout << "Computer won!" << endl;
                    file << "Result: Computer won.\n";
                    break;
                }

                playerOneTurn = true;
            }

            --moves;
        }

        if (moves == 0) {
            drawBoard(fieldSize, board);
            cout << "Draw!" << endl;
            file << "Result: Draw.\n";
        }

        // Release the memory for the board
        for (int i = 0; i < fieldSize; ++i) {
            delete[] board[i];
        }
        delete[] board;

        // Ask the user if they want to play again
        string playAgain;
        cout << "Do you want to play again? (yes/no): ";
        cin >> playAgain;

        while (playAgain != "yes" && playAgain != "no") {
            cout << "Invalid input. Please enter 'yes' or 'no': ";
            cin >> playAgain;
        }

        if (playAgain == "no") {
            break;
        }
    }

    file.close();
    return 0;
}