%-----------------------------------------------------------------------------% % NUMBER GUESSSING GAME % %-----------------------------------------------------------------------------% % Revision history: % % 11/01 First version for CS100M DIS % % 10/01 Updated version for CS99 DIS % % 02/04 Cleaned up for CS100M. Added readInt DIS % %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% % Variables % Definitions % % ================= % ======================================================= % clc, clear % clean things up % % lowGuess: % lowest value in range to guess % % highGuess: % highest value in range to guess % % guess: % value that user guesses % % target: % value that computer picks & user attempts to determine % MIN = -1000; % smallest value that user may pick % MAX = 1000; % largest value that user may pick % name = 'ng'; % name of the script % count = 0; % amount of valid guesses from user so far % %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% % Welcome the user: % disp('+-------------------------------------------------------------+'); % disp('| Welcome to the number guessing program! |'); % disp('| Guess the random number that MATLAB picked withing a |'); % disp('| range that you will enter. Enter only integers! |'); % disp('| To interrupt the game, enter a value 1 higher/lower than |'); % disp('| the range that you selected. |'); % disp('+-------------------------------------------------------------+'); % %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% % Prompt for the low and high values: % high = readInt(MIN, MAX,' Enter the highest value: '); % low = readInt(MIN,high,' Enter the lowest value: '); % %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% % Obtain a random number TARGET for the user to guess between and % % including low and high values: % target = ceil(rand*(high-low)) + low; % %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% % Get first guess. If first guess is OK, alert the player whether it is too % % high or low or too many guesses. If not done, process next guess and repeat:% guess = readInt(low-1,high+1,' Guess a number: '); % while guess ~= target && ... % guess <= high && ... % guess >= low && ... % count <= high-low % % % Increment the count of guesses: % count = count+1; % % % Rate the guesses: % if guess < target % disp([' ',num2str(guess),' is too low!']); % elseif guess > target % disp([' ',num2str(guess),' is too high!']); % end % % % Get next guess: % guess = readInt(low-1,high+1,' Guess another number: '); % end % %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% % Report results: % if guess==target % load handel; % sound(y,Fs); % disp([' Congratulations! ',num2str(target),' is the right answer.']); % % Use correct grammar: % if count==0 % label=' guess.'; % else % label=' guesses.'; % end % disp([' You took ',num2str(count+1),label]); % elseif guess > high | guess < low % disp(' Halting!'); % disp([' The correct guess was ',num2str(target),'.']); % elseif count > high-low % disp('You were taking too long, so I quit. Sucks to be you, I guess.'); % else % disp('Something horrible happened. Fix the program!'); % end % %-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------% % Repeat the game if user chooses to do so: % repeat = upper(input(' Play again? [Y/N]: ','s')); % while isempty(repeat) | (repeat~='Y' & repeat~='N') % disp(' Please enter "Y" or "N"!') % repeat = upper(input(' Play again? [Y/N]: ','s')); % end % % Run the program again: % if repeat=='Y' % pause(2); % pause for 2 seconds % eval(name); % rerun the script % % Stop running the program: % else % disp('+-------------------------------------------------------------+');% disp('| Done! Thanks for playing! |');% disp('+-------------------------------------------------------------+');% end % %-----------------------------------------------------------------------------%