LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2006, 07:26 AM   #31
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15

The problem with not being rewarded for the last try comes from the flow of the code:
Code:
begin_loop
  read_guess
  if_guesses_gt_max_exit
  check_guess
  if_correct_exit
end_loop
so on the last loop you're reading in the guess, then deciding that actually, they're not entitled to another guess. Check entitlement first:

Code:
begin_loop
  if_guesses_gt_max_exit
  read_guess
  check_guess
  if_correct_exit
end_loop
or:
Code:
loop_for_10_guesses
  read_guess
  check_guess
  if_correct_exit_0
end_loop
tell_gamer_that_they've_lost
exit_1

Last edited by Tischbein; 10-22-2006 at 07:44 AM.
 
Old 10-22-2006, 07:53 AM   #32
tizedboy
LQ Newbie
 
Registered: Oct 2006
Location: Germany
Distribution: Suse 10.1
Posts: 13

Original Poster
Rep: Reputation: 0
Hi guys :-), I thank you all for being so helpful especially for beginners. The script (I guess) is ready and if you could please find anything missing or bug(s) I would appreciate.

Code:
# Guessing script
# !/bin/bash
clear
	#Setting my variables: times for looping, guess - the randomly generated
	#number to be guessed (1-40 in this example), try - for counting number of tries(max 10 in this example), left - for counting number of remaining tries
	times=0
	guess=$[$RANDOM * 40 / 32767]
	try=0
	left=10
	
	echo "You have 10 tries "
	echo
	
	#starting my loop; as long as this loop remains true........
	while [ $times ]
		do
		
		#interact with the users , read and store thier inputs
		echo -n "Number between 1 and 40 please: "
		read input
		
		#start counting to max 10, so as not to loop endlessly!
		try=$[ $try + 1 ]
		
		#start counting to zero( for info only)
		left=$[ $left - 1 ]
		echo
		echo "Try number: $try "
		[ $left -eq 1 ] && echo "Your last try "
		[ $left -eq 0 ] && echo "You have exhausted all 10 tries"
		echo "Number of tries left: $left "
		
			 if [ $try = 10 ] 
			 then
			 echo
			 echo "The guess should have been $guess "
			 echo	
			 exit 0
		else
		
		#check what users have typed to make sure they typed only integers (1-40 in this example)
		echo $input | grep "^[1-9][0-9]*$" > /dev/null 2>&1
			
		#if something other than an integer was entered....
		if [ $? -ne 0 ]
			then
			[ -n $? ] && echo "Empty string or non-numeric encountered "
			echo "Only integers 1-40 please "
			echo
			continue
		else
			#if the user guessed........
			if [ $input -eq $guess ]
   			then
			
			#and it was their last try........
			[ $try -eq 10 ] && echo "And on the very last guess....."
   			echo "Congratulation "
   			exit 0
 		else
			#if they just missed: smaller or greater......
    			if [ $input -lt $guess ]
       			then
       			echo "Number you entered was too small "
			echo
    			else
       			echo "Number you entered was too big "
    			echo
			fi
		fi
	fi
fi
done
 
Old 10-22-2006, 09:15 AM   #33
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Looks good. Just one important thing that's not correct.

The first line of a script should always start with the so called hash-bang (#!/bin/bash) and there shouldn't be a space between the # and the !.

Change your first two lines with this:
#!/bin/bash
# Guessing script


Nice job!
 
  


Reply



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
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
starting xprogram without starting X server. bruse Linux - Newbie 9 04-26-2005 07:05 PM
Auto starting and/or scripting Infernal211283 Linux - Newbie 1 03-22-2005 12:41 PM
Won't Boot - Starting Printer Service - Starting CUPS jeansond Linux - Newbie 0 10-11-2004 06:39 PM
Starting apps after starting xfce?? gtgoku Slackware 2 08-21-2004 01:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 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
Open Source Consulting | Domain Registration