Rock, Paper, Scissors Game

126 views (last 30 days)
Caleb
Caleb on 23 Feb 2024
Commented: Caleb on 29 Feb 2024
Ive been working on a rock paper scissors game for a matlab class and have gotten up to this point with everything seeming to run fine. However, it doesnt seem to actually run the game. Any tips on what to change or add would be greatly appreciated.
function [weapon, user_weapon, comp_counter, user_counter, L] = RockPaperScissors_Patton_Feb21st()
prompt = 'choose a weapon: 1 = rock, 2 = paper,' + ...
" 3 = scissors \n";
user_input = input(prompt);
for j = user_input
if user_input == 1
user_weapon = "rock";
elseif user_input == 2
user_weapon = "paper";
elseif user_input == 3
user_weapon = "scissors";
end
end
fprintf("%s", user_weapon);
for i = 1:3
ran_num = randi(i);
if ran_num == 1
weapon = "rock";
elseif ran_num == 2
weapon = "paper";
elseif ran_num == 3
weapon = "scissors";
end
end
L = 0;
k = 0;
comp_counter = L;
user_counter = k;
while k <= 4 && L <= 4
if user_weapon == "rock" && weapon == "rock"
elseif user_weapon == "paper" && weapon == "paper"
elseif user_weapon == "scissors" && weapon == "scissors"
elseif user_weapon == "rock" && weapon == "paper"
L = L+1;
elseif user_weapon == "paper" && weapon == "rock"
k = k+1;
elseif user_weapon == "rock" && weapon == "scissors"
k = k+1;
elseif user_weapon == "scissors" && weapon == "rock"
L = L+1;
elseif user_weapon == "scissors" && weapon == "paper"
k = k+1;
elseif user_weapon == "paper" && weapon == "scissors"
L = L+1;
break
end
%comp_counter = L;
%user_counter = k;
end
  6 Comments
Jon
Jon on 23 Feb 2024
One suggestion I have is that rather than using all of those if statements to decide who won, you could do that by computing a winner matrix W, where the W(i,j) tells you who won, user, computer or tie. The rows (i's) are the indices of the users choice, and the columns are the indices of the computer's choice.
So if we use indices 1,2,3 for rock, paper, scissor, and values 0,1,2 for tie, user and computer resp, then our winner matrix would be
% Winner matrix,
% rows correspond to user choice 1 = rock,2 = paper,3 = scissor
% columns correspond to computer's choice 1 = rock, 2 = paper, 3 = scissor
% W(i,j) = 0 Tie, W(i,j) = 1, User wins, W(i,j) = 2, Computer wins
W = [
0 2 1
1 0 2
2 1 0]
W = 3×3
0 2 1 1 0 2 2 1 0
% So for example if the user picks rock, and computer picks scissors, find
% out who won at row 1, column 3, W(1,3) = 1, so user wins
W(1,3)
ans = 1
% User picks paper, computer picks scissors,computer wins
W(2,3)
ans = 2
You can then use this to decide the winner of each match and proceed with the rest of your logic for counting up wins etc.
Caleb
Caleb on 29 Feb 2024
Yea I went back and reworked the code and realized how much of a cluster it was. Thanks for yall suggestions and points on where I could improve.

Sign in to comment.

Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!