0

I have 3 Player objects that I have passed as reference arguments in a function. The function does the following:

  1. Creates a vector of the 3 objects and bubble sorts the them based on their score (data members).
  2. Then it checks for first, second or third ranks and returns a resized vector.

In main function, I am able to print the winner and her reward when I iterate through the vector but it doesn't seem to update the actual objects. How can I update the objects?

This is what I have so far:

int enterBet(int balance, int maxBet);
vector <Player> chkWinner(Player &a, Player &b, Player &c, int pot);

bool sorting_method(Player &_1, Player &_2) {
    //Bigger numbers go first
    return _1.m_total > _2.m_total;
}

int main()
{
    Player *player1 = new Player;
    Player *player2 = new Player;
    Player *player3 = new Player;
    vector <Player> winnerArr;
    winnerArr.resize(3);
    int pot = 0;
    int betAmount = 0;
    int maxBet = 10;
    player2->m_name = "Kelly";
    player3->m_name = "Martha";
    int x, y, z;

    winnerArr[0] = *player1;
    winnerArr[1] = *player2;
    winnerArr[2] = *player3;

    sort(winnerArr.begin(), winnerArr.end(), sorting_method);
    cout << winnerArr[0].m_total << ", "
        << winnerArr[1].m_total << ", "
        << winnerArr[2].m_total << endl;

    x = winnerArr[0].m_total;
    y = winnerArr[1].m_total;
    z = winnerArr[2].m_total;

    if (x == y && x == z){
        cout << "Divide pot by 3" << endl;
        //totalArr.resize(3);
    }
    else if (x == y && x != z){
        cout << "Divide pot by 2" << endl;
        winnerArr.resize(2);

    }
    else{
        cout << "Pay full Pot" << endl;
        winnerArr.resize(1);
    }

    int payout;

    if (winnerArr.size() == 3){
        payout = ((pot * 5) / 3);
    }
    else if (winnerArr.size() == 2){
        payout = ((pot * 5) / 2);
    }
    else if (winnerArr.size() == 1){
        payout = (pot * 5);

        for (int i = 0; i < winnerArr.size(); i++){
            cout << winnerArr[i].m_name << " wins " << payout << endl;
            winnerArr[i].m_balance += payout;
            cout << winnerArr[i].m_balance << endl;

        }
    }
    cout << player1->m_name << "'s total: " << player1->m_balance << endl;
    cout << player2->m_name << "'s total: " << player2->m_balance << endl;
    cout << player3->m_name << "'s total: " << player3->m_balance << endl;
    break;
}
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
Chintan
  • 25
  • 1
  • 6
  • 1
    You return a vector of COPIES of the players - is that what you intended? I suggest you pass a `vector&` and modify that with the new order. This also allows you to have any number of players. I suggest you use the built in sort function. What is the point of this loop: `for (int i = 0; i < 1; i++)` – Neil Kirk Oct 18 '14 at 14:30
  • I am sorry but I do not understand this. I tried passing a ref of vector array but it outputs 0 for al player's total. I wish to declare the winner/s and add the reward to the actual player not his copy. – Chintan Oct 19 '14 at 18:28
  • Post the code you tried. – Neil Kirk Oct 20 '14 at 01:38
  • 1. Why are you saying `vector array`? `vector` is enough. 2. Your code does not compile. Please post a compilable code. 3. When you have rewritten the `chkWinner` function, please remove the earlier version or atleast comment it out. And show the modified `main` function also. 4. Finally, how does your `Player` class look like? Post that code too. – elimad Oct 20 '14 at 05:32
  • The method `chkWinner()`returns `void`. But the code in `main()` - `winnerArr = chkWinner(winnerArr);` is expecting a return value !!! – CinCout Oct 20 '14 at 09:15
  • Thanks for helping me out here. I have posted the player class and the modified main function. hope this is compilable – Chintan Oct 20 '14 at 09:17
  • 1
    gargankit, that was a copying mistake, I have modified it, thanks – Chintan Oct 20 '14 at 09:19
  • Why don't you use the built-in `sort()` method as suggested by @NeilKirk ?? – CinCout Oct 20 '14 at 09:24
  • The best thing to do would be to reduce the code to a minimum that allows you to reproduce the problem. Instead of `Player`, create a class with only 1 member, then right a simplified `chkWinner` function and test if that works. If not, consider asking here with that simplified example. – jogojapan Oct 20 '14 at 09:40
  • Why, oh man, why you create players manually?? `std::vector` can do it for you: `std::vector players(3)`. [That `(3)` tells the `vector` to have 3 defaultly-constructed elements.](http://www.cplusplus.com/reference/vector/vector/vector/) – GingerPlusPlus Oct 25 '14 at 12:41
  • I am doing that because I want to store the same players to that array. I am also resizing that array to determine the winner. – Chintan Oct 25 '14 at 13:14

1 Answers1

0

As everyone pointed out, you shouldn't be creating a new array and modify the one you already have. STL give you a hand to accomplish what you want with the sort function.

Firstly, you need:

#include <algorithm>

Then you need a sorting function:

bool sorting_method(const Player &_1, const Player &_2){
    //Bigger numbers go first
    return _1.m_total > _2.m_total;
}

Finally, you use sort instead of your chained loops:

void chkWinner(std::vector <Player> &totalArr){
    sort(totalArr.begin(), totalArr.end(), sorting_method);
    // Add code here, if you need to perform something else. 
    //totalArr is already in the order you need it to.
{

More information about std::sort()

EDIT: I modified the code to create an array with pointers to Players instead of copies of the players.

bool sorting_method(Player *_1, Player *_2) {
    //Bigger numbers go first
    return _1->m_total > _2->m_total;
}

int main()
{
    Player *player1 = new Player(2, 1);
    Player *player2 = new Player(5 ,2);
    Player *player3 = new Player(3, 3);
    vector <Player *> winnerArr(3);
    int pot = 0;
    int betAmount = 0;
    int maxBet = 10;
    int x, y, z;

    winnerArr[0] = player1;
    winnerArr[1] = player2;
    winnerArr[2] = player3;

    sort(winnerArr.begin(), winnerArr.end(), sorting_method);
    cout << winnerArr[0]->m_name << ", "
        << winnerArr[1]->m_name << ", "
        << winnerArr[2]->m_name << endl;

Please note that because of that change you'll have to modify all the code that tries to access the array, including the sorting_method. This code I wrote is already modified, so you just need to modify the rest of it. I removed the resize function and added the (3) parameter when creating the array. I believe its simpler and clear. Have fun :D

SlySherZ
  • 1,631
  • 1
  • 16
  • 25
  • Hi, I have rewritten the code with the std:sort. But it still does not update the player.m_total outside the array. It seems like the vector array holds different players than the actually ones. – Chintan Oct 24 '14 at 05:57
  • @Chintan I probably misunderstood what you wanted in the first place. Do you want change the values inside the objects? If so, why? It looks like each one of them is a player, you would be messing up those values. What I wrote in my answer was designed to sort the array only, without changing each member. – SlySherZ Oct 24 '14 at 09:41
  • so, there are 3 players (1 Player + 2 AI). Each player has a 'm_balance' member variable. All players bet before the game and then the winner wins the entire pot. So the sort algorithm helps me find the winner and then I add the pot value to the winners 'm_balance'. I am able to derive the winner but unable to add the pot value to their 'm_balance' variable – Chintan Oct 25 '14 at 11:04
  • @Chintan I think I found the problem. When you create the array, you are actually copying your players, instead of passing a reference to them. After that you modify the copy and the originals stay intact. I'll change my answer to the new code I wrote. – SlySherZ Oct 25 '14 at 15:15
  • Thanks a lot. It finally worked but I am still not sure what does the '*' in this syntax do. vector winnerArr(3); Anyways, thank you every one, I learnt a lot. – Chintan Oct 25 '14 at 20:04
  • @Chintan the * means "address of": it's a pointer. What we are doing is to make arrays with the locations in memory of your player classes. – SlySherZ Oct 25 '14 at 21:34
  • thanks SlySherZ. here is a thread that could help other reading this post. It explains the concept http://stackoverflow.com/questions/6624819/c-vector-of-objects-vs-vector-of-pointers-to-objects – Chintan Oct 26 '14 at 07:10