17
\$\begingroup\$

The Problem

You must write a program that, when the Konami Code is typed in during runtime, prints the string "+30 lives" and sounds a noise of your choice from the computer's speaker.

Definition

The "Konami Code" is defined as UUDDLRLRBA followed by pressing the enter key.

The Rules

  • You may choose to use the up arrow for U, down for D, left for L, and right for R, as long as your code is consistent with either arrows or letters.

  • Your input may be accepted from a controller or a keyboard, but does not need to support both.

  • Existing answers may continue to use BABA instead of BA, but may also shorten it if they wish to do so. Future answers should all use BA for consistency.

  • Empty input doesn't need to be supported.

\$\endgroup\$
1
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ Commented Feb 20, 2018 at 8:27

22 Answers 22

13
\$\begingroup\$

05AB1E, 33 bytes (Fine on Empty Input)

Dg0Qiq}•ï“뙵yê!•36BQi7ç“+30™‹“J,

Try it online!

05AB1E, 30 26 bytes (Fails on Empty Input)

-4 thanks to Adnan

•ï“뙵yê!•36BQi7ç,“+30™‹“,

Try it online!

•ï“뙵yê!•36B                  # UUDDLRLRBABA in base-214, converted back to base-36.
             Qi                # If implicit input is equal to...
               7ç,             # Print 7 converted to a character to the console (bell sound)...
                  “+30™‹“, # Print +30 lives.

Disclaimer: Bell sound does not play on TryItOnline, maybe ask Dennis if one of his servers somewhere is pinging when you run it.

\$\endgroup\$
5
  • \$\begingroup\$ You almost won! Just one byte off \$\endgroup\$
    – user63187
    Commented Dec 13, 2016 at 2:32
  • 2
    \$\begingroup\$ A compressed version of "+30 lives" is “+30™‹“ :) \$\endgroup\$
    – Adnan
    Commented Dec 13, 2016 at 8:40
  • \$\begingroup\$ This always outputs "+30 lives" for me on the online interpreter, even with empty input. \$\endgroup\$ Commented Dec 13, 2016 at 15:12
  • 2
    \$\begingroup\$ @redstarcoder Only* with empty input, crap, fixing \$\endgroup\$ Commented Dec 13, 2016 at 15:13
  • \$\begingroup\$ @carusocomputing can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:35
9
\$\begingroup\$

JavaScript (ES6), 202 200 198 bytes

Tested only on Chrome and Firefox. Uses UDLR rather than the arrow keys. Please make sure that CapsLock is off.

(o=(A=new AudioContext()).createOscillator()).connect(A.destination);s='';document.onkeyup=e=>{(s+=e.key[0]).slice(-11)=='uuddlrlrbaE'&&setTimeout('o.stop()',500,console.log('+30 lives'),o.start())}
Please click inside this area to make sure it gets focus.

\$\endgroup\$
1
  • \$\begingroup\$ No need for parens after constructor and move s='' to argument of createOscillator. 196: (o=(A=new AudioContext).createOscillator(s='')).connect(A.destination);document.onkeyup=e=>{(s+=e.key[0]).slice(-11)=='uuddlrlrbaE'&&setTimeout('o.stop()',500,console.log('+30 lives'),o.start())} \$\endgroup\$ Commented Aug 9, 2021 at 15:41
8
\$\begingroup\$

Python 3,  55  53 bytes

-1 byte thanks to isaacg (use bitwise not to replace -10 with ~9).
-1 byte thanks to CalculatorFeline (it is possible to use a literal BEL byte).

while'UUDDLRLRBA'!=input()[~9:]:1
print('+30 lives7')

where the 7 shown above is a literal byte 0x07 (an unprintable in a code block).

Once enter is pressed the while loop condition is checked. If the last ~9 = 10 characters (at most) do not match the Contra command code then the no-op 1 is executed (replacement of pass for brevity); if they do match then the while loop ends and the print statement is executed, which writes the required text along with an ASCII bell character ("alarm"), 0x07, which produces a sound unless it has been explicitly disabled (or the terminal has no speaker!).

\$\endgroup\$
9
  • \$\begingroup\$ can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:36
  • \$\begingroup\$ Fixed, thanks for the heads up. \$\endgroup\$ Commented Dec 16, 2016 at 1:14
  • 1
    \$\begingroup\$ -10 = ~9, which can save you a byte. \$\endgroup\$
    – isaacg
    Commented Jun 19, 2017 at 9:49
  • \$\begingroup\$ Can't you put a literal \x07 in the string for -1 byte? \$\endgroup\$ Commented Jun 19, 2017 at 21:38
  • \$\begingroup\$ @CalculatorFeline I have no idea, can I? Note people often use \n in strings in Python golfs, why would they not use \x0a? \$\endgroup\$ Commented Jun 20, 2017 at 8:11
5
\$\begingroup\$

Jelly, 33 30 29 27 bytes

ɠ⁻“UUDDLRLRBA”$¿ṛ7Ọ“¡}ʠƈ[ỵ»

Turns out, the append () is not needed, strings are automatically joined together both printed.

ɠ⁻“UUDDLRLRBABA”$¿ṛ7Ọṭ“¡}ʠƈ[ỵ»

Uncompressed

ɠ⁻“UUDDLRLRBABA”$¿ṛ7Ọṭ“+30 lives”

Try it online
If you remove everything after ¿, you can mess with the input and see that it only returns when the input string is correct.

ɠ⁻“UUDDLRLRBABA”$¿ṛ7Ọṭ“+30 lives” - main link
ɠ                                 - takes a line of input
 ⁻“UUDDLRLRBABA”$                 - $ groups it into a monadic !="UUDDLRLRLRBABA"
                 ¿                - while (the inequality, which takes from the getline)
                  ṛ               - take the right argument (to ignore the input string)
                   7Ọ             - chr(7), the bell character
                     ṭ“+30 lives” - append that bell character to the output

Thanks @JonathanAllan for helping me figure out string compression :)

\$\endgroup\$
7
  • \$\begingroup\$ “+30 lives” compressed is “¡}ʠƈ[ỵ» (for future reference here is a post about it, it's linked in the tutorial on the Jelly wiki). \$\endgroup\$ Commented Dec 10, 2016 at 7:04
  • \$\begingroup\$ where is the sound? \$\endgroup\$
    – tuskiomi
    Commented Dec 13, 2016 at 18:31
  • 1
    \$\begingroup\$ I just tested it with the official interpreter and it does indeed loop until the Konami code is entered \$\endgroup\$
    – JayDepp
    Commented Dec 15, 2016 at 0:46
  • \$\begingroup\$ @JayDepp can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:36
  • \$\begingroup\$ I believe that it's not so much automatic string concatenation as an implicit print of the LLC (leading constant chain) 7Ọ followed by an implicit print of the nilad “¡}ʠƈ[ỵ», appearing on stdout as if it were concatenation. As a showcase you can try adding a 2 second sleep between with ɠ⁻“UUDDLRLRBABA”$¿ṛ7Ọ2“+30 lives”œS. \$\endgroup\$ Commented Dec 16, 2016 at 1:19
5
\$\begingroup\$

Processing, 161 157 155 bytes

String k="";void keyTyped(){if(match(k+=key,"uuddlrlrba\\n$")!=null){print("+30 lives");new processing.sound.SoundFile(this,"a.mp3").play();}}void draw(){}

The audio file must be saved as sketchName/data/a.mp3. Note: I have only tested this program without the audio file because I am too lazy to download an mp3 file (since only limited extensions are supported from processing.sound.SoundFile).

The draw() function is needed to be there in order for keyTyped to work.

The reason we are using keyTyped is because Processing does not have STDIN, it can only listen for keys being pressed via the sketch being run.

Explanation

String k="";
void keyTyped(){
  if(match(k+=key,"uuddlrlrba\\n$")!=null){
    print("+30 lives");
    new processing.sound.SoundFile(this,"a.mp3").play();
  }
}
void draw(){
}

All of the user's keystrokes are stored as chars inside the String k. The keyTyped is an inbuilt function that is called whenever the user types a key. Simultaneously, we are checking if this String ends with the keystrokes. Then we print +30 lives and play the sound file. And the draw function is there to continuously update keyTyped. After the Konami code is entered, then nothing else will be outputted and no audio will be played.

\$\endgroup\$
4
  • \$\begingroup\$ it seems like you should count the size of your mp3 file \$\endgroup\$
    – user60119
    Commented Dec 13, 2016 at 19:06
  • \$\begingroup\$ @ricyje Processing doesn't really have any other way of outputting sounds. Also, the size of the .mp3 file doesn't really matter since no matter what it is, Processing will play it as a sound \$\endgroup\$
    – user41805
    Commented Dec 13, 2016 at 19:09
  • \$\begingroup\$ @KritixiLithos can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:35
  • \$\begingroup\$ @ChristopherPeart Done! (I'm glad to save 2 more bytes :) \$\endgroup\$
    – user41805
    Commented Dec 16, 2016 at 7:43
4
\$\begingroup\$

><>, 132 69 64 62 bytes *><>, 84 bytes

i:1+?\0[
:1+?!\i
BA"a~/"UUDDLRLR
{v!?l/!?=
7/"+30 lives"
o<;!?l

Try it here!

This relys on the bell chime (ascii 7) for the noise at the end (not heard on the online interpreter).

Thanks to @TealPelican for saving another 15 bytes!

Saved two bytes checking for BA instead of BABA.

\$\endgroup\$
8
  • \$\begingroup\$ Does this make a sound? \$\endgroup\$
    – user41805
    Commented Dec 9, 2016 at 17:38
  • \$\begingroup\$ @KritixiLithos, yes you'll see on the second-last line at the very end is a "7", that's the bell chime. \$\endgroup\$ Commented Dec 9, 2016 at 17:39
  • \$\begingroup\$ I've refactored your code a little bit because i didn't quite understand the first lines use? Try it here This link is for ><> but works for *><> too (doesn't use the dive mechanic like you have) - it also saved on 19 bytes! :) \$\endgroup\$ Commented Dec 13, 2016 at 10:45
  • \$\begingroup\$ @Tealpelican, the first line is so it keeps running, waiting for input! Your linked version doesn't do that ;p. \$\endgroup\$ Commented Dec 13, 2016 at 15:01
  • \$\begingroup\$ You are right! I missed that! - I've added a line in to accommodate for my error and replaced some redundant reversing and printing Try the new version which I've gotten down to 67 bytes :D \$\endgroup\$ Commented Dec 13, 2016 at 15:27
3
\$\begingroup\$

C 119 -1 - 2 = 116 bytes

Golfed

i;f(){char a[14];a[13]=0;while(strcmp(a,"UUDDLRLRBA"))for(i=0;i<12;a[i]=a[i+++1]);a[i]=getch();puts("+30 Lives\a");}  

Ungolfed

#include<stdlib.h>
#include<conio.h>

i;
f()
{
    char a[14];
    a[13]=0;
    while(strcmp(a,"UUDDLRLRBA"))
    {
        for(i=0;i<12;a[i]=a[i+++1]);
        a[i]=getch();
        puts(a); //This is to print every step in the runtime
    }
    puts("+30 Lives\a"); // '\a' is the acsii char that makes sound when passed to STDOUT
}

main()
{
    f();
}
\$\endgroup\$
7
  • \$\begingroup\$ s/UUDDLRLR BABA/UUDDLRLRBABA for -1 byte and the strcmp will be looking for the correct string! \$\endgroup\$ Commented Dec 9, 2016 at 18:43
  • \$\begingroup\$ @Christopher please never make edits to people's code on this Stack Exchange site. your edit should never have been approved. \$\endgroup\$
    – cat
    Commented Dec 10, 2016 at 1:22
  • \$\begingroup\$ Mukul, you can remove the space yourself. \$\endgroup\$
    – cat
    Commented Dec 10, 2016 at 1:23
  • 1
    \$\begingroup\$ @MukulKumar Of course :) it's important that the answerer edit their own code. \$\endgroup\$
    – cat
    Commented Dec 10, 2016 at 2:14
  • 1
    \$\begingroup\$ @MukulKumar can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:37
3
\$\begingroup\$

Stacked, 41 bytes

['+30 lives'BEL+out]prompt'UUDDLRLRBA'=if

Reads input from keyboard.

['+30 lives'BEL+out]prompt'UUDDLRLRBA'=if
[                  ]prompt'UUDDLRLRBA'=if  if the input is the desired string,
 '+30 lives'BEL+out                           output '+30 lives' and the BEL character. 
\$\endgroup\$
3
\$\begingroup\$

AutoHotkey, 63 bytes

Input,K,L10
if(K="UUDDLRLRBA"){
MsgBox,"+30 lives" 
SoundBeep
}

After running this script, it will check if the next 10 keys are the Konami code and if it is, it will show a message box saying "+30 lives" and it should play a beep (I don't have speakers now to test).

\$\endgroup\$
1
  • \$\begingroup\$ Cool! AutoHotkey is a interesting program. \$\endgroup\$
    – user63187
    Commented Mar 4, 2017 at 12:55
3
\$\begingroup\$

GNU sed, 32 25 + 1 = 33 26 bytes

+1 byte for -n flag.

/UUDDLRLRBA$/a+30 Lives\a

Try it online!

Explanation

Every time enter is pressed ($), the program checks if the preceding 10 keystrokes were the code. If they were, the a command queues the text +30 Lives and the bell sound (\a) to be printed at the end of the current cycle (which, in this case, is immediately).

\$\endgroup\$
3
  • \$\begingroup\$ can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:36
  • \$\begingroup\$ This already has the BA just once. \$\endgroup\$
    – Jordan
    Commented Dec 15, 2016 at 23:29
  • \$\begingroup\$ Oh wow didn't notice \$\endgroup\$
    – user63187
    Commented Dec 16, 2016 at 0:23
3
\$\begingroup\$

Mathematica, 50 bytes

While[x=!=UUDDLRLRBA,x=Input[]];Beep[];"+30 lives"

Initially, x is just a symbol and is thus not identical (=!=) to the symbol UUDDLRLRBA, so x=Input[] is evaluated. This will open a dialog box with the cursor already in the input field, so the user can immediately start typing on the keyboard.

If Enter is pressed or OK is clicked without typing anything, then InputField[] will return Null, which is not identical to UUDDLRLRBA, so the loop continues and another dialog box will be opened.

If the user clicks Cancel or otherwise exits the dialog box, then InputField will return $Canceled, which is also not identical to UUDDLRLRBA, so the loop will continue.

The user can type in the dialog box to their heart's desire. When they hit Enter, their input is interpreted as a Wolfram language expression (possibly just boxes). If that expression is anything other than the symbol UUDDLRLRBA, the loop will continue.

"Must loop until the Konami code is entered" is a little vague, but I believe this satisfies it. Once the While loop is completed, Beep[];"+30 lives".

\$\endgroup\$
3
  • \$\begingroup\$ can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:37
  • 1
    \$\begingroup\$ UUDDLRLRB? Where's A? \$\endgroup\$ Commented Jun 20, 2017 at 15:01
  • \$\begingroup\$ @CalculatorFeline Fixed \$\endgroup\$
    – user61980
    Commented Jun 20, 2017 at 16:20
3
\$\begingroup\$

Petit Computer BASIC, 91 bytes

@L
WAIT 1S$=S$+CHR$(B)*!!B
B=BTRIG()IF B<1024GOTO@L
IF"xxxxxxxxxx"==S$THEN BEEP?"+30 lives

Finally a solution that actually uses controller input!

I used Petit Computer rather than the newer version (SmileBASIC) because it has access to the Start button, and BTRIG() is shorter than BUTTON().

I've replaced the data string with x's, it should contain characters with ascii codes 1,1,2,2,4,8,4,8,32,16

Boring version, 46 bytes

INPUT S$IF"UUDDLRLRBA"==S$THEN BEEP?"+30 lives
\$\endgroup\$
2
  • \$\begingroup\$ I would recommend using SmileBASIC 2 for the name of Petit Computer entries, and where necessary specify SmileBASIC as SmileBASIC 3. \$\endgroup\$
    – snail_
    Commented Feb 8, 2017 at 4:33
  • \$\begingroup\$ Nice job using controller! \$\endgroup\$
    – user63187
    Commented Feb 8, 2017 at 11:00
3
\$\begingroup\$

Vyxal, 21 bytes

«_sa}]∷«⇧=`+30 æ§\7`*

Based on @Makonede's 05AB1E answer.

Try it Online!

Explained:

«_sa}]∷«⇧=`+30 æ§\7`*    
                    *    # Repeat...
          `+30 æ§\7`     # the string '+30 lives␇'... (escape code \7 is BEL)
                         # once if ...
         =               # the (implicit) input equals...
«_sa}]∷«                 # the compressed code...
        ⇧                # in uppercase...
                         # otherwise repeat zero times
\$\endgroup\$
1
  • 1
    \$\begingroup\$ \a also works instead of \7. \$\endgroup\$
    – Makonede
    Commented Jun 2, 2021 at 16:47
2
\$\begingroup\$

Powershell, 89 86 Bytes

if((Read-Host)-eq"UUDDLRLRBABA"){"+30 Lives";[System.Media.SystemSounds]::Beep.Play()}

enter submits the string to the Read-Host, so it should.. work?

\$\endgroup\$
5
  • 2
    \$\begingroup\$ I think you just need to check for "UUDDLRLRBABA" not "UUDDLRLR BABA" as there's no " " in the Contra code. That'd also save you a byte. I think assuming enter because they have to press enter to send the input is valid. \$\endgroup\$ Commented Dec 9, 2016 at 18:02
  • \$\begingroup\$ I does work by the way \$\endgroup\$
    – user63187
    Commented Dec 9, 2016 at 22:28
  • 1
    \$\begingroup\$ @ChristopherPeart Please don't make edits to others' code even if you are the OP, or if you are saving them a byte. \$\endgroup\$
    – cat
    Commented Dec 10, 2016 at 1:26
  • \$\begingroup\$ Ok I will do that. But It was my fault \$\endgroup\$
    – user63187
    Commented Dec 10, 2016 at 3:08
  • \$\begingroup\$ @ConnorLSW can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:37
2
\$\begingroup\$

C# (the boring version), 80 bytes

void n(){while(Console.ReadLine()!="UUDDLRLRBA"){}Console.Write("+30 Lives\a");}

C# (The interesting one), 202 bytes

void n(){t();Console.Write("+30 Lives\a");}void t(){var k="UUDDLRLRBA";var s="";while(s!=k){s+=Console.ReadKey().Key.ToString();s=k.StartsWith(s)?s:"";}if(Console.ReadKey().Key!=ConsoleKey.Enter){t();}}

I think this works, sadly online testers do not support the existence of a console input, as such I will have to go on the tests I have done

Can most likely be golfed hugely - I'm not great at this! :)

Ungolfed:

void n()
{
    t();
    Console.WriteLine("+30 Lives\a");
}

void t()
{
    var k = "UUDDLRLRBA";
    var s = "";
    while(s != k)
    {
        s += Console.ReadKey().Key.ToString();
        s = k.StartsWith(s) ? s : "";
    }
    if(Console.ReadKey().Key != ConsoleKey.Enter)
    {
        t();
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Hmm, does C# have a way to read until a newline is detected? That'd save you from looking for the enter key. Look at how it's implemented in Python here. \$\endgroup\$ Commented Dec 13, 2016 at 15:14
  • \$\begingroup\$ There's Console.Readline but I thought that might be aganist the spirit of the challenge! If that's fine I can make this much shorter \$\endgroup\$ Commented Dec 13, 2016 at 15:15
  • \$\begingroup\$ @redstarcoder added a Console.ReadLine() version \$\endgroup\$ Commented Dec 13, 2016 at 15:20
  • \$\begingroup\$ @AlfieGoodacre can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:38
  • \$\begingroup\$ @ChristopherPeart It originally was but I had to change it ;) \$\endgroup\$ Commented Dec 15, 2016 at 23:45
2
\$\begingroup\$

Wonder, 50 bytes

ol"•";f\@[="UUDDLRLRBA"rl0?ol"+30 lives•"?f0];f0

Replace with the character of code \x07 (the BEL control char). Takes input through STDIN.

\$\endgroup\$
2
  • \$\begingroup\$ can you fix this the BA should only be once (my fault sorry) \$\endgroup\$
    – user63187
    Commented Dec 15, 2016 at 22:37
  • \$\begingroup\$ Well I fixed. Thanks for the notice! \$\endgroup\$ Commented Dec 16, 2016 at 1:58
1
\$\begingroup\$

flex, 37 + 5 = 42 bytes

%%
UUDDLRLRBA puts("+30 lives\a");

The code itself is 37 bytes, compile it with the "-main" option which adds 5 bytes. Naturally, you have to compile the resulting C file with your favorite C compiler, but I don't think that step should count toward the byte count.

I could save a byte by using a literal BEL character instead of \a, but I'd rather be able to read my own code.

\$\endgroup\$
3
  • 4
    \$\begingroup\$ You really don't need to be able to read the code. This is code golf \$\endgroup\$
    – user63187
    Commented Dec 16, 2016 at 14:02
  • \$\begingroup\$ Personally, I think I should get a bonus for using flex, due to how rarely it gets used here. :) \$\endgroup\$ Commented Dec 17, 2016 at 18:15
  • 1
    \$\begingroup\$ I believe you can reduce the penalty to 3 bytes via giving -ll as an option to the C compiler rather than -main as an option to flex. \$\endgroup\$
    – user62131
    Commented Dec 17, 2016 at 18:15
1
\$\begingroup\$

C, 87 85 81 bytes

s[13];f(){for(;strcmp(s,"UUDDLRLRBA\n");fgets(s,12,stdin));puts("+30 Lives!\a");}
\$\endgroup\$
1
\$\begingroup\$

shortC, 52 bytes

s[13];AO;Ms,"UUDDLRLRBA\n");Ys,12,N));J"+30 Lives!\a

Explanation:

s[13];                                                // declare array with 13 members
      A                                               // main function
       O;Ms,"UUDDLRLRBA\n");                          // for loop while input is not equal to the Konami Code
                            Ys,12,N));                // read 12 characters of input into array
                                      J"+30 Lives!\a  // print the string

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C, 114 bytes

#define X "UUDDLRLRBA"
main(i,j){for(i=j=0;!j;)(getchar()==X[i++])&&((j=1?!X[i]|puts("+30 lives\a"):0)|1)||(i=0);}
\$\endgroup\$
0
\$\begingroup\$

Stax, 21 bytes

é>Δ┼3Σöîc9qz╔°→cz╣╖↑└

Run and debug it

Explanation

`!j.o6F>"`=D"+30 lives"7+
`!j.o6F>"`=               input equals sequence?
           D              if so,
            "+30 lives"   push +30 lives
                       7+ add a bell to it           
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 22 bytes

.•›Gβ…Ýò•uQ7ç“+30™‹ÿ“×

Try it online!

.•...•uQ7ç“...“×  # trimmed program
          “...“   # push "+30 livesÿ"...
                  # (implicit) with ÿ replaced by...
         ç        # unicode character with codepoint...
        7         # literal...
               ×  # repeated...
       Q          # is...
                  # implicit input...
       Q          # equal to...
.•...•            # "uuddlrlrba"...
      u           # in uppercase...
               ×  # times
                  # implicit output
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.