Odpowiedź :
Odpowiedź:
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (unsigned i = 0; i < v.size(); ++i) {
os << v[i];
if (i != v.size() - 1) os << ", ";
}
return os;
}
struct dice {
int first_dice;
int secound_dice;
int score() { return first_dice + secound_dice; }
dice roll() {
first_dice = rand() % 6 + 1;
secound_dice = rand() % 6 + 1;
return *this;
}
} d;
struct player {
int balance;
int score_to_win;
int current_score;
player() {
balance = 100;
score_to_win = 0;
current_score = 0;
}
} p;
struct game {
vector<int> first_win = {7, 11};
vector<int> first_los = {2, 3, 12};
vector<int> win = {4, 5, 6, 8, 9, 10};
vector<int> los = {7};
int state = 0;
void event_game(dice& d, player& p) {
d.roll();
if (state == 0 && count(first_win.begin(), first_win.end(), d.score())) {
state = 1;
p.balance += 20;
return;
}
if (state == 0 && count(first_los.begin(), first_los.end(), d.score())) {
state = 2;
p.balance -= 30;
return;
}
if (state == 0 && count(win.begin(), win.end(), d.score())) {
state = 3;
return;
}
if (state == 4) {
if (d.score() == p.score_to_win) {
p.balance += 20;
state = 5;
return;
}
if (d.score() != p.score_to_win) {
state = 4;
return;
}
if (d.score() == count(los.begin(), los.end(), d.score())) {
p.balance -= 30;
state = 6;
return;
}
}
return;
}
} g;
int main() {
srand(time(NULL));
cout << "Test Gry" << endl;
cout << "Suma oczek wygrywających w pierwszym rzucie: " << g.first_win
<< endl;
cout << "Suma oczek przegrywających w pierwszym rzucie: " << g.first_los
<< endl;
for (int i = 0; i < 10; i++) {
g.state = 0;
cout << "---ITERACJA NR " << i + 1 << " ---" << endl;
cout << "Stan konta: " << p.balance << endl;
cout << "Etap 1 - Pierwszy rzut" << endl;
g.event_game(d, p);
cout << "Wyrzucono " << d.first_dice << " oraz " << d.secound_dice << endl;
cout << "Suma wynosi " << d.score() << endl;
if (g.state == 1) {
cout << "Brawo wygrałeś natychmiastowo! 20zł" << endl;
p.balance += 20;
continue;
}
if (g.state == 2) {
cout << "Przegrałeś natychmiastowo! 30zł" << endl;
p.balance -= 30;
continue;
}
if (g.state == 3) {
cout << "Etap 2 - traf w sume = " << d.score() << endl;
g.state = 4;
p.score_to_win = d.score();
}
do {
g.event_game(d, p);
cout << "--losowanie--" << endl;
cout << "Stan konta " << p.balance << endl;
cout << "Wartość wygrywająca " << p.score_to_win << endl;
if (g.state == 4) {
cout << "Wylosowano: " << d.first_dice << " oraz " << d.secound_dice
<< endl;
cout << "Suma wynosi " << d.score() << endl;
cout << "losujemy dalej" << endl;
}
if (g.state == 5) {
cout << "Wylosowano: " << d.first_dice << " oraz " << d.secound_dice
<< endl;
cout << "Suma wynosi " << d.score() << endl;
cout << "Brawo trafiłeś! 20zł" << endl;
break;
}
if (g.state == 6) {
cout << "Wylosowano: " << d.first_dice << " oraz " << d.secound_dice
<< endl;
cout << "Suma wynosi " << d.score() << endl;
cout << "Przegrałeś! 30 zł " << endl;
if (p.balance < 30) {
cout << "Skończyła Ci się kasa";
return 0;
}
break;
}
} while (g.state != count(g.los.begin(), g.los.end(), d.score()));
cout << "===============" << endl;
cout << "Gra zakończona" << endl;
cout << "Stan konta: " << p.balance << "zł" << endl;
}
return 0;
}
Wyjaśnienie:
Czy chodziło o coś takiego ? Możliwe, że nie zrozumiałem do końca zasad. Jak co mogę przerobić.