3 / 3
Just remember to have fun!
our powerpoint



Tutorials       Here's how you actually code in brainfuck......



1. Basics


      The idea behind brainfuck is memory manipulation. You are given a theoretically infinite sized array of 1 bytes.
Within this array, you can either:

' > ' = increases memory pointer, or moves the pointer to the right 1 block.
' < ' = d ecreases memory pointer, or moves the pointer to the left 1 block.
'+ ' = i ncreases value stored at the block pointed to by the memory pointer.
' - ' = decreases value stored at the block pointed to by the memory pointer.
' [ '= start the loop.
' ] '= if block currently pointed to 's value is not zero, jump back to ' [ '
' , ' = get 1 character from input and store it as ascii code value.
' . ' = print the current ascii code value as a character to the console.


If you're familiar with C++, the instruction would be equivalent to:

int arr[infinite] = { 0 }, ptr = 0;
' > ' : ptr++;
' < ' : ptr--;
' + ': arr[ptr]++;
' - ': arr[ptr]--;
' [ ' : for(;arr[ptr]!=0;){
' ] ' : }
' , ' : arr[ptr]=getchar()
' . ' : cout<<arr[ptr];



2.Tips n Tricks

      First of all, memorize the Ascii table. If you can't do it, aleast prepare one for the good.

Now, let 's look at our first program:

    [ < ]

What it does is basically move left to the first ' 0 ' you see.
But that's hardly count as a real code.
So, here's a real code:

+++++[>+<-]

What it does is add the first cell with a number of 5, and add the next cell with 1 then decrease the first cell with 1 everytime until the first cell reaches 0.
So what it translates to C++ is:

for(arr[0]=5;arr[0]!=0;arr[0]--){
    arr[1]++;
}

So it actually copies a cell value to another cell. By changing the inner or outer amount of ' + ', you change the value in the secend cell.


Just by theese knowledges you should be able to read the first simple code of every language : Hello World. We've prepared a simple version of it,because it's your first time.(Comments are written right after the code, it is legal after all. If you want to see how it works, you can copy it into our intepreter.)

++++++++ Set Cell #0 to 8
[
>++++ Add 4 to Cell #1; this will always set Cell #1 to 4
[ as the cell will be cleared by the loop
>++ Add 2 to Cell #2
>+++ Add 3 to Cell #3
>+++ Add 3 to Cell #4
>+ Add 1 to Cell #5
<<<<- Decrement the loop counter in Cell #1
] Loop till Cell #1 is zero; number of iterations is 4
>+ Add 1 to Cell #2
>+ Add 1 to Cell #3
>- Subtract 1 from Cell #4
>>+ Add 1 to Cell #6
[ <] Move back to the first zero cell you find; this will be Cell #1 which was cleared by the previous loop
<- Decrement the loop Counter in Cell #0
] Loop till Cell #0 is zero; number of iterations is 8
>>. Cell #2 has value 72 which is 'H'
>---. Subtract 3 from Cell #3 to get 101 which is 'e'
+++++++..+++. Likewise for 'llo' from Cell #3
>>. Cell #5 is 32 for the space
<-. Subtract 1 from Cell #4 for 87 to give a 'W'
<. Cell #3 was set to 'o' from the end of 'Hello'
+++.------.--------. Cell #3 for 'rl' and 'd'
>>+. Add 1 to Cell #5 gives us an exclamation point
>++. And finally a newline from Cell #6



If you want to know more, please visit Here