Skip to content
Snippets Groups Projects
Commit ac9d9edb authored by Jakub Oliberius's avatar Jakub Oliberius
Browse files

game loop handeling

parent 9ecbd0a7
No related branches found
No related tags found
1 merge request!1Game
......@@ -4,31 +4,16 @@
Game::Game() : ui(nullptr), playerTurnComplete(players.size(), false) {
deck.shuffle();
ui = new UI(this);
getPlayersInfo();
begin();
}
void Game::getPlayersInfo() {
int numPlayers;
std::cout << "Enter the number of players (up to 4): ";
std::cin >> numPlayers;
// Validate the number of players
while (numPlayers < 1 || numPlayers > 4) {
std::cerr << "Invalid number of players. Please enter a number between 1 and 4." << std::endl << std::flush;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter the number of players (up to 4): " << std::flush;
std::cin >> numPlayers;
std::cout.flush();
}
for (int i = 0; i < numPlayers; ++i) {
std::string playerName;
std::cout << "Enter name for Player " << i + 1 << ": ";
std::cin >> playerName;
addPlayer(playerName);
}
void Game::begin() {
ui->beginning();
dealInitialCards();
play();
}
void Game::getPlayersInfo() {
dealInitialCards();
play();
}
......@@ -62,12 +47,38 @@ void Game::dealerTurn() {
}
void Game::play() {
bool end = false;
// Start threads for player and dealer turns
// Wait for threads to finish
ui->draw();
while (!end) {
bool allPlayersStanding = true;
for (auto& player: players) {
if (!player.isStanding() && player.getHand().getHandValue() < 21) {
if (ui->playerMove(player)) {
drawCard(player);
allPlayersStanding = false;
} else
player.stand();
}
ui->draw();
}
if (allPlayersStanding) {
while (dealer.getHand().getHandValue() < 17) {
drawCard(dealer);
}
ui->draw();
end = true;
}
}
for (auto player : players) {
if (player.getHand().getHandValue() < dealer.getHand().getHandValue() ||
player.getHand().getHandValue() > 21)
std::cout << player.getName() << " ";
} std::cout << "lost";
// Additional game logic, determine the winner, etc.
}
......
......@@ -32,6 +32,7 @@ public:
void compute();
bool get_quit();
void input();
void begin();
private:
Deck deck;
Player dealer;
......
......@@ -16,11 +16,11 @@ size_t Hand::getHandValue() {
// Add Ace values to the total, counting them as 11 until it would exceed 21
for (size_t i = 0; i < acesInHand; ++i) {
if (handValue + 11 <= 21)
if (handValue + 11 <= 21) {
handValue += 11;
else
} else if (handValue + 11 > 21) {
handValue += 1;
}
}
return handValue;
......
......@@ -15,3 +15,11 @@ void Player::setName(const std::string& playerName) {
std::string &Player::getName() {
return name;
}
bool Player::isStanding() {
return standing;
}
void Player::stand() {
standing = true;
}
......@@ -13,11 +13,13 @@ public:
Hand & getHand();
void setName(const std::string& playerName);
std::string& getName();
bool isStanding();
void stand();
private:
std::string name;
Hand hand;
bool stand = false;
bool standing = false;
};
......
#include "UI.h"
#include "windows.devices.enumeration.h"
//#include "windows.devices.enumeration.h"
#include <iostream>
#include <string>
......@@ -9,6 +9,29 @@ void UI::setGame(Game* gamePtr) {
game = gamePtr;
}
void UI::beginning() {
int numPlayers;
std::cout << "Enter the number of players (up to 4): ";
std::cin >> numPlayers;
// Validate the number of players
while (numPlayers < 1 || numPlayers > 4) {
std::cerr << "Invalid number of players. Please enter a number between 1 and 4." << std::endl << std::flush;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter the number of players (up to 4): " << std::flush;
std::cin >> numPlayers;
std::cout.flush();
}
for (int i = 0; i < numPlayers; ++i) {
std::string playerName;
std::cout << "Enter name for Player " << i + 1 << ": ";
std::cin >> playerName;
game->addPlayer(playerName);
}
}
void UI::draw() {
printCards();
control();
......@@ -17,7 +40,10 @@ void UI::draw() {
void UI::printCards() {
auto dealer = game->getDealer();
for (auto player : game->getPlayers()) {
std::cout << player.getName() << ": " << player.getHand().toString() << std::endl;
std::cout << player.getName() << ": " << player.getHand().toString();
if (player.isStanding())
std::cout << " *STANDING*";
std::cout <<std::endl;
}
std::cout << "Dealer: " << dealer.getHand().toString() << std::endl;
}
......@@ -35,3 +61,25 @@ bool UI::getQuit() const {
UI::~UI() {
// Your cleanup code, if any
}
bool UI::playerMove(Player &player) {
std::string move;
while (true) {
std::cout << player.getName() << "'s turn: ";
std::cin >> move;
// Convert the input to uppercase for case-insensitive comparison
std::transform(move.begin(), move.end(), move.begin(), ::toupper);
if (move == "H" || move == "HIT") {
return true;
} else if (move == "S" || move == "STAND") {
return false;
} else {
std::cerr << "Invalid move. Please enter 'H' for hit or 'S' for stand." << std::endl << std::flush;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
......@@ -24,13 +24,16 @@ public:
// Check if the player wants to quit
bool getQuit() const;
void beginning();
bool playerMove(Player& player);
// ... (you can add more UI-related functions)
// Destructor
~UI();
private:
Game *game; // Reference to the Game object
};
#endif //BLACKJACK_UI_H
\ No newline at end of file
......@@ -5,7 +5,7 @@
#include <future>
#include <condition_variable>
#include <vector>
#include "windows.devices.enumeration.h"
//#include "windows.devices.enumeration.h"
#include "Card.h"
#include "Hand.h"
#include "Deck.h"
......@@ -13,7 +13,7 @@
#include "UI.h"
int main(/*int argc, char *argv[]*/) {
SetConsoleOutputCP(CP_UTF8);
// SetConsoleOutputCP(CP_UTF8);
//
// auto param = ArgParser::parseProgramArguments(argc, argv);
// tic_tac_toe PP(param.width, param.height, param.count);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment