Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to create a while loop for a dice game in MATLAB and have matlab ask each player if they want to roll a

I want to create a while loop for a dice game in MATLAB and have matlab ask each player if they want to roll a dice each time until either player reaches a score of 21 or higher. This is my code so far, how would I modify it in order to ask each player every time until they reach a score of 21 or higher.

player1.name = "Player 1"; player2.name = "Player 2"; player1.score = 0; player2.score = 0; player1.rollagain = ''; player2.rollagain = '';

% first dice roll for player1 player1.score = randi(6)

% first dice roll for player2 player2.score = randi(6)

% does player 1 want another roll? disp("Player 1, Would You Like To Roll Again? Enter - Yes or No") player1.rollagain = lower(input('Decision:','s'))

if player1.rollagain == 'yes' player1.addscore = randi(6) player1.score = player1.score + player1.addscore elseif player1.rollagain == 'no' player1.score = player1.score else disp('Invalid Input')

end

% does player 2 want another roll? disp("Player 2, Would You Like To Roll Again? Enter - Yes or No") player2.rollagain = lower(input('Decision:','s'))

if player2.rollagain == 'yes' player2.addscore = randi(6) player2.score = player2.score + player2.addscore elseif player2.rollagain == 'no' player2.score = player2.score else disp('Invalid Input')

end

while player1.score<21 player1.score=player1.score+player1.addscore end

while player2.score<21 player2.score=player2.score+player2.addscore end

% determining who wins the game for player 1 if player1.score > 21 disp('Player 1 has LOST the game') elseif player1.score == 21 disp('Player 1 has WON the game') end % determining who wins the game for player 2 if player2.score > 21 disp('Player 2 has LOST the game') elseif player2.score == 21 disp('Player 2 has WON the game') end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

List some popular analytical tools.

Answered: 1 week ago

Question

What is the relationship between diversity, inclusion, and equity?

Answered: 1 week ago