Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: nemo on August 16, 2010, 12:40:17 am

Title: Brainfuck Programs
Post by: nemo on August 16, 2010, 12:40:17 am
Brainfuck (http://en.wikipedia.org/wiki/Brainfuck) has become my new hobby. my average number of migraines per minute has skyrocketed. anyway, i decided i'd share some of my programs if anyone's interested. i'll edit in more as i go. if you program in brainfuck and want to add in a few, i'd gladly post them and give credit.

Addition
Code: [Select]
adds the first memory slot to the second
,>,<[>+<-]

Addition
Code: [Select]
adds the first memory slot to the second outputs the answer
,>,<[>+<-]>>++++++[<-------->-]<.

Subtraction
Code: [Select]
subtracts the second memory slot from the first
,>,[<->-]<.


i'll definitely add more later as i become familiar with the language
Title: Re: Brainfuck Programs
Post by: DJ Omnimaga on August 16, 2010, 01:37:53 am
I remember checking this language before once, I sure would get confused a lot at it x.x

Can games be done with this language, by the way?
/me also hopes that Brainfuck becoming Nemo's new hobby doesn't mean the end of his calculator hobby D:
Title: Re: Brainfuck Programs
Post by: miotatsu on August 16, 2010, 02:34:09 am
I am fairly certain there are errors in this program but there is no freaking way I am going to debug it. anyway it is a xp calculator for stick adventures online written in brainfuck. It takes lvl for input and outputs the answer as an ascii character (or at least thats what it would do if it actually worked)

>+++++++++++++++[>>>>>+>+<<<<<<-]>>>>>
>[<<<<<<+>>>>>>-]+++++<<[-]<[-]<[-]<,----------
-------------------------------------->[<->++++++++++][<
+>-]<>,------------------------------------------------[<+>-]
<[>+>+<<-]>>[<<+>>-]<<[>>>>[<<<+>>>-]<<<[
>>>>[<<<+>+>>-]<<[>>+<<-]<[>+<<-[>>-<<]>>[
<-[>>-<<[-]]+>-]<-]>>+<<<]>>>>>[<<<<<+>>>>>
-]<<<<<[>>>>>+>+<<<<<<-]>>>>>>[<<<<<<+>>
>>>>-]+++++<<<<<-]+++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++
+++++++.>.

thats the most complex bf program I have ever written.

@DJ:
http://www.49-6-dev.net/takingovertheworlden.htm
http://jonripley.com/brainfuck/games/

Title: Re: Brainfuck Programs
Post by: _player1537 on August 16, 2010, 03:23:25 am
Oooh, I love BrainFuck :D  Try this one on for size (routine sucks probably >.>)
Code: [Select]
->[->+>+>+<<<]>>>
[-<<<+>>>]<<<<
[->>[-<+>]>[->+<<+>]>[-<+>]<<<<]>,
Multiplies the first number by the second number, and leaves the result in the... 2nd slot I think... Line breaks were added for readability :D
Title: Re: Brainfuck Programs
Post by: thepenguin77 on August 17, 2010, 02:07:55 pm
/me also hopes that Brainfuck becoming Nemo's new hobby doesn't mean the end of his calculator hobby D:

Worry no more. Now you can combine brainfuck with your calculator.

prgmBRAINFCK
Language is exactly like the real thing, ignores formatting and illegal characters. $0A is even new line. On compile, it only includes the subroutine that you need, the text input is like 50 bytes so you don't always want that.

Here are the specifics to make a source file. The first two characters must be "BF". Then there is an optional comma indicating safe mode (more info later). Finally the name of your program followed by an enter. That's it! Now go program to your heart's content.

Then to get it to compile. Store your source name in string 1. Then just run prgmBRAINFCK. It even warns you if you have different amounts of ['s and ]'s.

The safe mode I added is so that your programs will never clear ram. It puts a little subroutine before every ] that checks for ON presses.

Enjoy.
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 17, 2010, 02:14:12 pm
Haha, nice. Where does the memory pointer start, so we can be sure not to corrupt RAM?
Title: Re: Brainfuck Programs
Post by: thepenguin77 on August 17, 2010, 04:01:56 pm
It uses appBackUpScreen. So as long as you don't do +[>[-]+]. You shouldn't have any problems.
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 17, 2010, 04:05:39 pm
Nice to know. :)
How hard (and how large in the generated program) would it be to support more characters? (Right now I'm thinking of the language's own []<>+-,. so I can run a self-interpreter ;D)
Title: Re: Brainfuck Programs
Post by: Builderboy on August 17, 2010, 04:07:22 pm
Does it start at the beginning or in the middle?  Like if we move back 1, are we out of the appBackUpScreen?  Oh and epic program ;D
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 17, 2010, 04:09:26 pm
Interpreters normally don't define what happens if you go back beyond the zero cell, and I assume thepenguin77 would do the same. I'd guess that it starts at appBackupScreen, then.
Title: Re: Brainfuck Programs
Post by: thepenguin77 on August 17, 2010, 04:25:17 pm
Silly fix. You couldn't turn your calculator off if you used the key input.

