-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
587 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
271 changes: 271 additions & 0 deletions
271
FileGatherer/Storage/9baea040-e015-4b9d-8424-8a6f3d8d4cba
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,271 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <ctime> | ||
#include <cstdlib> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
struct Card { | ||
string suit; | ||
string faceValue; | ||
}; | ||
|
||
class Deck { | ||
private: | ||
vector<Card> cards; | ||
|
||
public: | ||
Deck() { | ||
string suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"}; | ||
string faceValues[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; | ||
|
||
for (const auto &suit : suits) { | ||
for (const auto &faceValue : faceValues) { | ||
cards.push_back({suit, faceValue}); | ||
} | ||
} | ||
} | ||
|
||
void shuffle() { | ||
srand(time(nullptr)); | ||
for (int i = 0; i < cards.size(); ++i) { | ||
int randomIndex = rand() % cards.size(); | ||
swap(cards[i], cards[randomIndex]); | ||
} | ||
} | ||
|
||
void displayDeck() { | ||
for (const auto &card : cards) { | ||
cout << card.faceValue << " of " << card.suit << endl; | ||
} | ||
} | ||
|
||
Card drawCard() { | ||
if (cards.empty()) { | ||
string suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"}; | ||
string faceValues[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; | ||
|
||
for (const auto &suit : suits) { | ||
for (const auto &faceValue : faceValues) { | ||
cards.push_back({suit, faceValue}); | ||
} | ||
} | ||
shuffle(); | ||
} | ||
|
||
Card drawnCard = cards.back(); | ||
cards.pop_back(); | ||
return drawnCard; | ||
} | ||
}; | ||
|
||
//------------------------------------------------- | ||
|
||
int countPoints(const vector<Card>& hand) { | ||
int points = 0; | ||
int numAces = 0; | ||
|
||
for (const auto& card : hand) { | ||
if (card.faceValue == "Jack" || card.faceValue == "Queen" || card.faceValue == "King") { | ||
points += 10; | ||
} else if (card.faceValue == "Ace") { | ||
numAces++; | ||
points += 11; | ||
} else { | ||
points += stoi(card.faceValue); | ||
} | ||
} | ||
|
||
while (points > 21 && numAces > 0) { | ||
points -= 10; | ||
numAces--; | ||
} | ||
return points; | ||
} | ||
|
||
//------------------------------------------------- | ||
|
||
int dealerTurn(Deck& deck, std::vector<Card>& dealerHand) { | ||
int sum = 0; | ||
for (const auto& card : dealerHand) { | ||
if (card.faceValue == "Jack" || card.faceValue == "Queen" || card.faceValue == "King") { | ||
sum += 10; | ||
} else if (card.faceValue == "Ace") { | ||
sum += 11; | ||
} else { | ||
sum += std::stoi(card.faceValue); | ||
} | ||
} | ||
|
||
while (sum <= 17) { | ||
Card newCard = deck.drawCard(); | ||
dealerHand.push_back(newCard); | ||
sum = countPoints(dealerHand); | ||
} | ||
return sum; | ||
} | ||
void giveBotInfo(int botId, std::vector<Card>& playerHand, std::vector<Card>& dealerHand){ | ||
|
||
cout << botId<< endl; | ||
for (const auto& card : playerHand) { | ||
cout << card.faceValue.c_str() << "_"<<card.suit.c_str() << " "; | ||
} | ||
cout << "d: "; | ||
for (const auto& card : dealerHand) { | ||
cout << card.faceValue.c_str() << "_"<<card.suit.c_str()<<" "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
//------------------------------------------------- | ||
Deck deck; | ||
double countpoints(std::vector<int>& bot, std::vector<Card>& dealerHand){ //{0 handValue, 1 splitValue, 2 doubledBet, 3 surrendered, 4 insurance}; | ||
int dealer = countPoints(dealerHand); | ||
|
||
if (bot[3]) // jeśli się podda | ||
return -0.5; | ||
|
||
if (bot[4]) | ||
if(dealerHand.size() == 2 && dealer == 21) // ubezpieczenie | ||
return 0; | ||
else | ||
return -1.5; | ||
|
||
if (bot[1] != 0){ // jeśli jest 2 ręka | ||
double sum=0; | ||
if (bot[0] < 22) | ||
if (bot[0] > dealer || dealer > 21) | ||
sum += 1; | ||
else sum -=1; | ||
else sum -=1; | ||
|
||
|
||
if (bot[1] < 22) | ||
if (bot[1] > dealer || dealer > 21) | ||
sum += 1; | ||
else sum -=1; | ||
else sum -=1; | ||
return sum; | ||
} | ||
|
||
if ((bot[0] > dealer && bot[0] < 22) || (bot[0] < 22 && dealer > 21)) | ||
if (bot[2]) | ||
return 2; | ||
else return 1; | ||
else | ||
if (bot[2]) | ||
return -2; | ||
else return -1; | ||
} | ||
|
||
vector<int> servebot(int botId, std::vector<Card>& playerHand, std::vector<Card>& dealerHand){ | ||
if(playerHand.size()==0){ | ||
playerHand.push_back(deck.drawCard()); | ||
playerHand.push_back(deck.drawCard()); | ||
} | ||
|
||
int handValue = countPoints(playerHand); | ||
int splitValue = 0; | ||
int doubledBet = 0; | ||
int surrendered = 0; | ||
int insurance = 0; | ||
|
||
if(countPoints(playerHand) == 21) | ||
return {handValue, splitValue, doubledBet, surrendered, insurance}; | ||
|
||
giveBotInfo(botId, playerHand, dealerHand); | ||
string response; | ||
cin >> response; | ||
|
||
if (response == "hit") { | ||
while(response == "hit" && countPoints(playerHand) < 21){ | ||
playerHand.push_back(deck.drawCard()); | ||
giveBotInfo(botId, playerHand, dealerHand); | ||
cin >> response; | ||
} | ||
handValue = countPoints(playerHand); | ||
} else if (response == "double") { | ||
if(countPoints(playerHand) == 9 ||countPoints(playerHand) == 10 || countPoints(playerHand) == 11){ | ||
playerHand.push_back(deck.drawCard()); | ||
doubledBet = 1; | ||
} | ||
} else if (response == "split") { | ||
if (playerHand.size() == 2 && playerHand[0].faceValue == playerHand[1].faceValue) { | ||
std::vector<Card> splitHand1; | ||
std::vector<Card> splitHand2; | ||
splitHand1.push_back(playerHand[0]); | ||
splitHand2.push_back(playerHand[1]); | ||
splitHand1.push_back(deck.drawCard()); | ||
splitHand2.push_back(deck.drawCard()); | ||
|
||
giveBotInfo(botId, splitHand1, dealerHand); | ||
cin >> response; | ||
while(response == "hit"){ | ||
splitHand1.push_back(deck.drawCard()); | ||
giveBotInfo(botId, splitHand1, dealerHand); | ||
string response; | ||
cin >> response; | ||
} | ||
|
||
giveBotInfo(botId, splitHand2, dealerHand); | ||
cin >> response; | ||
while(response == "hit"){ | ||
splitHand2.push_back(deck.drawCard()); | ||
giveBotInfo(botId, splitHand2, dealerHand); | ||
string response; | ||
cin >> response; | ||
} | ||
|
||
handValue = countPoints(splitHand1); | ||
splitValue = countPoints(splitHand2); | ||
} | ||
} else if (response == "surrender") { | ||
surrendered = 1; | ||
} | ||
else if (response == "insurance") { | ||
if(countPoints(dealerHand) == 10 || countPoints(dealerHand) == 11) | ||
insurance = 1; | ||
} | ||
return {handValue, splitValue, doubledBet, surrendered, insurance}; | ||
} | ||
|
||
int main() { | ||
|
||
deck.shuffle(); | ||
double bot0money=10; | ||
double bot1money=10; | ||
std::vector<Card> dealerHand; | ||
int turn = 0; | ||
|
||
vector<int> bot0; | ||
vector<int> bot1; | ||
while((turn < 100 || bot0money != bot1money) && turn < 1000){ | ||
turn++; | ||
dealerHand.push_back(deck.drawCard()); | ||
|
||
vector<Card> playerHand1; | ||
vector<Card> playerHand2; | ||
|
||
bot0 = servebot(0, playerHand1, dealerHand); | ||
bot1 = servebot(1, playerHand2, dealerHand); | ||
|
||
dealerTurn(deck, dealerHand); | ||
|
||
bot0money += countpoints(bot0, dealerHand); | ||
bot1money += countpoints(bot1, dealerHand); | ||
|
||
if(bot0money < 1 || bot1money < 1) | ||
break; | ||
dealerHand = vector<Card>(); | ||
} | ||
|
||
|
||
cout << -1 << endl; | ||
if(bot0money > bot1money) | ||
cout << 0 << endl; | ||
else | ||
cout << 1 << endl; | ||
|
||
return 0; | ||
} |
112 changes: 112 additions & 0 deletions
112
FileGatherer/Storage/a626e863-d0c5-4da3-9c5a-eda55f8572d3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
using namespace std; | ||
|
||
struct Card { | ||
string suit; | ||
string faceValue; | ||
}; | ||
|
||
int countPoints(const vector<Card>& hand) { | ||
int points = 0; | ||
int numAces = 0; | ||
|
||
for (const auto& card : hand) { | ||
if (card.faceValue == "Jack" || card.faceValue == "Queen" || card.faceValue == "King") { | ||
points += 10; | ||
} else if (card.faceValue == "Ace") { | ||
numAces++; | ||
} else { | ||
try { | ||
int value = stoi(card.faceValue); | ||
points += value; | ||
} catch (const invalid_argument& e) { | ||
cerr << "Nieprawidłowa wartość karty: " << card.faceValue << endl;\ | ||
} | ||
} | ||
} | ||
while (points > 21 && numAces > 0) { | ||
points -= 10; | ||
numAces--; | ||
} | ||
return points; | ||
} | ||
|
||
bool isValidCard(const string& faceValue, const string& suit) { | ||
string acceptedFaceValues[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; | ||
string acceptedSuits[] = {"Hearts", "Diamonds", "Clubs", "Spades"}; | ||
|
||
bool isValidFaceValue = false; | ||
for (const string& acceptedValue : acceptedFaceValues) { | ||
if (faceValue == acceptedValue) { | ||
isValidFaceValue = true; | ||
break; | ||
} | ||
} | ||
|
||
bool isValidSuit = false; | ||
for (const string& acceptedSuit : acceptedSuits) { | ||
if (suit == acceptedSuit) { | ||
isValidSuit = true; | ||
break; | ||
} | ||
} | ||
|
||
return isValidFaceValue && isValidSuit; | ||
} | ||
|
||
int main() { | ||
string input; | ||
|
||
vector<Card> playerHand; | ||
vector<Card> dealerHand; | ||
|
||
while (true) { | ||
getline(cin, input); | ||
playerHand = vector<Card>(); | ||
dealerHand = vector<Card>(); | ||
size_t dealerIndex = input.find("d:"); | ||
string playerData = input.substr(0, dealerIndex); | ||
string dealerData = input.substr(dealerIndex + 2); | ||
|
||
size_t pos = 0; | ||
string delimiter = " "; | ||
while ((pos = playerData.find(delimiter)) != string::npos) { | ||
string cardStr = playerData.substr(0, pos); | ||
size_t underscorePos = cardStr.find("_"); | ||
string faceValue = cardStr.substr(0, underscorePos); | ||
string suit = cardStr.substr(underscorePos + 1); | ||
if (isValidCard(faceValue, suit)) { | ||
playerHand.push_back({suit, faceValue}); | ||
} | ||
playerData.erase(0, pos + delimiter.length()); | ||
} | ||
|
||
pos = 0; | ||
while ((pos = dealerData.find(delimiter)) != string::npos) { | ||
string cardStr = dealerData.substr(0, pos); | ||
size_t underscorePos = cardStr.find("_"); | ||
string faceValue = cardStr.substr(0, underscorePos); | ||
string suit = cardStr.substr(underscorePos + 1); | ||
if (isValidCard(faceValue, suit)) { | ||
dealerHand.push_back({suit, faceValue}); | ||
} | ||
dealerData.erase(0, pos + delimiter.length()); | ||
} | ||
|
||
size_t underscorePos = dealerData.find("_"); | ||
string faceValue = dealerData.substr(0, underscorePos); | ||
string suit = dealerData.substr(underscorePos + 1); | ||
if (isValidCard(faceValue, suit)) { | ||
dealerHand.push_back({suit, faceValue}); | ||
} | ||
if (countPoints(playerHand)>16) | ||
cout << "stand" << endl; | ||
else | ||
cout << "hit" << endl; | ||
} | ||
|
||
return 0; | ||
} | ||
|
Oops, something went wrong.