Why won't MATLAB display the new matrix?
Show older comments
I am trying to make a game in which user commands are input and a character moves about the matrix. I have the program able to manipulate the character throughout the matrix but the new matrix, the one in which the character has moved, won't display with the move counter. I have hit a dead end and I cannot figure out what to do, can you help?
world = ['#' '.' '.' '.' '.' '.' '#' '.' 'v' '#'
'.' '#' '.' '#' '#' '.' '.' '.' '.' '.'
'.' '.' '#' '#' '.' '.' '#' '#' '.' '#'
'#' '.' '.' '.' '.' '#' '.' '.' '.' '.'
'.' '#' '.' '#' '#' '.' '.' '#' '.' '.'
'.' '#' '.' '.' '.' '#' '#' '#' '.' '#'
'#' '.' '#' '.' '.' '#' '#' '#' '.' '.'
'.' '#' '.' '#' '.' '.' '.' '#' '.' '.'
'.' '.' '.' '#' '#' '.' '#' '.' '#' '.'
'G' '#' '.' '.' '.' '.' '.' '.' '#' '#']
counter = 0
fprintf(['Welcome to the RoboEscape Game.\n\n'... 'The objective of this game is to navigate the maze with your robot \n'... '''v'' to the exit ''G'' on the empty spaces ''.'' without touching \n'... 'wall ''#'' or falling off the map. To navigate use ''w'' to travel \n'... 'forward, ''a'' to rotate to the left, ''d'' to rotate to the right \n'... ', and if you feel like it is all just too much you can use ''q'' to\n'... ' escape.\n\n']);
while fprintf('Enter:')
move = input('Your move?(a,w,d,q)','s');
if move == ('q');
disp('Have a nice day you quitter!')
break
else
e = 2;
end
if find(world == 'v');
idx = find(world == 'v');
if (move == 'a')
world(idx) = '>';
e = 0;
end
if (move == 'w');
[r,c] = find(world == 'v');
if r < 10
world(r,c) = '.';
world(r+1,c) = 'v';
e = 0;
elseif r == 10;
e = 1;
end
end
if (move == 'd')
world(idx) = '<';
e = 0;
end
continue
end
if find(world == '^');
idx1 = find(world == '^');
if (move == 'a');
world(idx1) = '<';
e = 0;
end
if (move == 'w');
[r,c] = find(world == '^');
if r > 1;
world(r,c) = '.';
world(r-1,c) = '^';
e = 0;
elseif r == 1;
e = 1;
end
end
if (move == 'd')
world(idx1) = '>';
e = 0;
end
continue
end
if find(world == '<');
idx2 = find(world == '<');
if (move == 'a');
world(idx2) = 'v';
e = 0;
end
if (move == 'w');
[r,c] = find(world == '<');
if c > 1;
world(r,c) = '.';
world(r,c-1) = '<';
e = 0;
elseif c == 1;
e = 1;
end
end
if (move == 'd');
world(idx2) = '^';
e = 0;
end
continue
end
if find(world == '>');
idx3 = find(world == '>');
if (move == 'a');
world(idx3) = '^';
e = 0;
end
if (move == 'w');
[r,c] = find(world == '>');
if c < 10
world(r,c) = '.';
world(r,c+1) = '>';
e = 0;
elseif c == 10;
e = 1;
end
end
if (move == 'd')
world(idx3) = 'v';
e = 0;
end
continue
end
disp(world)
counter = counter + 1
if (e > 0)&&(e < 2);
disp('You fell off! GAME OVER');
fprintf('Your total moves: %f\n',counter);
break
elseif e > 1;
disp('Incorrect key, try again')
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Strategy & Logic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!