- 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]?
Rock, Paper, Scissors Game
99 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Jon
el 23 de Feb. de 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]
% 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)
% User picks paper, computer picks scissors,computer wins
W(2,3)
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.
Respuestas (1)
Steven Lord
el 29 de Oct. de 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
Instead of populating results with words you could make it indicate which player won (or 0 for a tie.)
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!