박현우

black jack auto finished

#include "Header.h"
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <map>
using namespace std;
Player::Player() :score(0), stop(false){};
Player::~Player() {};
void Player::sum(Card card, map<int, int>& rule) { score += rule[card.symbol]; }
void Player::get_card(Card card) {
hand.push_back(card);
score += card.symbol;
}
void Player::finish() { stop = true; }
Table::Table() :Alpha(), Dealer() {
for (int i = 0; i != 13; ++i) {
if (i == 0 || i == 9 || i == 10 || i == 11 || i == 12) {
Rule[i] = 10;
continue;
}
Rule[i] = (i+1);
}
};
Table::~Table() {};
void Table::create_deck() {
Card card;
for (int i = 0; i != 4; i++)
for (int j = 0; j != 13; j++) {
card.suit = i;
card.symbol = j;
Deck.push_back(card);
}
}
void Table::shuffle_card() {
random_device rd;
shuffle(Deck.begin(), Deck.end(), default_random_engine(rd()));
}
void Table::give_card(vector<Card>& hand) {
auto card = Deck.back();
Deck.pop_back();
hand.push_back(card);
}
\ No newline at end of file
......
#pragma once
#include <vector>
#include <list>
#include <map>
using namespace std;
enum class Suit { Diamonds, Hearts, Clubs, Spades };
enum class Symbol { Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
struct Card {
int suit; // Spade = 0, Dia = 1, Heart = 2, Clove = 3
int symbol; // a = 0, 2 = 1 ... 10 = 9, J = 10, Q = 11, K = 12
};
class Player {
public:
vector<Card> hand;
int score;
bool stop;
public:
Player();
~Player();
void sum(Card card, map<int, int>& rule);
void get_card(Card card);
void finish();
};
class Table {
public:
map<int, int> Rule;
vector<Card> Deck;
Player Alpha;
Player Dealer;
public:
Table();
~Table();
void create_deck();
void shuffle_card();
void give_card(vector<Card>& hand);
//void judge();
};
\ No newline at end of file
......
#include "Header.h"
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <functional>
using namespace std;
enum class State { lose, win, draw };
struct game_result {
Player Alpha;
Player Dealer;
State win;
};
game_result game() {
Table table;
auto alpha = table.Alpha;
auto dealer = table.Dealer;
auto& rule = table.Rule;
table.create_deck();
table.shuffle_card();
table.give_card(alpha.hand);
table.give_card(dealer.hand);
table.give_card(alpha.hand);
table.give_card(dealer.hand);
alpha.sum(alpha.hand[0], rule);
alpha.sum(alpha.hand[1], rule);
dealer.sum(dealer.hand[0], rule);
dealer.sum(dealer.hand[1], rule);
// machine will substitute decision part
auto decision_maker = bind(uniform_int_distribution<>{0, 2}, default_random_engine{});
while (true) {
bool decision = decision_maker();
if (decision) {
table.give_card(alpha.hand);
alpha.sum(alpha.hand.back(), rule);
}
else
alpha.finish();
if (dealer.score <= 16) {
table.give_card(dealer.hand);
dealer.sum(dealer.hand.back(), rule);
if (dealer.score > 21)
return { alpha, dealer, State::win };
}
else
dealer.finish();
if (alpha.score > 21)
return { alpha, dealer, State::lose };
if (alpha.stop && dealer.stop) {
if (alpha.score > dealer.score)
return { alpha, dealer, State::win };
else if (alpha.score = dealer.score)
return { alpha, dealer, State::draw };
else
return { alpha, dealer, State::lose };
}
}
}
int main() {
auto result = game();
for (auto& x : result.Alpha.hand)
cout << "Alpha : " << x.suit << "-" << x.symbol << endl;
cout << "Alpha Score : " << result.Alpha.score << endl;
for (auto& x : result.Dealer.hand)
cout << "Dealer : " << x.suit << "-" << x.symbol << endl;
cout << "Dealer Score : " << result.Dealer.score << endl;
cout << "Result : " << static_cast<int>(result.win) << endl;
return 0;
}
\ No newline at end of file
......