📜 ⬆️ ⬇️

The first game I just wrote for myself

Post nostalgia for toys that we wrote for ourselves in childhood.
Climbing across the expanses of the App Store, looking for another toy for my iPad, I came across an old toy “Pilot Brothers”. Immediately bought, set and passed in one breath (probably already 3 times). But most of all lingered on the episode with the refrigerator. This puzzle hurts me.



And suddenly I remembered that even during my school years, after going through the refrigerator, the puzzle covered me so much that I wrote my own toy on pascal - a puzzle emulator. But she did not just repeat the original game, but allowed to choose the size of the field, had a help, the ability to save and continue the game from the last place after the release.
')
Raising old backups, I found the original game. I started it in the dosbox and disappeared for the outside world for a couple of hours.



The original puzzle, notice the open left column and the position of the valves

What is the point?



The essence of the puzzle is quite simple. There is a 4-by-4-valve field; all valves on the refrigerator should be placed in a horizontal position. “But there is a nuance” (c): when you turn the valve, all the valves in the column and row in which the valve is located turn with it. And then the fun begins!
When I first sat down for this puzzle in the original quest, playing while still in school, I passed it, probably 40 minutes, no less. But I liked her so much that I played it, played and played, and played ...
But I wanted more: the ability to save the gameplay, generate different levels and change the size of the field to understand the algorithm of the game.
Then I realized that if I like the puzzle, and I want to try different scales of the playing field, then you should not wait for the weather by the sea. There is only one way out - to realize this game yourself.

Well, let's get started!



I can not accurately determine the date of the creation of the game, but this is exactly the end of the school, the beginning of the institute. In those years, I perfectly owned pascal. At the institute he solved problems for money, helped write cursors and in general it was a great logical language. Therefore, it was decided to write a toy on it.
Despite the fact that I knew how to implement graphics in DOS, I decided not to make a graphical application. This is due to the fact that it would be terribly buggy and slowed down that, firstly, I wanted to implement this application as quickly as possible, and the implementation of graphics would take a lot of time; and secondly, I like the text view more. While looking back, it seems to me that I would be better if I did the graphics and added support for the mouse.
The task was simple: to make a simple elegant game, which would not be ashamed to show. Of the possibilities: help, the ability to save and after launching the continuation of the game from the moment it was stopped, the ability to change the size of the playing field. Plus a little color and grace.

Implementation



I will not fully understand the source code and the logic of the entire program, because they are obvious, and what is not clear from the code. But I’ll focus on a couple of moments in more detail.
The basis of the game is a text array of NxN elements, where N can be equal to 2, 4, 8 and 16. The array is filled with only two characters X and Y. The position of the cursor is the position of the current element of the array i and j. When the array element is inverted, X is replaced by Y and Y by X with the column and row of the array. Well, plus there are hot keys (F1-F4, F10) that allow you to call Help, Save / load, create and exit. Everything is clear here, and how to implement simply and we will not dwell on this in detail.

From the cognitive point of view, the WRIFT procedure is of the greatest interest. *

* - note why wrift
In those days, as an economy, sms were written in transliteration. As we remember the letter “ ” is replaced by the combination “ sh ”, but to save space, I replaced it with an unused letter with a similar writing “ W ”. Hence the name Wrift (Font). Similarly, the letter “ X ” ( X ) replaced the letter “ X ” (Russian “ ha ”).


Since I decided to use the text mode for the game, I needed a letter that would replace the valve. What does this letter, which would have shown the valve in a vertical and horizontal position. The only candidates that are suitable for this purpose are the Latin letters “N” and “Z”. The letter "Z" resembles the laid on the side of the letter N, only stretched.
I tried this option and was not satisfied with it. And I realized that it was necessary to make something of my own, some kind of alternative solution. I already thought about writing a graphic version, when I came across the Pascal program “The Matrix” from the author Abrarov A.M. . The program starts the same way as the first “Matrix” - displaying lines, searching for code. And then hieroglyphic characters begin to pour. What does it do only in text mode.

A small educational program about the program Matrix.pas
The program was very beautiful and elegant for its time. And then I was very delivered. The most interesting thing is that the full font is implemented there, for 208 characters! But I assume that the author took the implementation of a program like our “localizer”, only for hieroglyphs and remade it for yourself.

True, now in order to make the program work normally, even at dosbox, it is necessary to reduce all delays every 10 (delay), otherwise your grandchildren will wait for the end of work. Since I am too lazy to alter the program, it is of purely academic interest. Or maybe someone will remake it and show it to the world, the softphone is beautiful (the more I give a link to the sorts). And if suddenly its author reads this post, then I give him my words of gratitude.


Most importantly, in that program, for “cracking” your font is implemented! And it is loaded. I borrowed this loading procedure, of course, by slightly changing it.
A few words about working with fonts.

In MS-DOS, BIOS tools support working with raster fonts. The BIOS functions allow you to get and install custom fonts, as well as get fonts from the video display character generator. All installed fonts have the same width of 8 points, and the height can have three fixed values ​​- 8, 14 or 16 points. The specific font height is determined by the video mode for which the font is loaded. The font height of 8 points corresponds to the video mode with 50/43 lines, the height of 14 points to the video mode with 25 lines for EGA, and the height of 16 points to the VGA video mode with 25 lines.


Taken from here

We will use the video mode 16 colors 40x25 characters. In this video mode, each character is an array of 8x16 points. Simply put, each character is 16 bytes, where in a byte a bit set to one means that it is painted black. If you now have a mess in your head, do not worry - it will become clearer.
To indicate a closed (vertical) valve, the letter “X” was chosen (Latin capital letter “X”) and for the open horizontal letter “Y” (Latin capital letter “Igrek”). The choice was made on these letters for several reasons. Firstly, they are familiar from the school curriculum and at the hearing. Secondly, and more importantly, they are in a row in the ASCII character table in a row (88d and 89d position, respectively), which simplifies their replacement. And the Latin ones are chosen so that you can make the menu in Russian, without reloading the font.
So, we decided on the symbols, now let's move on to their clothes, aka font. We take a sheet of paper in a cage, and make it a field of 8x16 and proceed to drawing the symbol of the horizontal and then the vertical valve. Since the valve must be the same in both vertical and horizontal state, we will use only the first 8 lines to get a square. And then we draw in this field two valves, vertically and horizontally. It should be something like this.


Font two gates

Further, the black box is denoted by one, white is zero. And form 8 bytes. Then we translate them into decimal format and form an array. First, 8 bytes of a character, then 8 bytes of zeros (we left the basement empty), we repeat this procedure for the second character.


Binary Codes Table and Decimal Translated

As a result, we get the following procedure header:

procedure WRIFT; {Zagruzka shrifta ("pesochnye chasy")} Const Font:array [1..32] of Byte=(129,195,231,153,153,231,195,129,0,0,0,0,0,0,0,0, 255,102,36,24,24,36,102,255,0,0,0,0,0,0,0,0); Var SegScr:Word; OfsScr:Word; screen1:Array [0..512] of Byte absolute Font; 


Font is an array of two valve symbols (each character on its own line). The most interesting variable is the array screen1 - the array, which is located at the absolute address of the variable Font ( absolute directive). Next comes the most interesting.

 begin ofsscr:=ofs(screen1); segscr:=seg(screen1); Asm push bp mov ax,segscr mov es,ax mov bp,ofsscr mov bx,1000h mov dx,88 {s kakogo simvola na4at' zapis' 88d-X,89d-Y} mov cx,2 {koli4estvo simvolov} mov ax,1100h int 10h pop bp mov ah,1 {ustanovit videorezhim txt, tsvetnoj (16 cvetov) 40x25 } mov cx,1000h int 10h End; end;{WRIFT} 


As is clear from the lines above, we load the ES segment into the register, and into the BP register the address offset is the location of our font. With the mov bx, 1000h command we say that we have 16 lines in the font (the number 10h = 16d is loaded into BH, and BL = 0). Next, in the DX register we load the character number from which we start changing the font. And in CX - the number of characters replaced. We load into the register AX - the number of the BIOS function for changing the font, and cause a BIOS interrupt. Then we restore the BP register and in the same way set the video adapter mode. Details described here: shackmaster.narod.ru/fonts.htm . For a better understanding of the structure of fonts, it will be better to read this site.

So here is a simple way, you can change the entire font in dos on your own. To unload the font, use the Restore procedure:

 Procedure Restore; {Sbros shrifta} Begin asm mov ax,3 int 10h End; End;{Restore} 


Which is performed when displaying the menu and exiting the program.

By the way, for reference, the so-called “locators” worked in the same way in DOS. They simply replaced the current font with a font with Russian letters.

One of the jokes
One of my friends , specifically, termi , when he studied programming in Borland C ++, made the font in the form of a MHP, also using the example from “The Matrix”. Those. just replaced all the characters with a single character MPH. Norton Commander looked scary! Sorry for pictures from that time left.
Such a program would run on computer science, then the teacher would be happy.


Modifications today



In fact, the game I give is not in the form in which it was written many years ago. It found very unpleasant shoals:
1. The program just crashed if the GAME.SAV file was not detected;
2. The random number generator was not initialized, so it always generated the same level.
Plus, due to problems with encodings, I created a second version of the game, completely in English. And if you read the source code in a non-DOS-compatible editor, I recommend using its source code - there will be no problems with encoding.

So it is gratifying that DOSbox is everywhere, I personally edited most of the program on iPAD in the subway. Can you imagine how funny pepper looks with an ipad, and Borland Pascal open on it?


Good old Borland Pascal on iPAD

So shoals promptly corrected.

Several screenshots of the program, with a brief description



For those who do not have the ability or desire to play the game, here are some key screenshots of the game.
After launch, we have a playing field with the last saved game


Start

The cursor is highlighted and flashing.

If you press F1, you can get such a simple help



HELP

To generate a level, press F4 and get into this menu.


Level generation menu

Well, after winning the game will congratulate you


Congratulations on winning

And will wait for pressing any key. In this case, the progress of the game is not saved.

How to install and play



Due to the features and the use of font download, igruha works only in a clean tube dos. According to this, for its work it is necessary to use dosbox or a similar program.
Installation is trivial. Download the archive with the program . Install dosbox . And, if you want, Russify it, the linux method is: old-game.org/?p=1717 .
In principle, all these files can also be used for Windows: For this, the file russian.txt is placed in the folder with the program and the config, and the label for the dosbox is edited. There should be something like this in the path of the shortcut: “ “ C: \ Program Files \ DOSBox-0.74 \ DOSBox.exe ”-userconf” Remove “-userconf” and your config will work.

In Ubuntu, I just right-click on the program and choose to run it in dosbox, as in Windows, I do not know, because it is not on any of the machines. Well, in the extreme case, the path to the game is mounted, with the mount command, the path.
You have a choice of Russian and English version. You can not fool with Russification and hemorrhoids with configs, but simply bullet into English. There is practically no difference between them. Although, personally, as an author, I prefer the Russian version.
The control is performed using the keyboard arrows, selecting and inverting with the insert key or by double pressing the spacebar (yes, there is a cant). If you suddenly pick a position, press the space button once.

Remember that each time the last saved game is loaded. To generate a new one, press F4. I recommend starting with a 4x4 field. I'm playing 8x8 now, and for now I'm afraid to switch to 16x16. For 4x4 I pass in less than a minute, 8x8 minutes in 7-10, and I'm afraid to even imagine 16x16.
Remember that the boot field labeled HABR and my nickname (picture in the entry point of the article) will disappear after saving or legal exit. It is stored in the file GAME.SAV . If you want to restore, then replace this file with a file from the archive.

Field specifically for Habr
I did not want to just let the game start, even if it was completed and finished for the habr. I wanted some kind of chip. And I decided to make an inscription in the largest field 16x16.
The format of the save file is simple: the beginning is the size of the field, for example, the number 16, the carriage return, then 256 (for the 16x16 field) X and Y characters that make up the field drawing, and at the end the number of Y in the field (such a checksum).
To do this, I formed such a field in OF Calc, made a color in the cells in color. And then put down the letters X and Y, respectively.

