Rock, Paper, Scissors GUI?

조회 수: 35 (최근 30일)
Kelsey
Kelsey 2013년 3월 27일
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!

답변 (1개)

Doug Hull
Doug Hull 2013년 3월 27일
Check out the others in the GUI building section of the blog.

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by