How to write a game for a computer that has no input and output devices
What can be done with a computer on electromagnetic relays which has no input devices, and from output devices only a dump of eight registers?
Play games, of course! The ancient people played on the MK-61/52 calculators, which are only slightly clearer.
Guess the number
I started with the game, where the computer with the help of the half division should find the integer from 0 to 100, which the player has thought up. ')
The computer writes its guess on register A, and then stops.
The values ​​of all registers can be seen thanks to the LEDs built into the relay:
Now the player must change the program code in the ROM, replacing the constant that is loaded into the register D. If there is not 0, this means that the assumption is greater than the guess number. Otherwise - less. After that, you need to press the "continue" button. Checking the register D, the computer uses the previous assumption as the upper or lower limit in the next attempt.
The first version of the program
MOVI B, 1 ; 00: 10000001 00000001 MOVI C, 101 ; 01: 10000010 01100101 Loop: ADD A, B, C ; 02: 01001000 00011010 SHR A, A ; 03: 01111000 00001100 HALT ; 04: 00010000 00000000 MOVI D, guess ; 05: 10000011 iiiiiiii OR F, D, D ; 06: 01101000 00110011 JMP NZ, Greater ; 07: 11100111 00001010 MOV B, A ; 08: 00011001 00000000 JMP Loop ; 09: 10000111 00000010 Greater: MOV C, A ; 0a: 00011010 00000000 JMP Loop ; 0b: 10000111 00000010
The disadvantage of the first program is that it is inconvenient to use. This can be understood by looking at the photograph of the ROM - it is not very easy to switch such small toggle switches while playing the game (and you also have to make sure that the program doesn’t spoil the program accidentally).
So I came up with another input option. For debugging , a toggle switch is provided next to each register to reset its value. Its switch is much more convenient than changing the ROM.
Now the computer enters the unit in register D in advance, and the player either drops it (if the guess is less than the intended number), or leaves it as is (if the guess is more).
The final version of the program
MOVI B, 1 ; 00: 10000001 00000001 MOVI C, 101 ; 01: 10000010 01100101 Loop: ADD A, B, C ; 02: 01001000 00011010 SHR A, A ; 03: 01111000 00001100 MOVI D, 1 ; 04: 10000011 00000001 HALT ; 05: 00010000 00000000 OR F, D, D ; 06: 01101000 00110011 JMP NZ, Greater ; 07: 11100111 00001010 MOV B, A ; 08: 00011001 00000000 JMP Loop ; 09: 10000111 00000010 Greater: MOV C, A ; 0a: 00011010 00000000 JMP Loop ; 0b: 10000111 00000010
Subtraction game (Bache game)
Another classic game that is often implemented by novice programmers is the Bache game. In it, two players from a handful containing initially N items take turns taking at least one and no more than M items. The loser is the one who has nothing to take.
In my version the game goes with a computer. Initially, a handful of 21 items (for example, 21 matches). Players take 1 to 3 matches in their turn, and the one who takes the last match wins.
The game has a winning strategy for the first player - always keep the number of matches, which is a multiple of four. Since 0 is also a multiple of four, the second player will not be able to bring the game to this position, which means he will lose.
The computer goes second, leaving a chance to people, but it does not make mistakes. And if the player does not follow the winning strategy, then the computer will do it.
The first version of the program
MOVI A, 21 ; 00: 10000000 00001101 Loop: HALT ; 01: 00010000 00000000 MOVI B, move ; 02: 10000001 000000mm SUB A, A, B ; 03: 01011000 00001001 AND C, A, 3 ; 04: 01100010 10001011 MOVI Z, C, 1 ; 05: 10010010 00000001 SUB A, A, C ; 06: 01011000 00001010 JMP Loop ; 07: 10000111 00000001
In the first version, the same approach was used as for “guess the number” - in its turn it was necessary to encode the number of matches inside one of the instructions. It was even possible to count and take all the matches at once, but the computer would not notice anything.
But then I rewrote this program too. Now in three registers (B, C, D) the computer enters 1 each, and the player must reset 0, 1 or 2 of them. The remaining units are those matches that he takes. Cheat, too, can be, if you reset all three registers (as if you do not take anything).
The final version of the program
MOVI A, 21 ; 00: 10000000 00010101 Loop: MOVI B, 1 ; 01: 10000001 00000001 MOVI C, 1 ; 02: 10000010 00000001 MOVI D, 1 ; 03: 10000011 00000001 HALT ; 04: 00010000 00000000 SUB A, A, B ; 05: 01011000 00001001 SUB A, A, C ; 06: 01011000 00001010 SUB A, A, D ; 07: 01011000 00001011 AND C, A, 3 ; 08: 01100010 10001011 MOVI Z, C, 1 ; 09: 10010010 00000001 SUB A, A, C ; 0a: 01011000 00001010 JC Exit ; 0b: 10110111 00001101 JNZ Loop ; 0c: 11100111 00000001 Exit: HALT ; 0d: 00010000 00000000
Of course, the program turned out to be somewhat longer, but it is much more convenient to use it.
Next, I plan to add a ROM (now there are only 32 words out of a possible 64), and also to add an input device in the form of a matrix from toggle switches. Maybe then it will turn out to make at least tic-tac-toe.