#include <iostream> #include <cstdlib> #include <ctime> #include <cassert> #include <memory> using namespace std; // Implementacja MyVector z unique_ptr template <typename T> class MyVector { private: unique_ptr<T[]> data; size_t capacity; size_t count; void resize(size_t newCapacity) { unique_ptr<T[]> newData = make_unique<T[]>(newCapacity); for (size_t i = 0; i < count; i++) { newData[i] = data[i]; } data = move(newData); capacity = newCapacity; } public: MyVector() : capacity(4), count(0), data(make_unique<T[]>(4)) {} void push_back(const T& value) { if (count >= capacity) resize(capacity * 2); data[count++] = value; } void pop_back() { if (count > 0) count--; } void remove_at(size_t index) { assert(index < count); data[index] = data[count - 1]; pop_back(); } size_t size() const { return count; } T& operator[](size_t index) { assert(index < count); return data[index]; } const T& operator[](size_t index) const { assert(index < count); return data[index]; } }; struct Coordinate { int row; int col; }; const int BOARD_SIZE = 5; const int NUM_SHIPS = 5; void initBoard(MyVector<char>& board) { for (int i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) { board.push_back('.'); } } void printBoard(const MyVector<char>& board, bool hideShips = false) { cout << " "; for (int j = 0; j < BOARD_SIZE; j++) { cout << j + 1 << " "; } cout << "\n"; for (int i = 0; i < BOARD_SIZE; i++) { cout << i + 1 << " "; for (int j = 0; j < BOARD_SIZE; j++) { int index = i * BOARD_SIZE + j; char c = board[index]; if (hideShips && c == 'S') cout << ". "; else cout << c << " "; } cout << "\n"; } } bool allShipsSunk(const MyVector<char>& board) { for (int i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) { if (board[i] == 'S') return false; } return true; } int main() { srand(static_cast<unsigned int>(time(0))); MyVector<char> userBoard; MyVector<char> compBoard; initBoard(userBoard); initBoard(compBoard); cout << "=== Gra w statki ===\n"; cout << "Plansza ma rozmiar 5x5, a liczba statkow wynosi 5.\n"; int placed = 0; while (placed < NUM_SHIPS) { printBoard(userBoard); int r, c; cout << "Podaj wspolrzędne statku nr " << placed + 1 << " (wiersz i kolumna): "; cin >> r >> c; if (r < 1 || r > BOARD_SIZE || c < 1 || c > BOARD_SIZE) { cout << "Niepoprawne współrzędne. Spróbuj ponownie.\n"; continue; } int index = (r - 1) * BOARD_SIZE + (c - 1); if (userBoard[index] == 'S') { cout << "Na tym polu już znajduje się statek. Spróbuj inne pole.\n"; continue; } userBoard[index] = 'S'; placed++; } placed = 0; while (placed < NUM_SHIPS) { int r = rand() % BOARD_SIZE; int c = rand() % BOARD_SIZE; int index = r * BOARD_SIZE + c; if (compBoard[index] == 'S') continue; compBoard[index] = 'S'; placed++; } MyVector<Coordinate> availableShots; for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { availableShots.push_back({ i, j }); } } while (true) { cout << "\nTwoja plansza:\n"; printBoard(userBoard); cout << "\nPlansza przeciwnika:\n"; printBoard(compBoard, true); int r, c; cout << "\nTwoj ruch! Podaj wspolrzedne strzalu: "; cin >> r >> c; if (r < 1 || r > BOARD_SIZE || c < 1 || c > BOARD_SIZE) { cout << "Niepoprawne współrzędne. Spróbuj ponownie.\n"; continue; } int index = (r - 1) * BOARD_SIZE + (c - 1); if (compBoard[index] == 'X' || compBoard[index] == '*') { cout << "To pole bylo juz strzelone. Sprobuj inne.\n"; continue; } compBoard[index] = (compBoard[index] == 'S') ? 'X' : '*'; if (allShipsSunk(compBoard)) { cout << "\nGratulacje! Zatopiles wszystkie statki przeciwnika.\n"; break; } int index2 = rand() % availableShots.size(); Coordinate compShot = availableShots[index2]; availableShots.remove_at(index2); cout << "\nRuch komputera: strzela na pole (" << compShot.row + 1 << ", " << compShot.col + 1 << ").\n"; int compIndex = compShot.row * BOARD_SIZE + compShot.col; userBoard[compIndex] = (userBoard[compIndex] == 'S') ? 'X' : '*'; if (allShipsSunk(userBoard)) { cout << "\nPrzegrales! Komputer zatopil wszystkie twoje statki.\n"; break; } } cout << "\nKoniec gry.\n"; return 0; }