create rock paper scissors game against computer with scoreboard used tablelookup in matlab
main.m
% Define the choiceschoices = {'rock', 'paper', 'scissors'};
numChoices = numel(choices);
% Define the lookup table for the resultsresults = [
0-11;
10-1;
-110];
% Initialize scoreplayerScore = 0;
computerScore = 0;
% Play the gamewhiletrue% Computer's choice computerChoiceIndex = randi(numChoices);
computerChoice = choices{computerChoiceIndex};
% Player's choice playerChoice = input('Enter your choice (rock, paper, or scissors): ', 's');
if ~ismember(playerChoice, choices)
disp('Invalid choice. Please choose rock, paper, or scissors.');
continue;
end% Determine the winner using the lookup table result = results(strcmp(choices, playerChoice), computerChoiceIndex);
% Update scores and display resultif result == 1 playerScore = playerScore + 1;
disp('You win!');
elseif result == -1 computerScore = computerScore + 1;
disp('Computer wins!');
elsedisp('It\'s a tie!');
enddisp(['Score: Player - ', num2str(playerScore), ', Computer - ', num2str(computerScore)]);
% Ask to play again playAgain = input('Do you want to play again? (yes or no): ', 's');
if ~strcmp(playAgain, 'yes')
disp('Thanks for playing!');
break;
endend
1316 chars
52 lines
This code sets up a rock-paper-scissors game against the computer in MATLAB using a table lookup for results. It keeps track of the score using a scoreboard and allows the player to play multiple rounds.