Why won't MATLAB display the new matrix?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Clayton
el 15 de Oct. de 2014
Respondida: Geoff Hayes
el 15 de Oct. de 2014
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
0 comentarios
Respuesta aceptada
Geoff Hayes
el 15 de Oct. de 2014
Clayton - whenever the user makes a move (so doesn't quit), the code enters an if statement, does some stuff, and then executes the continue statement which skips the remainder of code in the while loop and "jumps" back to the beginning of the loop (asking the user for his/her next move). The lines of code that are skipped are
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
Since you have four if blocks each ending with a continue, I suggest that you just convert this into an if/elseif/elseif/elseif block and remove the continue statements. Something like
if find(world == 'v');
% do stuff
elseif find(world == '^');
% do stuff
elseif find(world == '<');
% do stuff
elseif find(world == '>');
% do stuff
end
disp(world);
% etc.
Also, you will need some code to check to see if the user has reached G. (This is a fun game!)
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Strategy & Logic en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!