How can I fix this error in my code creating a maze game?
14 views (last 30 days)
Show older comments
I am trying to create a maze type game in Matlab for a class project. I am following my professor's algorithm key, but I keep getting an error when I run the function. The maze_size is supposed to be a random string array, and I am also unsure if this is correct for that. Below is the error I am receiving.
Not enough input arguments.
Error in create_maze (line 3)
maze = [maze_size, maze_size];
My code is below. There will be a second function that will have the player randomly move through the "game board" and encounter monsters they will have to "fight" by guessing a correct number before moving to try and find the exit.
function maze = create_maze(maze_size, monsters)
maze = [maze_size, maze_size];
maze = (1:maze_size), (1:maze_size) == '0'
maze(randX, randY) = 'P'
tempX = maze_size(randi(numel(maze_size)))
tempY = maze_size(randi(numel(maze_size)))
if maze(tempX, tempY) == 'P'
maze(tempX, tempY) = 'E';
elseif tempX < maze_size
maze(tempX + 1, tempY) = 'E'
elseif tempY < maze_size
maze(tempX, tempY + 1) = 'E';
elseif tempX > 1
maze(tempX - 1, tempY) = 'E';
else
maze(tempX, tempY - 1) = 'E';
end
if monsters > 0
tempX = rand(1:maze_size);
tempY = rand(1:maze_size);
if maze(tempX, tempY) == 'P' && maze(tempX, tempY) == 'E'
maze(tempX, tempY) = 'M';
end
tempX = rand(1:maze_size);
tempY = rand(1:maze_size);
if maze(tempX, tempY) == 'P' && maze(tempX, tempY) == 'E'
maze(tempX, tempY) = 'M'
end
end
end
Answers (1)
TADA
on 1 Nov 2018
generally this exception is thrown when you are trying to use an input argument which was not set when calling this function.
2 Comments
Walter Roberson
on 2 Nov 2018
You are trying to run the code by clicking on the green Run button. You need to go down to the command line and invoke the function passing in appropriate values.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!