create rock paper scissors game against computer with scoreboard used tablelookup in matlab

main.m
% Define the choices
choices = {'rock', 'paper', 'scissors'};
numChoices = numel(choices);

% Define the lookup table for the results
results = [
    0 -1 1;
    1 0 -1;
    -1 1 0
];

% Initialize score
playerScore = 0;
computerScore = 0;

% Play the game
while true
    % 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 result
    if result == 1
        playerScore = playerScore + 1;
        disp('You win!');
    elseif result == -1
        computerScore = computerScore + 1;
        disp('Computer wins!');
    else
        disp('It\'s a tie!');
    end

    disp(['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;
    end
end
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.

gistlibby LogSnag