Created
March 26, 2014 07:58
-
-
Save bsdlme/9778574 to your computer and use it in GitHub Desktop.
Add score to bash2048
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/bash2048.sh b/bash2048.sh | |
index 111d6ae..bcca3d1 100755 | |
--- a/bash2048.sh | |
+++ b/bash2048.sh | |
@@ -14,6 +14,9 @@ declare header="Bash 2048 v1.0 (bugs: https://github.com/mydzor/bash2048/issues) | |
declare -i board_size=4 | |
declare -i target=2048 | |
+#score variable | |
+declare -i score=0 | |
+ | |
#for colorizing numbers | |
declare -a colors | |
colors[2]=33 # yellow text | |
@@ -30,6 +33,8 @@ colors[2048]=41 # red background (won with default target) | |
exec 3>/dev/null # no logging by default | |
+trap "end_game 0" INT | |
+ | |
#simplified replacement of seq command | |
function _seq { | |
local cur=1 | |
@@ -52,9 +57,9 @@ function _seq { | |
# print currect status of the game, last added pieces are marked red | |
function print_board { | |
clear | |
- echo $header pieces=$pieces target=$target | |
- echo Board status: >&3 | |
- echo | |
+ printf "$header pieces=$pieces target=$target score=$score\n" | |
+ printf "Board status:\n" >&3 | |
+ printf "\n" | |
printf '/------' | |
for l in $(_seq 1 $index_max); do | |
printf '|------' | |
@@ -105,7 +110,7 @@ function generate_piece { | |
let value=RANDOM%10?2:4 | |
board[$pos]=$value | |
last_added=$pos | |
- echo Generated new piece with value $value at position [$pos] >&3 | |
+ printf "Generated new piece with value $value at position [$pos]\n" >&3 | |
break; | |
} | |
done | |
@@ -150,7 +155,7 @@ function push_pieces { | |
board[$first]=${board[$second]} | |
let board[$second]=0 | |
let change=1 | |
- echo "move piece with value ${board[$first]} from [$second] to [$first]" >&3 | |
+ printf "move piece with value ${board[$first]} from [$second] to [$first]\n" >&3 | |
else | |
let moves++ | |
fi | |
@@ -166,7 +171,8 @@ function push_pieces { | |
let board[$second]=0 | |
let pieces-=1 | |
let change=1 | |
- echo "joined piece from [$second] with [$first], new value=${board[$first]}" >&3 | |
+ let score=$score+${board[$first]} | |
+ printf "joined piece from [$second] with [$first], new value=${board[$first]}\n" >&3 | |
else | |
let moves++ | |
fi | |
@@ -174,7 +180,7 @@ function push_pieces { | |
} | |
function apply_push { | |
- echo input: $1 key >&3 | |
+ printf "\n\ninput: $1 key\n" >&3 | |
for i in $(_seq 0 $index_max); do | |
for j in $(_seq 0 $index_max); do | |
flag_skip=0 | |
@@ -221,12 +227,13 @@ function key_react { | |
function end_game { | |
print_board | |
- echo GAME OVER | |
+ printf "GAME OVER\n" | |
+ printf "Your score: $score\n" | |
let $1 && { | |
- echo "Congratulations you have achieved $target" | |
+ printf "Congratulations you have achieved $target\n" | |
exit 0 | |
} | |
- echo "You have lost, better luck next time." | |
+ printf "You have lost, better luck next time.\n" | |
exit 0 | |
} | |
@@ -248,21 +255,21 @@ while getopts "b:t:l:h" opt; do | |
case $opt in | |
b ) board_size="$OPTARG" | |
let '(board_size>=3)&(board_size<=9)' || { | |
- echo "Invalid board size, please choose size between 3 and 9" | |
+ printf "Invalid board size, please choose size between 3 and 9\n" | |
exit -1 | |
};; | |
t ) target="$OPTARG" | |
- echo "obase=2;$target" | bc | grep -e '^1[^1]*$' | |
+ printf "obase=2;$target\n" | bc | grep -e '^1[^1]*$' | |
let $? && { | |
- echo "Invalid target, has to be power of two" | |
+ printf "Invalid target, has to be power of two\n" | |
exit -1 | |
};; | |
h ) help $0 | |
exit 0;; | |
l ) exec 3>$OPTARG;; | |
- \?) echo "Invalid option: -"$OPTARG", try $0 -h" >&2 | |
+ \?) printf "Invalid option: -"$OPTARG", try $0 -h\n" >&2 | |
exit 1;; | |
- : ) echo "Option -"$OPTARG" requires an argument, try $0 -h" >&2 | |
+ : ) printf "Option -"$OPTARG" requires an argument, try $0 -h\n" >&2 | |
exit 1;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment