View Single Post
Old 07-14-2012, 06:59 AM   #1
mmatej
Connoisseur
mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.mmatej is less competitive than you.
 
Posts: 91
Karma: 14730
Join Date: Jun 2012
Device: none
Puzzle swapping minigame

I was so bored that I've made this stupid minigame. It's written as shell script, so don't expect it wil be fast as hell. (init takes 3 mins)
Spoiler:

Code:
#!/bin/sh
[ "${#}" -ne 2 ] &&
{
echo "Usage: ${0} <blocks per X> <blocks per Y>"
return 1
}
fb='/dev/fb0'
ddfb()# <bs> <count> [skip] [seek]
{
sk=0
se=0
[ -n "${3}" ] && sk="${3}"
[ -n "${4}" ] && se="${4}"
dd if="${fb}" of="${fb}" bs=${1} count=${2} skip=${sk} seek=${se} > /dev/null 2>&1
}
copyH()# <fromY> <toY> <height>
{
ddfb 608 "${3}" "${1}" "${2}"
}
copyrect()# <fromX> <fromY> <width> <height> <toX> <toY>
{
fendY=$(expr ${2} + ${4})
taY=${6}
for i in $(seq ${2} ${fendY}); do
ddfb 1 ${3} $(expr 608 \* ${i} + ${1}) $(expr 608 \* ${taY} + ${5})
taY=$(expr ${taY} + 1)
done
}
fillrect()# <toX> <toY> <width> <height> [hex_color]
{
hex='00'
[ -n "${5}" ] &&
{
hex=${5}
[ "${#hex}" -ne 2 ] && hex='0'"${hex}"
}
fendY=$(expr ${2} + ${4})
taY=${2}
for i in $(seq ${2} ${fendY}); do
[ "${hex}" == '00' ] && dd if="/dev/zero" of="${fb}" bs=1 count=${3} seek=$(expr ${i} \* 608 + ${1}) > /dev/null 2>&1
[ "${hex}" != '00' ] && cat /dev/zero|tr '\x00' '\x'"${hex}"|dd of="${fb}" bs=1 count=${3} seek=$(expr ${i} \* 608 + ${1}) > /dev/null 2>&1
taY=$(expr ${taY} + 1)
done
}
grid()
{
for x in $(seq 1 "$(expr ${X} - 1)"); do fillrect $(expr ${x} \* ${W} - ${T}) 0 $(expr 2 \* ${T}) 800; done
for y in $(seq 1 "$(expr ${Y} - 1)"); do fillrect 0 $(expr ${y} \* ${H} - ${T}) 600 $(expr 2 \* ${T}); done
fillrect 0 0 600 1
fillrect 0 800 600 1
fillrect 0 0 1 800
fillrect 600 0 1 800
}
blitblock()
{
fr="$(expr ${2} + ${NUM} - 1)"
to="$(expr ${1} - 1)"
fx=$(expr "$(expr ${fr} % ${X})" \* ${W})
fy=$(expr "$(expr ${fr} / ${X})" \* ${H})
tx=$(expr "$(expr ${to} % ${X})" \* ${W})
ty=$(expr "$(expr ${to} / ${X})" \* ${H})
copyrect ${fx} ${fy} ${W} ${H} ${tx} ${ty}
}
blitchanged()
{
[ "${oblocks}" == "${blocks}" ] && return
echo -n "Updating changed blocks... "
for i in $(seq 1 ${NUM}); do
old="$(getf "$oblocks" ${i})"
new="$(getf "$blocks" ${i})"
if [ "${old}" != "${new}" ]; then
blitblock ${i} ${new}
fi
done
oblocks="${blocks}"
echo 'done'
}
swap()
{
to1="$(getf "${blocks}" ${1})"
to2="$(getf "${blocks}" ${2})"
nblocks=''
for i in $(seq 1 ${NUM}); do
[ ${i} -ne 1 ] && nblocks="${nblocks}"' '
[ ${i} -eq ${1} ] &&
{
nblocks="${nblocks}""${to2}"
continue
}
[ ${i} -eq ${2} ] &&
{
nblocks="${nblocks}""${to1}"
continue
}
a="$(getf "${blocks}" ${i})"
nblocks="${nblocks}""${a}"
done
blocks="${nblocks}"
}
decode16()
{
local b="$(echo "$1"|cut -d' ' -f 1)"
local a="$(echo "$1"|cut -d' ' -f 2)"
echo "$(expr 256 \* $a + $b)"
}
randnum()
{
rstr="$(xd -qdbl 3 /dev/urandom|cut -d' ' -f 2,3,4)"
c="$(getf "${rstr}" 1)"
b="$(getf "${rstr}" 2)"
a="$(getf "${rstr}" 3)"
rnum="$(expr 65536 \* ${a} + 256 \* ${b} + ${c})"
echo "$(expr ${rnum} % "$(expr ${2} - ${1} + 1)" + ${1})"
}
getf()
{
echo "$(echo "${1}"|cut -d' ' -f ${2})"
}
getdiff()
{
first=${1}
second=${2}
[ ${2} -gt ${1} ] &&
{
first=${2}
second=${1}
}
echo "$(expr ${first} - ${second})"
}
checkgame()
{
local count=0
for i in $(seq 1 ${NUM}); do
local acb="$(getf "${blocks}" ${i})"
[ ${acb} -ne ${i} ] && count="$(expr ${count} + 1)"
done
echo "${count}"
}

[ "$(expr 608 % ${1})" -ne 0 ] && { echo "608 not divisable by ${1} (X)";return 1; }
[ "$(expr 800 % ${2})" -ne 0 ] && { echo "800 not divisable by ${2} (Y)";return 1; }
killall -stop cvm framework Xorg
trap "copyH 1600 0 800;eips -f '';killall -cont cvm framework Xorg;exit;" SIGINT SIGTERM EXIT
echo "Initializing..."
X=${1}
Y=${2}
W="$(expr 600 / ${X})"
H="$(expr 800 / ${Y})"
NUM="$(expr ${X} \* ${Y})"
T=1
IFS='
'
blocks=''
for i in $(seq 1 ${NUM}); do
[ ${i} -ne 1 ] && blocks="${blocks}"' '
blocks="${blocks}""${i}"
done
oblocks="${blocks}"
copyH 0 1600 800
echo "Creating grid..."
grid
copyH 0 800 800
eips ''
echo "Shuffling blocks..."
rseq=''
for i in $(seq 1 ${NUM}); do
[ ${i} -ne 1 ] && rseq="${rseq}"' '
rseq="${rseq}""${i}"
done
blocks=''
for i in $(seq 1 ${NUM}); do
echo "${i}/${NUM}"
to="$(expr ${NUM} - ${i} + 1)"
rn="$(randnum 1 "${to}")"
nseq=''
added=0
for j in $(seq 1 "${to}"); do
tnum="$(getf "${rseq}" ${j})"
[ ${j} -eq ${rn} ] &&
{
[ ${i} -ne 1 ] && blocks="${blocks}"' '
blocks="${blocks}""${tnum}"
continue
}
[ ${added} -eq 1 ] && nseq="${nseq}"' '
nseq="${nseq}""${tnum}"
added=1
done
rseq="${nseq}"
done
#blocks="${oblocks}"
blitchanged
echo "Done."
eips -f ''

sel=0
oldx=0
oldy=0
while true; do
bb=-1
[ ${sel} -eq 0 ] && bb="$(checkgame)"
[ ${bb} -eq 0 ] &&
{
trap - SIGINT SIGTERM EXIT
trap "eips -f '';killall -cont cvm framework Xorg;exit;" SIGINT SIGTERM EXIT
copyH 1600 0 800
eips ''
echo "You have won!"
sleep 5
break
}
[ ${bb} -ne -1 ] && echo "You have ${bb} misplaced blocks."
have_e=0
xa=0
ya=0
while [ ${have_e} -eq 0 ]; do
for data in $(xd -qdbl 96 /dev/input/event3); do
[ "$(getf "${data}" 10)" -ne '003' ] && continue
code="$(getf "${data}" 12)"
[ "${code}" == '057' ] && continue
pos="$(echo "${data}"|cut -d' ' -f 14,15)"
rpos="$(decode16 "${pos}")"
[ ${code} -eq 53 ] && xa="${rpos}"
[ ${code} -eq 54 ] && ya="${rpos}"
done
have_e=1
[ ${xa} -eq 0 ] && { have_e=0;continue; }
[ ${ya} -eq 0 ] && { have_e=0;continue; }
break
done
xr="$(expr ${xa} \* 600 / 4095)"
yr="$(expr ${ya} \* 800 / 4095)"
x="$(expr ${xr} / ${W})"
y="$(expr ${yr} / ${H})"
bid="$(expr ${y} \* ${X} + ${x} + 1)"
[ ${sel} -eq 0 ] &&
{
sel="${bid}"
echo -n "${sel} -> "
oldx=${x}
oldy=${y}
continue
}
[ ${bid} -eq ${sel} ] &&
{
echo 'X'
sel=0
continue
}
xchange="$(getdiff ${oldx} ${x})"
ychange="$(getdiff ${oldy} ${y})"
poschange="$(expr ${xchange} + ${ychange})"
[ ${poschange} -ne 1 ] &&
{
echo 'X'
sel=0
continue
}
echo "${bid}"
swap ${sel} ${bid}
blitchanged
eips ''
sel=0
done
dd if="/dev/zero" of="/dev/fb0" bs=608 seek=800 > /dev/null 2>&1
exit

Screen:
Spoiler:

You can swap only adjacent blocks. Have fun!
mmatej is offline   Reply With Quote