Rock, Paper, Scissors GUI?
32 views (last 30 days)
Show older comments
I am creating a GUI based on a script that generates a rock, paper, or scissors input, compares it to the user's input, then states whether the game is a win/loss/tie. Here is my code:
iHumanPlay=input('Choose 1 for rock, 2 for paper, or 3 for scissors.');
iComputerPlay=randi(1,3);
if iHumanPlay == 1
if iComputerPlay == 1
gameResult = 'Tie!'
elseif iComputerPlay == 2
gameResult = 'You lose!'
else gameResult = 'You win!'
end
elseif iHumanPlay == 2
if iComputerPlay == 1
gameResult = 'You win!'
elseif iComputerPlay == 2
gameResult = 'Tie!'
else gameResult = 'You lose!'
end
elseif iHumanPlay == 3
if iComputerPlay == 1
gameResult = 'You lose!'
elseif iComputerPlay == 2
gameResult = 'You win!'
else gameResult = 'Tie!'
end
end
showGame(iComputerPlay, iHumanPlay, gameResult)
for the function:
function showGame(iComputerPlay, iHumanPlay, gameResult)
if iComputerPlay == 1
iComputerPlay = 'Rock'
elseif iComputerPlay == 2
iComputerPlay = 'Paper'
else iComputerPlay == 'Scissors'
end
if iHumanPlay == 1
iHumanPlay = 'Rock'
elseif iHumanPlay == 2
iHumanPlay = 'Paper'
else iHumanPlay = 'Scissors'
end
disp('You chose',num2str(iHumanPlay),'.')
disp('Computer chose',num2str(iComputerPlay),'.')
if gameResult == 'Tie!'
disp('It is a tie!')
elseif gameResult == 'You lose!'
disp('You lose!')
else disp('You win!')
end
showGame(iComputerPlay, iHumanPlay, gameResult);
I've added the buttons and text onto the GUI, but I am unsure how to wire the script to it so that it actually displays what it's supposed to, as it does in the picture in the problem. If anyone could provide their input, I would be really happy! Thank you!
0 Comments
Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!