
From the previous habratopic, I realized that habrazhiteli really liked the
sapper on his batch file and therefore ventured to write another game, namely Sudoku. And again, on their batch file.
Like Minesweeper, Sudoku for cmd.exe has several advantages:
- Original logo
- Two-color text graphical interface (background - black, text - gray)
- The ability to play the program on almost any computer
In general, this is Sudoku for real men. And you can download this wonderful game right now using these links:
Plain Text |
Google docs
In general, the game Sudoku is one of the most difficult to implement. The problem is in generating a normal table. So, we have a field of 9x9 = 81 cells. In each cell, you can enter one of nine digits. Extremely many variations to solve by brute force. Instead, ready-made tables are used. I will explain the essence.
You can create several tables in which letters are used instead of numbers (Latin square :-)). Only nine letters - abcdefghi. The letter 'a' can be replaced by 9, 8, 7, etc. As a result, we can get 9! = 362880 variations from one table. Not bad? And here is the code that interferes with the numbers:
REM Mysterious algorithm that provides 9! = 362880 variations of the sudoku table.
for / L %% i in (1,1,9) do set replmap %% i = %% i
set replcounter = 0
: replcycle
set temp1 =% random: ~ -1%
if% temp1% == 0 goto replcycle
set temp2 =% random: ~ -1%
if% temp2% == 0 goto replcycle
set / a replcounter + = 1
call set temp = %% replmap% temp1 %%%
call set replmap% temp1% = %% replmap% temp2 %%%
call set replmap% temp2% =% temp%
set "temp ="
if% replcounter% LSS 504 goto replcycle
REM END CYCLE: replcycle
The brief essence of the algorithm: the variables replmap1 - replmap9 are entered numbers from 1 to 9 in a random order. After doing the replacement of letters on the numbers contained in these variables. For example, in replma1 there will be number 5. Then the letter 'a' will be replaced with the number '5'. The map variable contains a randomly selected map.
call: replacestr map a% replmap1%
call: replacestr map b% replmap2%
call: replacestr map c% replmap3%
call: replacestr map d% replmap4%
call: replacestr map e% replmap5%
call: replacestr map f% replmap6%
call: replacestr map g% replmap7%
call: replacestr map h% replmap8%
call: replacestr map i% replmap9%
REM ... A lot of interesting and not very code ...
: replacestr
REM line replacement function.
call set temp = %%% 1 %%
call set temp = %% temp:% 2 =% 3 %%
set% 1 =% temp%
goto: eof
After we have to remove a few dozen digits from the table, which does not constitute a special problem. Also not a problem is the UI, which was brazenly stolen from Minesweeper and slightly modified. The time counting function was also stolen.
')
The second problem in the implementation of Sudoku was the final check: did the user win? One could stupidly count the number of twos, triples and other numbers in each row, column, square, but this is too boring. It was decided to simply count the sum and the product of numbers. A simple, elegant method that turned the devil knows what. By the way, it is possible to determine the exact location of the error by such a method only up to a row / column / square and only with the complete filling of the latter.
I repeat that you can download Sudoku here:
Plain Text |
Google docs