📜 ⬆️ ⬇️

We learn bash-scripts, we write Sokoban

It seems to me that there are still people in the world who know several programming languages ​​well, but do not write scripts for bash, because the bash scripting language looks too strange for them. To prove that bash is easy, I wrote the game Sokoban (or Loader, as you like), and I want to tell you how it works.



Briefly about the main thing


Bash variables

In bash there are no data types, and even when assigning, you cannot put extra spaces:
y=11 # B="" #    x = 3 #  -  

To read the value of a variable, you must put a dollar sign in front of its name. It is not forbidden (and sometimes necessary) to enclose the name of a variable in curly braces, and then put a dollar sign in front of the opening bracket. Arithmetic operations (integer) can be performed as follows:
 r=$(( $x + $y )) #        r=$(( ${x} + ${y} )) #    

Arrays in bash are only one-dimensional, they do not need to be initialized. You can access the data by index, as well as set in batches:
 map[3]=4 map[${r}]="_" map=( 1 2 3 4 5 6 ) #    


Input and Output

The echo command is used for output. It supports escape sequences, and it is very convenient.
 echo "Hello, world!" #, ! echo -en "\E[3;3f Hello, world!" #     (3;3),      echo "${x}" #    echo -en "\E[${x};${y}f Hello!" #,       escape- 

Use the read command to enter.
 read B # ,     B read -n 1 B #      read -t 1 -n 1 B #        

')
Control structures

If-then-else construction:
 if [[ "$B" = "Q" ]] # ,       then #  fi #  if [[ "$B" -eq 3 ]] #  then # fi #  

Constructions while and case:
 while ( [ "$B" != "Q" ] ) do #  - ,       # done case "$B" in #  switch "W" ) 1;; #      "S" ) 2;; [QZ] ) 3;; #  ,    esac # case 

For loop:
 for (( value=1 ; value<LIMIT; value++ )) do # LIMIT -  # done 


Sokoban interactive computer game


 #!/bin/bash #  Sokoban #    map=( WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWW _ _ _ WWWWWWWWWWWW WWWWW = _ _ WWWWWWWWWWWW WWWWW _ _ = WWWWWWWWWWWW WWW _ _ = _ = _ WWWWWWWWWWW WWW _ W _ WW _ WWWWWWWWWWW W _ _ _ W _ WW _ WWWWWW _ _ oo W W _ = _ _ = _ _ _ _ _ _ _ _ _ _ _ oo W WWWWW _ WWW _ W _ WWW _ _ oo W WWWWW _ _ _ _ _ WWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW ) #     x=9 y=11 #       B,        B="" #   (     ) LIMIT=20 #  echo -en "\E[2J" #     while ( [ "$B" != "q" ] ) do #    for (( mx=1 ; mx<LIMIT; mx++ )) do for (( my=1 ; my<LIMIT; my++ )) do r=$(($mx*20+$my)) #    ,    echo -en "\E[${mx};${my}f${map[${r}]}" done done #      echo -en "\E[22;2fWASD - move, Q - quit" echo -en "\E[23;2fW - wall, X - hero, = and @ - chest, o - place for chest" #  -  (     ,   ) echo -en "\E[${x};${y}fX\E[${x};${y}f" #       B="" #    read -s -t 1 -n 1 B # ,        nx=0 ny=0 #  ,        case "$B" in [wW] ) nx=$(( - 1));; [sS] ) nx=$(( 1));; [aA] ) ny=$(( - 1));; [dD] ) ny=$(( 1));; # ,   -  CAPS LOCK [qQ] ) B="q";; esac #  ,      r=$(( ($x + $nx) * $LIMIT + $y + $ny )) #  -    r2=$(( ($x + $nx + $nx) * $LIMIT + $y + $ny +$ny )) #    ,  if [[ "${map[${r}]}" = "_" ]] then #    x=$(( $x + $nx )) y=$(( $y + $ny )) fi #       if [[ "${map[${r}]}" = "o" ]] then x=$(( $x + $nx )) y=$(( $y + $ny )) fi #,    ? if [[ "${map[${r}]}" = "=" ]] then #   ,    if [[ "${map[${r2}]}" = "_" ]] then map[${r2}]="=" map[${r}]="_" x=$(( $x + $nx )) y=$(( $y + $ny )) fi #     -    if [[ "${map[${r2}]}" = "o" ]] then map[${r2}]="@" map[${r}]="_" x=$(( $x + $nx )) y=$(( $y + $ny )) fi fi #  ,     if [[ "${map[${r}]}" = "@" ]] then #    - ,   if [[ "${map[${r2}]}" = "_" ]] then map[${r2}]="=" map[${r}]="o" x=$(( $x + $nx )) y=$(( $y + $ny )) fi #     -    if [[ "${map[${r2}]}" = "o" ]] then map[${r2}]="@" map[${r}]="o" x=$(( $x + $nx )) y=$(( $y + $ny )) fi fi #        done #   Q -       echo -en "\E[2J" 


Whew! Look like that's it. Of course, I recommend reading more about bash scripting .

I look forward to the emergence of a 3D engine, or at least an arcade platform game on bash. In JavaScript, you can ...

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


All Articles