How can I choose which car I want to bet on, and then get 9x the amount wagered if I win?

Each player has to start with 1000 points and then wagers a certain amount on a horse (picked with a click). If you win, you get 9 times your wager back. If you lose, you lose the amount you wagered. At the end of the race, the name of the player that won is displayed, along with the amount of their winnings. Each player's "bank" is then updated to reflect the win/loss.

Answers

  • edited May 2017

    You don't have a proper use of arrays.

    Arrays are lists, therefore they can and should be used with a for loop.

    This belongs into setup after loadImages, because it is time consuming for the processor

        Lambos[0].resize(90, 30);
        Lambos[1].resize(90, 30);
        Lambos[2].resize(90, 30);
        Lambos[3].resize(90, 30);
        Lambos[4].resize(90, 30);
    

    it can also be done with a for loop, e.g.

    for (int i=0; i< Lambos[0].length; i++) {
        Lambos[i].resize(90, 30); // using i here !!!!
    }
    

    Similarly this

    image(Lambos[0], 620, 400);
    image(Lambos[1], 725, 400);
    image(Lambos[2], 830, 400);
    image(Lambos[3], 935, 400);
    image(Lambos[4], 1040, 400);
    

    becomes

        for (int i=0; i< Lambos[0].length; i++) {
               image(Lambos[0], 620 + (105*i), 400);
        }
    

    Similarly this

        image(Lambos[0], horsePos[0], car1Y);
        image(Lambos[1], horsePos[1], car2Y);
        image(Lambos[2], horsePos[2], car3Y);
        image(Lambos[3], horsePos[3], car4Y);
        image(Lambos[4], horsePos[4], car5Y);
    

    becomes

        for (int i=0; i< Lambos[0].length; i++) {
             image(Lambos[i], horsePos[i], carY[i]);
        }
    

    (this should become an array too:

    //Image location on Y coordinate
    int car1Y = 302;
    int car2Y = 390;
    int car3Y = 490;
    int car4Y = 585;
    int car5Y = 680;
    

    )

  • edited May 2017

    Better than if

    for

    if (state == 1) { 
    
    if (state == 2) {
    
    if (state == 3) {
    

    better use a

    if...
    
    else if....
    
    else if....
    

    construct or even

    switch(state) { 
    
        case 0: 
        ....
        break;
    
        case 1: 
        ....
        break;
    
        case 2: 
        ....
        break;
    
    }
    

    Name the states

    also I would name your states 0,1,2.... with names like

        // constants 
        final int setWagers=0; 
        final int playGame=1; 
        final int showHighscore=2; 
        int state = setWagers; 
    

    A sub function for each state

    also make a sub function for each state. Your draw() would look like this:

    void draw() {
    
        switch(state) { 
    
            case setWagers: 
                 manageStateSetWagers();
            break;        
    
            case playGame: 
                  manageStatePlayGame();
            break; 
    
            case showHighscore: 
                  manageStateShowHighscore(); 
            break; 
    
            default: 
    
            break; 
    
        } // switch 
    
    } // function 
    

    ALSO

    also, we can't run your code since you use images.

    make a secondary version with text() instead of images for the forum

    Your issue

    You don't really say what is missing in your code.

    Maybe you need this:

    register the click on a horse in wager mode.

    Best store these positions in two arrays

        image(Lambos[0], 70, 400);
        image(Lambos[1], 175, 400);
        image(Lambos[2], 280, 400);
        image(Lambos[3], 385, 400);
        image(Lambos[4], 490, 400);
    
        image(Lambos[0], 620, 400);
        image(Lambos[1], 725, 400);
        image(Lambos[2], 830, 400);
        image(Lambos[3], 935, 400);
        image(Lambos[4], 1040, 400);
    

    Then for loop over the array and use dist() for each horse and store the clicked horse number (index in the array) in extra variable and store the wager.

    At the end of the game check for each player if index of winner horse is equal the initial clicked horse number.

  • What I need is for each player to click on a car and then that will be the one they bet on, if the horse they get on wins the wager/bet they put will be multiplied by 9 and put into their total bank. If they pick the wrong horse then they lose the amount they wagered/bet on.

  • Yes, I told you how to do that

    Step by step.....

    See end of my last post

  • Ok I made all my code organized now.

  • Does the wager work now?

    You need to conpare the index of the car the player bet on and the index of the winner car

  • edited May 2017

    OK I did this:

        if (p1Choice == currPlace) {
          playerBank = playerBank + playerWager * 9;
        } else {
          playerBank = playerBank - playerWager;
        }
    
    
        if (p2Choice == currPlace) {
          player2Bank = player2Bank + player2Wager * 9;
        } else {
          player2Bank = player2Bank - player2Wager;
        }
    
  • Bu, I don't know how to make it say if the player chooses the 1st place car, then their wager is multiplied by 9 and put into the bank.

  • And if the player chooses the wrong car then they lose the amount they wagered.

  • That looks correct, if currPlace is the car that won. Is it?

    if (p1Choice == currPlace) {
      playerBank = playerBank + playerWager * 9;
    } else {
      playerBank = playerBank - playerWager;
    }
    
  • does it work?

    did you define currPlace and p1Choice?

  • I don't know how to make a variable that is the car that won.

  • show your entire current code please

  • Do you know how to identify the car that has been clicked on to bet the wager in the beginning?

  • this line

     checkered.resize(540, 505);
    

    should be in setup() please after you loaded this image

  • edited May 2017

  • Answer ✓

    you wrote:

    I don't know how to make a variable that is the car that won.

    here:

    this is your old code

    for (int i = 0; i < 5; i ++) {
          if (horsePos[i] > 990 && results[i] == 0) {
            results[i] = currPlace;
            currPlace ++;
    
            if (currPlace > 5) {
              state = 3;
            }
          }
        }
    

    change this to

    theWinnerIs = -1 ;   // this means undefined!!!!
    
    for (int i = 0; i < 5; i ++) {
          if (horsePos[i] > 990 && results[i] == 0) {
            results[i] = currPlace;
            currPlace ++;
            if (theWinnerIs==-1) {
                  theWinnerIs = i;  // the winner 
             }
            if (currPlace > 5) {
              state = 3;
            }
          }
        }
    

    this

        if (p1Choice == currPlace) {
          playerBank = playerBank + playerWager * 9;
        } else {
          playerBank = playerBank - playerWager;
        }
    

    becomes

        if (p1Choice == theWinnerIs) { // !!!!!!!!!!!!!!!!!!
          playerBank = playerBank + playerWager * 9;
        } else {
          playerBank = playerBank - playerWager;
        }
    

    OK?

  • YES! THANK YOU!

  • edited May 2017

    this

        textSize(20);
        text("Car 1:", 100, 180);
        text("Car 2:", 100, 210);
        text("Car 3:", 100, 240);
        text("Car 4:", 100, 270);
        text("Car 5:", 100, 300);
    
        text(results[0], 300, 180);
        text(results[1], 300, 210);
        text(results[2], 300, 240);
        text(results[3], 300, 270);
        text(results[4], 300, 300);
    

    becomes

    textSize(20);
    for (int i=0; i < 5; i++) 
      text("Car "+str(i+1)+":      "+results[i], 100, 180+i*30);
    

    see, when you now want to change the line spacing, you just change one number (30) and not 10 (or 8)

  • the winner thing does not work, it goes from -1 to 0 every time, after the race goes to state 2 the winner becomes 0

  • even if I println the winner it stays at 1 every time

  • Forget about it I fixed it

  • Answer ✓

    well done!

  • Does it work now completely, like selecting car to bet a wager, winner gets wager in his bank; bank amount gets displayed, new gane with this bank amount until one bank is empty...?

  • I have everything, except this constantly goes on, and adds, and subtracts the wager from/to the bank more than once:

    if (p1Choice == Winner) {
              playerBank = playerBank + playerWager;
            } else {
              playerBank = playerBank - playerWager;
            }
    
            if (p2Choice == Winner) {
              player2Bank = player2Bank + player2Wager;
            } else {
              player2Bank = player2Bank - player2Wager;
            }
    

    plz help so the wager is either added/subtracted only once.

  • make sure those lines get called only once.

    This means, you can implement the whole block into an if-clause

    if (!wagerIsAdded) {
         wagerIsAdded=true;
         ...................
    }
    

    before setup say boolean wagerIsAdded=false;

    also set wagerIsAdded=false; when placing a new wager etc.

    (since you work with states there is also a place where you move onto the state; it could be right before it, but let's skip that here)

  • I did this but it doesn't work:

        if (!wagerIsAdded) {
          wagerIsAdded=true;
          if (p1Choice == Winner) {
            playerBank = playerBank + playerWager;
          } else {
            playerBank = playerBank - playerWager;
          }
    
          if (p2Choice == Winner) {
            player2Bank = player2Bank + player2Wager;
          } else {
            player2Bank = player2Bank - player2Wager;
          }
        }
        wagerIsAdded = false;
    
Sign In or Register to comment.