📄 tetris.ksh
字号:
# $1 = X, $2 = Y, $3 = ROTATION, $4 = 0/3#function Define_or_Wipe_Five{# Define these as localtypeset -i POS X Y# Assign the argumentsX=$1; Y=$2# Calculate position on board arraylet POS=$X/20+$Y/2# Determine position, then wipe or drawif [[ $3 -eq 0 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS+2]=$4 let BOARD[$POS+11]=$4elif [[ $3 -eq 1 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS-9]=$4 let BOARD[$POS+11]=$4elif [[ $3 -eq 2 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS+2]=$4 let BOARD[$POS-9]=$4elif [[ $3 -eq 3 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS-10]=$4 let BOARD[$POS+10]=$4fi}function Action_Five{# Define these as localtypeset -i POS STATUS=1# Wipe the piece from arrayDefine_or_Wipe_Five $CURX $CURY $CUR_ROTATION 0# Check board boundariesif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWX -gt 140 ]]then NEWX=140fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWX -gt 160 ]]then NEWX=160fiif [[ $NEW_ROTATION -eq 1 && $NEWY -lt 20 ]]then NEWY=20fiif [[ $NEW_ROTATION -eq 0 && $NEWY -gt 460 ]]then NEWY=460; STATUS=0fiif [[ $NEW_ROTATION -eq 2 && $NEWY -gt 480 ]]then NEWY=480; STATUS=0fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWY -gt 460 ]]then NEWY=460; STATUS=0; CURY=$NEWY; CUR_ROTATION=$NEW_ROTATIONfi# Calculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Check if move to left or right was legalif [[ $NEW_ROTATION -eq 0 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+11]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 0 && (${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS+11]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 1 && (${BOARD[$POS-9]} -ne 0 || ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+11]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 1 && (${BOARD[$POS-9]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+11]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 2 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS-9]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 2 && (${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS-9]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 3 && (${BOARD[$POS-10]} -ne 0 || ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 3 && (${BOARD[$POS-10]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+10]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXfi# Recalculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Now check if all new positions are availableif [[ $NEW_ROTATION -eq 0 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS+11]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 1 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS-9]} -ne 0 || ${BOARD[$POS+11]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 2 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS-9]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 3 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS-10]} -ne 0 || ${BOARD[$POS+10]} -ne 0 ]] then STATUS=0 fifi# If all positions are available, draw piece '5' on new coordnates in array, set returnvalueif [[ $STATUS -eq 1 ]]then Define_or_Wipe_Five $NEWX $NEWY $NEW_ROTATION 5 PIECE=5else # If not, draw piece '5' on old coordinates in array, set returnvalue Define_or_Wipe_Five $CURX $CURY $CUR_ROTATION 5 if [[ $CUR_ROTATION -eq $NEW_ROTATION ]] then PIECE=0 else NEW_ROTATION=$CUR_ROTATION fifi}#----------------------------------------------------------------------------------------## The 'OOO' piece - code 6# 'O'##----------------------------------------------------------------------------------------## $1 = X, $2 = Y, $3 = ROTATION, $4 = 0/3#function Define_or_Wipe_Six{# Define these as localtypeset -i POS X Y# Assign the argumentsX=$1; Y=$2# Calculate position on board arraylet POS=$X/20+$Y/2# Determine position, then wipe or drawif [[ $3 -eq 0 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS+2]=$4 let BOARD[$POS+10]=$4elif [[ $3 -eq 1 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS+11]=$4 let BOARD[$POS+21]=$4elif [[ $3 -eq 2 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS+2]=$4 let BOARD[$POS-8]=$4elif [[ $3 -eq 3 ]]then let BOARD[$POS]=$4 let BOARD[$POS+10]=$4 let BOARD[$POS+20]=$4 let BOARD[$POS+21]=$4fi}function Action_Six{# Define these as localtypeset -i POS STATUS=1# Wipe the piece from arrayDefine_or_Wipe_Six $CURX $CURY $CUR_ROTATION 0# Check board boundariesif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWX -gt 140 ]]then NEWX=140fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWX -gt 160 ]]then NEWX=160fiif [[ $NEW_ROTATION -eq 2 && $NEWY -lt 40 ]]then NEWY=40fiif [[ $NEW_ROTATION -eq 0 && $NEWY -gt 460 ]]then NEWY=460; STATUS=0fiif [[ $NEW_ROTATION -eq 2 && $NEWY -gt 480 ]]then NEWY=480; STATUS=0fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWY -gt 440 ]]then NEWY=440; STATUS=0; CURY=$NEWY; CUR_ROTATION=$NEW_ROTATIONfi# Calculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Check if move to left or right was legalif [[ $NEW_ROTATION -eq 0 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 0 && (${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS+10]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 1 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+11]} -ne 0 || ${BOARD[$POS+21]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 1 && (${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+11]} -ne 0 || ${BOARD[$POS+21]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 2 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS-8]} -ne 0 )&& $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 2 && (${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS-8]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 3 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+20]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 3 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+21]} -ne 0 ) && $NEWX -gt $CURX ]]then NEWX=$CURXfi# Recalculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Now check if all new positions are availableif [[ $NEW_ROTATION -eq 0 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS+10]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 1 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+11]} -ne 0 || ${BOARD[$POS+21]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 2 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS-8]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 3 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+20]} -ne 0 || ${BOARD[$POS+21]} -ne 0 ]] then STATUS=0 fifi# If all positions are available, draw piece '6' on new coordnates in array, set returnvalueif [[ $STATUS -eq 1 ]]then Define_or_Wipe_Six $NEWX $NEWY $NEW_ROTATION 6 PIECE=6else # If not, draw piece '6' on old coordinates in array, set returnvalue Define_or_Wipe_Six $CURX $CURY $CUR_ROTATION 6 if [[ $CUR_ROTATION -eq $NEW_ROTATION ]] then PIECE=0 else NEW_ROTATION=$CUR_ROTATION fifi}#----------------------------------------------------------------------------------------## The 'OOO' piece - code 7# 'O'##----------------------------------------------------------------------------------------## $1 = X, $2 = Y, $3 = ROTATION, $4 = 0/3#function Define_or_Wipe_Seven{# Define these as localtypeset -i POS X Y# Assign the argumentsX=$1; Y=$2# Calculate position on board arraylet POS=$X/20+$Y/2# Determine position, then wipe or drawif [[ $3 -eq 0 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS+2]=$4 let BOARD[$POS+12]=$4elif [[ $3 -eq 1 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS-9]=$4 let BOARD[$POS-19]=$4elif [[ $3 -eq 2 ]]then let BOARD[$POS]=$4 let BOARD[$POS+10]=$4 let BOARD[$POS+11]=$4 let BOARD[$POS+12]=$4elif [[ $3 -eq 3 ]]then let BOARD[$POS]=$4 let BOARD[$POS+1]=$4 let BOARD[$POS+10]=$4 let BOARD[$POS+20]=$4fi}function Action_Seven{# Define these as localtypeset -i POS STATUS=1# Wipe the piece from arrayDefine_or_Wipe_Seven $CURX $CURY $CUR_ROTATION 0# Check board boundariesif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWX -gt 140 ]]then NEWX=140fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWX -gt 160 ]]then NEWX=160fiif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWY -gt 460 ]]then NEWY=460; STATUS=0fiif [[ $NEW_ROTATION -eq 1 && $NEWY -lt 40 ]]then NEWY=40fiif [[ $NEW_ROTATION -eq 1 && $NEWY -gt 480 ]]then NEWY=480; STATUS=0fiif [[ $NEW_ROTATION -eq 3 && $NEWY -gt 440 ]]then NEWY=440; STATUS=0; CURY=$NEWY; CUR_ROTATION=$NEW_ROTATIONfi# Calculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Check if move to left or right was legalif [[ $NEW_ROTATION -eq 0 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+12]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 0 && (${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS+12]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 1 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS-9]} -ne 0 || ${BOARD[$POS-19]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 1 && (${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS-9]} -ne 0 || ${BOARD[$POS-19]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 2 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 2 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+12]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 3 && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+20]} -ne 0) && $NEWX -lt $CURX ]]then NEWX=$CURXelif [[ $NEW_ROTATION -eq 3 && (${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+20]} -ne 0) && $NEWX -gt $CURX ]]then NEWX=$CURXfi# Recalculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Now check if all new positions are availableif [[ $NEW_ROTATION -eq 0 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+2]} -ne 0 || ${BOARD[$POS+12]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 1 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS-9]} -ne 0 || ${BOARD[$POS-19]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 2 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+11]} -ne 0 || ${BOARD[$POS+12]} -ne 0 ]] then STATUS=0 fielif [[ $NEW_ROTATION -eq 3 ]]then if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+20]} -ne 0 ]] then STATUS=0 fifi# If all positions are available, draw piece '7' on new coordnates in array, set returnvalueif [[ $STATUS -eq 1 ]]then Define_or_Wipe_Seven $NEWX $NEWY $NEW_ROTATION 7 PIECE=7else # If not, draw piece '7' on old coordinates in array, set returnvalue Define_or_Wipe_Seven $CURX $CURY $CUR_ROTATION 7 if [[ $CUR_ROTATION -eq $NEW_ROTATION ]] then PIECE=0 else NEW_ROTATION=$CUR_ROTATION fifi}#----------------------------------------------------------------------------------------## Setup the board array##----------------------------------------------------------------------------------------function Init_Board{typeset -i COUNT=0until [[ $COUNT -eq 250 ]]do let BOARD[$COUNT]=0 ((COUNT+=1))done}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -