CS 99
Summer 2002: Lab 5
Solutions

 

%game1.m

disp(' Guess the random number between 1 and 20');

disp(' To interrupt the game, enter 0');

MIN = 1;

MAX = 20;

target = floor( rand*(MAX – MIN + 1) + MIN );

 

guess = input(' Enter first guess: ');

 

while guess ~= target & guess ~= 0

    disp(' Wrong!  Try again.');

    guess = input(' Enter next guess: ');

end

 

if guess == target

    disp(' You guessed it!');

else

    fprintf(' Halting!  The correct guess was %d.\n', target);

end

disp(' Thanks for playing!');

 

 

%game2.m

disp(' Welcome to the number guessing game!');

disp(' Guess the random number between the bounds you set.');

disp(' To interrupt the game, enter an out of bounds guess.');

MIN = input(' Enter lower bound: ');

MAX = input(' Enter upper bound: ');

while MAX <= MIN

    disp(' The upper bound must be greater than the lower bound!');

    MAX = input(' Enter upper bound: ');

end

target = floor( rand*(MAX – MIN + 1) + MIN );

 

guess = input(' Enter first guess: ');

 

while guess ~= target & guess >= MIN & guess <= MAX

    disp(' Wrong!  Try again.');

    guess = input(' Enter next guess: ');

end

 

if guess == target

    disp(' You guessed it!');

else

    fprintf(' Halting!  The correct guess was %d.\n', target);

end

disp(' Thanks for playing!');

 

 

%game3.m

disp(' Welcome to the number guessing game!');

disp(' Guess the random number between the bounds you set.');

disp(' To interrupt the game, enter an out of bounds guess.');

MIN = input(' Enter lower bound: ');

MAX = input(' Enter upper bound: ');

while MAX <= MIN

    disp(' The upper bound must be greater than the lower bound!');

    MAX = input(' Enter upper bound: ');

end

target = floor( rand*(MAX – MIN + 1) + MIN );

 

guess = input(' Enter first guess: ');

 

while guess ~= target & guess >= MIN & guess <= MAX

    if guess > target

        disp(' Too high!');

    else

        disp(' Too low!');

    end

    guess = input(' Enter next guess: ');

end

 

if guess == target

    disp(' You guessed it!');

else

    fprintf(' Halting!\n The correct guess was %d.\n', target);

end

 

 

%game.m

disp(' Welcome to the number guessing game!');

disp(' Guess the random number between the bounds you set.');

disp(' To interrupt the game, enter an out of bounds guess.');

MIN = input(' Enter lower bound: ');

MAX = input(' Enter upper bound: ');

while MAX <= MIN

    disp(' The upper bound must be greater than the lower bound!');

    MAX = input(' Enter upper bound: ');

end

target = floor( rand*(MAX – MIN + 1) + MIN );

 

guess = input(' Enter first guess: ');

count = 0;

 

while guess ~= target & guess >= MIN & guess <= MAX

    count = count + 1;

    if guess > target

        disp(' Too high!');

    else

        disp(' Too low!');

    end

    guess = input(' Enter next guess: ');

end

 

if guess == target

    fprintf(' Congratulations!  The correct number was %d.  You guessed it in %d guesses.\n', target, count + 1);

else

    fprintf(' Halting!\n The correct guess was %d.\n', target);

end

 

repeat = input(' Would you like to play again (y/n)?: ', 's');

if strcmp( lower( repeat ), 'y' )

    game;

else

    disp(' Thanks for playing.');

end