mercredi 4 décembre 2019

C++ - Custom Object Not Updating as Expected

Please note -- I am trying to post a minimal, complete, verifiable example. If more code is needed, please tell me and I will post.

I have a 5-card draw Poker application. I am attempting to do the following:

For any number of players (2-7), shuffle the deck, and deal out the cards. Take the bets. Compare the hand. Update the winner's money balance, and keep playing until all but one player is out of money. "Out of money" can be defined as having a balance of 0 or less.

My code is below:

#include <iostream>
#include <random>
#include <vector>
#include <ctime>

#include "Card.h"
#include "Deck.h"
#include "Hand.h"
#include "Player.h"
#include "Utilities.h"

using namespace std;

int main(){
    srand(time(NULL));
    int ante = 50;
    int pool = 0;

    Deck myDeck;
    vector<Card> theDeck = myDeck.getDeck();
    myDeck.shuffleDeck(theDeck);

    Player playerOne("tony", 1);
    Player playerTwo("comp", 1);

    vector<Player> players;
    players.push_back(playerOne);
    players.push_back(playerTwo);

    int iter = 0;
    Hand best = Hand();
    Player bestPlayer = Player();

    while(players.size() > 1){
        best = players[0].getHand();
        for(unsigned int i = 0; i < players.size(); i++){
            cout << "ROUND " << iter << endl;
            cout << " --------------------------- " << "\n" << endl;
            cout << "My name is: " << players[i].getName() << ", I am a " << players[i].getType() << " and my balance is: " << players[i].getBalance() << "\n" << endl;
            players[i].fillHand(theDeck);
            players[i].placeBet(ante, pool); 

            cout << "My current hand is: " << endl;
            players[i].getHand().printHand();
            cout << "\n" << endl;

            players[i].tradeCards(theDeck);
            cout << "My new hand is: " << endl;
            players[i].getHand().printHand();
            cout << "\n" << endl;

            if(players[i].getHand().highHand() > best.highHand()){
                cout << players[i].getName() << " has the high hand. " << endl;
                best = players[i].getHand();
                bestPlayer = players[i];

            }else if (players[i].getHand().highHand() < best.highHand()){
                cout << players[i].getName() << " loses to the high hand. " << endl;
            }else{
                int result = compareHands(players[i].getHand(), best);
                cout << " RESULT: " << result << endl;
                if(result == 1){
                    best = players[i].getHand();
                    bestPlayer = players[i];
                }else if (result == 3){
                    cout << "There was a tie" << endl;
                }else{
                    continue;
                } 
            } 
        } 

        bestPlayer.updateBalance(pool);
        checkPlayers(players);
        pool = 0;
        iter += 1;
    } 
    cout << players[0].getName() << " wins! " << endl;
    return 0;
} 

This produces the following for a result:

ROUND 0
 ---------------------------

My name is: tony, I am a bot and my balance is: 100

My current hand is:
AC AD 7H 2C QD

tony decided to trade 3 cards

My new hand is:
9H 9S KH 2C QD

tony loses to the high hand.
ROUND 0
 ---------------------------

My name is: comp, I am a bot and my balance is: 100

My current hand is:
8C 9D 4D AS 5C

comp decided not to trade cards

My new hand is:
8C 9D 4D AS 5C

comp loses to the high hand.
ROUND 1
 ---------------------------

My name is: tony, I am a bot and my balance is: 50

My current hand is:
3S 2D 6C 7S 6S

tony decided to trade 3 cards

My new hand is:
2S 4S AH 7S 6S

tony loses to the high hand.
ROUND 1
 ---------------------------

My name is: comp, I am a bot and my balance is: 50

My current hand is:
4H KD 10S 8D 3D

comp decided not to trade cards

My new hand is:
4H KD 10S 8D 3D

comp loses to the high hand.
comp wins!

If you observe ROUND 1 - tony object won the first round. So that balance should be 100, while the comp balance should be 50. Yet, tony reports a balance of 50.

So the bestPlayer.updateBalance(pool); line is failing but I cannot understand why.

Can somebody help me understand why this function is not performing as expected?

For reference, my **`Player.h`** class is contained below:
#pragma once
#include "Hand.h"
#include "Card.h"
#include <vector>
#include <array>
using namespace std;

class Player
{
    public:
    explicit Player(string name, int type){
        this->name = name;
        this->balance = 100; 
        this->type = type;
    } 

    Player() { };

    void tradeCards(vector<Card> &deck){       
        if(this->type == 1){           
            int decision = decide();            
            if(decision == 1){                
                int trades = rand() % 3 + 1;                
                cout << this->name << " decided to trade " << trades << " cards" << "\n" << endl;                
                for (unsigned int i = 0; i < trades; i++){                     
                    playerHand.addCardToHand(deck[i], i);                    
                    deck.erase(deck.begin() + i);
                } 

            }else{
                cout << this->name << " decided not to trade cards" << "\n" << endl;
            }         
        }else{
            string input;
            vector<int> trades;
            int curr;            
            cout << "Please enter the indices you want to switch cards for. If you don't want a switch, simply type x" << endl;            
            cin >> input;
            for(char& c : input){                
                if(c == 'x'){
                    return;                
                }else{                    
                    curr = (int)c - 48;                    
                    trades.push_back(curr);
                } 
            } 
            for (unsigned int i = 0; i < trades.size(); i++){                 
                playerHand.addCardToHand(deck[i], trades[i]);                
                deck.erase(deck.begin() + i);
            } 
        } 
    } 

    int getBalance(){
        return this->balance;
    } 

    string getType(){       
        if(this->type == 1){          
            return "bot";       
        }else{           
            return "human";
        } 
    } 

    Hand getHand(){
        return this->playerHand;
    } 

    string getName(){
        return this->name;
    } 

    void fillHand(vector<Card> &deck){
        for (unsigned int i = 0; i < 5; i++){
            Card curr = deck[i];
            playerHand.addCardToHand(curr, i);
            deck.erase(deck.begin() + i);
        } 
        playerHand.convertHandtoInt();
    } 

   void updateBalance(int const&n){
        this->balance += n;
    } 

   void placeBet(int &ante, int &pool){
        pool += ante;
        updateBalance(-ante);
    } 

    protected:
    int decide(){
        return rand() % 2;
    } 
    int balance, type;
    Hand playerHand;
    string name;
}; 

Aucun commentaire:

Enregistrer un commentaire