Rock, Paper, Scissors GUI?

35 visualizaciones (últimos 30 días)
Kelsey
Kelsey el 27 de Mzo. de 2013
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!

Respuestas (1)

Doug Hull
Doug Hull el 27 de Mzo. de 2013
Check out the others in the GUI building section of the blog.

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by