Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I get the monsters to not move outside of the array? I\'ve provided my co

ID: 3665638 • Letter: H

Question

How do I get the monsters to not move outside of the array? I've provided my code underneath.

%Clear workspace
clear; close all; clc;
%Begin Game
load Adventure;
%Turn off Warning Signs
warning('off','all');
%Show the board, select difficulty, give introduction
imshow([World{1,:};World{2,:};World{3,:};World{4,:};World{5,:};World{6,:};World{7,:};World{8,:};World{9,:};World{10,:}]);
difficulty=menu('How many times do you want to die?????','None','Alot');
fprintf('Congratulations on being stuck in the dungeon. Use the arrow keys to navigate your way out. GOOD LUCK!!!!!!! ');
%Set stats for difficulties
switch difficulty
    case 1
        r=1;
        c=1;
        door=[randi([1,10]),randi([1,10])];
        World{door(1),door(2)}=Door;
        sword=[randi([1,10]),randi([1,10])];
        World{sword(1),sword(2)}=Sword;
        boots=[randi([1,10]),randi([1,10])];
        World{boots(1),boots(2)}=Boots;
        shield=[randi([1,10]),randi([1,10])];
        World{shield(1),shield(2)}=Shield;
        h1=[randi([1,10]),randi([1,10])];
        World{h1(1),h1(2)}=Health;
        h2=[randi([1,10]),randi([1,10])];
        World{h2(1),h2(2)}=Health;
        m1=[randi([1,10]),randi([1,10])];
        World{m1(1),m1(2)}=Monster;
        m1H=randi([5, 30]);
        m1attack=randi([5, 10]);
        m2=[randi([1,10]),randi([1,10])];
        World{m2(1),m2(2)}=Monster;
        m2H=randi([5, 30]);
        m2attack=randi([5, 10]);
        m3=[randi([1,10]),randi([1,10])];
        World{m3(1),m3(2)}=Monster;
        m3H=randi([5, 30]);
        m3attack=randi([5, 10]);
        m4=[randi([1,10]),randi([1,10])];
        World{m4(1),m4(2)}=Monster;
        m4H=randi([5, 30]);
        m4attack=randi([5, 10]);
        m5=[randi([1,10]),randi([1,10])];
        World{m5(1),m5(2)}=Monster;
        m5H=randi([5, 30]);
        m5attack=randi([5, 10]);
        player=[r,c];
        World{r,c}=Player;
        imshow([World{1,:};World{2,:};World{3,:};World{4,:};World{5,:};World{6,:};World{7,:};World{8,:};World{9,:};World{10,:}]);
        pH=50;
        fprintf('Player health: %i ',pH);
        pattack=1;
        pdefense=1;
        pspeed=1
        %set the original player to 1 then when you move the player each
        %time instead of using + or - 1 use + or minus pspeed. then have a
        %condition if pspeed is bigger than 1 open a menu to ask how many
        %spaces to move. options will be either 1 or 2 since we have only 1
        %boots.
        %set win condition
        while sum(player==door)~=2;
            %set lose condition
            while pH>0
            % Player Movement
            if pspeed==1;
                h=figure(1);
                waitforbuttonpress;
                k=get(h,'CurrentKey');
                if strcmp(k,'uparrow')
                    World{r,c}=Blank;
                    World{r-1,c}=Player;
                    player=[r-1,c];
                    r=player(1);
                elseif strcmp(k,'downarrow')
                    World{r,c}=Blank;
                    World{r+1,c}=Player;
                    player=[r+1,c];
                    r=player(1);
                elseif strcmp(k,'rightarrow')
                    World{r,c}=Blank;
                    World{r,c+1}=Player;
                    player=[r,c+1];
                    c=player(2);
                else strcmp(k,'leftarrow')
                    World{r,c}=Blank;
                    World{r,c-1}=Player;
                    player=[r,c-1];
                    c=player(2);
                end
            else pspeed==2;
                move=menu('How many spaces would you like to move','1','2');
                switch move
                    case 1
                        h=figure(1);
                        waitforbuttonpress;
                        k=get(h,'CurrentKey');
                        if strcmp(k,'uparrow')
                            World{r,c}=Blank;
                            World{r-1,c}=Player;
                            player=[r-1,c];
                            r=player(1);
                        elseif strcmp(k,'downarrow')
                            World{r,c}=Blank;
                            World{r+1,c}=Player;
                            player=[r+1,c];
                            r=player(1);
                        elseif strcmp(k,'rightarrow')
                            World{r,c}=Blank;
                            World{r,c+1}=Player;
                            player=[r,c+1];
                            c=player(2);
                        else strcmp(k,'leftarrow')
                            World{r,c}=Blank;
                            World{r,c-1}=Player;
                            player=[r,c-1];
                            c=player(2);
                        end
                    case 2
                        h=figure(1);
                        waitforbuttonpress;
                        k=get(h,'CurrentKey');
                        if strcmp(k,'uparrow')
                            World{r,c}=Blank;
                            World{r-2,c}=Player;
                            player=[r-2,c];
                            r=player(1);
                        elseif strcmp(k,'downarrow')
                            World{r,c}=Blank;
                            World{r+2,c}=Player;
                            player=[r+2,c];
                            r=player(1);
                        elseif strcmp(k,'rightarrow')
                            World{r,c}=Blank;
                            World{r,c+2}=Player;
                            player=[r,c+2];
                            c=player(2);
                        else strcmp(k,'leftarrow')
                            World{r,c}=Blank;
                            World{r,c-2}=Player;
                            player=[r,c-2];
                            c=player(2);
                        end
                end
            end
            %Monster movement
            for z=randi([1 2])
                World{m1(1),m1(2)}=Blank;
                if z==1
                    m1=m1+[randi([-1,1]),0];
                    if m1(1)<1
                        m1(1)=1;
                    else m1(1)>10
                        m1(1)=10;
                    end
                else z==2
                    m1=m1+[0,randi([-1,1])];
                    if m1(2)<1
                        m1(2)=1;
                    else m1(2)>10
                        m1(2)=10;
                    end
                end
                World{m1(1),m1(2)}=Monster;
            end
            for l=randi([1 2])
                World{m2(1),m2(2)}=Blank;
                if l==1
                    m2=m2+[randi([-1,1]),0];
                    if m2(1)<1
                        m2(1)=1;
                    end
                    if m2(1)>10
                        m2(1)=10;
                    end
                else l==2
                    m2=m2+[0,randi([-1,1])];
                    if m2(2)<1
                        m2(2)=1;
                    else m2(2)>10
                        m2(2)=10;
                    end
                end
                World{m2(1),m2(2)}=Monster;
            end
            for m=randi([1 2])
                World{m3(1),m3(2)}=Blank;
                if m==1
                    m3=m3+[randi([-1,1]),0];
                    if m3(1)<1
                        m3(1)=1;
                    else m3(1)>10
                        m3(1)=10;
                    end
                else m==2
                    m3=m3+[0,randi([-1,1])];
                    if m3(2)<1
                        m3(2)=1;
                    else m3(2)>10
                        m3(2)=10;
                    end
                    World{m3(1),m3(2)}=Monster;
                end
                for n=randi([1 2])
                    World{m4(1),m4(2)}=Blank;
                    if n==1
                        m4=m4+[randi([-1,1]),0];
                        if m4(1)<1
                            m4(1)=1;
                        else m4(1)>10
                            m4(1)=10;
                        end
                    else n==2
                        m4=m4+[0,randi([-1,1])];
                        if m4(2)<1
                            m4(2)=1;
                        else m4(2)>10
                            m4(2)=10;
                        end
                        World{m4(1),m4(2)}=Monster;
                    end
                    for p=randi([1 2])
                        World{m5(1),m5(2)}=Blank;
                        if p==1
                            m5=m5+[randi([-1,1]),0];
                            if m5(1)<1
                                m5(1)=1;
                            else m5(1)>10
                                m5(1)=10;
                            end
                        else p==2
                            m5=m5+[0,randi([-1,1])];
                            if m5(2)<1
                                m5(2)=1;
                            else m5(2)>10
                                m5(2)=10;
                            end
                            World{m5(1),m5(2)}=Monster;
                        end
                        imshow([World{1,:};World{2,:};World{3,:};World{4,:};World{5,:};World{6,:};World{7,:};World{8,:};World{9,:};World{10,:}]);
                    end
                end
            end
            % Battles
            if player==m1
                fight=menu('You ran into a monster. Do you want to fight or run?','Fight','Run');
                switch fight
                    case 1
                        while pH>0&&m1H>0
                            fprintf('Player health: %i Monster health: %i ',pH,m1H);
                            m1H=m1H-(pattack*10);
                            pH=pH-(m1attack/pdefense);
                            fprintf('player health: %i ',pH);
                            fprintf('monster health: %i ',m1H);
                        end
                    case 2
                        pH=pH-5;
                end
            end
            if player==m2
                fight=menu('You ran into a monster. Do you want to fight or run?','Fight','Run');
                switch fight
                    case 1
                        while pH>0&&m2H>0
                            fprintf('Player health: %i Monster health: %i ',pH,m2H);
                            m2H=m2H-(pattack*10);
                            pH=pH-(m2attack/pdefense);
                            fprintf('player health: %i ',pH);
                            fprintf('monster health: %i ',m2H);
                        end
                    case 2
                        pH=pH-5;
                end
            end
            if player==m3
                fight=menu('You ran into a monster. Do you want to fight or run?','Fight','Run');
                switch fight
                    case 1
                        while pH>0&&m3H>0
                            fprintf('Player health: %i Monster health: %i ',pH,m3H);
                            m3H=m3H-(pattack*10);
                            pH=pH-(m3attack/pdefense);
                            fprintf('player health: %i ',pH);
                            fprintf('monster health: %i ',m3H);
                        end
                    case 2
                        pH=pH-5;
                end
            end
            if player==m4
                fight=menu('You ran into a monster. Do you want to fight or run?','Fight','Run');
                switch fight
                    case 1
                        while pH>0&&m4H>0
                            fprintf('Player health: %i Monster health: %i ',pH,m4H);
                            m4H=m4H-(pattack*10);
                            pH=pH-(m4attack/pdefense);
                            fprintf('player health: %i ',pH);
                            fprintf('monster health: %i ',m4H);
                        end
                    case 2
                        pH=pH-5;
                end
            end
            if player==m5
                fight=menu('You ran into a monster. Do you want to fight or run?','Fight','Run');
                switch fight
                    case 1
                        while pH>0&&m5H>0
                            fprintf('Player health: %i Monster health: %i ',pH,m5H);
                            m5H=m5H-(pattack*10);
                            pH=pH-(m5attack/pdefense);
                            fprintf('player health: %i ',pH);
                            fprintf('monster health: %i ',m5H);
                        end
                    case 2
                        pH=pH-5;
                end
            end
            %Pickups
            if player==h1
               pH=pH+10;
              fprintf('Player Health: %i ',pH);
            end
            if player==h2
               pH=pH+10;
              fprintf('Player Health: %i ',pH);
            end
            if player==shield
                pdefense=pdefense+1;
                fprintf('Player Defense: %i ',pdefense);
            end
            if player==sword
                pattack=pattack+1;
                fprintf('Player Attack: %i ',pattack);
            end
            if player==boots
                pspeed=pspeed+1;
                fprintf('You can now move an extra space');
            end
            end
        end
end
fprintf('Player health: ',pH)

Explanation / Answer

using randi([-1,1]) gives negative values .. less than zero which intends to make monsters out of the array ..

change them to randi([0,1])

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote