Posted by: Sourav | November 10, 2014

Random number guessing game in TCL


#!/usr/bin/tclsh
# guess_rand.tcl
# Guess a random number between 1 and 20
# Block 1
# Algorithm is “min + (random * (max – min))”
set target [expr {int(1 + (rand() * 19))}]
# Block 2
# Read the user’s guess
set count 0
#set count [expr $count+1]
puts “$count”
for {set i 1} {$i <= 10} {incr i} {
puts -nonewline “Enter a number between 1 and 20: ”
flush stdout
gets stdin guess
# Block 3
# Validate the input
set count [expr $count+1]
if {$guess < 1 || $guess > 20} {
puts “Your guess must be between 1 and 20”

} elseif { $guess < $target } {
puts “guess higher”
exit 1

} elseif { $guess > $target } {
puts “guess lower”

} else {

puts “you have guess the number is $count chances”
break;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: