LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-19-2014, 08:49 PM   #1
taz210
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Rep: Reputation: Disabled
I need help with a loop game in script


I need to make a game for my linux class and im so lost on how to do this.
I have to make a game and here is what i was told to do:

Display:

Do you want to do even or odd numbers? Enter 1 for odd or 2 for even.
Ask the user which number will come next?
Show the option they entered and then the correct number.
Tell the child if they got it correct. (display the “correct” in green if the child got it right and “wrong” with the correct answer in red if they got it wrong)
Keep asking until you get to 20.

If someone can help me with this i would greatly appreciate it
 
Old 11-19-2014, 08:54 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

there are literally thousands of examples on how to write this type of script available online. You favourite search engine will help you find them. If you have trouble, post what you have done, explain the problem and we will try to guide you in the right direction. We will not however do your homework for you.

Evo2.
 
Old 11-19-2014, 10:08 PM   #3
taz210
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Not asking for anyone to do my homework for me Im asking if anyone has an example I looked up on google and didnt find anything that remotely helped me.
 
Old 11-19-2014, 10:20 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

I guess your search terms were off. Here is the first hit that google returned when I searched for "bash script example read input from user"

http://tldp.org/LDP/Bash-Beginners-G...ect_08_02.html

Then you'll need to learn about for (or while) loops simple integer math, if statements and escape sequences to change the output colour.

Evo2.
 
Old 11-19-2014, 10:39 PM   #5
taz210
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks
 
Old 11-20-2014, 12:42 AM   #6
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 87

Rep: Reputation: Disabled
You can implement all of this using bash, below is an example. If you don't take the time to actually learn this you will be cheating yourself out of an education.


Code:
#!/bin/bash
# © 2014 Salokin Solutions, LLC
# License: ISC License

my_function() {
    printf "What is the next number in the sequence?: ";
    read answer;
    if [ "$answer" = "$i" ]; then
        printf "\033[1;32mCorrect!\033[0m\n";
    else
        printf "\033[1;31mWrong!\033[0m The correct answer is $i.\n";
    fi
}

echo "Do you want to do even or odd numbers? Enter 1 for odd or 2 for even.";
read answer;

case $answer in
    1)
        for i in $(seq 3 2 20); do
            my_function;
        done
    ;;
    2)
        for i in $(seq 4 2 20); do
            my_function;
        done
    ;;
    *)
        echo "Ask your teacher for help.";
        exit 1;
    ;;
esac

Last edited by nbritton; 11-20-2014 at 04:17 PM.
 
Old 11-20-2014, 02:52 PM   #7
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 87

Rep: Reputation: Disabled
Quote:
Originally Posted by nbritton View Post
If you don't take the time to actually learn this you will be cheating yourself out of an education.
Note that the script I wrote is posix compliant, this was intentional, it was tested and will run in bash, sh, ksh, and zsh. The "echo -e", "\e and \x1b", "==", "[[ ]]", and "backticks: ` " are not posix compliant, this is why I didn't use them. Also, the function syntax that I picked is universally compatible with *sh shells. Error handling is also a necessary component of proper programming. Also note that my excessive use of semicolons are for educational purposes, they are superfluous; my personal preference is to punctuation my statements for the sake of clarity, it's also a habit I picked up from programming with perl.

shebang: http://wiki.bash-hackers.org/dict/te...eter_directive
comments: http://wiki.bash-hackers.org/scripting/basics#comments
functions: http://wiki.bash-hackers.org/syntax/...on_definitions
grouping: https://www.gnu.org/software/bash/ma...mmand-Grouping
printf: http://wiki.bash-hackers.org/commands/builtin/printf
double quotes: http://www.gnu.org/software/bash/man...le-Quotes.html
colors: http://misc.flogisoft.com/bash/tip_c...and_formatting
semicolons: http://wiki.bash-hackers.org/syntax/basicgrammar#lists
read: http://wiki.bash-hackers.org/commands/builtin/read
variables: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html
if: http://wiki.bash-hackers.org/syntax/ccmd/if_clause
test: http://wiki.bash-hackers.org/commands/classictest
test: http://wiki.bash-hackers.org/syntax/...e_test-command
comparison operators: http://tldp.org/LDP/abs/html/comparison-ops.html
echo: http://wiki.bash-hackers.org/commands/builtin/echo
case: http://wiki.bash-hackers.org/syntax/ccmd/case
for: http://wiki.bash-hackers.org/syntax/ccmd/classic_for
command substitution: http://wiki.bash-hackers.org/syntax/expansion/cmdsubst
seq: http://en.wikipedia.org/wiki/Seq_(Unix)

http://wiki.bash-hackers.org/syntax/basicgrammar
http://wiki.bash-hackers.org/scripting/style
http://wiki.bash-hackers.org/scripting/obsolete

Last edited by nbritton; 11-20-2014 at 04:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 03:14 AM
LXer: The feedback loop can improve your game LXer Syndicated Linux News 1 10-06-2012 03:25 AM
Loop a script asking questions snowman81 Programming 5 01-22-2008 02:50 PM
How to loop a script? rollo Linux - General 3 12-21-2006 06:39 AM


All times are GMT -5. The time now is 04:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration