% Guess my number - Exercise % MATLAB® - A Practical Introduction to Programming % and Problem Solving - Sixth Edition - Stormy Attaway % p. 200 - n. 34 % Write a “Guess My Number Game” program. The program generates a random % integer in a specified range, and the user (the player) has to guess the % number. The program allows the use to play as many times as he/she would % like; at the conclusion of each game, the program asks whether the player % wants to play again. % The basic algorithm is: % 1. The program starts by printing instructions on the screen. % 2. For every game: % 2.1 the program generates a new random integer in the range from MIN to % MAX. Treat MIN and MAX like constants; start by initializing them % to 1 and 100 % 2.2 loop to prompt the player for a guess until the player correctly % guesses the integer % 2.3 for each guess, the program prints whether the player2s guess was % too low, too high, or correct % 2.4 at the conclusion (when the integer has been guessed): % 2.4.1 print the total number of guesses for that game % 2.4.2 print a message regarding how well the player did in that % game (e.g., the player took way too long to guess the number, % the player was awesome, etc.). To do this, you will have to % decide on ranges for your messages and give a rationale for % your decision in a comment in the program. % 3. After all games have been played, print a summary showing the average % number of guesses. % Viene fissato il range MIN=1; MAX=100; % Si inizializza il seme per determinare il numero casuale rng('shuffle'); % Variabili per calcolare il numero medio di tentativi complessivo e il % numero di giocate mediatent=0; contagioc=0; % Ciclo per effettuare più giocate fprintf("Stai per partecipare al gioco Guess my number!\n\n") fine=false; while ~fine % Si incrementa il numero di giocate contagioc=contagioc+1; % Si visualizza il numero della giocata fprintf("Giocata numero %d\n\n",contagioc) % Viene inizializzata una variabile che conta il numero di tentativi % nella singola giocata contatent=1; % Istruzioni fprintf("Indovina un numero tra "+MIN+" e "+MAX+"\n") % Generazione dell'intero casuale nel range % Togliere il punto e virgola per visualizzare il numero in fase di % test num=randi([MIN, MAX]); % Viene richiesto un numero guess=input("Inserisci il tuo numero: "); % Ciclo di richieste del numero while guess~=num if guessnum fprintf("Il tuo numero %d è troppo alto\n\n", guess) end guess=input("Inserisci un altro numero: "); contatent = contatent+1; end if contatent==1 fprintf("\nHai indovinato il numero giusto %d in %d tentativo\n", guess, contatent) else fprintf("\nHai indovinato il numero giusto %d in %d tentativi\n", guess, contatent) end % La ricerca binaria è la strategia migliore % Numero massimo di tentativi necessario con la ricerca binaria oktent=ceil(log2(MAX-MIN+1)); % Feedback sulla bravura if contatent<=oktent/2 fprintf('Sei stato molto bravo\n') elseif contatent <=3*oktent/2 fprintf('Te la sei cavata bene\n') else fprintf("Avresti potuto fare decisamente meglio\n") end % Aggiornamento della somma del numero di tentativi mediatent=mediatent+contatent; % Viene chiesto se si vuole giocare ancora chfine=upper(input('\nVuoi giocare ancora? ("n" oppure "N" per uscire, un altro carattere per continuare) ','s')); % Se la risposta è negativa si termina il ciclo e si mostra il numero % medio di tentativi if chfine=='N' fine=true; end end fprintf("\nHai impiegato in media %.2f tentativi\n", mediatent/contagioc)