Rock, Paper, Scissors Game

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

Aquatris
Aquatris 2024년 2월 23일
편집: Aquatris 2024년 2월 23일
Your code has a lot of issues and I do not think it is a simple fix. You need to work on the logic of the code a bit more.
Also this is not a matlab class but a matlab function.
Somethings I notice on a first glance:
  • No idea what you are trying to do with the for loop since you overwrite the 'weapon' variable in each iteration, and do not use it each iteration
  • What are you trying to achieve within the while loop? Neither 'user_weapon' nor 'weapon' changes during the while loop so assuming the purpose is to run the game until one wins 5 times, this is not right implementation. You should be asking for a new rock-paper-scissor from the user in each game.
  • The use of break is wrong in your while loop. Why are your breaking the loop if the pairs is [paper scissor]?
Also I suggest using the code formatting button when posting code. It makes life easier for others :)
Jon
Jon 2024년 2월 23일
@Aquatris, minor point, but I think when the OP said they were working on a matlab class, they weren't distinguishing between MATLAB functions and MATLAB classes. The OP was saying the progra was was for homework for a course in MATLAB.
Aquatris
Aquatris 2024년 2월 23일
편집: Aquatris 2024년 2월 23일
@Jon I have been away from school/uni for too long that programming classes are the only class for me my bad :D
Jon
Jon 2024년 2월 23일
Ahh, so you really did mean that you were working a MATLAB class (object).
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 2024년 2월 29일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2024년 10월 29일

1 개 추천

You could, instead of making a matrix of results, make a table.
possibilities = ["rock", "scissors", "paper"];
results = array2table(repmat("tie", 3, 3), RowNames = possibilities, VariableNames = possibilities);
results{"rock", "scissors"} = "rock";
results{"rock", "paper"} = "paper";
results{"scissors", "rock"} = "rock";
results{"scissors", "paper"} = "scissors";
results{"paper", "rock"} = "paper";
results{"paper", "scissors"} = "scissors";
Now you can ask results using the names.
for player1 = possibilities
for player2 = possibilities
fprintf("If player 1 selects %s and player 2 selects %s, the result is %s.\n", ...
player1, player2, results{player1, player2})
end
end
If player 1 selects rock and player 2 selects rock, the result is tie. If player 1 selects rock and player 2 selects scissors, the result is rock. If player 1 selects rock and player 2 selects paper, the result is paper. If player 1 selects scissors and player 2 selects rock, the result is rock. If player 1 selects scissors and player 2 selects scissors, the result is tie. If player 1 selects scissors and player 2 selects paper, the result is scissors. If player 1 selects paper and player 2 selects rock, the result is paper. If player 1 selects paper and player 2 selects scissors, the result is scissors. If player 1 selects paper and player 2 selects paper, the result is tie.
Instead of populating results with words you could make it indicate which player won (or 0 for a tie.)

카테고리

도움말 센터File Exchange에서 Just for fun에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 2월 23일

답변:

2024년 10월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by