Initial
This commit is contained in:
51
Blackjack.cpp
Normal file
51
Blackjack.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
//
|
||||||
|
// Created by alex on 7/21/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Blackjack.h"
|
||||||
|
|
||||||
|
Blackjack::Blackjack(int numDecks) : deck(numDecks) { }
|
||||||
|
|
||||||
|
|
||||||
|
void Blackjack::play() {
|
||||||
|
std::vector<Card> playerCards;
|
||||||
|
std::vector<Card> dealerCards;
|
||||||
|
bool playerwin=false;
|
||||||
|
bool dealerwin=false;
|
||||||
|
|
||||||
|
//player gets 2 cards
|
||||||
|
playerCards.push_back(deck.draw());
|
||||||
|
playerCards.push_back(deck.draw());
|
||||||
|
displayCards(playerCards);
|
||||||
|
|
||||||
|
//dealer gets 2 cards
|
||||||
|
dealerCards.push_back(deck.draw());
|
||||||
|
dealerCards.push_back(deck.draw());
|
||||||
|
displayCards(dealerCards);
|
||||||
|
|
||||||
|
while (!playerwin && !dealerwin) {
|
||||||
|
if (sum_cards(playerCards) == 21)
|
||||||
|
playerwin=true;
|
||||||
|
if (sum_cards(dealerCards) == 21)
|
||||||
|
dealerwin=true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Blackjack::displayCards(std::vector<Card> &cards) {
|
||||||
|
for (const Card& card : cards) {
|
||||||
|
std::cout << card << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Blackjack::sum_cards(std::vector<Card> &cards) {
|
||||||
|
int sum = 0;
|
||||||
|
for (const Card& card : cards) {
|
||||||
|
sum+=card.getValue();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
37
Blackjack.h
Normal file
37
Blackjack.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
//
|
||||||
|
// Created by alex on 7/21/25.
|
||||||
|
//
|
||||||
|
/*
|
||||||
|
*Game loop:
|
||||||
|
*idk bets yet
|
||||||
|
* player draws cards, displayed
|
||||||
|
* dealer draws cards, display one (add flipped to card?)
|
||||||
|
* if blackjacks, win/lose/tie
|
||||||
|
* else
|
||||||
|
* hit, stand, [LATER] DD, Split
|
||||||
|
* hit gets card and adds and checks
|
||||||
|
* if over 21, lose[OR DOES DEALER DRAW? CHECK]
|
||||||
|
* if 21, stand
|
||||||
|
* once stand commanded
|
||||||
|
* dealer begins drawing until point where it dont, or bust
|
||||||
|
* compare, get winner
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef BLACKJACK_H
|
||||||
|
#define BLACKJACK_H
|
||||||
|
#include "GameDeck.h"
|
||||||
|
|
||||||
|
|
||||||
|
class Blackjack {
|
||||||
|
public:
|
||||||
|
Blackjack(int numDecks);
|
||||||
|
void play();
|
||||||
|
private:
|
||||||
|
void displayCards(std::vector<Card> &cards);
|
||||||
|
int sum_cards(std::vector<Card> &cards);
|
||||||
|
GameDeck deck;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //BLACKJACK_H
|
||||||
14
CMakeLists.txt
Normal file
14
CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
cmake_minimum_required(VERSION 4.0)
|
||||||
|
project(Blackjack)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
|
add_executable(Blackjack main.cpp
|
||||||
|
Deck.cpp
|
||||||
|
Deck.h
|
||||||
|
Card.cpp
|
||||||
|
Card.h
|
||||||
|
GameDeck.cpp
|
||||||
|
GameDeck.h
|
||||||
|
Blackjack.cpp
|
||||||
|
Blackjack.h)
|
||||||
62
Card.cpp
Normal file
62
Card.cpp
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
//
|
||||||
|
// Created by alex on 7/14/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Card.h"
|
||||||
|
|
||||||
|
Card::Card(Suit s, Rank r) : suit(s), rank(r) {
|
||||||
|
value = static_cast<int>(r) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Suit Card::getSuit() const {
|
||||||
|
return suit;
|
||||||
|
}
|
||||||
|
Rank Card::getRank() const {
|
||||||
|
return rank;
|
||||||
|
}
|
||||||
|
int Card::getValue() const {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
|
||||||
|
// Setter for changing value to 1 for ace?
|
||||||
|
|
||||||
|
// Overload operator<<
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Card& card) {
|
||||||
|
std::string rankString;
|
||||||
|
std::string clubString;
|
||||||
|
switch (card.getRank()) {
|
||||||
|
case Rank::Ace: rankString = "Ace"; break;
|
||||||
|
case Rank::Jack: rankString = "Jack"; break;
|
||||||
|
case Rank::Queen: rankString = "Queen"; break;
|
||||||
|
case Rank::King: rankString = "King"; break;
|
||||||
|
default: rankString = std::to_string(card.getValue()); break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
switch (card.getRank()) {
|
||||||
|
case Rank::Ace: rankString = "Ace"; break;
|
||||||
|
case Rank::Two: rankString = "2"; break;
|
||||||
|
case Rank::Three: rankString = "3"; break;
|
||||||
|
case Rank::Four: rankString = "4"; break;
|
||||||
|
case Rank::Five: rankString = "5"; break;
|
||||||
|
case Rank::Six: rankString = "6"; break;
|
||||||
|
case Rank::Seven: rankString = "7"; break;
|
||||||
|
case Rank::Eight: rankString = "8"; break;
|
||||||
|
case Rank::Nine: rankString = "9"; break;
|
||||||
|
case Rank::Ten: rankString = "10"; break;
|
||||||
|
case Rank::Jack: rankString = "Jack"; break;
|
||||||
|
case Rank::Queen: rankString = "Queen"; break;
|
||||||
|
case Rank::King: rankString = "King"; break;
|
||||||
|
}
|
||||||
|
switch (card.getSuit()) {
|
||||||
|
case Suit::Clubs: clubString = "Clubs"; break;
|
||||||
|
case Suit::Diamonds: clubString = "Diamonds"; break;
|
||||||
|
case Suit::Hearts: clubString = "Hearts"; break;
|
||||||
|
case Suit::Spades: clubString = "Spades"; break;
|
||||||
|
}
|
||||||
|
os << rankString << " of " << clubString;
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
42
Card.h
Normal file
42
Card.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
//
|
||||||
|
// Created by alex on 7/14/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef CARD_H
|
||||||
|
#define CARD_H
|
||||||
|
#include <array>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
enum class Suit {
|
||||||
|
Clubs, Diamonds, Hearts, Spades
|
||||||
|
};
|
||||||
|
enum class Rank {
|
||||||
|
Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,
|
||||||
|
Ten, Jack, Queen, King
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class Card {
|
||||||
|
public:
|
||||||
|
Card(Suit s, Rank r);
|
||||||
|
// Getters
|
||||||
|
Suit getSuit() const;
|
||||||
|
Rank getRank() const;
|
||||||
|
int getValue() const;
|
||||||
|
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const Card& card);
|
||||||
|
|
||||||
|
// These arrays exist to store all types of cards
|
||||||
|
static std::array<Suit, 4> constexpr Suits = {Suit::Clubs, Suit::Diamonds, Suit::Hearts, Suit::Spades};
|
||||||
|
static std::array<Rank, 13> constexpr Ranks = {Rank::Ace, Rank::Two, Rank::Three, Rank::Four, Rank::Five, Rank::Six,
|
||||||
|
Rank::Seven, Rank::Eight, Rank::Nine, Rank::Ten, Rank::Jack, Rank::Queen, Rank::King};
|
||||||
|
|
||||||
|
private:
|
||||||
|
Suit suit;
|
||||||
|
Rank rank;
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //CARD_H
|
||||||
52
Deck.cpp
Normal file
52
Deck.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
//
|
||||||
|
// Created by alex on 7/14/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Deck.h"
|
||||||
|
|
||||||
|
Deck::Deck() : Deck(false) { }
|
||||||
|
|
||||||
|
|
||||||
|
Deck::Deck(bool shuffled) {
|
||||||
|
initialize_deck();
|
||||||
|
if (shuffled) {
|
||||||
|
shuffle();
|
||||||
|
}
|
||||||
|
cards_in_deck = deck.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Deck::initialize_deck() {
|
||||||
|
for (auto r : Card::Ranks) {
|
||||||
|
for (auto c : Card::Suits) {
|
||||||
|
deck.emplace_back(c, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Deck::print_deck() {
|
||||||
|
std::cout << deck.size() << std::endl;
|
||||||
|
for (auto c : deck) {
|
||||||
|
std::cout << c << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Deck::shuffle() {
|
||||||
|
std::random_device rd;
|
||||||
|
std::mt19937 g(rd());
|
||||||
|
|
||||||
|
std::ranges::shuffle(deck, g);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return The card drawn from the top of the deck
|
||||||
|
*/
|
||||||
|
Card Deck::draw() {
|
||||||
|
const Card temp = deck.at(cards_in_deck - 1);
|
||||||
|
deck.pop_back();
|
||||||
|
cards_in_deck--;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
46
Deck.h
Normal file
46
Deck.h
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
#ifndef DECK_H
|
||||||
|
#define DECK_H
|
||||||
|
#include <random>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
|
#include <vector>
|
||||||
|
#include "Card.h"
|
||||||
|
|
||||||
|
|
||||||
|
// Standard deck with 52 cards
|
||||||
|
|
||||||
|
class Deck {
|
||||||
|
public:
|
||||||
|
Deck();
|
||||||
|
|
||||||
|
Deck(bool shuffled);
|
||||||
|
|
||||||
|
void print_deck();
|
||||||
|
|
||||||
|
Card draw();
|
||||||
|
|
||||||
|
void shuffle();
|
||||||
|
|
||||||
|
auto begin() { return deck.begin(); }
|
||||||
|
|
||||||
|
auto end() { return deck.end(); }
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
int get_num_cards() { return cards_in_deck; }
|
||||||
|
private:
|
||||||
|
void initialize_deck();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int cards_in_deck;
|
||||||
|
std::vector<Card> deck;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //DECK_H
|
||||||
19
GameDeck.cpp
Normal file
19
GameDeck.cpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Created by alex on 7/17/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "GameDeck.h"
|
||||||
|
|
||||||
|
// THIS HAS LAZY FIX FOR DECK CONSTRUCTOR BEING CALLED
|
||||||
|
|
||||||
|
GameDeck::GameDeck(int numDecks) {
|
||||||
|
for (int i = 1; i < numDecks; i++) {
|
||||||
|
Deck tempDeck(true);
|
||||||
|
|
||||||
|
for (Card &c : tempDeck) {
|
||||||
|
deck.push_back(std::move(c));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cards_in_deck = deck.size();
|
||||||
|
}
|
||||||
|
|
||||||
20
GameDeck.h
Normal file
20
GameDeck.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// Created by alex on 7/17/25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef GAMEDECK_H
|
||||||
|
#define GAMEDECK_H
|
||||||
|
#include "Deck.h"
|
||||||
|
|
||||||
|
class GameDeck : public Deck {
|
||||||
|
public:
|
||||||
|
GameDeck(int numDecks);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //GAMEDECK_H
|
||||||
39
main.cpp
Normal file
39
main.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Blackjack.h"
|
||||||
|
#include "Card.h"
|
||||||
|
#include "Deck.h"
|
||||||
|
#include "GameDeck.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* A deck class which has
|
||||||
|
* - Cards in deck in vector
|
||||||
|
* - Shuffle deck function
|
||||||
|
* - A map of cards to numerical values
|
||||||
|
*
|
||||||
|
* A Game class
|
||||||
|
* - Adds decks to game and calls their shuffles
|
||||||
|
*
|
||||||
|
* Blackjack class, not sure hierarchy yet
|
||||||
|
* - has a gamedeck
|
||||||
|
* - game() method with main loop
|
||||||
|
* -
|
||||||
|
*
|
||||||
|
* Game deck is-a deck, inherits functions, difference is constructor takes amount of decks,
|
||||||
|
* creates that many, and slaps them into one bigger deck
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Blackjack bj(4);
|
||||||
|
bj.play();
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user