Back to Home

The story of insanity or your naval battle at BrainFuck`e

brainfuck · bf · abnormal programming

The story of insanity or your naval battle at BrainFuck`e


    Good day, habra Here is the self-diagnosis of a hopeless BrainFuck patient.
    Those who understand everything from the name and do not want to read the whole post can download the game and BFDev and immediately go under the cut to the end of the post to the "How to Play" section. The post tells how I got sick with BrainFuck, and also describes the process of creating the game "Sea Battle" in this wonderful language.


    Disease history


    Infection

    BrainFuck was created solely to break (if translated culturally, not verbatim) the brain. In short, there are only 8 operators in the language, which allow working with a memory tape of 8-bit cells. More is written on Wikipedia ( eng) When I first read this article, I thought that the language is really esoteric and coding on this is impossible at all. Unless on something normal, write a program that will write programs that will write programs ... However, it was a false impression. Since there was nothing to do, I decided to figure it out. As you can learn on the same Wikipedia, there are many derived languages: both parallel BrainFork and procedural pBF. After re-reading the wikipedia article again, I found a link to the BrainFuck development environment - BFDev. Fortunately, BFDev supports the procedural BrainFuck, which significantly reduces the code without changing the atmosphere of the perverted language. In fact, this is just a way to get away from copy-paste. Everything is implemented through 3 new operators: (and) to describe the procedure, and: to call. The name of the procedure is a number

    The first symptoms

    I immediately started programming: I quickly wrote an adder program that adds up the entered numbers, or rather numbers. Then I learned to multiply and ... And that’s it. I could not invent anything else. It was not possible for me to come up with a division algorithm. Almost a feeling that I could write on “this” anything instantly disappeared, and I returned to earth. I still think that my opinion about the language is true: it is possible to code on this, but BrainFuck in its true meaning begins very quickly. After that, BFDev was abandoned for a long time until again he wanted to stretch his brains.

    Second wave

    Again I was covered after posts on a habre about the sapper and a sudoku on body shirts. I decided that I also knew a lot about perversions, and started writing tic-tac-toe on BrainFack. For this, an algorithm for working with arrays was invented. All the algorithms that will be discussed are described at the end of the post. Drawing a pseudographics field using the data from the array turned out to be quite simple. Writing an element to an array in two coordinates was not a problem either. In what I wrote, it was already possible for two players to play, but the computer did not take any part. The program only dutifully drew crosses and zeros on the screen, without thinking about anything. All this is of course great, but not interesting. Then I tried Sudoku. The system of working with tables has been improved, implemented in tic-tac-toe,

    Beginning of the end

    And now, finally, I switched to the program to which this post is dedicated - Naval battle. Immediately make a reservation that I understand by this name. This is not a classic naval battle, but its modification with a 5x5 field and 4 single-deck ships, the location of which is not subject to any restrictions. And so, by this moment I already knew how to interact with the user and work with tables. Actually, the matter remains small: to realize all kinds of game situations and teach how to play a computer. Problems appeared immediately. There is not even the simplest if on bf, so I had to invent it. When this problem was solved, I turned to the problem with intelligence.

    Computer infection

    Here you need your own random number generator. Thanks to Google, the simplest linear congruent method was found.generating pseudo-random variables. During implementation, two problems arose: in the method, division with a remainder is required, and again, division with a remainder is required to obtain coordinates. Honestly, in solving both problems I cheated. As it turned out, for a random generator it was not necessary to divide at all, it was enough to be able to add modulo n, and to calculate the coordinates, not all numbers had to be divided by a predetermined constant. A random generator is needed for seed, which is the simplest way from the sum of the coordinates of the ships placed by man. Everything seems to be good in the algorithm: uniform distribution over the field, an algorithm for calculating the next move, which is not obvious to humans, and, most importantly, I was able to implement this!

    What is so bad about this algorithm?

    But the bad thing is that it generates a static sequence, and only the number in this sequence, from which the countdown starts, depends on the sid. For this reason, the computer can only play 25 games, and if you remember everything, then calculate the location of its ships is not difficult. Although I still have not remembered what speaks of the not so great significance of this problem. You can certainly use other algorithms for generating random numbers, but, unfortunately, so far the charge of enthusiasm has ended.

    An autopsy revealed (Descriptions of the algorithms)


    The simplest algorithms

    [-] - zeroing the current cell.
    [>] - we go right until we find 0. It is convenient to store the array in the style of si strings.
    [> + <-] - transfer of an element to a cell to the right
    [+++++++++++>, ––––––––––] - request of a line from the user. The only algorithm that has been spied on in the BFDev examples.

    For the incurable

    Comparing a cell value with a given number
    In Brainfuck, you can only compare with zero, so first we subtract from what we are comparing what we are comparing with. Thus, it is possible to use the [] operator to perform the action provided for the case of else and to reset the previously set flag. Accordingly, in [], we write the action in the case of equality on the flag. The subtlety is that the code in square brackets must end on the same cell where it started. Then do not forget to restore the value. Suppose you want to compare with 1, and we are standing on the cell with the element with which we are comparing.
    [> +> + << -] >> [<< + >> -] backup
    + flag setting
    > [code for the case of mismatch> - <[-]]
    > [code for the case of coincidence [-]]

    Plus modulo 25
    The idea is the usual plus, only with a check to see if 25 worked, if so, then we reset it. Saving the old value is not interesting, so there is no backup.
    ([-]> + -------------------------> [-] - <[> + <[<-> +]]
    > [ <<------------------------->> [-]] << +++++++++++++++++ ++++++++++ [> + <-])

    Working with a two-dimensional array
    Suppose we have a two-dimensional array with dimensions 6x22. Such an array is used in the program. Such array sizes are related to the fact that both displayed fields, as well as table image symbols, are stored in one array.
    First, we need to get the number in the array from two coordinates. To do this, add the row number multiplied by 22 to the column number. Next, the most interesting. We need to somehow get to this element. Suppose we are standing in a cell with a counted number, we have a character that we want to write to, or in which we want to write the value to the left, and another empty cell. This whole thing is located right in front of the first element of the array. Copy the first element to an empty cell. We shift two cells with a number and a value by one to the right and decrement the number. Repeat until the number becomes zero. In fact, we shift the first n-1 elements of the array two cells to the left to get to the nth. Next, we copy and shift the first elements into place. To stop, we need a zero in front of the array.

    Division with remainder
    The algorithm for dividing not any number by any, but some, not exceeding a predetermined constant, by a constant. In our case, the dividend is limited to 25, and the divisor is 5. Everything is simple - we decrement the number until it is reset to zero, and increment the result every 5 iterations. The remainder increases at each iteration and is zeroed at every fifth.
    [-> + <[-> + <[-> + <[-> + <[- >> + <[-] <
    [-> + <[-> + <[-> + <[-> + < [- >> + <[-] <
    [-> + <[-> + <[-> + <[-> + <[- >> + <[-] <
    [-> + <[-> + < [-> + <[-> + <[- >> + <[-] <
    [-> + <[-> + <[-> + <[-> + <[- >> + <[-] <






    According to the formula, it is necessary to multiply by a and add c, while everything is modulo 25. Counting according to a simple formula, we get a = 6. c can be anything, and generally speaking, it would be better to count it from Sid, but only in this program c = 2. Using the plus modulo 25 algorithm, we get the code:
    [> [-] :::::: <-]> [-] ::, where: call the plus modulo 25 procedure. This is a simple random number generator.

    How to play


    To run the program, you need the BFDev environment. Through it, open the game file. Next, you need to select the procedural BrainFuck on the toolbar (the rightmost button, near the button with the number 16). Everything is ready, press F9 to start and enjoy the game. The game will start in the Output window, which is below. First you need to arrange 4 of your ship. Read carefully what the game writes. Coordinates are entered as two digits in row-column order (without spaces or separators). Do not enter coordinates greater than 5, or more than 2 digits. Do not put ships in one cell! There are no checks, so the program will just start doing whatever it takes. In case of incorrect input, a loop may occur. For emergency completion, press Ctrl + F2. After arranging the ships, the game will ask you for a seat. A seed can be any number from 0 to 9. This is necessary, so that with the same arrangement of the player’s ships there would be possible different computer behavior. You go first. In case of a hit, the move remains with the one who walked. The game will end as soon as someone breaks all the ships of the enemy. There is one glitch here. The game can only end with a slip. If you win, the game will not end immediately. To complete, you need to shoot at any cell or press Ctrl + F2.
    That's all! Have a nice game!
    PS The link is on dropbox, as the original Bfdev site is not distinguished by sustainable hosting.

    Read Next