박현우

black jack auto finished

1 +#include "Header.h"
2 +#include <iostream>
3 +#include <vector>
4 +#include <random>
5 +#include <algorithm>
6 +#include <iterator>
7 +#include <numeric>
8 +#include <map>
9 +using namespace std;
10 +
11 +Player::Player() :score(0), stop(false){};
12 +Player::~Player() {};
13 +
14 +void Player::sum(Card card, map<int, int>& rule) { score += rule[card.symbol]; }
15 +
16 +void Player::get_card(Card card) {
17 + hand.push_back(card);
18 + score += card.symbol;
19 +}
20 +
21 +void Player::finish() { stop = true; }
22 +
23 +
24 +Table::Table() :Alpha(), Dealer() {
25 + for (int i = 0; i != 13; ++i) {
26 + if (i == 0 || i == 9 || i == 10 || i == 11 || i == 12) {
27 + Rule[i] = 10;
28 + continue;
29 + }
30 + Rule[i] = (i+1);
31 + }
32 +};
33 +Table::~Table() {};
34 +
35 +void Table::create_deck() {
36 + Card card;
37 + for (int i = 0; i != 4; i++)
38 + for (int j = 0; j != 13; j++) {
39 + card.suit = i;
40 + card.symbol = j;
41 +
42 + Deck.push_back(card);
43 + }
44 +}
45 +void Table::shuffle_card() {
46 + random_device rd;
47 + shuffle(Deck.begin(), Deck.end(), default_random_engine(rd()));
48 +}
49 +void Table::give_card(vector<Card>& hand) {
50 + auto card = Deck.back();
51 + Deck.pop_back();
52 +
53 + hand.push_back(card);
54 +}
...\ No newline at end of file ...\ No newline at end of file
......
1 #pragma once 1 #pragma once
2 +#include <vector>
3 +#include <list>
4 +#include <map>
5 +using namespace std;
6 +
7 +enum class Suit { Diamonds, Hearts, Clubs, Spades };
8 +enum class Symbol { Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
9 +
10 +struct Card {
11 + int suit; // Spade = 0, Dia = 1, Heart = 2, Clove = 3
12 + int symbol; // a = 0, 2 = 1 ... 10 = 9, J = 10, Q = 11, K = 12
13 +};
14 +
15 +class Player {
16 +public:
17 + vector<Card> hand;
18 + int score;
19 + bool stop;
20 +public:
21 + Player();
22 + ~Player();
23 +
24 + void sum(Card card, map<int, int>& rule);
25 + void get_card(Card card);
26 + void finish();
27 +};
28 +
29 +class Table {
30 +public:
31 + map<int, int> Rule;
32 + vector<Card> Deck;
33 + Player Alpha;
34 + Player Dealer;
35 +public:
36 + Table();
37 + ~Table();
38 +
39 + void create_deck();
40 + void shuffle_card();
41 + void give_card(vector<Card>& hand);
42 + //void judge();
43 +};
...\ No newline at end of file ...\ No newline at end of file
......
1 +#include "Header.h"
2 +#include <iostream>
3 +#include <vector>
4 +#include <random>
5 +#include <algorithm>
6 +#include <functional>
7 +using namespace std;
8 +
9 +enum class State { lose, win, draw };
10 +
11 +struct game_result {
12 + Player Alpha;
13 + Player Dealer;
14 + State win;
15 +};
16 +
17 +game_result game() {
18 + Table table;
19 + auto alpha = table.Alpha;
20 + auto dealer = table.Dealer;
21 + auto& rule = table.Rule;
22 +
23 + table.create_deck();
24 + table.shuffle_card();
25 +
26 + table.give_card(alpha.hand);
27 + table.give_card(dealer.hand);
28 + table.give_card(alpha.hand);
29 + table.give_card(dealer.hand);
30 +
31 + alpha.sum(alpha.hand[0], rule);
32 + alpha.sum(alpha.hand[1], rule);
33 + dealer.sum(dealer.hand[0], rule);
34 + dealer.sum(dealer.hand[1], rule);
35 +
36 + // machine will substitute decision part
37 + auto decision_maker = bind(uniform_int_distribution<>{0, 2}, default_random_engine{});
38 +
39 + while (true) {
40 + bool decision = decision_maker();
41 +
42 + if (decision) {
43 + table.give_card(alpha.hand);
44 + alpha.sum(alpha.hand.back(), rule);
45 + }
46 + else
47 + alpha.finish();
48 +
49 + if (dealer.score <= 16) {
50 + table.give_card(dealer.hand);
51 + dealer.sum(dealer.hand.back(), rule);
52 + if (dealer.score > 21)
53 + return { alpha, dealer, State::win };
54 + }
55 + else
56 + dealer.finish();
57 +
58 + if (alpha.score > 21)
59 + return { alpha, dealer, State::lose };
60 +
61 +
62 + if (alpha.stop && dealer.stop) {
63 + if (alpha.score > dealer.score)
64 + return { alpha, dealer, State::win };
65 + else if (alpha.score = dealer.score)
66 + return { alpha, dealer, State::draw };
67 + else
68 + return { alpha, dealer, State::lose };
69 + }
70 + }
71 +}
72 +
73 +
74 +int main() {
75 + auto result = game();
76 +
77 + for (auto& x : result.Alpha.hand)
78 + cout << "Alpha : " << x.suit << "-" << x.symbol << endl;
79 + cout << "Alpha Score : " << result.Alpha.score << endl;
80 + for (auto& x : result.Dealer.hand)
81 + cout << "Dealer : " << x.suit << "-" << x.symbol << endl;
82 + cout << "Dealer Score : " << result.Dealer.score << endl;
83 + cout << "Result : " << static_cast<int>(result.win) << endl;
84 +
85 + return 0;
86 +}
...\ No newline at end of file ...\ No newline at end of file
......