Rock, Paper, Scissors Game

56 vues (au cours des 30 derniers jours)
Caleb
Caleb le 23 Fév 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 commentaires
Jon
Jon le 23 Fév 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 le 29 Fév 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.

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 29 Oct 2024
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.)

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by