Treasure Hunt Game MATLAB

조회 수: 22 (최근 30일)
Chase Dierkers
Chase Dierkers 2020년 7월 31일
답변: Rik 2020년 8월 1일
I am creating a MATLAB game for my school project. The goal of the game is to create a 'Treasure Hunt Game' that asks the user to input the number of players, the difficult (easy, medium, or hard), and asks the user(s) to pick spots on a matrix until the correct spot is chosen, therefore winning the game. If a player misses the spot, the command window doesn't show how far away the treasure is, but what direction it is in relation to the previously entered guess. At the start of the game, there is 1000 gold. Every time a player chooses a spot and misses, the game takes away 200 gold from the 'treasure' and the gold that is left is what you win when guessed correctly.
Here's the problem. I cannot figure out how to input the option for 2 players, 3 players, and 4 players. I have already created the code for single player with 3 difficulties, but I am totally clueless as to how to insert that option for the 2,3, and 4 players.
The code should ask for player 1 first, have them guess, then ask for player 2, have them guess, and so forth.
Here is my code so far. I know it can be shortened, but this is the way that best makes sense to me. If you have any suggestions as to how to implement this, that would be great!
Thank you!!!
%% Treasure Hunt Game
START=input('Would you like to play the Treasure Hunt Game? (1 for YES, 2 for NO)');
if START==1;
fprintf('\n')
fprintf('Welcome to Treasure Hunt!\n');
fprintf('\n')
PLAYERS=input('Enter number of players:');
fprintf('\n')
if PLAYERS==1 %%START OF EASY SETTING WITH ONE PLAYER
fprintf('What difficulty would you like to play on? (1 for Easy, 2 for Medium, or 3 for Hard)');
fprintf('\n')
MODE=input('Enter difficulty mode:');
if MODE==1
EZMAP=zeros(5,5);
mat=zeros(5,5);
fprintf('Treasure Map');
GROW=randi(5)
GCOLUMN=randi(5);
mat(GROW,GCOLUMN)=1;
disp(EZMAP)
GUESSROW=input('Guess the row:');
fprintf('\n');
GUESSCOLUMN=input('Guess the column:');
fprintf('\n');
GUESS=mat(GUESSROW,GUESSCOLUMN);
gold=0;
while GUESS~=1
GUESS=mat(GUESSROW,GUESSCOLUMN);
gold=gold+200;
points=0;
if GUESS==1
points=points+goldp;
fprintf('Congratulations! You have found the hidden treasure! You have earned %.f pieces of gold!\n',points);
break
elseif GUESS~=1
goldp=1000-gold;
fprintf('There are now %.f pieces of gold left. Hurry before it runs out!\n',goldp);
if GUESSROW>GROW
UP='North';
if GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',UP,RIGHT);
elseif GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',UP,LEFT);
else
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',UP);
end
elseif GUESSROW<GROW
DOWN='South';
if GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',DOWN,RIGHT);
elseif GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',DOWN,LEFT);
else
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',DOWN);
end
else
if GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',LEFT);
elseif GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',RIGHT);
end
end
GUESSROW=input('Guess the row');
GUESSCOLUMN=input('Guess the column');
end
if goldp==1200;
fprintf('Unfortunately you took too long to find the treasure. You lose:(');
break
end
end
end %%END OF EASY SETTING WITH ONE PLAYER
if MODE==2 %%START OF MEDIUM SETTING WITH ONE PLAYER
MMAP=zeros(10,10);
mat=zeros(10,10);
fprintf('Treasure Map');
GROW=randi(10)
GCOLUMN=randi(10);
mat(GROW,GCOLUMN)=1;
disp(EZMAP)
GUESSROW=input('Guess the row:');
fprintf('\n');
GUESSCOLUMN=input('Guess the column:');
fprintf('\n');
GUESS=mat(GUESSROW,GUESSCOLUMN);
gold=0;
while GUESS~=1
GUESS=mat(GUESSROW,GUESSCOLUMN);
gold=gold+200;
points=0;
if GUESS==1
points=points+goldp;
fprintf('Congratulations! You have found the hidden treasure! You have earned %.f pieces of gold!\n',points);
break
elseif GUESS~=1
goldp=1000-gold;
fprintf('There are now %.f pieces of gold left. Hurry before it runs out!\n',goldp);
if GUESSROW>GROW
UP='North';
if GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',UP,RIGHT);
elseif GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',UP,LEFT);
else
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',UP);
end
elseif GUESSROW<GROW
DOWN='South';
if GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',DOWN,RIGHT);
elseif GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',DOWN,LEFT);
else
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',DOWN);
end
else
if GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',LEFT);
elseif GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',RIGHT);
end
end
GUESSROW=input('Guess the row');
GUESSCOLUMN=input('Guess the column');
end
if goldp==1200;
fprintf('Unfortunately you took too long to find the treasure. You lose:(');
break
end
end
end %%END OF MEDIUM SETTING WITH ONE PLAYER
if MODE==3 %%START OF HARD SETTING WITH ONE PLAYER
HMAP=zeros(15,15);
mat=zeros(15,15);
fprintf('TREASURE MAP');
GROW=randi(15)
GCOLUMN=randi(15);
mat(GROW,GCOLUMN)=1;
disp(EZMAP)
GUESSROW=input('Guess the row:');
fprintf('\n');
GUESSCOLUMN=input('Guess the column:');
fprintf('\n');
GUESS=mat(GUESSROW,GUESSCOLUMN);
gold=0;
while GUESS~=1
GUESS=mat(GUESSROW,GUESSCOLUMN);
gold=gold+200;
points=0;
if GUESS==1
points=points+goldp;
fprintf('Congratulations! You have found the hidden treasure! You have earned %.f pieces of gold!\n',points);
break
elseif GUESS~=1
goldp=1000-gold;
fprintf('There are now %.f pieces of gold left. Hurry before it runs out!\n',goldp);
if GUESSROW>GROW
UP='North';
if GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',UP,RIGHT);
elseif GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',UP,LEFT);
else
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',UP);
end
elseif GUESSROW<GROW
DOWN='South';
if GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',DOWN,RIGHT);
elseif GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s%s from your previously guessed spot.\n',DOWN,LEFT);
else
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',DOWN);
end
else
if GUESSCOLUMN<GCOLUMN
LEFT='West';
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',LEFT);
elseif GUESSCOLUMN>GCOLUMN
RIGHT='East';
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',RIGHT);
end
end
GUESSROW=input('Guess the row');
GUESSCOLUMN=input('Guess the column');
end
if goldp==1200;
fprintf('Unfortunately you took too long to find the treasure. You lose:(');
break
end
end
end %%END OF HARD SETTING WITH ONE PLAYER
  댓글 수: 4
Image Analyst
Image Analyst 2020년 8월 1일
I would not ask them if they want to play. That is just an extra unneeded step. If they didn't want to play, they would not have started the program. And I find the choice of capitals or lower case confusing. I just use camelCase for all variables/parameters. Like numberOfPlayers instead of PLAYERS. Since RIGHT, etc. are constants, you can assign them before the while loops instead of inside the loops. And since gold is an integer, you should use %d in fprintf() instead of %.f.
Chase Dierkers
Chase Dierkers 2020년 8월 1일
Unfortunately, asking the player if they want to play is part of the project.
As for the other suggestions, thank you. I will take those into account and apply it to my code.

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2020년 8월 1일
I would propose a different tactic: use the map itself to keep track of the remaining gold. Your current method is fairly confusing and has the implicit encoding of both the starting gold and the penalty in several places.
I don't know if I made the code too explicit. I tried to leave as much spot open for you to fill in as I could, while showing how you could restructure your code to make it less repeating, more compact, and more configurable.
%% Treasure Hunt Game
clc
StartingGold=2000;
PenaltyPerGuess=200;
SizeByDifficulty=[5 10 15];
PlayGame=input('Would you like to play the Treasure Hunt Game? (1 for YES, 2 for NO)');
if PlayGame~=1
return
end
fprintf('\nWelcome to Treasure Hunt!\n\n');
NumberOfPlayers=input('Enter number of players:');
for player=1:NumberOfPlayers
fprintf('\nWelcome Player %d\n',player)
%You could allow a scale here: 'on a scale from 1 (easy) to N (hard)' (don't forget to round
%the input of the user, they might try 2.5). Then you can edit the SizeByDifficulty variable to
%add or remove difficulty levels.
___
difficulty=input('Enter difficulty mode:');
sz=size_by_difficulty(difficulty);
mat=zeros(sz,sz);
mat(randi(end))=StartingGold+PenaltyPerGuess;% (end is replaced by numel(mat) internally)
while true
mat=mat-PenaltyPerGuess;
GoldRemaining=___
if GoldRemaining>0
fprintf('There are %d pieces of gold remaining.\n',GoldRemaining)
else
fprintf('Unfortunately you took too long to find the treasure. You lose :(\n');
break
end
%get the guess
____
GoldAtLocation=mat(GuessRow,GuessCol);
if GoldAtLocation>0
fprintf('Congratulations! You have found the hidden treasure! You have earned %d pieces of gold!\n',...
GoldAtLocation);
break
end
%find 'North'/'South'/'' (empty for the case where the column is already correct)
____
%find 'East'/'West'/'' (empty for the case where the row is already correct)
____
%you could get fance here and test if both are non-empty, in which case you could add a
%dash (i.e. North-East)
direction=sprintf('%s%s',NorthSouth,EastWest);
%print in one go
fprintf('Incorrect spot. Please try again. The treasure lies %s from your previously guessed spot.\n',...
direction);
end
end

카테고리

Help CenterFile Exchange에서 Strategy & Logic에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by