Created
September 11, 2023 21:47
-
-
Save xsa-dev/adfcd614ce0193d6231d7bce788b1cdf to your computer and use it in GitHub Desktop.
cpp tictactoe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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