/
YouUrble
/
Tic-tac-toe
Обзор
Документация
Войти
/
YouUrble
/
Tic-tac-toe
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.cpp
296 строк
7 KB
YouUrble
Первый коммит
07 окт 2025, 09:48
07 окт 2025, 09:48
50e39dc
Код
Авторство
О чём код?
#include <iostream> #include <iomanip> #include <vector> using namespace std; class CrossesNoughts{ int weight = 3; int height = 3; bool firstDraw = true; public: CrossesNoughts() = default; CrossesNoughts(CrossesNoughts&) = delete; void _play(); void _settings(); void _clear() { system("clear"); } private: void _main(); void _draw(vector<int> field); int _checkWin(vector<int> field); }; void CrossesNoughts::_main(){ cout << "| Crosses and noughts" << endl; cout << "| 1. Play" << endl; cout << "| 2. Settings" << endl; cout << "| 3. Exit" << endl; } int CrossesNoughts::_checkWin(const vector<int> field) { // Проверка строк for (int i = 0; i < height; ++i) { int sum = 0; bool use = true; for (int j = 0; j < weight; ++j) { sum += field[i * weight + j]; if (field[i*weight+j]==0) use = false; } if (sum == weight && use == true) return 1; if (sum == weight * 2 && use == true) return 2; } // Проверка столбцов for (int j = 0; j < weight; ++j) { int sum = 0; bool use = true; for (int i = 0; i < height; ++i) { sum += field[i * weight + j]; if (field[i * weight + j] == 0) use = false; } if (sum == height && use == true) return 1; // все крестики if (sum == height * 2 && use == true) return 2; // все нолики } // Главная диагональ (только если height == weight, или проверяем min) if (height == weight) { int sum = 0; bool use = true; for (int i = 0; i < height; ++i) { sum += field[i * weight + i]; if (field[i * weight + i] == 0) use = false; } if (sum == height && use == true) return 1; if (sum == height * 2 && use == true) return 2; } // Побочная диагональ if (height == weight) { int sum = 0; bool use = true; for (int i = 0; i < height; ++i) { sum += field[i * weight + (weight - 1 - i)]; if (field[i * weight + (weight - 1 - i)] == 0) use = false; } if (sum == height && use == true) return 1; if (sum == height * 2 && use == true) return 2; } if (height == weight){ int count = 0; for (int i = 0; i < height * weight; i++) { if (field[i] != 0){ count++; } } if (count == height * weight) { return 4; } } // Никто не победил return 0; } void CrossesNoughts::_draw(vector<int> field){ _clear(); cout << "| "; for (size_t k = 0; k < weight; k++) { cout << k + 1 << " "; } cout << endl; if (firstDraw){ firstDraw = false; for (int i = 0; i < height; i++) { cout << "| " << i + 1 << " "; for (int j = 0; j < weight; j++) { cout << "n" << " "; } cout << endl; } return; } for (int i = 0; i < height; i++) { cout << "| " << i + 1 << " "; for (int j = 0; j < weight; j++) { if (field[i * weight + j] == 0) cout << "n" << " "; else if (field[i * weight + j] == 1){ cout << "\033[31mX\033[0m" << " "; } else { cout << "\033[32m0\033[0m" << " "; } } cout << endl; } int res = _checkWin(field); switch (res) { case 1: cout << "| \033[1;34mX win!\033[0m" << endl; exit(0); break; case 2: cout << "| \033[1;34m0 win!\033[0m" << endl; exit(0); case 4: cout << "| \033[1;34mDraw!!\033[0m" << endl; exit(0); default: break; } } void CrossesNoughts::_settings() { _clear(); cout << "| Settings:" << endl; cout << "| 1. Size" << endl; char choice{'\0'}; cin >> choice; if (choice == '1') { int _size = 3; cout << "| Enter size: "; cin >> _size; if (_size > 10 || _size < 1) { cout << "| Incorrect value, set default: 3x3" << endl; weight = _size; height = _size; } else { weight = _size; height = _size; } } _clear(); _main(); } void CrossesNoughts::_play(){ vector<int> field; for (int k = 0; k < weight * height; k++){ field.push_back(0); } _draw(field); for (int count = 0; count < height * weight; count++){ int x = 0; int y = 0; if (count % 2 == 0) { wrongX:{}; cout << endl; cout << "| choose X" << endl; cout << "x: "; cin >> x; cout << "y: "; cin >> y; x--; y--; if (field[y * weight + x] == 0) field[y * weight + x] =1; // 1 = X else { _draw(field); cout << "| Wrong coordinate" << endl; goto wrongX; } _draw(field); } else { wrong0:{}; cout << endl; cout << "| choose 0" << endl; cout << "x: "; cin >> x; cout << "y: "; cin >> y; x--; y--; if (field[y * weight + x] == 0) field[y * weight + x] = 2; // 2 = 0 else { _draw(field); cout << "| Wrong coordinate" << endl; goto wrong0; } _draw(field); } } firstDraw = true; } void _main(){ cout << "| Crosses and noughts" << endl; cout << "| 1. Play" << endl; cout << "| 2. Settings" << endl; cout << "| 3. Exit" << endl; } int main() { CrossesNoughts game; game._clear(); _main(); char choice = '\0'; while (cin >> choice){ game._clear(); _main(); if (choice == '1') { game._play(); } else if (choice == '2'){ game._settings(); } else if (choice == '3'){ game._clear(); cout << "| Exit" << endl; exit(0); } } return 0; }