Untitled
unknown
c_cpp
a year ago
7.3 kB
3
Indexable
Never
#include <iostream> #include <limits> using namespace std; const int MAX_SIZE = 100; char field[MAX_SIZE][MAX_SIZE]; int player1Score = 0, player2Score = 0; void createField(int fieldSize) { for (int i = 0; i < fieldSize; i++) { for (int j = 0; j < fieldSize; j++) { field[i][j] = '-'; } } } void printField(int fieldSize) { for (int i = 0; i < fieldSize; i++) { for (int j = 0; j < fieldSize; j++) { cout << field[i][j] << " "; } cout << endl; } } bool checkWin(char player, int fieldSize) { int requiredSymbols = 3; if (fieldSize >= 5) { requiredSymbols = 5; } else if (fieldSize == 4) { requiredSymbols = 4; } for (int i = 0; i < fieldSize; i++) { int symbolCount = 0; for (int j = 0; j < fieldSize; j++) { if (field[i][j] == player) { symbolCount++; if (symbolCount == requiredSymbols) { return true; } } else { symbolCount = 0; } } } for (int j = 0; j < fieldSize; j++) { int symbolCount = 0; for (int i = 0; i < fieldSize; i++) { if (field[i][j] == player) { symbolCount++; if (symbolCount == requiredSymbols) { return true; } } else { symbolCount = 0; } } } for (int i = 0; i <= fieldSize - requiredSymbols; i++) { for (int j = 0; j <= fieldSize - requiredSymbols; j++) { int symbolCount = 0; for (int k = 0; k < requiredSymbols; k++) { if (field[i + k][j + k] == player) { symbolCount++; if (symbolCount == requiredSymbols) { return true; } } else { symbolCount = 0; } } } } for (int i = 0; i <= fieldSize - requiredSymbols; i++) { for (int j = requiredSymbols - 1; j < fieldSize; j++) { int symbolCount = 0; for (int k = 0; k < requiredSymbols; k++) { if (field[i + k][j - k] == player) { symbolCount++; if (symbolCount == requiredSymbols) { return true; } } else { symbolCount = 0; } } } } return false; } void makeComputerMove(char player, int fieldSize) { int row, col; cout << "Computer's move: "; for (int i = 0; i < fieldSize; i++) { for (int j = 0; j < fieldSize; j++) { if (field[i][j] == '-') { field[i][j] = player; if (checkWin(player, fieldSize)) { row = i; col = j; field[i][j] = '-'; goto found_move; } field[i][j] = '-'; } } } row = rand() % fieldSize; col = rand() % fieldSize; while (field[row][col] != '-') { row = rand() % fieldSize; col = rand() % fieldSize; } found_move: field[row][col] = player; cout << row+1 << " " << col+1 << endl; } void makePlayerMove(char player, int fieldSize) { int row, col; cout << "Player " << player << ", enter row and column to make a move: "; while (!(cin >> row >> col) || cin.get() != '\n' || row < 1 || row > fieldSize || col < 1 || col > fieldSize || field[row - 1][col - 1] != '-') { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Invalid input. Enter row and column again: "; } field[row - 1][col - 1] = player; } void addResult(char winner, string player1Name) { if (winner == 'X') { player1Score++; cout << "Player " << player1Name << " wins!" << endl; } else if (winner == 'O') { player2Score++; cout << "Computer wins!" << endl; } else { cout << "No one wins!" << endl; } } void printResult(string player1Name) { cout << "Score: " << player1Name << " - " << player1Score << ", Computer - " << player2Score << endl; } int main() { int fieldSize; cout << "Enter the field size (more than 2): "; while (!(cin >> fieldSize) || cin.get() != '\n' || fieldSize < 3) { cin.clear(); cin.ignore(numeric_limits<streamsize> ::max(), '\n'); cout << "Invalid input. Enter the field size again: "; } createField(fieldSize); printField(fieldSize); string player1Name; cout << "Player 1, enter your name: "; cin >> player1Name; char currentPlayer = 'X'; while (true) { if (currentPlayer == 'O') { makeComputerMove(currentPlayer, fieldSize); } else { makePlayerMove(currentPlayer, fieldSize); } printField(fieldSize); if (checkWin(currentPlayer, fieldSize)) { addResult(currentPlayer, player1Name); printResult(player1Name); char answer; do { cout << "Do you want to continue? (y/n): "; cin >> answer; } while (answer != 'y' && answer != 'n'); if (answer == 'y') { createField(fieldSize); printField(fieldSize); continue; } else { cout << "Final score: " << player1Name << " - " << player1Score << ", Computer - " << player2Score << endl; break; } } if (currentPlayer == 'X') { currentPlayer = 'O'; } else { currentPlayer = 'X'; } // Check for no possible moves left bool possibleMovesLeft = false; for (int i = 0; i < fieldSize; i++) { for (int j = 0; j < fieldSize; j++) { if (field[i][j] == '-') { possibleMovesLeft = true; break; } } if (possibleMovesLeft) { break; } } if (!possibleMovesLeft) { addResult(' ', player1Name); printResult(player1Name); char answer; do { cout << "Do you want to continue? (y/n): "; cin >> answer; } while (answer != 'y' && answer != 'n'); if (answer == 'y') { createField(fieldSize); printField(fieldSize); continue; } else { cout << "Final score: " << player1Name << " - " << player1Score << ", Computer - " << player2Score << endl; break; } } } return 0; }