LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I need help with a loop game in script (https://www.linuxquestions.org/questions/linux-newbie-8/i-need-help-with-a-loop-game-in-script-4175525930/)

taz210 11-19-2014 07:49 PM

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

evo2 11-19-2014 07:54 PM

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.

taz210 11-19-2014 09:08 PM

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.

evo2 11-19-2014 09:20 PM

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.

taz210 11-19-2014 09:39 PM

Thanks :)

nbritton 11-19-2014 11:42 PM

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


nbritton 11-20-2014 01:52 PM

Quote:

Originally Posted by nbritton (Post 5272277)
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


All times are GMT -5. The time now is 06:58 AM.