Search code examples
javascriptrandomaddeventlistenerpoker

addEventListeter to an EventListener? (change the button)


I'm kinda new to js language, and I have this interesting project. Have you ever played Poker? So, when the user pressed the button, he sees three cards on the table. Then he presses the same 'check' button and he should see the fourth card, and then fifth. How to change the button "job"? I mean. instead of making extra buttons for 4th and 5th card, can I make the game with only one?


Solution

  • Something like this can get you started:

    var gamestate = "";
    var update = function() {
      switch (gamestate) {
        case "":
          $('#state').text("A(H) J(D) 10(S)") ;
          gamestate = "flop";
          break;
        case "flop":
          $('#state').text($('#state').text() + " 9(S)");
          gamestate = "turn";
          break;
        case "turn":
          $('#state').text($('#state').text() + " 4(H)");
          gamestate = "river";
          break;
        case "river":
        $('#state').text("");
        gamestate = "";
        break;
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="state">
    
    </div>
    <a id='clickme' href="javascript:;" onclick="update()">Update</a>

    where you have the ...text('info') blocks you would put your dealing cards code based on the game state.