This is a script that I have to do : Write a game of rock, paper and scissors. You play against the computer. Scissors beat paper, Paper beats rock, Rock beats scissors. If both players choose the same object, they have to play until there is a winner. Create a game where the first player to win three 'battles' wins.
The thing I dont understand how to do is the last part where it says that they play until there is a person who wins three games. This is what ive tried so far everything works except for the loop
Thank you
player=input('You play:','s');
if strcmpi(player,'rock')
player_score=1;
disp(['You choose: ' num2str(player)]);
elseif strcmpi(player,'paper')
player_score=2;
disp(['You choose: ' num2str(player)]);
elseif strcmpi(player,'scissors')
player_score=3;
disp(['You choose: ' num2str(player)]);
else
player_score=0;
end
computer_score=ceil(rand*3);
if computer_score==1
comupter='rock';
disp(['Computer choose: ' num2str(computer)]);
elseif computer_score==2;
computer='paper';
disp(['Computer choose: ' num2str(computer)]);
else
computer='scissor';
disp(['Computer choose: ' num2str(computer)]);
end
if player_score>computer_score;
disp('Player wins');
elseif computer_score>player_score;
disp('Computer wins');
else
disp('Draw');
player_score = 0;
computer_score = 0;
while sum(computer_score) < 3 || sum(player_score) < 3 % This is the part I dont understand how to make the game runs until one wins 3 games
end
end

댓글 수: 7

Amit
Amit 2014년 1월 23일
what have you tried for this so far?
Image Analyst
Image Analyst 2014년 1월 23일
Do you have a question?
Amit
Amit 2014년 1월 23일
The question was locked, because there was only the problem statement before. Now just added a last minute something to show that you've tried. There is no description of what is scissor, paper etc in your code.
Amit
Amit 2014년 1월 24일
This is very same code you posted earlier. The one modification you did is what Image Analyst mentioned and you copied even that wrong.
Before writing this, I did a simple google search with this kind of problem and along with many other scripts in other languages, I came across 1 even on Matlab website. Its a bit strange that you don't even make this effort before posting a question. Your questions mostly starts with 'write a script' which make me think that this is a homework problem which you want others to do for you.
Please show some effort on your part and then people will help you in finding your mistakes.
Riri
Riri 2014년 1월 24일
편집: Riri 2014년 1월 24일
Thank you for your previous help but before posting a question im always trying a bunch of script on matlab and its only when im stuck that I come here for help. And even if i didnt do it for this question I do normally google my question to see if there is not a similar answer somewhere else or a different infos that I could use and no I dont want no one else to work for me or there would be way much more questions. So for my other questions ill post my work and be more specific. Thank you
Walter Roberson
Walter Roberson 2014년 1월 24일
Where do you define the variable "Rock" ?

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

답변 (2개)

Image Analyst
Image Analyst 2014년 1월 23일

0 개 추천

This could be helpful:
player1Choice = menu('Player 1 selects', 'Rock', 'Paper', 'Scissors')
player2Choice = menu('Player 2 selects', 'Rock', 'Paper', 'Scissors')
You will get the button number: 1, 2, or 3. I guess you can figure out what to do from there.

댓글 수: 1

To quit when one or the other player gets 3 wins, you need to increment the scores at the appropriate times (when they win, ONLY )
computer_score = computer_score + 1; % Computer won another game.
or
player_score = player_score + 1; % Player won another game.
Then at the end of your loop, check if either is 3 and break out of the loop if that's the case:
if player_score >= 3 || computer_score >= 3
break;
end

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

Walter Roberson
Walter Roberson 2014년 1월 24일

0 개 추천

uscore = 0;
cscore = 0;
while uscore < 3 & cscore < 3
....
end

카테고리

도움말 센터File Exchange에서 Just for fun에 대해 자세히 알아보기

태그

질문:

2014년 1월 23일

댓글:

2014년 1월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by