Supporting extra characters is super easy. I currently have a big list of (cp xx \ jr nz, next)'s that I use. The finished product is output in pure assembly so it's size all varies with what you want. Very unoptimized assembly I might add. (32 straight inc (hl)'s in some cases)

The program starts right at the beginning of appBackUpScreen. If you need more space, use a whole bunch of >'s.

Here is a program that takes a 1 digit input, squares it, and outputs the 2 digit result.
Code: [Select]
:BF,AA
:,----- ----- ----- ----- ----- ----- ----- ----- ----- ---
:[
:>+
:>+
:<<-
:]
:>
:[
:>
:[>+>+<<-]
:>
:[<+>-]
:<<-
:]
:>>+++++ +++++.
:>[>+<-]     ;hex to decimal starts here (after the >), give it the number in (current) and it will output the result
:>
:>+++++ +++++
:<
:[
:>
:[>+>+<<-]
:>
:[-<<-[>]>]
:>
:[<<+>>-]
:>+
:<<<<
:]
:+++++ +++++
:>[<->-]<
:[>+<-]
:+++++ +++++ +++++ +++++ +++++ +++++ +++++ +++++ +++++ +++
:[>+>>>>+<<<<<-]
:>>>>>.<<<<.
:>+++++ +++++.

There's a hex to decimal converter in there. It just can't accept numbers that end in 0. 20 is 1:.

Edit:
   Forgot to attach program. Source's there too if you want it.
Title: Re: Brainfuck Programs
Post by: DJ Omnimaga on August 18, 2010, 12:17:44 pm
lol nice program ThePenguin77 ;D

I remember juju2143 made one a few months ago but it was in BASIC and interpreted, so some stuff probably took an insane amount of time to execute. It was still cool, though.
Title: Re: Brainfuck Programs
Post by: ztrumpet on August 18, 2010, 12:25:28 pm
Wow, that's neat.  Excellent job The Penguin! ;D
Title: Re: Brainfuck Programs
Post by: patriotsfan on August 18, 2010, 09:00:18 pm
Brainfuck on the calculator? That is pretty impressive to say the least. Awesome job! :D
Title: Re: Brainfuck Programs
Post by: DJ Omnimaga on August 21, 2010, 12:22:50 pm
/me wonders if Ztrumpet could write Starcraft in brainfuck
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 21, 2010, 12:59:09 pm
I managed to find a decent sized game here: http://jonripley.com/i-fiction/games/LostKingdomBF.html (http://jonripley.com/i-fiction/games/LostKingdomBF.html)
The game was originally written in BBCBasic at 2.74KB, but the BF is 2.08MB x.x
Too bad it's too large for the calc :P
Title: Re: Brainfuck Programs
Post by: DJ Omnimaga on August 21, 2010, 01:01:56 pm
Yeah x.x. I wonder if anyone ever did some sort of semi graphical games with that.
Title: Re: Brainfuck Programs
Post by: skuller972 on August 21, 2010, 01:15:51 pm
Fibonacci Sequence!!!!

Code: [Select]
BF,FIB
>+                 ;sets cell 0 as 0 and cell 1 as 1, to start the sequence
[                  ;holds entire sequence loop in brackets
[>+>+<<-]          ;duplicates cell 1 into cell 2 and cell 3
>[<+>-]            ;scootches cell 2 over to cell 1
>[<+>-]            ;scootches cell 3 to cell 2
<<<[>>+<<-]        ;adds cell 0 to cell 2, deleting cell 0, and setting cell 2 as the current answer
.>,[-]<            ;displays current answer, then asks for input, then deletes inputed cell, for a pseudo pause
>[<+>-]            ;scootches cells over again
>[<+>-]<           ;now all cells are scootched and set for another loop
]                  ;ends loop
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 21, 2010, 01:49:36 pm
Nice, except that will display the characters $00,$01,$02,$03,$05,$08,$0D,$15,$22,$37, and so on, not the numbers.
Who wants to write a divby10 routine? (I will momentarily if no one else does)
Title: Re: Brainfuck Programs
Post by: skuller972 on August 21, 2010, 03:07:35 pm
whoops! i forgot i didnt show numbers. my bad.
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 21, 2010, 04:01:24 pm
Blah, I'll write the routine when I have more free time. It's not very fun when you have to test two conditions at once (which is required in the routine AFAICT).
Or maybe I can find one online :P
Title: Re: Brainfuck Programs
Post by: SirCmpwn on August 21, 2010, 04:02:40 pm
Oh god, I hate brainfuck so much x.x
Title: Re: Brainfuck Programs
Post by: skuller972 on August 21, 2010, 04:13:39 pm
i just spent like 30 minutes trying to figure out how to display decimal. no luck. ps SirCmpwn you are about to be 1337 posts
Title: Re: Brainfuck Programs
Post by: SirCmpwn on August 21, 2010, 04:27:47 pm
And......
YEAH!
Brainfuck is named quite well, it fucks with your brain.

1337 Posts!!!!1!111!!!!
Title: Re: Brainfuck Programs
Post by: patriotsfan on August 21, 2010, 08:55:57 pm
Unless it provides pleasure for the select few who actually like being confused! :P
Title: Re: Brainfuck Programs
Post by: skuller972 on August 22, 2010, 01:15:14 pm
got it! fibonacci sequence, printing the decimal values of the numbers. it only goes up to 255 though, since its hex, so after 233 the fibonacci seequence gets wonky. I didnt write the part that prints the decimal values, so i enclosed it in asterisks. heres where i got it http://mazonka.com/brainf/


Code: [Select]
>+
[
[>+>+<<-]
>[<+>-]
>[<+>-]
<<<[>>+<<-]
>[<+>-]
>[<+>-]<

[>>>>>+>+<<<<<<-]>>>>>[-<<<<<+>>>>>]>              ;copies cell 1 to cell 5 and 6, then moves cell 5 back to cell 1, in essence just copying cell 1 to cell 6, giving space for the decimal printer

***************************************
  [ -<++>
  [ -<+>
    [ -<+>
      [ -<+>
        [ -<+>
          [ -<+>
            [ -<+>
              [ -<+>
                [ -<+>
                  [ <[-]+> ->+< [<-] ]
  ]]]]]]]] >]

<<[>++++++[<++++++++>-]<-.[-]<]
***************************************

<<<<                                                ;moves pointer back to cell 1 for sequence
>,[-]<
]
Title: Re: Brainfuck Programs
Post by: DJ Omnimaga on August 22, 2010, 04:12:21 pm
darn this language looks so much harder to read than z80. It has to do with the fact it uses no alphabet and the like x.x
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 22, 2010, 07:42:54 pm
That, combined with the fact that z80 ASM has nice mnemonics, like add, sub, ld, etc. while BF has only 8 symbols and can't have subroutines that make for nice, structured programming ;D
I doubt the existence of a good number of people who can easily understand what a piece of BF does. :P
Title: Re: Brainfuck Programs
Post by: skuller972 on August 23, 2010, 10:08:20 am
I tried learning asm, but either I didnt try hard enough to learn or I just didnt get it, but I understand brainfuck a lot more than asm
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 23, 2010, 10:21:09 am
What does this do? (No running it ;D)
Code: [Select]
>+++++++++[<+++++++++++>-]<[>[-]>[-]<<[>+>+<<-]>>[<<+>>-]>>>
[-]<<<+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+<
-]<<-<-]+++++++++>[<->-]>>+>[<[-]<<+>>>-]>[-]+<<[>+>-<<-]<<<
[>>+>+<<<-]>>>[<<<+>>>-]>[<+>-]<<-[>[-]<[-]]>>+<[>[-]<-]<+++
+++++[<++++++<++++++>>-]>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-
]<<<<<<.>>[-]>[-]++++[<++++++++>-]<.>++++[<++++++++>-]<++.>+
++++[<+++++++++>-]<.><+++++..--------.-------.>>[>>+>+<<<-]>
>>[<<<+>>>-]<[<<<<++++++++++++++.>>>>-]<<<<[-]>++++[<+++++++
+>-]<.>+++++++++[<+++++++++>-]<--.---------.>+++++++[<------
---->-]<.>++++++[<+++++++++++>-]<.+++..+++++++++++++.>++++++
++[<---------->-]<--.>+++++++++[<+++++++++>-]<--.-.>++++++++
[<---------->-]<++.>++++++++[<++++++++++>-]<++++.-----------
-.---.>+++++++[<---------->-]<+.>++++++++[<+++++++++++>-]<-.
>++[<----------->-]<.+++++++++++..>+++++++++[<---------->-]<
-----.---.>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>>+++
+[<++++++>-]<--.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.
><+++++..--------.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++
++++++++++++.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<++
+++++++>-]<--.---------.>+++++++[<---------->-]<.>++++++[<++
+++++++++>-]<.+++..+++++++++++++.>++++++++++[<---------->-]<
-.---.>+++++++[<++++++++++>-]<++++.+++++++++++++.++++++++++.
------.>+++++++[<---------->-]<+.>++++++++[<++++++++++>-]<-.
-.---------.>+++++++[<---------->-]<+.>+++++++[<++++++++++>-
]<--.+++++++++++.++++++++.---------.>++++++++[<---------->-]
<++.>+++++[<+++++++++++++>-]<.+++++++++++++.----------.>++++
+++[<---------->-]<++.>++++++++[<++++++++++>-]<.>+++[<----->
-]<.>+++[<++++++>-]<..>+++++++++[<--------->-]<--.>+++++++[<
++++++++++>-]<+++.+++++++++++.>++++++++[<----------->-]<++++
.>+++++[<+++++++++++++>-]<.>+++[<++++++>-]<-.---.++++++.----
---.----------.>++++++++[<----------->-]<+.---.[-]<<<->[-]>[
-]<<[>+>+<<-]>>[<<+>>-]>>>[-]<<<+++++++++<[>>>+<<[>+>[-]<<-]
>[<+>-]>[<<++++++++++>>>+<-]<<-<-]+++++++++>[<->-]>>+>[<[-]<
<+>>>-]>[-]+<<[>+>-<<-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<>>[<+>-]<
<-[>[-]<[-]]>>+<[>[-]<-]<++++++++[<++++++<++++++>>-]>>>[>+>+
<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>[-]>[-]++++[<++++++++>
-]<.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.><+++++..---
-----.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++++++++++++++
.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<+++++++++>-]<-
-.---------.>+++++++[<---------->-]<.>++++++[<+++++++++++>-]
<.+++..+++++++++++++.>++++++++[<---------->-]<--.>+++++++++[
<+++++++++>-]<--.-.>++++++++[<---------->-]<++.>++++++++[<++
++++++++>-]<++++.------------.---.>+++++++[<---------->-]<+.
>++++++++[<+++++++++++>-]<-.>++[<----------->-]<.+++++++++++
..>+++++++++[<---------->-]<-----.---.+++.---.[-]<<<]
If you do run it though, it's a fun program (not mine, though, I must say)
My only point is that things get confusing really quickly :P
But who knows, you probably understand this language better than I do :)
Title: Re: Brainfuck Programs
Post by: skuller972 on August 23, 2010, 10:38:17 am
Yea. It sets up cell 1 as 90 for the ten's place and cell 2 as 9 for the one's place, then does a few functions that subtract ten's places if theres no one's place, and sets the one's place back as 9. Then it starts putting out the text and the values of cell 1 and 2 and singing '99 bottles of beer on the wall, 99 bottles of beer take one down pass it around 98 bottles of beer on the wall' etc. You can slow it down and go step by step by putting a comma after the last decimal and before the '[-]' on the very last line. and just hit enter to keep going. I did run the program to see the text, but up till that part I kind of figured out how the program worked. Im not exactly sure about it all though.
Title: Re: Brainfuck Programs
Post by: calcdude84se on August 23, 2010, 10:50:38 am
Good job :D
Also, offtopic, you have int(100*pi) posts :P
Title: Re: Brainfuck Programs
Post by: DJ Omnimaga on August 23, 2010, 10:09:26 pm
/me wonders if calc84maniac could write Starcraft in BF