function func() { cmd1 cmd2 ... cmdn }
func param1 param2 ... paramn
$1
, $2
, ... You can also declare local variables with local
. But there are two features. The first is that the variables have a dynamic scope, rather than the usual static one, that is, the following code i=3 function f() { echo $i } function g() { local i=5 f } function h() { local i=10 f } g h
var i = 3; function f() { alert(i); } function g() { var i = 5; f(); } function h() { var i = 10; f(); } g(); h();
function f() { : }
#
you can simply write :
function f() { : cmd1 : cmd2 : cmd3 }
"\e[ ; ; m"
. The cursor is positioned using the "\e[ ; f"
escape sequence. In this case, the count comes from the unit. Two more escape sequences are convenient: "\e[2K"
to clear the entire current line, "\e[2J"
to clear the entire screen. More details can be found in man console_codes
.COLUMNS
and LINES
. But they are only available online, so the first line is to write #!/bin/bash -i
stty -echo
stty echo
function runLevel() { local -l key local -i time=0 local -i newTime ... while true; do key="" read -t $TIMEOUT -n 1 key newTime=$((`date +%s` * 100 + (10#`date +%N` / 10000000))) case "$key" in $UP_KEY) playerDY=-1 playerDX=0;; $DOWN_KEY) playerDY=1 playerDX=0;; $LEFT_KEY) playerDX=-1 playerDY=0;; $RIGHT_KEY) playerDX=1 playerDY=0;; $EXIT_KEY) break 3;; "") ;; esac if [[ $((newTime - time)) -ge DELAY ]]; then movePlayer moveOpponents time=newTime fi ... done }
-l
attribute of the key
variable allows you to forget about the fact that the CAPS LOCK key can be pressed, since the value of the variable will always be in lower case. And the expression whose value is assigned to newTime calculates the time with an accuracy of millisecond. function initMap() { local -ii local -ij for ((i = 1; i < MAP_HEIGHT - 1; ++i)); do for ((j = 1; j < MAP_WIDTH - 1; ++j)); do map[i * MAP_WIDTH + j]=$SEA done done for ((i = 0; i < MAP_HEIGHT; ++i)); do map[i * MAP_WIDTH]=$LAND map[i * MAP_WIDTH + MAP_WIDTH - 1]=$LAND done for ((j = 0; j < MAP_WIDTH; ++j)); do map[j]=$LAND map[(MAP_HEIGHT - 1) * MAP_WIDTH + j]=$LAND done }
function writeTopScores() { (IFS=$'\n'; echo "${topScores[*]}" > "$TOP_SCORES_FILE_NAME") }
${a[*]}
means the substitution of all elements of an array. Moreover, if this expression is found in double quotes, then the first character from the IFS
variable will be taken as the delimiter. function readTopScores() { topScores=() if [[ -r "$TOP_SCORES_FILE_NAME" ]]; then readarray -t topScores < "$TOP_SCORES_FILE_NAME" fi }
function deleteFreeAreas() { local -a points local -ii for ((i = 0; i < countOpponents; ++i)); do points[i]=$((opponents[4 * i] * mapWidth + opponents[4 * i + 1])) done local -i countPoints=$countOpponents local -i index while [[ countPoints -ne 0 ]]; do index=$((points[--countPoints])) map[index]=$opponentArea if [[ ${map[index + 1]} == $sea ]]; then points[countPoints++]=$((index + 1)) fi if [[ ${map[index - 1]} == $sea ]]; then points[countPoints++]=$((index - 1)) fi if [[ ${map[index + mapWidth]} == $sea ]]; then points[countPoints++]=$((index + mapWidth)) fi if [[ ${map[index - mapWidth]} == $sea ]]; then points[countPoints++]=$((index - mapWidth)) fi done ... }
function deleteFreeAreas() { local -a marks=() local -i mark=MARK_0 ... for ((i = 1; i < MAP_HEIGHT - 1; ++i)); do for ((j = 1; j < MAP_WIDTH - 1; ++j)); do index=$((i * MAP_WIDTH + j)) if [[ ${tracks[index]} ]]; then map[index]=$LAND fi a=${map[index]} b=${map[(i - 1) * MAP_WIDTH + j]} c=${map[i * MAP_WIDTH + j - 1]} if [[ a -eq SEA ]]; then if [[ b -ne LAND && c -ne LAND ]]; then if [[ ${marks[b]} -eq ${marks[c]} ]]; then map[index]=${marks[b]} else d=$(((b < c) ? b : c)) e=$(((b < c) ? c : b)) map[index]=${marks[d]} m=${marks[e]} for ((k = MARK_0; k < mark; ++k)); do if [[ ${marks[k]} -eq m ]]; then marks[k]=${marks[d]} fi done fi elif [[ b -eq LAND && c -eq LAND ]]; then map[index]=$mark marks[mark]=$mark ((++mark)) elif [[ b -ne LAND && c -eq LAND ]]; then map[index]=${marks[b]} elif [[ b -eq LAND && c -ne LAND ]]; then map[index]=${marks[c]} fi fi done done for ((i = 0; i < countOpponents; ++i)); do m=${marks[${map[$(( ${opponents[4 * i]} * MAP_WIDTH + ${opponents[4 * i + 1]}))]}]} for ((j = 0; j < mark; ++j)); do if [[ ${marks[j]} -eq m ]]; then marks[j]=$OPPONENT_AREA fi done done ... }
_ X _ XXXXX _ _ XX _ 1 _ _ 1 _ _ 1 _ _ 1 _ _ _ 2 _ _ 2 _ _ 2 _ _ 2
? X ? ? o _ ? _ _
function compare() { local -r -a pattern=($1) local -r -ax=($2) local -ii for ((i = 0; i < 8; ++i)); do if [[ ${pattern[i]} -ne ANY && ${pattern[i]} -ne ${x[i]} ]]; then return 1 fi done return 0 } rules=( # pattern dy dx "$ANY $SEA $SEA $ANY $SEA $ANY $ANY $ANY" 1 1 "$ANY $LAND $ANY $ANY $SEA $ANY $SEA $SEA" -1 1 "$SEA $SEA $ANY $SEA $LAND $ANY $ANY $ANY" 1 -1 "$ANY $ANY $LAND $ANY $ANY $LAND $ANY $ANY" 0 0 "$ANY $LAND $ANY $ANY $ANY $ANY $LAND $ANY" 0 0 "$ANY $ANY $ANY $LAND $LAND $ANY $ANY $ANY" 0 0 ... "$ANY $LAND $ANY $ANY $ANY $LAND $ANY $LAND" 0 0 ) declare -r -i COUNT_RULES=$((${#rules[*]} / 3)) function findRule() { local -rx=$1 for ((i = 0; i < COUNT_RULES; ++i)); do if compare "${rules[i * 3]}" "$x"; then echo ${rules[i * 3 + 1]} ${rules[i * 3 + 2]} break fi done } function moveOpponents() { local -ii local -iy local -ix local -i dx local -i dy local -a cells local -a rule for ((i = 0; i < countOpponents; ++i)); do y=${opponents[4 * i]} dy=${opponents[4 * i + 2]} x=${opponents[4 * i + 1]} dx=${opponents[4 * i + 3]} clearOpponent $y $x cells[0]=${map[(y + dy) * MAP_WIDTH + x - dx]} cells[1]=${map[(y + dy) * MAP_WIDTH + x]} cells[2]=${map[(y + dy) * MAP_WIDTH + x + dx]} cells[3]=${map[y * MAP_WIDTH + x - dx]} cells[4]=${map[y * MAP_WIDTH + x + dx]} cells[5]=${map[(y - dy) * MAP_WIDTH + x - dx]} cells[6]=${map[(y - dy) * MAP_WIDTH + x]} cells[7]=${map[(y - dy) * MAP_WIDTH + x + dx]} rule=(`findRule "${cells[*]}"`) ((dy *= rule[0])) ((dx *= rule[1])) ((y += dy)) ((x += dx)) opponents[4 * i]=$y opponents[4 * i + 2]=$dy opponents[4 * i + 1]=$x opponents[4 * i + 3]=$dx drawOpponent $y $x done ... }
readarray -t topScores < "$TOP_SCORES_FILE_NAME"
cat "$TOP_SCORES_FILE_NAME" | readarray -t topScores
Source: https://habr.com/ru/post/122029/
All Articles