Preparation for the field.

Then saved it to CSV. Removed all delimiters. I counted the number of Y (very simple, you make a search in OF Writer, replace it with and replace Y with a thread, it says the number of replacements is the desired number). Put the number 16 first, the carriage transfer and at the end the number of players. You can evaluate the result yourself :).


Disadvantages of the current version of the program.



I wrote the current version ... It is unlikely that I will rewrite the program and improve it, because it is terribly outdated. But on the other hand, now dosbox can almost be put on a flashlight, so it can gain a new life, and it may be worth rewriting and correcting the main flaws.

So:

1. The first, and most unpleasant, flaw is poor management. Firstly, the invert goes with the Insert key, or double space. With that, if you press the space an odd number of times, the program “sticks” until you make an even number of clicks. It is annoying.
2. The second major drawback is drawing the entire game using the write / writeln functions. At each event, the screen is redrawn anew. When playing on a 16x16 field, brakes and flash flashes are already noticeable. Plus, this approach has complicated the logic of the program, and now it is a huge problem to fix.
It was necessary to implement the drawing using the function goto (x; y); Then it would be possible to change a single character without redrawing the entire screen.
3. The game just asks for mouse support. Again, mouse support needs to be implemented a couple of paragraph 2, so that you can accurately “catch” the coordinates.
4. A minor bug that follows from step 2 - when you go down to the lower right corner, one character runs out of the frame. I tried to fix it, and I realized that it’s easier to rewrite this place of drawing at all, than to figure out how it works.

What I would like to do



Of course, I would like such an application under the bucket or iPad. To be able to play it with your finger. Or, if not an application, then a browser-based toy (but one that works on Android and iOS). That there was a beautiful menu and easy navigation. There was a table of records. There was a time counter that you could brave with friends. There was also statistics so that you can estimate the average time and speed of the skill.
In general, a huge flight for creativity, in the absence of free time to learn programming for these devices. On the other hand, dosbox is everywhere, and probably more universal portability will be difficult to achieve, and can it make sense to finish this program? What do you think?

Results ...



The purpose of the article is not to show how good I am, but just to remember how it was. Surely almost every one of us wrote something like this in childhood. This is not ah what a difficult program, and it can be written by each student, with sufficient perseverance. Here, rather, post memory.
I used to use assembler a lot in programs. I liked to “talk” with the iron directly, without intermediaries. I felt the car better. And even then, for AVR, I wrote exclusively in assembly language. And now I do not understand how a computer functions. Of course, I present the basic blocks, etc., but I don’t know the detailed picture, and the funny thing is that no one knows it!
And now ... Languages ​​of high and super high levels of abstraction generally force us not to think about how and where the code is executed. This is undoubtedly good, because they allow you to focus directly on the task, but on the other hand that warm lamp charm of working with iron goes away ...

References and references:

1. Description of how to load a font under DOS shackmaster.narod.ru/fonts.htm
2. An excellent guide to assembler, which gave me an understanding of how to work with the functions of the BIOS, and indeed with the hardware: “Programming at the hardware level: a special reference.” Fists
3. DOS emulator dosbox www.dosbox.com/download.php?main=1
4. Program and source narod.ru/disk/65499856001.6f143511c7c3c2ad7866f07008d8db29/INVERTOR.zip.html
5. The sources of the “Matrix” program narod.ru/disk/65499898001.a6c953f20e2ee023bd00e3518e22fea8/Matrix.pas.html

PS Fuh, I probably remembered a couple of days and googled, how did I make this font. The code is there, it works, but as I do not remember and do not understand.
PPS If people are interested, I will add a video of how I walk the 4x4 field in less than a minute (using the example of the Pilot Brothers refrigerator).
PPPS I would be grateful for all the comments on spelling and punctuation sent by private message!

Source: https://habr.com/ru/post/166057/


All Articles