Problem of the Day
A new programming or logic puzzle every Mon-Fri

Button Masher

Today we're going to work on a game that you can play with your friends. The way the game works is each player is assigned a key on the keyboard (max 2 players). A timer will start with 30 seconds on the clock. During that 30 seconds each player is to hit their key as many times as possible. After the time is up the program will print out the key presses for each button and who won. For a bonus play with a co-worker or friend and see who wins. Enjoy!

Permalink: http://problemotd.com/problem/button-masher/

Comments:

  • bumbleguppy - 10 years, 4 months ago

    javascript

    var timer = player1key = player2key = player1 = player2 = 0;
    
    function aWinnerIsYou(){
       var msg = 'Final Score:\n_________________________\n\nPlayer 1:  '+ player1 + '\n\nPlayer 2:  ' + player2 + '\n\n\n*** ' + ((player1 > player2) ? 'Player 1 is the winner!' : ((player1 < player2) ? 'Player 2 is the winner!' : 'Tie game!')) + ' ***';
       alert(msg);
    }
    window.onload = function(){
        player1key = prompt("Keypress Race - Player 1 choose a key", "Player 1 choose a key").charCodeAt(0);
        player2key = prompt("Keypress Race - Player 2 choose a key", "Player 2 choose a key").charCodeAt(0);
        window.addEventListener("keypress", function(e)
            {
                var charCode = (typeof e.which == "number") ? e.which : e.keyCode
                switch(charCode){
                    case player1key:
                        player1++;
                    case player2key:
                        player2++;
                }
            }, false);
        timer = window.setTimeout(aWinnerIsYou, 5000);
    }
    

    reply permalink

  • bumbleguppy - 10 years, 4 months ago

    I guess I should have added a "while(player1key == player2key){ player2key = prompt('Enter a different key, jackass...')}" after the first prompt for second player :)

    reply permalink

Content curated by @MaxBurstein