How to resolve the algorithm Guess the number step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Guess the number step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   and the program exits. A   conditional loop   may be used to repeat the guessing until the user is correct.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Guess the number step by step in the UNIX Shell programming language

Source code in the unix programming language

#!/bin/sh
# Guess the number
# This simplified program does not check the input is valid

# Use awk(1) to get a random number. If awk(1) not found, exit now.
number=`awk 'BEGIN{print int(rand()*10+1)}'` || exit

echo 'I have thought of a number. Try to guess it!'
echo 'Enter an integer from 1 to 10.'
until read guess; [ "$guess" -eq "$number" ]
do
  echo 'Sorry, the guess was wrong! Try again!'
done
echo 'Well done! You guessed it.'


#!/bin/csh -f
# Guess the number

# jot(1) a random number. If jot(1) not found, exit now.
@ number = `jot -r 1 1 10` || exit

echo 'I have thought of a number. Try to guess it!'
echo 'Enter an integer from 1 to 10.'
@ guess = "$<"
while ( $guess != $number )
	echo 'Sorry, the guess was wrong! Try again!'
	@ guess = "$<"
end
echo 'Well done! You guessed it.'


  

You may also check:How to resolve the algorithm String interpolation (included) step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Host introspection step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the Maxima programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the PL/I programming language