Advertisement
Guest User

game2048

a guest
May 18th, 2014
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.93 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. declare -r SIZE=4
  4.  
  5. declare -r KEY_UP="w"
  6. declare -r KEY_DOWN="s"
  7. declare -r KEY_RIGHT="d"
  8. declare -r KEY_LEFT="a"
  9.  
  10. declare -r BEST_SCORE_FILE="best-score.txt"
  11.  
  12. declare -i fixedTileIndex
  13. declare -i floatTileIndex
  14.  
  15. declare -i score
  16. declare -i bestScore
  17. declare shifted
  18. declare -a tiles
  19. declare keepGoing=false
  20.  
  21. function writeBestScore() {
  22.     if [[ -w $BEST_SCORE_FILE ]]; then
  23.         if (( score > bestScore )); then
  24.             echo "$score" > $BEST_SCORE_FILE
  25.         fi
  26.     fi
  27. }
  28.  
  29. function readBestScore() {
  30.     if [[ -r $BEST_SCORE_FILE ]]; then
  31.         bestScore=$(cat $BEST_SCORE_FILE)
  32.     else
  33.         bestScore=0
  34.     fi
  35. }
  36.  
  37. function display() {
  38.     local -i i
  39.     local -i j
  40.     local -i tileValue
  41.     local generatedColor
  42.  
  43.     readBestScore
  44.  
  45.     echo -e "\e[1;1f\e[2J" # clear display
  46.     echo -e "\e[31mScore: $score    Best: $bestScore"
  47.     echo
  48.  
  49.     for (( i = 0; i < SIZE; i++ )); do
  50.         for (( j = 0; j < SIZE; j++ )); do
  51.             tileValue=${tiles[i * SIZE + j]}
  52.             generatedColor="\e[3$(( (tileValue % 10) / 2 + 1 ))m"
  53.             echo -n -e " $generatedColor" $tileValue " "
  54.         done
  55.         echo
  56.     done
  57. }
  58.  
  59. function placeRandomTile() {
  60.     local -a freeTiles
  61.     local -i i
  62.     local randomTile
  63.  
  64.     for (( i = 0; i < (SIZE * SIZE); i++ )); do
  65.         if (( tiles[i] == 0 )); then
  66.             freeTiles[${#freeTiles[*]}]=$i
  67.         fi
  68.     done
  69.  
  70.     randomTile=${freeTiles[$(( $RANDOM % ${#freeTiles[@]} ))]}
  71.  
  72.     if (( $RANDOM % 100 <= 90 )); then
  73.         tiles[$randomTile]=2
  74.     else
  75.         tiles[$randomTile]=4
  76.     fi
  77. }
  78.  
  79. function incOrDecFixedTileIndex() {
  80.     local -l direction=$1
  81.     local -l incOrDec=$2
  82.  
  83.     if [[ x"$direction" = x"h" ]]; then
  84.         if [[ x"$incOrDec" = x"inc"  ]]; then
  85.             ((fixedTileIndex++))
  86.         else
  87.             ((fixedTileIndex--))
  88.         fi
  89.     else
  90.         if [[ x"$incOrDec" = x"inc"  ]]; then
  91.             ((fixedTileIndex += SIZE))
  92.         else
  93.             ((fixedTileIndex -= SIZE))
  94.         fi
  95.     fi
  96. }
  97.  
  98. function shiftTile() {
  99.     local -l direction=$1
  100.     local -l incOrDec=$2
  101.  
  102.     local -i fixedTile=${tiles[$fixedTileIndex]}
  103.     local -i floatTile=${tiles[$floatTileIndex]}
  104.     local -i sum
  105.  
  106.     if [[ $floatTile -eq 0 ]]; then
  107.         return
  108.     fi
  109.  
  110.     tiles[$floatTileIndex]=0
  111.  
  112.     if [[ $fixedTile -eq 0 ]]; then
  113.         tiles[$fixedTileIndex]=$floatTile
  114.     elif [[ $floatTile -eq $fixedTile ]]; then
  115.         sum=$((floatTile + fixedTile))
  116.         tiles[$fixedTileIndex]=$sum
  117.         incOrDecFixedTileIndex $direction $incOrDec
  118.         (( score += sum))
  119.         writeBestScore
  120.     else
  121.         incOrDecFixedTileIndex $direction $incOrDec
  122.         tiles[$fixedTileIndex]=$floatTile
  123.     fi
  124.     shifted=true
  125. }
  126.  
  127. function shiftToLeft() {
  128.     for (( i = 0; i < SIZE; i++ )); do
  129.         fixedTileIndex=$((i * SIZE))
  130.         for (( j = 1; j < SIZE; j++ )); do
  131.             floatTileIndex=$((i * SIZE + j))
  132.             shiftTile "h" "inc"
  133.         done
  134.     done
  135. }
  136.  
  137. function shiftToRight() {
  138.     for (( i = 0; i < SIZE; i++ )); do
  139.         fixedTileIndex=$((i * SIZE + (SIZE - 1)))
  140.         for (( j = SIZE - 2; j >= 0; j-- )); do
  141.             floatTileIndex=$((i * SIZE + j))
  142.             shiftTile "h" "dec"
  143.         done
  144.     done
  145. }
  146.  
  147. function shiftToTop() {
  148.     for (( j = 0; j < SIZE; j++ )); do
  149.         fixedTileIndex=$((j))
  150.         for (( i = 1; i < SIZE; i++ )); do
  151.             floatTileIndex=$((i * SIZE + j))
  152.             shiftTile "v" "inc"
  153.         done
  154.     done
  155. }
  156.  
  157. function shiftToBottom() {
  158.     for (( j = 0; j < SIZE; j++ )); do
  159.         fixedTileIndex=$((SIZE * (SIZE - 1) + j))
  160.         for (( i = SIZE - 2; i >=0 ; i-- )); do
  161.             floatTileIndex=$((i * SIZE + j))
  162.             shiftTile "v" "dec"
  163.         done
  164.     done
  165. }
  166.  
  167. function action() {
  168.     shifted=false
  169.     local -l key
  170.  
  171.     read -n 1 key
  172.     case $key in
  173.         $KEY_UP ) shiftToTop ;;
  174.         $KEY_DOWN ) shiftToBottom ;;
  175.         $KEY_LEFT ) shiftToLeft ;;
  176.         $KEY_RIGHT ) shiftToRight ;;
  177.     esac
  178. }
  179.  
  180. function askWantContinue() {
  181.     local -l key
  182.  
  183.     echo "You win!"
  184.     echo "k) Keep going"
  185.     echo "r) Try again!"
  186.     read -n 1 key
  187.  
  188.     case $key in
  189.         "r" ) newGame ;;
  190.         * ) keepGoing="true";;
  191.     esac
  192. }
  193.  
  194. function isYouWin() {
  195.     if [[ ($score -eq 2048) && ($keepGoing = "false") ]]; then
  196.         return 0;
  197.     else
  198.         return 1;
  199.     fi
  200. }
  201.  
  202. function newGame() {
  203.     score=0
  204.     shifted=false
  205.     tiles=(
  206.         0 0 0 0
  207.         0 0 0 0
  208.         0 0 0 0
  209.         0 0 0 0
  210.     )
  211.  
  212.     placeRandomTile; placeRandomTile;
  213.     display
  214. }
  215.  
  216. function main() {
  217.     stty -echo
  218.  
  219.     newGame
  220.  
  221.     while [[ true ]]; do
  222.         action
  223.         if [[ $shifted = "true" ]]; then
  224.             placeRandomTile
  225.         fi
  226.         display
  227.  
  228.         if isYouWin; then
  229.             askWantContinue
  230.             display
  231.         fi
  232.     done
  233. }
  234.  
  235. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement