Have to make a game, help?
25 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello, we are a group the had a task of making a game in matlab. It is supposed to have two players, both players start at 0. The game starts with matlab wholenumber between 1000 and 10000 that are saved in a variable S. The players are then supposed to chose a positive number in turn. S is updated and gets a new value every time a player makes a choice. When a player chooses the integer x then g (x) −f (x, S) must be added to this player's score. Every time a score is updated, the program should tell players about the new score. The program should set S equal to x/2^g(x) and inform the players about the new value of S. The game ends when S is equal to 1
Now i've done some parts, but i suck at matlab and having 4 exams to read to so if someone could give me some pointer and maybe an example on how to continue i would be so gratefull!
The main code i have so far is:
player1=0;
player2=0;
S=R();
run R.m
disp(S);
while true
x = input('Enter a number: ', 's');
x = str2double(x);
if ~isnan(x)
break;
else
disp('Only numbers supported');
end
end
player1=player1 + g(x)-f(x,S);
fprintf('\n Thank you \n \n spiller1:');
disp(spiller1)
the R.m:
function [out] = R()
out = randi([1000,10000]);
end
the f(x):
function [z]=f(x,y)
z=abs(x-y);
end
and the g(x)
function [y]=g(x)
i=0;
while true
if (mod(x,2)==0)
i=i+1;
x=(x/2);
else
break
end
end
y=i;
end
1 Commento
Rik
circa 2 ore fa
You can find guidelines for posting homework on this forum here. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks). For general advice about asking questions here: have a read here and here. It will greatly improve your chances of getting an answer.
Now to your question specifically:
You are quite far along already. The main problem I can see is the complete lack of comments. Your variable names and function names are very short, so they can't communicate information. Your program structure is also not explained at all.
What you should start with is a rough sketch of your program in comments, one comment for every 'thing' that needs to happen. Then you write the code that does that thing. Doing it in that order makes sure that any assumptions are clear (because they are in the comments) and your code can be maintained and updated later on. You will forget why you wrote the code the way you did. If not in a day, then in a week or a year or a decade. Any code worth writing is code worth documenting.
For now you can keep all code in a single file. Every Matlab version back to R2016b support having local functions in a script file. Keeping everything together will help you write it better.
Does this help? Once you write the comments and put in the code you already have it will be easier to see what you are still missing. Formulating a good question will usually help you solve it on your own.
Risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!