remove 2048

parent bbfd78a3
......@@ -50,7 +50,7 @@ export_result ()
echo -e "PREFIX=\"${PREFIX}\"
prefix=\"${PREFIX}\"
DATADIR=\"${DATADIR}/bashstyle-ng\"
DATADIR_LIST=( ui rc/{2048,functions,internal,settings,styles} vim/colors )
DATADIR_LIST=( ui rc/{functions,internal,settings,styles} vim/colors )
DOCDIR=\"${DOCDIR}/bashstyle-ng\"
MANDIR=\"${MANDIR}/man1\"
BINDIR=\"${BINDIR}\"
......
......@@ -14,7 +14,6 @@ DATA_FILES=( ui/*.{py,ui}:${DATADIR}/ui
rc/internal/*:${DATADIR}/rc/internal
rc/settings/*:${DATADIR}/rc/settings
rc/styles/*:${DATADIR}/rc/styles
rc/2048/*.sh:${DATADIR}/rc/2048
data/vim/colors/*:${DATADIR}/vim/colors
data/*.ini:${DATADIR}
data/*.desktop:${DESKTOPDIR} )
......@@ -29,8 +28,7 @@ done
BIN_FILES=( data/bashstyle:${BINDIR}
data/bashstyle-config-helper:${BINDIR}
rc/functions/*:${DATADIR}/rc/functions
rc/2048/2048:${DATADIR}/rc/2048)
rc/functions/*:${DATADIR}/rc/functions)
ICON_FILES=( data/icons/*.png:${ICONDIR} )
......
......@@ -18,10 +18,6 @@
- Aanand Natarajan [text2morse]
- hackerb9 [xmlpager]
- 2048 game
- mydzor
- rho
- vim color scheme creators:
- Glenn T. Norton [adaryn]
- Joe Estock [advantage]
......
#!/usr/bin/env bash
# variables
declare -i board_size=4
target=2048
function Usage {
echo -e "2048.bash (https://github.com/rhoit/2048)"
echo -e "Usage: 2048.bash [OPTIONS]"
echo -e "\t-b | --board\tboard size"
echo -e "\t-l | --level\tlevel 3-9"
echo -e "\t-d | --debug\tdebug info"
echo -e "\t-h | --help\tDisplay this message"
}
TEMP=$(getopt -o b:l:d:h\
-l board:,level,debug,help\
-n "2048"\
-- "$@")
if [ $? != "0" ]; then exit 1; fi
eval set -- "$TEMP"
while true; do
case $1 in
-b|--board) board_size=$2; shift 2;;
-l|--level) level=$2; shift 2;;
-d|--debug) exec 3>$2; shift 2;;
-h|--help) Usage; exit;;
--) shift; break
esac
done
# extra argument
for arg do
level=$arg
break
done
#----------------------------------------------------------------------
# late loading
WD="$(dirname $(readlink $0 || echo $0))"
header="2048 (https://github.com/rhoit/2048)"
export WD
source $WD/board.sh
declare score=0
declare moves=0
declare ESC=$'\e' # escape byte
#exec 3>/dev/null # no logging by default
#printf "debug mode on" >&3
won_flag=0
trap "end_game 0; exit" INT #handle INT signal
function generate_piece {
change=1
while (( blocks < N )); do
let index=RANDOM%N
let board[index] || {
local val=$((RANDOM%10?2:4))
let blocks++
# just for some delay effects/invert color
local r=$((index/board_size))
local c=$((index-r*board_size))
local c_temp=${_colors[val]}
_colors[$val]="\033[30;48;5;15m"
box_board_block_update $r $c $val
_colors[$val]=$c_temp
let board[index]=val
break;
}
done
}
# perform push operation between two blocks
# inputs:
# $1 - push position, for horizontal push this is row, for vertical column
# $2 - recipient piece, this will hold result if moving or joining
# $3 - originator piece, after moving or joining this will be left empty
# $4 - direction of push, can be either "up", "down", "left" or "right"
# $5 - if anything is passed, do not perform the push, only update number of valid moves
function push_blocks {
case $4 in
u) let "first=$2*board_size+$1";
let "second=($2+$3)*board_size+$1";;
d) let "first=(index_max-$2)*board_size+$1";
let "second=(index_max-$2-$3)*board_size+$1";;
l) let "first=$1*board_size+$2";
let "second=$1*$board_size+($2+$3)";;
r) let "first=$1*$board_size+(index_max-$2)";
let "second=$1*$board_size+(index_max-$2-$3)";;
esac
let ${board[$first]} || {
let ${board[$second]} && {
if test -z $5; then
board[$first]=${board[$second]}
let board[$second]=0
let change=1
else
let next_mov++
fi
}
return
}
let ${board[$second]} && let flag_skip=1
let "${board[$first]}==${board[second]}" && {
if test -z $5; then
let board[$first]*=2
test "${board[first]}" = "$target" && won_flag=1
let board[$second]=0
let blocks-=1
let change=1
let score+=${board[$first]}
else
let next_mov++
fi
}
}
function apply_push { # $1: direction; $2: mode
for ((i=0; i <= $index_max; i++)); do
for ((j=0; j <= $index_max; j++)); do
flag_skip=0
let increment_max=index_max-j
for ((k=1; k <= $increment_max; k++)); do
let flag_skip && break
push_blocks $i $j $k $1 $2
done
done
done
let won_flag && end_game 1
}
function check_moves {
next_mov=0
apply_push u fake
apply_push d fake
apply_push l fake
apply_push r fake
let next_mov==0 && end_game 0
}
function key_react {
read -d '' -sn 1
test "$REPLY" = "$ESC" && {
read -d '' -sn 1 -t1
test "$REPLY" = "[" && {
read -d '' -sn 1 -t1
case $REPLY in
A) apply_push u;;
B) apply_push d;;
C) apply_push r;;
D) apply_push l;;
esac
}
}
}
function figlet_wrap {
> /dev/null which figlet && {
/usr/bin/figlet $@
return
}
shift 3
echo $*
echo "install 'figlet' to display large characters."
}
function end_game {
if (( $1 == 1 )); then
box_board_update
status="YOU WON"
tput cup $offset_figlet_y 0; figlet_wrap -c -w $COLUMNS $status
tput cup $LINES 0;
echo -n "Want to keep on going (Y/N): "
read -d '' -sn 1 result > /dev/null
if [[ $result != 'n' && $result != 'N' ]]; then
echo -n "Y"
target="∞"
won_flag=0
tput cup 0 0
box_board_print $index_max
unset old_board
return
fi
else
status="GAME OVER"
tput cup $offset_figlet_y 0; figlet_wrap -c -w $COLUMNS $status
fi
box_board_terminate
exit
}
function main {
let N=board_size*board_size
let index_max=board_size-1
let blocks=0
for ((i=0; i < N; i++)); do
let board[i]=0 #$i%3?0:1024
# let board[i] && let blocks++
done
# board[0]=0
# board[4]=0
# board[12]=2
box_board_init $board_size
clear
box_board_print $index_max
generate_piece
while true; do
let change && {
generate_piece
box_board_update
change=0
let moves++
#sleep .01 &
} #<&-
key_react # before end game check, so player can see last board state
let blocks==N && check_moves
done
}
main
#!/bin/bash
source $WD/font.sh
lcorn=("╔" "╟" "╚" "║")
rcorn=("╗" "╢" "╝" "║")
cross=("╤" "┼" "╧" "│")
lines=("═" "─" "═" " ")
_colors[0]="\033[m"
_colors[2]="\033[1;33;48;5;24m"
_colors[4]="\033[1;39;48;5;12m"
_colors[8]="\033[1;38;5;227;48;5;202m"
_colors[16]="\033[1;39;48;5;208m"
_colors[32]="\033[1;39;48;5;9m"
_colors[64]="\033[1;39;48;5;1m"
_colors[128]="\033[46;39m"
_colors[256]="\033[48;5;27;39m"
_colors[512]="\033[1;38;5;9;48;5;11m"
_colors[1024]="\033[1;38;5;22;48;5;226m"
_colors[2048]="\033[1;38;5;8;48;5;237m"
function print_x { # $1: char, $2:repeate
for ((l=0; l<$2; l++)); do
echo -en "$1";
done
}
function line_printer { # $1: total_columns, $2: field
printf "%${offset_x}s" " "
printf "${lcorn[$2]}";
for ((j=0; j < $1; j++)); do
print_x "${lines[$2]}" $b_width
printf "${cross[$2]}";
done
print_x "${lines[$2]}" $b_width
echo "${rcorn[$2]}"
}
function box_board_print { # $1: size
echo -e "$header"
status
#print_x "\n" $offset_y
line_printer $1 0
for ((r=0; r <= $1; r++ )); do
let field=($r == $1)?2:1
for ((i=1; i <= $b_height; i++)); do
line_printer $1 3
done
line_printer $1 $field
done
}
function status {
printf "blocks: %-9d" "$blocks"
printf "score: %-9d" "$score"
printf "moves: %-9d" "$moves"
printf "target: %-9s" "$target"
echo
}
function font_map { # $1: x_position, $2: y_position, $3: val
case $3 in
2) two ;;
4) four ;;
8) eight;;
16) sixteen;;
32) thirtytwo;;
64) sixtyfour;;
128) onetwoeight;;
256) twofivesix;;
512) fiveonetwo;;
1024) onezerotwofour;;
2048) twozerofoureight;;
*) block_update2 $1 $2 $3; return;;
esac
printf "${_colors[$3]}"
for ((i=0; i < $b_height; i++)); do
tput cup $(($1+i+1)) $2
printf "${word[i]}"
done
printf "${_colors[0]}"
}
function block_update { # $1: x_position, $2: y_position, $3: val
if [[ $FONT_SH == "1" ]]; then
font_map $1 $2 $3;
return
fi
block_update2 $1 $2 $3;
}
function box_board_block_update { # $1: row, $2: column, $3: val
local r=$1
local c=$2
local x=$((2+r*b_height+$r))
local y=$((1+offset_x+b_width*c+c))
block_update $x $y $3
}
function block_update2 { # $1: x_position, $2: y_position, $3: val
val=$3
if [[ "$val" == 0 ]]; then
val=" "
fi
for ((i=1; i <= $b_height; i++)); do
tput cup $(($1+i)) $2
printf "${_colors[$val]}"
if (( i == mid_y )); then
printf "%${mid_x}s" $val
print_x " " $mid_xr
else
print_x "${lines[3]}" b_width
fi
printf "${_colors[0]}"
done
}
function box_board_update {
tput cup 1 0
status
local index=0
for ((r=0; r < $size; r++)); do
for ((c=0; c < $size; c++)); do
if [[ ${old_board[index]} != ${board[index]} ]]; then
box_board_block_update $r $c ${board[index]}
fi
old_board[$index]=${board[index]}
let index++
done
done
}
function box_board_init { # $1: size
size=$1
LINES=$(tput lines)
COLUMNS=$(tput cols)
b_height=$((LINES/size))
if ((b_height*size > LINE-5)); then
b_height=$(((LINES-4-size)/size))
fi
let b_width=b_height*2+3
let mid_x=b_width/2+1
let mid_y=b_height/2+1
let mid_xr=b_width-mid_x
let screen_mid=LINES/2
let offset_x=COLUMNS/2-b_width*size/2-3
let offset_y=screen_mid-b_height*size/2
let offset_figlet_y=screen_mid-3
screen_x=$((2+(b_height+1)*size))
tput civis # hide cursor
stty -echo # disable output
}
function box_board_terminate {
tput cnorm # show cursor
stty echo # enable output
tput cup $screen_x $COLUMNS
}
if [ `basename $0` == "board.sh" ]; then
s=4
if [[ $# -eq 1 ]] && (( "$1" > -1 )); then
s=$1
fi
trap "box_board_terminate; exit" INT
box_board_init $s
clear
box_board_print $((s-1))
tput cup 0 0
echo -n "block_size(hxw):${b_height}x$b_width "
echo -n "mid(x,y):($mid_x,$mid_y) "
echo -n "offset(x,y):($offset_x,$offset_y) "
echo -n "size:${COLUMNS}x$LINES"
let N=s*s-1
declare -ia board
for ((i=N; i>= 0; i--)); do
board[$i]=$(echo 2^$i | bc)
done
box_board_update
box_board_terminate
fi
#!/bin/bash
# for colorizing numbers
FONT_SH=1
c0="\033[1;m"
c1="\033[1;31m"
c2="\033[1;38;5;22m"
c3="\033[1;32m"
c4="\033[1;38;5;226m"
c5="\033[1;34m"
c6="\033[1;35m"
header="${c1}2${c4}0${c5}4${c3}8${c0} (https://github.com/rhoit/2048)"
function printer { # $1: value
for ((i=0; i<4; i++)); do
echo "${word[$i]}"
done
}
declare -a word
function two {
word[0]=" ▃▄▄▃ "
word[1]=" ▝▘ ▄▀ "
word[2]=" ▄▀▀ "
word[3]=" ▀▀▀▀▀▀ "
}
function four {
word[0]=" ▄▆ "
word[1]=" ▄▀ █ "
word[2]=" ▀▀▀▀█▀ "
word[3]=" ▀ "
}
function eight {
word[0]=" ▄▄▄▄ "
word[1]=" █ ▂▃▄▀ "
word[2]=" ▄▀▔ █ "
word[3]=" ▀▀▀▀ "
}
function sixteen {
word[0]=" ▗▄ ▗▄▄▖ "
word[1]=" █ █▄▄ "
word[2]=" █ █ █ "
word[3]=" ▀ ▀▀ "
}
function thirtytwo {
word[0]=" ▗▄▄▄▖▂▄▄ "
word[1]=" ▄▀ ▔ ▄█ "
word[2]=" ▗▖ ▜▖▃█▀ "
word[3]=" ▝▀▀ ▀▀▀▀ "
}
function sixtyfour {
word[0]=" ▗▄▄▖ ▄▄ "
word[1]=" █▄▄ ▄▀ █ "
word[2]=" █ █▝▀▀▀█▘"
word[3]=" ▀▀ ▀ "
}
function onetwoeight {
word[0]=" "
word[1]="▝▌▀▀▀▖▗▀▀▀▖"
word[2]=" ▌▄▀▀ ▗▀▀▀▖"
word[3]=" ▘▀▀▀▘▝▀▀▀ "
}
function twofivesix {
word[0]=" "
word[1]=" ▀▀▖▐▀▀▗▀▀ "
word[2]=" ▄▀ ▀▚▐▀▀▖"
word[3]=" ▀▀▘▝▀▘ ▀▀ "
}
function fiveonetwo {
word[0]=" "
word[1]="▐▀▀▀▝█ ▀▀▀▖"
word[2]=" ▀▀▄ █ ▄▀▀ "
word[3]="▝▀▀ ▀ ▀▀▀▘"
}
function onezerotwofour {
word[0]=" "
word[1]="${c2}▝▌${c1}▛▜${c5}▝▀▚${c6} ▞▌ "
word[2]="${c2}${c1}▙▟${c5}▗▟▙${c6}▝▀▛ "
word[3]=" "
}
function twozerofoureight {
word[0]=" ${c3}▁▁ "
word[1]="${c1}▝▀▚${c4}▛▜${c5} ▞▌${c3}▙▟ "
word[2]="${c1}▗▟▙${c4}▙▟${c5}▝▀▛${c3}▙▟ "
word[3]=" "
}
if [ `basename $0` == "font.sh" ]; then
two; printer
#four; printer
eight; printer
#sixteen; printer
thirtytwo; printer
sixtyfour; printer
onetwoeight; printer
twofivesix; printer
fiveonetwo; printer
twozerofoureight; printer
fi
# glyph_printer 2
# tput cup $((2+0*b_height+1+0)) #$((1+offset_x+b_width*0+0))
# #echo a
# figlet -f small 2 -w $((1+offset_x+b_width*0+0+b_width)) -r
#!/bin/bash
#########################################################
# #
# This is BashStyle-NG #
# #
# Licensed under GNU GENERAL PUBLIC LICENSE v3 #
# #
# Copyright 2007 - 2017 Christopher Bratusek #
# #
#########################################################
"${BSNG_RCDIR}"/2048/2048 "${@}"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment