Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- declare -r SIZE=4
- declare -r KEY_UP="w"
- declare -r KEY_DOWN="s"
- declare -r KEY_RIGHT="d"
- declare -r KEY_LEFT="a"
- declare -r BEST_SCORE_FILE="best-score.txt"
- declare -i fixedTileIndex
- declare -i floatTileIndex
- declare -i score
- declare -i bestScore
- declare shifted
- declare -a tiles
- declare keepGoing=false
- function writeBestScore() {
- if [[ -w $BEST_SCORE_FILE ]]; then
- if (( score > bestScore )); then
- echo "$score" > $BEST_SCORE_FILE
- fi
- fi
- }
- function readBestScore() {
- if [[ -r $BEST_SCORE_FILE ]]; then
- bestScore=$(cat $BEST_SCORE_FILE)
- else
- bestScore=0
- fi
- }
- function display() {
- local -i i
- local -i j
- local -i tileValue
- local generatedColor
- readBestScore
- echo -e "\e[1;1f\e[2J" # clear display
- echo -e "\e[31mScore: $score Best: $bestScore"
- echo
- for (( i = 0; i < SIZE; i++ )); do
- for (( j = 0; j < SIZE; j++ )); do
- tileValue=${tiles[i * SIZE + j]}
- generatedColor="\e[3$(( (tileValue % 10) / 2 + 1 ))m"
- echo -n -e " $generatedColor" $tileValue " "
- done
- echo
- done
- }
- function placeRandomTile() {
- local -a freeTiles
- local -i i
- local randomTile
- for (( i = 0; i < (SIZE * SIZE); i++ )); do
- if (( tiles[i] == 0 )); then
- freeTiles[${#freeTiles[*]}]=$i
- fi
- done
- randomTile=${freeTiles[$(( $RANDOM % ${#freeTiles[@]} ))]}
- if (( $RANDOM % 100 <= 90 )); then
- tiles[$randomTile]=2
- else
- tiles[$randomTile]=4
- fi
- }
- function incOrDecFixedTileIndex() {
- local -l direction=$1
- local -l incOrDec=$2
- if [[ x"$direction" = x"h" ]]; then
- if [[ x"$incOrDec" = x"inc" ]]; then
- ((fixedTileIndex++))
- else
- ((fixedTileIndex--))
- fi
- else
- if [[ x"$incOrDec" = x"inc" ]]; then
- ((fixedTileIndex += SIZE))
- else
- ((fixedTileIndex -= SIZE))
- fi
- fi
- }
- function shiftTile() {
- local -l direction=$1
- local -l incOrDec=$2
- local -i fixedTile=${tiles[$fixedTileIndex]}
- local -i floatTile=${tiles[$floatTileIndex]}
- local -i sum
- if [[ $floatTile -eq 0 ]]; then
- return
- fi
- tiles[$floatTileIndex]=0
- if [[ $fixedTile -eq 0 ]]; then
- tiles[$fixedTileIndex]=$floatTile
- elif [[ $floatTile -eq $fixedTile ]]; then
- sum=$((floatTile + fixedTile))
- tiles[$fixedTileIndex]=$sum
- incOrDecFixedTileIndex $direction $incOrDec
- (( score += sum))
- writeBestScore
- else
- incOrDecFixedTileIndex $direction $incOrDec
- tiles[$fixedTileIndex]=$floatTile
- fi
- shifted=true
- }
- function shiftToLeft() {
- for (( i = 0; i < SIZE; i++ )); do
- fixedTileIndex=$((i * SIZE))
- for (( j = 1; j < SIZE; j++ )); do
- floatTileIndex=$((i * SIZE + j))
- shiftTile "h" "inc"
- done
- done
- }
- function shiftToRight() {
- for (( i = 0; i < SIZE; i++ )); do
- fixedTileIndex=$((i * SIZE + (SIZE - 1)))
- for (( j = SIZE - 2; j >= 0; j-- )); do
- floatTileIndex=$((i * SIZE + j))
- shiftTile "h" "dec"
- done
- done
- }
- function shiftToTop() {
- for (( j = 0; j < SIZE; j++ )); do
- fixedTileIndex=$((j))
- for (( i = 1; i < SIZE; i++ )); do
- floatTileIndex=$((i * SIZE + j))
- shiftTile "v" "inc"
- done
- done
- }
- function shiftToBottom() {
- for (( j = 0; j < SIZE; j++ )); do
- fixedTileIndex=$((SIZE * (SIZE - 1) + j))
- for (( i = SIZE - 2; i >=0 ; i-- )); do
- floatTileIndex=$((i * SIZE + j))
- shiftTile "v" "dec"
- done
- done
- }
- function action() {
- shifted=false
- local -l key
- read -n 1 key
- case $key in
- $KEY_UP ) shiftToTop ;;
- $KEY_DOWN ) shiftToBottom ;;
- $KEY_LEFT ) shiftToLeft ;;
- $KEY_RIGHT ) shiftToRight ;;
- esac
- }
- function askWantContinue() {
- local -l key
- echo "You win!"
- echo "k) Keep going"
- echo "r) Try again!"
- read -n 1 key
- case $key in
- "r" ) newGame ;;
- * ) keepGoing="true";;
- esac
- }
- function isYouWin() {
- if [[ ($score -eq 2048) && ($keepGoing = "false") ]]; then
- return 0;
- else
- return 1;
- fi
- }
- function newGame() {
- score=0
- shifted=false
- tiles=(
- 0 0 0 0
- 0 0 0 0
- 0 0 0 0
- 0 0 0 0
- )
- placeRandomTile; placeRandomTile;
- display
- }
- function main() {
- stty -echo
- newGame
- while [[ true ]]; do
- action
- if [[ $shifted = "true" ]]; then
- placeRandomTile
- fi
- display
- if isYouWin; then
- askWantContinue
- display
- fi
- done
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement