Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- class Game {
- public:
- virtual void play() = 0;
- };
- class RockPaperScissors : public Game {
- private:
- int playerChoice;
- int computerChoice;
- int playerWins = 0;
- int computerWins = 0;
- void display_choice(int choice) {
- switch(choice) {
- case 1: cout << "Papier"; break;
- case 2: cout << "Kamień"; break;
- case 3: cout << "Nożyce"; break;
- }
- }
- public:
- RockPaperScissors() {
- srand(time(0));
- }
- void get_player_choice() {
- cout << "Wybierz: 1. Papier, 2. Kamień, 3. Nożyce: ";
- cin >> playerChoice;
- }
- void play_round() {
- computerChoice = rand() % 3 + 1;
- get_player_choice();
- cout << "Wybrałeś: ";
- display_choice(playerChoice);
- cout << "\nKomputer wybrał: ";
- display_choice(computerChoice);
- cout << endl;
- if(playerChoice == computerChoice) {
- cout << "Remis!" << endl;
- } else if((playerChoice == 1 && computerChoice == 3) ||
- (playerChoice == 2 && computerChoice == 1) ||
- (playerChoice == 3 && computerChoice == 2)) {
- cout << "Wygrałeś rundę!" << endl;
- playerWins++;
- } else {
- cout << "Przegrałeś rundę!" << endl;
- computerWins++;
- }
- }
- void play() override {
- while(playerWins < 3 && computerWins < 3) {
- play_round();
- cout << "Wynik: Gracz - " << playerWins << ", Komputer - " << computerWins << endl << endl;
- }
- if(playerWins == 3) {
- cout << "Gratulacje! Wygrałeś całą grę!" << endl;
- } else {
- cout << "Niestety, przegrałeś całą grę." << endl;
- }
- }
- };
- class WarCardGame : public Game {
- private:
- int playerCard;
- int computerCard;
- void display_card(int card) {
- cout << card;
- }
- public:
- WarCardGame() {
- srand(time(0));
- playerCard = rand() % 10 + 1; // Losowanie karty od 1 do 10
- computerCard = rand() % 10 + 1;
- }
- void play() override {
- cout << "Twoja karta to: ";
- display_card(playerCard);
- cout << "\nKarta komputera to: ";
- display_card(computerCard);
- cout << endl;
- if(playerCard == computerCard) {
- cout << "Remis!" << endl;
- } else if(playerCard > computerCard) {
- cout << "Wygrałeś!" << endl;
- } else {
- cout << "Przegrałeś!" << endl;
- }
- }
- };
- int main() {
- RockPaperScissors rpsGame;
- WarCardGame warGame;
- cout << "Gra Papier Kamień Nożyce:" << endl;
- rpsGame.play();
- cout << "\nGra karciana Wojna:" << endl;
- warGame.play();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement