【Tetris Game -- based on a shell script】(new algorithm)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
【Tetris Game -- based on a shell script】(new algorithm)
Hi all, i have successfully developed a shell version of the Tetris Game based on a new algorithm yesterday 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! Rename the attachment Tetris_Game.txt to Tetris_Game.sh before you run it! how to play with it?
Your effort is quite impressive. Sadly, after a short period of play, I got a repeated series of error messages, followed by a process termination:
Code:
./tetris.sh: cannot make pipe for process substitution: Too many open files
./tetris.sh: cannot make pipe for process substitution: Too many open files
./tetris.sh: line 258: <(echo ${!1} | xargs -n 2 | sort -n -k1): ambiguous redirect
./tetris.sh: cannot make pipe for command substitution: Too many open files
I was running 'GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)' on a Debian 6 host.
I will find it interesting to study your code, as I've never seen a shell script operate in such an interactive way.
I'm quite pleased with seeing this kind of stuff in bash. I'm too inclined to think about games and other seemingly "unrealizable" things written in it, "for the lulz", because of the challenge, and to bother all those people that always whine "use another laaanguaaaaage" . I hope to take some time whenever I can to help enhance the program. One thing that would be cool to add is dual mode playing, you know like the original Tetris where two players had half screen. Was fun hehehe.
Didn't look in depth, but more or less how many processes are running while the game is playing? And also, at a glance, I see you use few parameter substitutions, arguably your best chance in bash at minimizing calls to external programs (if that would be absolutely necessary though, I'll probably calling "sed"). Perhaps some tweaking in some of the multi-pipe lines might speed it up a little, and be related to the "many open files" ?
Also, can you write a bit on your approaches, and any tricky parts (any amount would be helpful) to speed up grasping the script?
My script is the shell version of the classic Tetris Game!
It runs pretty well under Fedora15, and you can see the screenshot from the link that i have already given above: http://bbs.chinaunix.net/thread-3614425-1-1.html
I'm quite pleased with seeing this kind of stuff in bash. I'm too inclined to think about games and other seemingly "unrealizable" things written in it, "for the lulz", because of the challenge, and to bother all those people that always whine "use another laaanguaaaaage" . I hope to take some time whenever I can to help enhance the program. One thing that would be cool to add is dual mode playing, you know like the original Tetris where two players had half screen. Was fun hehehe.
Didn't look in depth, but more or less how many processes are running while the game is playing? And also, at a glance, I see you use few parameter substitutions, arguably your best chance in bash at minimizing calls to external programs (if that would be absolutely necessary though, I'll probably calling "sed"). Perhaps some tweaking in some of the multi-pipe lines might speed it up a little, and be related to the "many open files" ?
Also, can you write a bit on your approaches, and any tricky parts (any amount would be helpful) to speed up grasping the script?
There are two processes running while the game is playing:
bash $0 0 &--the one that running in the background, and
getsig $!--the one that running in the current shell, this function takes the $! as the parameter and it sends the input signals to the processes that running in the background, this is the way that the two processes communicate!
My laptop is running Fedora15, and this script runs pretty well under it,the multi-pipe lines are most needed and
necessary; in fact i have optimized my script as much as possible, as you can see that i have used so many function invocations, that is to say, the matter that result in "many open files" is mainly due to the distro of you Linux, not the
multi-pipe lines! For a better way to speed up the script, i know ,and i'm trying!
I'll add a detailed comments to the code when i'm free!
Hey the code looks so much better this time! Bet that it runs way smoother than version 1. Congrats man! Still haven't found the time to play with the code, but I definitely want to.
I always marveled at what could be done in sh, but this opened my eyes!
I ran
Code:
ulimit -n30000
before running it to avoid the too many open files errors.
After a while of having my mind blown, I started to get this:
Code:
./tetris: line 590: warning: run_pending_traps: bad value in trap_list[26]: (nil)
./tetris: line 590: warning: run_pending_traps: signal handler is SIG_DFL, resending 26 (SIGVTALRM) to myself
./tetris: line 590: warning: run_pending_traps: bad value in trap_list[26]: (nil)
./tetris: line 590: warning: run_pending_traps: signal handler is SIG_DFL, resending 26 (SIGVTALRM) to myself
is really a bug that i'm trying my best to fix it!
The code below:
Code:
concxy()
{
local x y sup
while read x y
do
cdn="${cdn}\e[${x};${y}H${mrx}"
sup="${sup} ${x} ${y}"
done < <(srbox ${1})
${2}
}
is mainly the reason that result in the errors occurred while i running the script!
But, it seems nothing wrong with those code, as i have debugged it so much!
Any good solutions that can fix the bug will be appreciated!
I need all your participation!
Join me, why not?
Some (I for an example) really whish, but don't have the time to intepret a respectable amount of uncommented code with nondescriptive identifiers: no function or var gives a clue about anything! And some functions are "one-lined".
Why don't you make your code friendlier? It won't impact in performance. That way you'll encourage participation.
Tetris Game Version 2.0 Beta3 released!
Features:
1.Bomb is enabled(ID=2)!
2.Customization is enabled under the given value range of coordinates!
3.Various kinds of color(64 kinds of color are available for randomly choose)!
4.Support 4-level preview(can preview the next 4 pieces generated randomly)!
The final version is coming soon!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.