Skip to content

Instantly share code, notes, and snippets.

@xsa-dev
Created September 11, 2023 21:47
Show Gist options
  • Save xsa-dev/adfcd614ce0193d6231d7bce788b1cdf to your computer and use it in GitHub Desktop.
Save xsa-dev/adfcd614ce0193d6231d7bce788b1cdf to your computer and use it in GitHub Desktop.
cpp tictactoe
#include <iostream>
#include <ctime>
#include <cstdlib>
// Функция для получения выбора компьютера
int computerChoice() {
// Генерируем случайное число от 0 до 2
return rand() % 3;
}
int main() {
// Массив с вариантами выбора
const char* choices[] = {"Камень", "Ножницы", "Бумага"};
int userWins = 0; // Счетчик побед пользователя
int computerWins = 0; // Счетчик побед компьютера
bool playAgain = true;
while (playAgain) {
// Получаем выбор пользователя
int userChoice;
std::cout << "Выберите вариант (0 - Камень, 1 - Ножницы, 2 - Бумага): ";
std::cin >> userChoice;
// Проверяем корректность ввода пользователя
if (userChoice < 0 || userChoice > 2) {
std::cout << "Некорректный выбор. Попробуйте еще раз." << std::endl;
continue; // Пропускаем текущую итерацию цикла
}
// Получаем выбор компьютера
int computer = computerChoice();
// Выводим выборы на экран
std::cout << "Вы выбрали: " << choices[userChoice] << std::endl;
std::cout << "Компьютер выбрал: " << choices[computer] << std::endl;
// Определяем победителя
if (userChoice == computer) {
std::cout << "Ничья!" << std::endl;
} else if ((userChoice == 0 && computer == 1) || (userChoice == 1 && computer == 2) || (userChoice == 2 && computer == 0)) {
std::cout << "Вы победили!" << std::endl;
userWins++;
} else {
std::cout << "Компьютер победил!" << std::endl;
computerWins++;
}
// Выводим счет
std::cout << "Счет: Пользователь " << userWins << " : " << computerWins << " Компьютер" << std::endl;
// Спрашиваем пользователя, хочет ли он продолжить игру
char playAgainChoice;
std::cout << "Хотите сыграть еще раз? (y/n): ";
std::cin >> playAgainChoice;
if (playAgainChoice != 'y' && playAgainChoice != 'Y') {
playAgain = false;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment