use array to create rock paper scissors game, do not use loops, use tablelookup in matlab

main.m
% Create lookup table for rock, paper, scissors outcomes
% 0 means a draw, 1 means player1 wins, -1 means player2 wins

lookup_table = [0, -1, 1; 1, 0, -1; -1, 1, 0]; % (rock, paper, scissors)

player1_choice = 'rock'; % Player 1's choice
player2_choice = 'scissors'; % Player 2's choice

% Convert choices to indices (1: rock, 2: paper, 3: scissors)
player1_idx = find(strcmp({'rock', 'paper', 'scissors'}, player1_choice));
player2_idx = find(strcmp({'rock', 'paper', 'scissors'}, player2_choice));

% Use lookup table to determine the winner
result = lookup_table(player1_idx, player2_idx);

% Display the winner
if result == 1
    disp('Player 1 wins');
elseif result == 0
    disp('It\'s a draw');
else
    disp('Player 2 wins');
end
739 chars
24 lines

related categories

gistlibby LogSnag