SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | #include <ctime> | |
3 | #include <cstdlib> | |
4 | using namespace std; | |
5 | class Game { | |
6 | public: | |
7 | virtual void play() = 0; | |
8 | }; | |
9 | class RockPaperScissors : public Game { | |
10 | private: | |
11 | int playerChoice; | |
12 | int computerChoice; | |
13 | int playerWins = 0; | |
14 | int computerWins = 0; | |
15 | - | void displayChoice(int choice) { |
15 | + | void display_choice(int choice) { |
16 | switch(choice) { | |
17 | case 1: cout << "Papier"; break; | |
18 | case 2: cout << "Kamień"; break; | |
19 | case 3: cout << "Nożyce"; break; | |
20 | } | |
21 | } | |
22 | public: | |
23 | RockPaperScissors() { | |
24 | srand(time(0)); | |
25 | } | |
26 | - | void getPlayerChoice() { |
26 | + | void get_player_choice() { |
27 | cout << "Wybierz: 1. Papier, 2. Kamień, 3. Nożyce: "; | |
28 | cin >> playerChoice; | |
29 | } | |
30 | - | void playRound() { |
30 | + | void play_round() { |
31 | computerChoice = rand() % 3 + 1; | |
32 | - | getPlayerChoice(); |
32 | + | get_player_choice(); |
33 | cout << "Wybrałeś: "; | |
34 | - | displayChoice(playerChoice); |
34 | + | display_choice(playerChoice); |
35 | cout << "\nKomputer wybrał: "; | |
36 | - | displayChoice(computerChoice); |
36 | + | display_choice(computerChoice); |
37 | cout << endl; | |
38 | if(playerChoice == computerChoice) { | |
39 | cout << "Remis!" << endl; | |
40 | } else if((playerChoice == 1 && computerChoice == 3) || | |
41 | (playerChoice == 2 && computerChoice == 1) || | |
42 | (playerChoice == 3 && computerChoice == 2)) { | |
43 | cout << "Wygrałeś rundę!" << endl; | |
44 | playerWins++; | |
45 | } else { | |
46 | cout << "Przegrałeś rundę!" << endl; | |
47 | computerWins++; | |
48 | } | |
49 | } | |
50 | void play() override { | |
51 | while(playerWins < 3 && computerWins < 3) { | |
52 | - | playRound(); |
52 | + | play_round(); |
53 | cout << "Wynik: Gracz - " << playerWins << ", Komputer - " << computerWins << endl << endl; | |
54 | } | |
55 | if(playerWins == 3) { | |
56 | cout << "Gratulacje! Wygrałeś całą grę!" << endl; | |
57 | } else { | |
58 | cout << "Niestety, przegrałeś całą grę." << endl; | |
59 | } | |
60 | } | |
61 | }; | |
62 | class WarCardGame : public Game { | |
63 | private: | |
64 | int playerCard; | |
65 | int computerCard; | |
66 | ||
67 | - | void displayCard(int card) { |
67 | + | void display_card(int card) { |
68 | cout << card; | |
69 | } | |
70 | ||
71 | public: | |
72 | WarCardGame() { | |
73 | srand(time(0)); | |
74 | playerCard = rand() % 10 + 1; // Losowanie karty od 1 do 10 | |
75 | computerCard = rand() % 10 + 1; | |
76 | } | |
77 | ||
78 | void play() override { | |
79 | cout << "Twoja karta to: "; | |
80 | - | displayCard(playerCard); |
80 | + | display_card(playerCard); |
81 | cout << "\nKarta komputera to: "; | |
82 | - | displayCard(computerCard); |
82 | + | display_card(computerCard); |
83 | cout << endl; | |
84 | ||
85 | if(playerCard == computerCard) { | |
86 | cout << "Remis!" << endl; | |
87 | } else if(playerCard > computerCard) { | |
88 | cout << "Wygrałeś!" << endl; | |
89 | } else { | |
90 | cout << "Przegrałeś!" << endl; | |
91 | } | |
92 | } | |
93 | }; | |
94 | ||
95 | int main() { | |
96 | RockPaperScissors rpsGame; | |
97 | WarCardGame warGame; | |
98 | ||
99 | cout << "Gra Papier Kamień Nożyce:" << endl; | |
100 | rpsGame.play(); | |
101 | cout << "\nGra karciana Wojna:" << endl; | |
102 | warGame.play(); | |
103 | ||
104 | return 0; | |
105 | } |