Untitled
unknown
c_cpp
a year ago
3.8 kB
2
Indexable
Never
#include <iostream> #include <limits> #include <random> 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) { std::cout << board[i][j] << ' '; } std::cout << std::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) { std::random_device rd; std::mt19937 gen(rd()); std::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); } std::cout << "Computer move: row " << row + 1 << ", col " << col + 1 << std::endl; board[row][col] = player; } int main() { int fieldSize; std::cout << "Enter the field size: "; if (!(std::cin >> fieldSize)) { std::cout << "Error. Field size can only be set as a number" << std::endl; return 1; } 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; std::cout << "Player 1, enter your move (row, col): "; std::cin >> row >> col; row -= 1; // subtract 1 from row col -= 1; // subtract 1 from column while (std::cin.fail() || row < 0 || row >= fieldSize || col < 0 || col >= fieldSize || board[row][col] != EMPTY) { std::cin.clear(); std::cin.ignore(std::numeric_limits<std:: streamsize>::max(), '\n'); std::cout << "Invalid input. Please enter a valid move (row, col): "; std::cin >> row >> col; row -= 1; // subtract 1 from row col -= 1; // subtract 1 from column } // Fix the bug here: board[row][col] = PLAYER_ONE; if (checkWin(fieldSize, board, PLAYER_ONE)) { drawBoard(fieldSize, board); std::cout << "You win!" << std::endl; break; } // Add computer move here: int computerRow, computerCol; do { computerRow = rand() % fieldSize; computerCol = rand() % fieldSize; } while (board[computerRow][computerCol] != EMPTY); board[computerRow][computerCol] = PLAYER_TWO; if (checkWin(fieldSize, board, PLAYER_TWO)) { drawBoard(fieldSize, board); std::cout << "Computer wins!" << std::endl; break; } --moves; } if (moves == 0) { drawBoard(fieldSize, board); std::cout << "It's a draw!" << std::endl; } } for (int i = 0; i < fieldSize; ++i) { delete[] board[i]; } delete[] board; return 0; }