4 views (last 30 days)

Show older comments

I am trying to graph a bar graph from a number guessing game which asks users to input values. I want to make a bar graph of the numbers guessed but am only getting the bar graph to display the final number guessed and not all numbers guessed. Any feedback is great thank you! Code is below.

disp('Are you ready for your opponent to guess your number, begin playing.')

pause(1)

Player_1_Guess = input('Enter a number between 1 and 200:');

chance = 5;

clc

while chance <= 5 && chance >= 1;

chance = chance - 1;

for Player_2_Guess = input('Take your guesses on what the correct number is between 1 and 200\n');

if Player_2_Guess < 1;

disp('!NUMBER NOT IN RANGE!')

elseif Player_2_Guess > 200;

disp('!NUMBER NOT IN RANGE!')

end

if Player_1_Guess < Player_2_Guess

disp('!TOO HIGH!')

elseif Player_1_Guess > Player_2_Guess;

disp('!TOO LOW!')

end

fprintf('Chances remaining: %d, ', chance);

if Player_1_Guess == Player_2_Guess;

chance = 6;

end

end

end

if chance == 0;

disp('!NO MORE CHANCES!')

disp('!GAME OVER YOU LOSE!')

elseif chance == 6;

fprintf('!CONGRATS YOU WIN!')

end

ca = categorical({'P1 number','P2 guess1','P2 guess2','P2 guess3', 'P2 guess4', 'P2 guess5'});

bar(ca(1),Player_1_Guess)

hold on

bar(ca(2),Player_2_Guess)

hold on

bar(ca(3),Player_2_Guess)

hold on

bar(ca(4),Player_2_Guess)

hold on

bar(ca(5),Player_2_Guess)

hold on

bar(ca(6),Player_2_Guess)

grid on

xlabel('Player Guesses')

ylabel('Guessed Number Value')

title('Numbers Guessed by P2 & P1 Input')

Constantino Carlos Reyes-Aldasoro
on 4 May 2021

Several errors,

First, clear your figure every time you play otherwise they will overlap (use clf).

Second, you are always displaying the last value that was guessed

bar(ca(1),Player_1_Guess)

hold on

bar(ca(2),Player_2_Guess)

hold on

bar(ca(3),Player_2_Guess)

You are not recording the previous attempts, you can solve this by using another variable, e.g. PlayerRecord like this

while chance <= 5 && chance >= 1;

chance = chance - 1;

for Player_2_Guess = input('Take your guesses on what the correct number is between 1 and 200\n');

PlayerRecord(chance+1) = Player_2_Guess;

Whilst I was running that I noticed an error when chance was 0 you were still entering the for loop, no need for that, that is why I used chance + 1 instead of chance. That would sort the problem, now display like this

bar(ca(1),Player_1_Guess)

hold on

bar(ca(2),PlayerRecord(1))

hold on

bar(ca(3),PlayerRecord(2))

hold on

bar(ca(4),PlayerRecord(3))

hold on

bar(ca(5),PlayerRecord(4))

hold on

bar(ca(6),PlayerRecord(5))

grid on

There are more efficient ways to use bar, but for the time being this works:

There are many extra ";" at the end of the lines that are not necessary, those are highlighted by the Matlab editor, but that will not prevent the code from running.

Hope this solves the problem, if it does, please accept the answer. If not, let me know.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!