PDA

View Full Version : 【Tetris Game -- based on a shell script】(new algorithm)


huaihaizi3
28th November 2011, 11:30 AM
Hi all, i have successfully developed a shell version of the Tetris Game based on a new algorithm few days ago all by myself, below is the link of the source code that i posted at a Linux/Unix forum the first in China:
http://bbs.chinaunix.net/thread-3614425-1-1.html
I'd love to know that someone can be able to optimize my source code and enhance it!
how to play with it?
bash Tetris_Game.sh [runlevel] [previewlevel] [speedlevel]

Goddard
8th December 2011, 08:04 PM
Thats pretty nifty. A shell script game :D Didn't know anyone did that. I will check it out.

GoinEasy9
8th December 2011, 09:39 PM

It works, although some of the symbols falling contain way to many nooks and crannies. I might try to reduce the symbol size if I get some time. My grandaughter should like this. Thanks.

huaihaizi3
10th December 2011, 05:14 AM
Just as i have post that :
Features:
1.Pause/Resume!
2.Bomb is enabled(ID=23)!
3.Across area moving(ID=0)!
4.Customization is enabled under the given value range of coordinates!
5.Various kinds of color(64 kinds of color are available for randomly choose)!
6.Support 4-level preview(can preview the next 4 pieces generated randomly)!

The shape of the tetris is highly Customization, some of the shape are hard to fill up , this is the challenge and pleasure!
If you want to play the game more easily, just change the range of the ID of the boxes in a continuous range: from box0 to boxUPLIMIT, and change the value of of the num in $((RANDOM/num)), while num is the smallest number that can guarantee the value of $((RANDOM/num)) isn't greater than UPLIMIT when RANDOM is 32767! For instance, the current id of the boxes is from box0 to box27, and 1171 is the smallest number that can guarantee: 0 ≤ $((RANDOM/1171)) ≤ 27, as 32767/1171=27, and 32767/1170=28!
Tetris Game Version 3.0 Beta2 is coming soon!

GoinEasy9
10th December 2011, 09:54 PM
Thanks for the update, it's interesting.

huaihaizi3
12th December 2011, 02:20 AM
Tetris Game Version 3.0 Beta2 released!

bob
12th December 2011, 02:40 AM
Well, it's a couple of weeks late, but gotta move the thread to Gamer's Lounge.

GoinEasy9
12th December 2011, 05:59 AM
Gonna have to take a look, thanks for posting the Beta2.

huaihaizi3
14th December 2011, 04:27 AM
#!/bin/bash

for((i=32767/${1}; i>0; --i))
do
if (( 32767/i == ${1} && 32767/(i-1) == ${1}+1 )); then
echo ${i}
exit
fi
done

huaihaizi3
17th December 2011, 05:58 PM
Tetris Game Version 3.0 Beta3 released!

huaihaizi3
25th December 2011, 09:30 AM
Tetris Game Version 3.0 ---Final Version released!

sea
25th December 2011, 11:23 AM
Very nice, Chie chie for sharing :)

smr54
25th December 2011, 12:13 PM
Doesn't work for me on CentOS or Arch. Different errors on each. It's possible that something got lost in the copying and pasting--I'd suggest that you make it available as an attachment or download or something so that one doesn't have to copy and paste it from the forum thread. (For example, with google chrome on CentOS 6, when I first did it, it included the line numbers when I pasted it into a text file).

sea
25th December 2011, 12:23 PM
Very nice, Chie chie for sharing :)

Should have checked if its working, sadly its not..
/ $ tetris
bash: /home/simon/bin/tetris: Permission denied
/ $ cd
~ $ cd bin
bin $ chmod +x tetris
bin $ tetris
/home/simon/bin/tetris: line 722: warning: here-document at line 7 delimited by end-of-file (wanted `EOF')
/home/simon/bin/tetris: line 7: ordxy: command not found
/home/simon/bin/tetris: line 7: m-1: command not found
/home/simon/bin/tetris: line 7: m+1: command not found
/home/simon/bin/tetris: line 7: radom: command not found
/home/simon/bin/tetris: line 7: racol: command not found
/home/simon/bin/tetris: line 7: radom: command not found
/home/simon/bin/tetris: line 7: racol: command not found
/home/simon/bin/tetris: line 7: radom: command not found
/home/simon/bin/tetris: line 7: racol: command not found
/home/simon/bin/tetris: line 7: dropa: command not found
/home/simon/bin/tetris: line 7: corev: command not found
/home/simon/bin/tetris: line 7: rmaxy: command not found
/home/simon/bin/tetris: line 7: rmaxx: command not found
bin $

huaihaizi3
25th December 2011, 01:16 PM
My script is the shell version of the classic Tetris Game!
It runs pretty well under Fedora16, and you can see the screenshot from the link that i have already given above:
http://bbs.chinaunix.net/thread-3614425-1-1.html
just run the script as this:

bash Tetris_Game.sh

or

./Tetris_Game.sh


---------- Post added at 06:16 AM ---------- Previous post was at 06:02 AM ----------

I added the attachment!

smr54
25th December 2011, 01:29 PM
Thanks for adding the attachment--honestly, I think it's a good idea. It works for me, so I'm guessing something went wrong with trying to copy it from the web link.

(I should add, it works with bash Tetris_Game.sh, but does NOT work with sh Tetris_Game.sh. Nothing wrong with that of course, it's a bash script, not a /bin/sh script).

Thanks for your efforts, and thank you for creating it as an attachment.

huaihaizi3
26th December 2011, 01:21 PM
I added some New code!

huaihaizi3
2nd January 2012, 12:10 PM
Tetris Game Version 4.0 ---Final Version released!

huaihaizi3
7th January 2012, 05:00 PM
new updates!

stevea
7th January 2012, 06:14 PM
Hi all, i have successfully developed a shell version of the Tetris Game based on a new algorithm few days ago all by myself, below is the link of the source code that i posted at a Linux/Unix forum the first in China:
http://bbs.chinaunix.net/thread-3614425-1-1.html
I'd love to know that someone can be able to optimize my source code and enhance it!


Nice game.

The bash coding style is good. Good clear names and a good attempt to decompose the task into utility functions. I might have made a different decomposition - but that's a matter of the specifics of the goal.

Criticism: Are sed & awk needed ? Have you looked for a bash feature instead ? For example this sort of thing is very awkward ..
echo -e "$(sed 's/\[\]/ /g' <(echo ${oldbox}))\e[0m";
might be replaced with the bash pattern substitution ${parameter/pattern/string}.

The screen codes are non-portable. Things like this ....
echo -e "\e[?25h\e[36;4H"
only apply to VT100 terminals and xterms that emulate these. Better to use the 'tput' function to access curses. tput can give you the portable strings to position the cursor, set color foreground and background ,set bold, normal etc. like tput cup 35 3 .




--------

When the thought has been dominated by a scientific sense, is influenced by a rational concept, the fate of the result will change the trajectory of life will also be moving in the direction of extension of the successful

I imagine that made a lot more sense before translation :confused:

huaihaizi3
8th January 2012, 04:15 AM
Criticism: Are sed & awk needed ? Have you looked for a bash feature instead ? For example this sort of thing is very awkward ..
echo -e "$(sed 's/\[\]/ /g' <(echo ${oldbox}))\e[0m";
might be replaced with the bash pattern substitution ${parameter/pattern/string}.

The screen codes are non-portable. Things like this ....
echo -e "\e[?25h\e[36;4H"
only apply to VT100 terminals and xterms that emulate these. Better to use the 'tput' function to access curses. tput can give you the portable strings to position the cursor, set color foreground and background ,set bold, normal etc. like tput cup 35 3 .

Thank you!
Good advices!
There is a better solution that can replace the maxxy function

maxyx()
{
local i a b
set -- ${box[@]}
a[$2]=${1}
b[$2]="${1} ${2}" && shift 2
while (( ${#} > 0 ))
do
(( a[$2] < ${1} )) && a[$2]=${1}
b[$2]="${a[$2]} ${2}"
shift 2
done
echo ${b[@]}
}

I also replaced the sed with the bash pattern substitution, just as you said ,it's better to use the shell builtin features!
For the issue of portable, i'm sorry ,i have no much time to deal with it!

huaihaizi3
10th January 2012, 03:28 PM
I added runlevel support!

huaihaizi3
21st January 2012, 06:18 PM
Tetris Game Version 5.0 released!
Added comments!

GoinEasy9
6th February 2012, 03:21 AM
Just got a chance to look at it again. Even better now with runlevel support. Thanks.

huaihaizi3
5th March 2012, 01:49 PM
Tetris Game Version 6.0 Beta1 is available now: https://github.com/yongye/shell

huaihaizi3
8th March 2012, 06:39 AM
Updated 6.0 Beta1

huaihaizi3
14th March 2012, 02:14 AM
Tetris Game Version 6.0 Beta2 released!

huaihaizi3
19th March 2012, 01:50 AM
Tetris Game Version 6.0 Beta3 released!

huaihaizi3
1st April 2012, 05:26 PM
Tetris Game Version 6.0 Beta5 released!

huaihaizi3
17th April 2012, 06:24 PM
Tetris Game Version 6.0 released!