Code Monkey home page Code Monkey logo

Comments (3)

bsdlme avatar bsdlme commented on August 18, 2024

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

from bash2048.

bsdlme avatar bsdlme commented on August 18, 2024

Sorry, the comment field sucks, so here's a link to the patch

https://gist.github.com/larsengels/9778574

from bash2048.

JosefZIla avatar JosefZIla commented on August 18, 2024

merged, also I noticed that after exiting the game with this patch, echo on terminal was disabled, not sure what caused this, I added "stty echo" to end_game as a workaround until I can find the cause.

from bash2048.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.