Nimbin[12]?Games / 2048_bash / commits / d1306b5

2048_bash git · main

2048 in pure bash with highscores

game bash terminal · first commit 2022-06-06 · last commit 2022-06-06 (4 years ago) · synced 1 hour ago · upstream: github.com/nimbin2/2048_bash

Shell 99.4%
git clone https://git.christianimmanuel.de/games/2048_bash.gitwget https://git.christianimmanuel.de/games/2048_bash/archive/2048_bash.tar.gz

initial commit new file: 2048.sh new file: README.md new file: autorun.sh new file: functions.sh new file: gameover.sh new file: matrix.log new file: printMatrix.sh new file: printstart.sh new file: rank.sh new file: scores

nimbin2 · 2022-06-06 09:37 · d1306b59fc4e61a0d66ecbc98622ac1368a3819b

 2048.sh        |  64 ++++++++++++++++++++
 README.md      |   1 +
 autorun.sh     |  35 +++++++++++
 functions.sh   | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gameover.sh    | 112 +++++++++++++++++++++++++++++++++++
 matrix.log     |   7 +++
 printMatrix.sh |  93 +++++++++++++++++++++++++++++
 printstart.sh  |  65 +++++++++++++++++++++
 rank.sh        | 145 +++++++++++++++++++++++++++++++++++++++++++++
 scores         |   2 +
 10 files changed, 705 insertions(+)

diff --git a/2048.sh b/2048.sh
new file mode 100755
index 0000000..fd51f90
--- /dev/null
+++ b/2048.sh
@@ -0,0 +1,64 @@
+#!/bin/bash
+### 2048.sh
+
+param=$1
+sleepTime=$2
+if [ -z "$sleepTime" ]; then sleepTime=0.0; fi
+source functions.sh
+source printMatrix.sh
+source autorun.sh
+rm log.log
+clear -x
+
+#_STTY=$(stty -g)      # Save current terminal setup
+#printf "\e[?25l"      # Turn of cursor
+#stty -echo -icanon
+
+
+function at_exit() {
+	printf "\e[?9l"          # Turn off mouse reading
+        printf "\e[?12l\e[?25h"  # Turn on cursor
+        stty "$_STTY"            # reinitialize terminal settings
+	tput sgr0
+	clear -x
+}
+trap at_exit ERR EXIT
+
+autorun=false
+if [ "$param" = "-a" ]; then
+	autorun=true
+	source autorun.sh
+fi
+
+
+startRun
+
+########
+##MAIN##
+while true; do
+	mainLoop=$((mainLoop+1))
+	swipeState=("${newSwipeState[@]}")
+	frameBottom=0
+
+
+	log "main:$mainLoop: ${matrix[*]}"
+	getRank
+# auto or user
+	if $autorun; then
+		runAutoMx
+	else
+		waitForKey
+	fi
+# swipe to direction X
+	swipeX
+# set Random num matrix
+	#if $setNewRanN; then setRanN && wait; fi
+	if ${swipeState[$swipeValue]}; then setRanN; fi
+# write matrix
+	logMatrix
+	if $autorun; then sleep $sleepTime; fi
+	printMatrix
+
+# test 
+	testSwipe
+done
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b39d612
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# 2048_bash
diff --git a/autorun.sh b/autorun.sh
new file mode 100755
index 0000000..6441eb5
--- /dev/null
+++ b/autorun.sh
@@ -0,0 +1,35 @@
+### autorun.sh
+
+setSwipeDirection() {
+	swipeWhile=true
+	sCc=0
+	while [ "$swipeWhile" = "true" ]; do
+		sCc=$((sCc+1))
+		maxRank=$(printf '%s\n' "${TRank[@]}" | sort -n | tail -$sCc | head -1)
+		for setSwipeC in {0..3}; do
+			if [[ ${TRank[$setSwipeC]} -eq $maxRank && ${swipeState[$setSwipeC]} ]]; then
+				if ${swipeState[$setSwipeC]}; then
+					directionX=$setSwipeC
+					log "if1b: $setSwipeC, ${swipeState[*]}"
+					swipeWhile=false
+				fi
+			fi
+			
+		done
+
+		if [ $sCc -eq 4 ]; then
+			testSwipe
+			swipeWhile=false
+		fi
+	done
+}
+
+
+
+########
+##MAIN##
+runAutoMx() {
+	getRank
+	setSwipeDirection
+	${swipeXf[$directionX]}
+}
diff --git a/functions.sh b/functions.sh
new file mode 100755
index 0000000..eabe957
--- /dev/null
+++ b/functions.sh
@@ -0,0 +1,181 @@
+#!/bin/bash
+
+### functions.sh
+
+source scores
+source printstart.sh
+source gameover.sh
+source matrix.log
+source rank.sh
+
+swipeXf=(swipeA swipeB swipeC swipeD)
+newSwipeState=(false false false false)
+swipeL=0
+MW=$(tput cols)
+MH=$(tput lines)
+
+log() {
+	msg=$1
+	add=$2
+	echo "$msg##$add" >> log.log
+
+	re='^[0-9]+$'
+	if [[ $msg =~ $re ]] ; then
+		printf "\e[1;47;30m"
+		printf "\e[$((MH/2+5+msg));1f$(seq -s' ' $((MW+1))|tr -d '[:digit:]')"
+		printf "\e[$((MH/2+5+msg));$((MW/2-16))f  $add  "
+		printf "\e[0m"
+	fi
+}
+logMatrix() {
+	echo "matrix$mainLoop=(${matrix[*]})" >> matrix.log
+}
+
+swipeA() {
+	keyP=A
+	swipeValue=0
+	swipeN=$((4+swipeL))
+	swipeSetNn=-4
+}
+swipeB() {
+	keyP=B
+	swipeValue=1
+	swipeSetNn=+4
+	swipeN=$((11-swipeL))
+}
+swipeC() {
+	keyP=C
+	swipeValue=2
+	swipeSetNn=+1
+	swipeN=$(( ((swipeL/-4 ) + 2) + ((swipeL-((swipeL/4)*4))*4) ))
+}
+swipeD() {
+	keyP=D
+	swipeValue=3
+	swipeSetNn=-1
+	swipeN=$(( (swipeL/4) + ((swipeL - ((swipeL/4)*4))*4) +1))
+}
+
+ran16() {
+	echo $(( ( RANDOM % 16 )  + 0 ))
+}
+
+ran2o4() {
+	# get 2(80%) or 4(20%)
+	if [ $((( RANDOM % 100 ) + 1 )) -le 80 ]; then
+		echo 2
+	else
+		echo 4
+	fi
+}
+
+waitForKey() {
+	while true; do
+		read -rsn3 -d '' PRESS
+		keyP=${PRESS:2}
+		if [[ "$keyP" = "A" || "$keyP" = "B" || "$keyP" = "C" || "$keyP" = "D" ]]; then
+			break
+		fi
+	done
+}
+
+swipeX() {
+	for i in {0..3}; do
+		for swipeL in {0..11}; do
+			swipe"$keyP"
+			swipeNn=$((swipeN+swipeSetNn))
+			if [[ "${matrix[$swipeNn]}" -eq "0" && "${matrix[$swipeN]}" -gt "0" || 
+			   "${matrix[$swipeN]}" -gt  "0" && "${matrix[$swipeN]}" -eq "${matrix[swipeNn]}" ]]; then
+				swipeState[$swipeValue]=true
+				matrix[$swipeNn]=$((matrix[swipeN]+matrix[swipeNn]))
+				matrix[$swipeN]="0"
+			fi
+		done	
+	done
+}
+
+testSwipe() {
+	swipeState=("${newSwipeState[@]}")
+        testSwipeMx=("${matrix[@]}")
+	log "TS:$mainLoop: ${matrix[*]}"
+	for testX in "${swipeXf[@]}"; do
+		$testX	
+		matrix=("${testSwipeMx[@]}")
+		swipeX
+	done
+        matrix=("${testSwipeMx[@]}")
+
+	if [[ "$mainLoop" -gt "0" && $(printf "%s\000" "${swipeState[@]}" |
+			LC_ALL=C sort -z -u | grep -z -c .) -eq 1 ]]; then
+		if [ ${swipeState[0]} = false ]; then 
+			setHighscore
+	       	fi
+	fi
+}
+
+setRanN() {
+	while true; do
+		setRanN=$(ran16)
+		if [ ${matrix[$setRanN]} -eq 0 ]; then
+			matrix[$setRanN]=$(ran2o4)
+			break
+		fi	
+	done
+}
+
+fillScore() {
+	fillScoreS=("" "" "")
+	fillScoreN=("" "" "")
+	longestScore=${scores[0]}
+	longestName=$(echo "${scoreNames[*]}" | sed 's/ /\n/g' | sort | uniq | awk '{print length, $0}' | sort -nr | head -n 1 | awk '{print $NF}')
+	fillAll=$((${#longestScore}+${#longestName}+8))
+	fillAllS=$(seq -s' ' $fillAll|tr -d '[:digit:]')
+	fillHigh=$(seq -s' ' $((fillAll-12))|tr -d '[:digit:]')
+	for fillS in {0..2}; do
+		spacesFs=$((${#longestScore}-${#scores[$fillS]}+1))
+		spacesFn=$((${#longestName}-${#scoreNames[$fillS]}+1))
+		fillSpaceS=$(seq -s' ' $spacesFs|tr -d '[:digit:]')
+		fillSpaceN=$(seq -s' ' $spacesFn|tr -d '[:digit:]')
+		fillScoreS[$fillS]="$fillSpaceS${scores[$fillS]}"
+		fillScoreN[$fillS]="${scoreNames[$fillS]}$fillSpaceN"
+	done
+}
+
+blackScreen() {
+	for ((i = 0 ; i <= MH ; i++)); do
+		printf "\e[37;40m%$((MW))s\e[0m" |tr " " " "
+		#for ((c = 0 ; c <= MW ; c++)); do
+			#printf "\e[37;40m\e[%s;%s\nf \e[0m" "$i" "$c"
+		#done
+	done
+
+}
+
+startRun() {
+	matrix=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
+	#matrix=(4 0 0 0 8 0 0 0 128 0 0 0 2 0 0 0)
+	#matrix=(8 2 0 0 4 0 0 0 4 0 0 0 0 0 0 0)
+	#matrix=(16 2 0 0 8 2 0 0 0 128 0 0 0 0 16 16)
+	#matrix=(2110 0 64 16 16 256 32 0 0 1281 0 16 3 5 2 8)
+	#matrix=(3 7 16 128 0 4 32 0 9 4 16 512 0 4 8 128)
+	mainLoop=0
+	blackScreen
+	printStart
+	blackScreen
+	if [ "$loadSaved" = "true" ]; then
+		log "loadSaved: $loadSaved"
+		oldMx=$(tail -n 1 matrix.log | grep -o -P '(?<=\().*(?=\))')
+		IFS=', ' read -r -a matrix <<< "$oldMx"
+		mainLoop=$(tail -n 1 matrix.log | grep -o -P '(?<=x).*(?==)')
+	else
+		rm matrix.log >> /dev/null 2>&1
+		setRanN
+		setRanN
+		logMatrix
+	fi
+	reloadFrame=1
+	log "$mainLoop, ${matrix[*]}"
+	printMatrix
+}
+
+
diff --git a/gameover.sh b/gameover.sh
new file mode 100755
index 0000000..cd91648
--- /dev/null
+++ b/gameover.sh
@@ -0,0 +1,112 @@
+#!/bin/bash
+## gameover.sh
+
+
+centerG=$[MW/2-26]
+
+
+
+gameover() {
+	source functions.sh
+	fillScore
+
+
+
+	printf "\e[37;40m\e[1;$[centerG]f█████▀████████████████████████████████████████████"
+	printf "\e[2;$[centerG]f█ ▄▄▄▄██▀▄ ██▄ ▀█▀ ▄█▄ ▄▄ █ ▄▄ █▄ █ ▄█▄ ▄▄ █▄ ▄▄▀█"
+	printf "\e[3;$[centerG]f█ ██▄ ██ ▀ ███ █▄█ ███ ▄█▀█ ██ ██▄▀▄███ ▄█▀██ ▄ ▄█"
+	printf "\e[4;$[centerG]f█▄▄▄▄▄█▄▄█▄▄█▄▄▄█▄▄▄█▄▄▄▄▄█▄▄▄▄███▄███▄▄▄▄▄█▄▄█▄▄█\e[0m"
+
+	scoreP="  Your Score: $(IFS=+; echo "$((${matrix[*]}))")"
+	fillScoreP=$[${#fillAllS}-${#scoreP}]
+	fillScoreP=$(seq -s' ' $[${#fillAllS}-${#scoreP}]|tr -d '[:digit:]')
+	score0="   ${fillScoreN[0]}  ${fillScoreS[0]}   "
+	score1="   ${fillScoreN[1]}  ${fillScoreS[1]}   "
+	score2="   ${fillScoreN[2]}  ${fillScoreS[2]}   "
+	printW=$[MW/2-${#fillAllS}/2-1]
+# fillOverflow
+	if [ ${#scoreP} -gt $[${#fillAllS}-1] ]; then
+		fillO=$(seq -s' ' $[${#scoreP}+3]|tr -d '[:digit:]')
+		printW=$[MW/2-${#scoreP}/2-1]
+		for i in {0..7}; do
+			printf "\e[1;47;30m\e[$[MH/2-4+$i];$[printW]f$fillO\e[0m"
+		done
+	elif [ ${#scoreP} -lt $[${#fillAllS}-1] ]; then
+		printf "\e[1;47;30m\e[$[MH/2-3];$[printW+${#scoreP}]f$fillO\e[0m"
+
+	fi
+	printf "\e[1;47;30m\e[$[MH/2-4];$[printW]f$fillAllS "
+	printf "\e[$[MH/2-3];$[printW]f$scoreP  $fillScoreP"
+	printf "\e[$[MH/2-2];$[printW]f$fillAllS "
+	printf "\e[$[MH/2-1];$[printW]f  Highscores:$fillHigh"
+	printf "\e[$[MH/2];$[printW]f$score0"
+	printf "\e[$[MH/2+1];$[printW]f$score1"
+	printf "\e[$[MH/2+2];$[printW]f$score2"
+	printf "\e[$[MH/2+3];$[printW]f$fillAllS \e[0m"
+
+
+	restartT=" Restart by pressing Enter"
+	exitT=" Exit by pressing CTRL-C    "
+	printf "\e[1;40;37m\e[$[MH-1];$[MW/2-${#restartT}/2]f$restartT"
+	printf "\e[$[MH];$[MW/2-${#restartT}/2]f$exitT\e[0m"
+
+	read -rs
+	frameBottom=0
+	acHighestNum=1
+	startRun
+}
+
+readScoreName() {
+
+        centerH=$[MW/2-24]
+
+        printf "\e[37;40m\e[1;$[centerH]f█████████████▀██████████████████████████████████"
+        printf "\e[2;$[centerH]f█ █ █▄ ▄█ ▄▄▄▄█ █ █ ▄▄▄▄█ ▄▄▄ █ ▄▄ █▄ ▄▄▀█▄ ▄▄ █"
+        printf "\e[3;$[centerH]f█ ▄ ██ ██ ██▄ █ ▄ █▄▄▄▄ █ ███▀█ ██ ██ ▄ ▄██ ▄█▀█"
+        printf "\e[4;$[centerH]f█▄█▄█▄▄▄█▄▄▄▄▄█▄█▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█\e[0m"
+
+        nameLabel="   Enter your name:   "
+        printf "\e[1;47;30m\e[$[MH/2-1];$[MW/2-${#nameLabel}/2]f$(seq -s' ' $[${#nameLabel}+1]|tr -d '[:digit:]')\e[0m"
+        printf "\e[1;47;30m\e[$[MH/2];$[MW/2-${#nameLabel}/2]f$nameLabel\e[0m"
+        printf "\e[1;47;30m\e[$[MH/2+1];$[MW/2-${#nameLabel}/2]f$(seq -s' ' $[${#nameLabel}+1]|tr -d '[:digit:]')\e[0m"
+        printf "\e[1;47;30m\e[$[MH/2+2];$[MW/2-${#nameLabel}/2]f$(seq -s' ' $[${#nameLabel}+1]|tr -d '[:digit:]')\e[0m"
+        printf "\e[$[MH/2+1];$[MW/2-${#nameLabel}/2+3]f"
+        read pName
+	pName=$(echo $pName | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]' | sed -e "s/[[:space:]]\+/ /g")
+}
+
+setHighscore() {
+        newScore=$(IFS=+; echo "$((${matrix[*]}))")
+        newScores=("${scores[@]}")
+        newScoreNames=("${scoreNames[@]}")
+        setC=-1
+        breakSet=true
+
+
+        blackScreen
+	frameBottom=1
+	acHighestNum=1
+	printMatrix
+        for ((setC=0;setC<3;setC++)); do
+                #setC=$((setC+1))
+                if [ $breakSet = true ]; then
+                        if [[ "$newScore" -gt "${scores[$setC]}" ]]; then
+                                if $autorun; then pName="autorun"; echo $pName;  else
+                                        readScoreName
+                                fi
+                                for ((n=$setC;n<2;n++)); do
+                                        newScores[$((n+1))]=${scores[$n]}
+                                        newScoreNames[$((n+1))]="${scoreNames[$n]}"
+                                done 
+                                newScores[$setC]=$newScore
+                                newScoreNames[$setC]="$pName"
+                                breakSet=false
+                        fi
+                fi
+        done
+	echo "scores=(${newScores[*]})" > scores
+	echo "scoreNames=(${newScoreNames[*]})" >> scores
+        gameover
+	loadSaved=false
+}
+                                   
diff --git a/matrix.log b/matrix.log
new file mode 100644
index 0000000..9889cbe
--- /dev/null
+++ b/matrix.log
@@ -0,0 +1,7 @@
+matrix0=(0 0 0 0 2 0 0 0 0 0 0 0 0 2 0 0)
+matrix1=(4 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0)
+matrix2=(2 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0)
+matrix3=(0 0 0 2 0 0 0 0 0 0 4 0 0 0 0 8)
+matrix4=(0 0 0 0 0 0 0 0 0 0 0 2 0 2 4 8)
+matrix5=(0 0 0 0 0 0 0 0 0 0 0 2 0 2 4 8)
+matrix6=(0 2 4 2 0 0 0 8 0 2 0 0 0 0 0 0)
diff --git a/printMatrix.sh b/printMatrix.sh
new file mode 100755
index 0000000..8e08028
--- /dev/null
+++ b/printMatrix.sh
@@ -0,0 +1,93 @@
+#!/bin/bash
+## printMatrix.sh
+
+# print frame
+printFrame() {
+	printf "\e[1;30;107m\e[$[pFh+1];$[pFw+1]f┌$colS┬$colS┬$colS┬$colS┐";
+	printf "\e[$[pFh+2];$[pFw+1]f│\e[$[pFh+2];$[pFw+2+colW]f│\e[$[pFh+2];$[pFw+3+(colW*2)]f│\e[$[pFh+2];$[pFw+4+(colW*3)]f│\e[$[pFh+2];$[pFw+5+(colW*4)]f│";
+	printf "\e[$[pFh+3];$[pFw+1]f├$colS┼$colS┼$colS┼$colS┤";
+	printf "\e[$[pFh+4];$[pFw+1]f│\e[$[pFh+4];$[pFw+2+colW]f│\e[$[pFh+4];$[pFw+3+(colW*2)]f│\e[$[pFh+4];$[pFw+4+(colW*3)]f│\e[$[pFh+4];$[pFw+5+(colW*4)]f│";
+	printf "\e[$[pFh+5];$[pFw+1]f├$colS┼$colS┼$colS┼$colS┤";
+	printf "\e[$[pFh+6];$[pFw+1]f│\e[$[pFh+6];$[pFw+2+colW]f│\e[$[pFh+6];$[pFw+3+(colW*2)]f│\e[$[pFh+6];$[pFw+4+(colW*3)]f│\e[$[pFh+6];$[pFw+5+(colW*4)]f│";
+	printf "\e[$[pFh+7];$[pFw+1]f├$colS┼$colS┼$colS┼$colS┤";
+	printf "\e[$[pFh+8];$[pFw+1]f│\e[$[pFh+8];$[pFw+2+colW]f│\e[$[pFh+8];$[pFw+3+(colW*2)]f│\e[$[pFh+8];$[pFw+4+(colW*3)]f│\e[$[pFh+8];$[pFw+5+(colW*4)]f│";
+	printf "\e[$[pFh+9];$[pFw+1]f└$colS┴$colS┴$colS┴$colS┘\e[0m";
+	
+	if [[ "$frameBottom" -eq "0" ]]; then
+## print scores
+		printf "\e[1;97;40m\e[$[pFh+4];$[pFw+matrixW+1]f Highscores:$fillHigh"
+		printf "\e[$[pFh+5];$[pFw+matrixW+1]f$fillAllS"
+		printf "\e[$[pFh+6];$[pFw+matrixW+1]f   ${fillScoreN[0]}  ${fillScoreS[0]}  "
+		printf "\e[$[pFh+7];$[pFw+matrixW+1]f   ${fillScoreN[1]}  ${fillScoreS[1]}  "
+		printf "\e[$[pFh+8];$[pFw+matrixW+1]f   ${fillScoreN[2]}  ${fillScoreS[2]}  "
+		printf "\e[$[pFh+9];$[pFw+matrixW+1]f$fillAllS\e[0m"
+	fi
+}
+
+
+# print number into fieds
+printNumbers() {
+	y=$[pFh+2]
+	c=0
+	for i in "${!matrix[@]}"; do 
+		c=$[c+1]
+		printM=${matrix[$i]}
+		preMatrixLoop="matrix$[mainLoop-1][$i]"
+		spacesGet=$[highestNumC-${#matrix[$i]}+4]
+		spacesB=$(seq -s' ' $[spacesGet/2]|tr -d '[:digit:]')
+		spacesA=$(seq -s' ' $[spacesGet-(spacesGet/2)]|tr -d '[:digit:]')
+		x=$[pFw+c+((c-1)*colW)+1]
+		ul=$y";"$x
+		if [ $printM -eq "0" ]; then printM=" "; fi
+# print if needed
+		if [ $acHighestNum -ne $highestNumC ]; then
+			printf "\e[1;30;107m\e[${ul}f$spacesB$printM$spacesA\e[0m";
+		elif [[ "$reloadFrame" -eq "1" || "$printM" -ne "${!preMatrixLoop}" ]]; then
+			printf "\e[1;30;107m\e[${ul}f$spacesB$printM$spacesA\e[0m";
+
+		fi
+
+		if (( $[i+1] % 4 == 0 )); then
+			y=$[y+2]
+			c=0
+		fi
+	done
+	if [ $reloadFrame -eq 1 ]; then reloadFrame=0; fi
+}
+
+########
+##MAIN##
+printMatrix() {
+	source matrix.log
+	fillScore
+	highestNumC=$(printf '%s\n' "${matrix[@]}" | awk '$1 > m || NR == 1 { m = $1 } END { print m }')
+	highestNumC=${#highestNumC}
+	colW=$[highestNumC+2]
+	colS=$(seq -s'─' $[colW+1]|tr -d '[:digit:]')
+	matrixW=$[$colW*4+5]
+	
+	if [[ "$frameBottom" -eq "0" ]]; then
+		log "fuuuuu$frameBottom"
+		pFh=$[MH/2-4]	
+		pFw=$[MW/2-17]
+	elif [[ "$frameBottom" -eq "1" ]]; then
+## set framePosition
+		pFh=$[MH-9]	
+		pFw=0
+	fi
+
+## print actual score
+	if [[ "$frameBottom" -eq "0" ]]; then
+		printf "\e[1;97;40m\e[$[pFh+1];$[pFw+matrixW+1]f Your Score: $(IFS=+; echo "$((${matrix[*]}))") \e[0m"
+	fi
+
+
+	if [ -z $acHighestNum ] || [ $mainLoop -eq 0 ]; then 
+		let acHighestNum=0
+		printFrame
+	elif [ $acHighestNum -ne $highestNumC ]; then
+		printFrame
+	fi
+	printNumbers
+	let acHighestNum=$highestNumC
+}
diff --git a/printstart.sh b/printstart.sh
new file mode 100755
index 0000000..6323946
--- /dev/null
+++ b/printstart.sh
@@ -0,0 +1,65 @@
+#!/bin/bash
+## startgame.sh
+
+printStart() {
+	blackScreen
+
+	centerS=$[MW/2-29]
+	printf "\e[37;40m\e[1;$[centerS]f█████████████████████████████████████▀█████████████████████"
+	printf "\e[2;$[centerS]f█ ▄▄▄▄█ ▄ ▄ ██▀▄ ██▄ ▄▄▀█ ▄ ▄ ███ ▄▄▄▄██▀▄ ██▄ ▀█▀ ▄█▄ ▄▄ █"
+	printf "\e[3;$[centerS]f█▄▄▄▄ ███ ████ ▀ ███ ▄ ▄███ █████ ██▄ ██ ▀ ███ █▄█ ███ ▄█▀█"
+	printf "\e[4;$[centerS]f█▄▄▄▄▄██▄▄▄██▄▄█▄▄█▄▄█▄▄██▄▄▄████▄▄▄▄▄█▄▄█▄▄█▄▄▄█▄▄▄█▄▄▄▄▄█\e[0m"
+
+	startLabel="   Start by pressing Enter   "
+	startAutoLabel="   Autorun by pressing a   "
+	startPrevLabel="   Load last game by pressing s   "
+	fillL=$(seq -s' ' $[${#startPrevLabel}+1]|tr -d '[:digit:]')
+	messageAuto="   Enter delay in 0.X seconds"
+	printf "\e[1;47;30m\e[$[MH/2];$[MW/2-${#startPrevLabel}/2]f$fillL"
+	printf "\e[$[MH/2+1];$[MW/2-${#startPrevLabel}/2]f$fillL"
+	printf "\e[$[MH/2+1];$[MW/2-${#startPrevLabel}/2]f$startLabel"
+	printf "\e[$[MH/2+2];$[MW/2-${#startPrevLabel}/2]f$fillL"
+	printf "\e[$[MH/2+3];$[MW/2-${#startPrevLabel}/2]f$fillL\e"
+	printf "\e[$[MH/2+3];$[MW/2-${#startPrevLabel}/2]f$startAutoLabel"
+	printf "\e[$[MH/2+4];$[MW/2-${#startPrevLabel}/2]f$fillL\e"
+	printf "\e[$[MH/2+5];$[MW/2-${#startPrevLabel}/2]f$startPrevLabel"
+	printf "\e[$[MH/2+6];$[MW/2-${#startPrevLabel}/2]f$fillL\e[0m"
+
+	keyS="0"
+	loadSaved=false
+
+	printf "\e[$[MH/2+7];$[MW/2]f"
+	while true; do
+
+		read -rsn1 keyS
+# normal game
+		if [ -z $keyS ]; then
+			echo "##$keyS++"
+			autorun=false
+			break
+# auto game
+		elif [[ "$keyS" == "a" ]]; then
+			autorun=true
+			printf "\e[1;47;30m\e[$[MH/2+1];$[MW/2-${#startPrevLabel}/2]f$fillL"
+			printf "\e[$[MH/2+1];$[MW/2-${#startPrevLabel}/2]f$messageAuto"
+			printf "\e[$[MH/2+3];$[MW/2-${#startPrevLabel}/2]f$fillL"
+			printf "\e[$[MH/2+5];$[MW/2-${#startPrevLabel}/2]f$fillL\e[0m"
+# auto game set sleep time
+			printf "\e[$[MH/2+7];$[MW/2]f"
+			re='^[0-9]+$'
+			read -t 1.5 -n1 sleepTime
+			if ! [[ $sleepTime =~ $re ]] || [[ -z $sleepTime ]]; then sleepTime=0; fi
+			sleepTime=0.$sleepTime
+			break
+# saved game
+		elif [[ "$keyS" == "s" ]]; then
+			autorun=false
+			loadSaved=true
+			break
+		fi
+	done
+# clear tty
+	_STTY=$(stty -g)      # Save current terminal setup
+	printf "\e[?25l"      # Turn of cursor
+	stty -echo -icanon
+}
diff --git a/rank.sh b/rank.sh
new file mode 100755
index 0000000..ad35841
--- /dev/null
+++ b/rank.sh
@@ -0,0 +1,145 @@
+#!/bin/bash
+## rank.sh
+
+swipeValue=""
+mainLoop=""
+swipeXf=()
+
+countMatrixZeros() {
+        CZ=$(grep -ow 0 <<< "${countMx[*]}" | wc -l)
+}
+
+multiplyByZ() {
+	countMatrixZeros
+	mBz=$(echo "scale=2;$RV*((16-$CZ)*2/16)" | bc | awk '{print int($1)}')
+}
+
+getCornerR() {
+
+	if [[ "$postArrC" -eq "0" || "$postArrC" -eq "3" || "$postArrC" -eq "12" || "$postArrC" -eq "15" ]]; then
+	       if [[ "$tN" -ge "$tNO" ]]; then
+			multiplyByZ
+			GCRank[$swipeValue]=$((GCRank[swipeValue]+mBz))
+	       fi
+	fi
+}
+
+testPostArroundN() {
+        setTestNnI=+1
+        for postArrI in {0..1}; do
+                if [ "$postArrI" -eq 1 ]; then
+                        setTestNnI=+4
+                fi
+                for postArrC in {0..15}; do
+                        tN=${matrix[$postArrC]}
+                        tNO=${originalMx[$postArrC]}
+                        tNnI=$postArrC"$setTestNnI"
+                        tNn=${matrix[$tNnI]}
+                        if [[ "$tN" -eq "0" ]]; then
+                                RV=0
+                        else
+                                RV=$(echo "l($tN)/l(2)" | bc -l | awk '{print int($1)}')
+                        fi
+                        if [[ "$tNn" -eq "0" ]]; then
+                                RnV=0
+                        else
+                                RnV=$(echo "l($tNn)/l(2)" | bc -l | awk '{print int($1)}')
+                        fi
+			if [ "$postArrI" -eq 0 ]; then
+				getCornerR
+			fi
+                        if [[ -n "$tNn" ]]; then
+                                if [[ "$tN" -gt "0" && "$tNn" -gt "$tN" ]]; then
+                                        nPAr=$((RnV/((RnV/RV)*(RnV/RV))))
+                                        #PARank[$swipeValue]=$((PARank[$swipeValue]+nPAr))
+                                elif [[ "$tNn" -gt "0" && "$tN" -gt "$tNn" ]]; then
+                                        nPAr=$((RV/((RV/RnV)*(RV/RnV))))
+                                        #PARank[$swipeValue]=$((PARank[$swipeValue]+nPAr))
+                                elif [[ "$tN" -gt "0" && "$tN" -eq "$tNn" ]]; then
+                                        # multiply by part of left zeros
+					mBz=0
+					nPAr=$((mBz+RV))
+                                        PARank[$swipeValue]=$((PARank[swipeValue]+nPAr))
+                                fi
+                        fi
+                done
+        done
+
+}   
+
+countEqualN() {
+# uniqueValue countValue
+	uVn=($(echo "${matrix[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
+	#mapfile -t uVn < <(echo "${matrix[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')
+	uVn=("${uVn[@]:1}")
+	uV=($(echo "${originalMx[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
+	#mapfile -t uV < <(echo "${originalMx[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')
+	uV=("${uV[@]:1}")
+	uVAuVn=("${uV[@]} ${uVn[@]}")
+        uVAuVn=($(echo "${uVAuVn[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
+	#$([ -n "$uV" ] && mapfile -t uVAuVn < <(echo "${uV[@]} ${uVn[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ') )
+	log "$mainLoop: $uV, $uVn, $uVAuVn"
+	uVi=()
+	uVin=()
+	for i in "${!uVAuVn[@]}"; do
+		cVn=0
+		cV=0
+		for c in  "${!matrix[@]}"; do
+			if [ "${uVAuVn[$i]}" -eq "${matrix[$c]}" ]; then
+				cVn=$((cVn+1))
+			else
+				cVn=$((cVn+0))
+			fi
+			if [ "${uVAuVn[$i]}" -eq "${originalMx[$c]}" ]; then
+				cV=$((cV+1))
+			else
+				cV=$((cV+0))
+			fi
+		done
+		uVi[$i]=$cV
+		uVin[$i]=$cVn
+	done
+	for rUvi in "${!uVAuVn[@]}"; do
+		restUvA[$rUvi]=$((${uVin[$rUvi]}-${uVi[$rUvi]}))
+		if [[ ${restUvA[$rUvi]} -gt 0 ]]; then
+			uVAuVnI=$(echo "l(${uVAuVn[$rUvi]})/l(2)" | bc -l | awk '{print int($1)}')
+			CERank[$swipeValue]=$((${CERank[$swipeValue]}+(${restUvA[$rUvi]}*uVAuVnI) ))
+		else
+			CERank[$swipeValue]=$((${CERank[$swipeValue]}+0))
+		fi
+	done
+
+}
+
+TRank() {
+	TRank=(0 0 0 0)
+	for totalC in {0..3}; do
+		TRank[$totalC]=$((${CERank[$totalC]}+${PARank[$totalC]}+${GCRank[$totalC]}))
+	done
+}
+
+getNextN() {
+        PARank=(0 0 0 0)
+	CERank=(0 0 0 0)
+	GCRank=(0 0 0 0)
+        for getXf in "${swipeXf[@]}"; do
+                swpieC=${getXf}
+                matrix=("${originalMx[@]}")
+                $getXf
+                swipeX
+		countEqualN
+                testPostArroundN
+        done
+}
+
+getRank() {
+        originalMx=("${matrix[@]}")
+	countMx=("${matrix[@]}")
+        getNextN
+        TRank
+        #log 1 "countEqRank: ${countEqRank[*]}"
+        log 1 "GCRank: ${GCRank[*]}"
+        log 2 "PARank: ${PARank[*]}"
+        log 3 "total  Rank: ${TRank[*]}"
+        matrix=("${originalMx[@]}")	
+}
diff --git a/scores b/scores
new file mode 100644
index 0000000..b188572
--- /dev/null
+++ b/scores
@@ -0,0 +1,2 @@
+scores=(1692 1688 1678)
+scoreNames=(autorun autorun autorun)