The following built-in MATLAB functions and commands are permitted for this assignment.
Vector/Matrix
Flow Control
Strings and Character Arrays
Other
Points will be deducted from any programs using functions outside this list.
AI for any part of this problem.function_name.m, where each function_name is listed below.list_occurrences. I think you will find this function useful and I recommend adding it to your current directory so that you can call it inside this assignmentβs programs. If any of your programs use it, include it with your submission to this assignment.
roll_dice
Inputs:
|
n (1x1) positive integer β number of dice to roll.
|
Outputs:
|
roll (1 x n) integer vector β contains random integers from 1 - 6.
|
Details:
|
βΈ Download the sound file, throw_dice.mat, and place it in your current working directory.
|
(1) Load the sound file with the command |
|
(2) Play it with the command β1βBy βsilentlyβ, I mean donβt print a warning in the catch block. |
|
βΈ Use the randi command to produce the roll.
|
r = roll_dice(5) % Outputs are random and will vary
% returns: r = 2 6 1 1 5
r = roll_dice(3) % Outputs are random and will vary
% returns: r = 6 6 3
get_face_counts
Inputs:
|
roll (1 x 5) integer vector β roll die face values.
|
Outputs:
|
|
Details:
|
βΈ None.
|
counts = get_face_counts([2 6 6 1 5])
% returns counts = 1 1 0 0 1 2
counts = get_face_counts([3 3 3 5 5])
% returns counts = 0 0 3 0 2 0
sort_by_rank
Inputs:
|
raw_roll (1 x 5) integer vector β dice values sorted into rank order.
|
Outputs:
|
sorted_roll (1 x 5) integer vector β the dice values reordered so that higher multiplicities and values appear earlier.
|
Details:
|
βΈ Both roll and counts are provided as inputs. You may use one or both to code this. If one is not used, replace the input variable with ~.
|
βΈ Remember, you can make for loop counters decrease rather than increase using a step of -1. For example, k = 3:-1:1 = [3 2 1].
|
raw_roll = [2 5 5 2 5]; % full-house
rc = [0 2 0 0 3 0];
sorted_roll = sort_by_rank(raw_roll, rc)
% returns sorted_roll = 5 5 5 2 2
raw_roll = [3 4 6 1 6]; % one-pair
rc = [1 0 1 1 0 2];
sorted_roll = sort_by_rank(raw_roll, rc)
% returns sorted_roll = 6 6 4 3 1
raw_roll = [4 5 1 3 2]; % straight
rc = [1 1 1 1 1 0];
sorted_roll = sort_by_rank(raw_roll, rc)
% returns sorted_roll = 5 4 3 2 1
is_5kind
Inputs:
|
roll (1 x 5) integer vector β dice values sorted into rank order.
|
Outputs:
|
|
Details:
|
βΈ You may assume that the input roll will be sorted by rank.
|
βΈ Both roll and counts are provided as inputs. You may use one or both to code this. If one is not used, replace the input variable with ~.
|
|
βΈ Your list_occurrences program may be useful.
|
tf = is_5kind([4 4 2 2 5], [0 2 0 2 1 0])
% returns tf = (logical) 0
tf = is_5kind([3 3 3 3 3], [0 0 5 0 0 0])
% returns tf = (logical) 1
is_4kind
Inputs:
|
roll (1 x 5) integer vector β dice values sorted into rank order.
|
Outputs:
|
|
Details:
|
βΈ You may assume that the input roll will be sorted by rank.
|
βΈ Both roll and counts are provided as inputs. You may use one or both to code this. If one is not used, replace the input variable with ~.
|
|
βΈ Your list_occurrences program may be useful.
|
tf = is_4kind([4 4 2 2 5], [0 2 0 2 1 0])
% returns tf = (logical) 0
tf = is_4kind([3 3 3 3 1], [1 0 4 0 0 0])
% returns tf = (logical) 1
is_3kind
Inputs:
|
roll (1 x 5) integer vector β dice values sorted into rank order.
|
Outputs:
|
|
Details:
|
βΈ You may assume that the input roll will be sorted by rank.
|
βΈ Both roll and counts are provided as inputs. You may use one or both to code this. If one is not used, replace the input variable with ~.
|
|
βΈ Your list_occurrences program may be useful.
|
tf = is_3kind([4 4 2 2 5], [0 2 0 2 1 0])
% returns tf = (logical) 0
tf = is_3kind([3 3 3 5 4], [0 0 3 1 1 0])
% returns tf = (logical) 1
is_1pair
Inputs:
|
roll (1 x 5) integer vector β dice values sorted into rank order.
|
Outputs:
|
|
Details:
|
βΈ You may assume that the input roll will be sorted by rank.
|
βΈ Both roll and counts are provided as inputs. You may use one or both to code this. If one is not used, replace the input variable with ~.
|
|
βΈ Your list_occurrences program may be useful.
|
tf = is_1pair([4 4 6 5 2], [0 1 0 2 1 1])
% returns tf = (logical) 1
tf = is_1pair([3 3 1 1 4], [2 0 2 1 0 0])
% returns tf = (logical) 0
is_2pair
Inputs:
|
roll (1 x 5) integer vector β dice values sorted into rank order.
|
Outputs:
|
|
Details:
|
βΈ You may assume that the input roll will be sorted by rank.
|
βΈ Both roll and counts are provided as inputs. You may use one or both to code this. If one is not used, replace the input variable with ~.
|
|
tf = is_2pair([4 4 2 2 5], [0 2 0 2 1 0])
% returns tf = (logical) 1
tf = is_2pair([3 3 3 5 5], [0 0 3 0 2 0])
% returns tf = (logical) 0
tf = is_2pair([6 6 6 6 2], [0 1 0 0 0 4])
% returns tf = (logical) 0
is_straight
Inputs:
|
roll (1 x 5) integer vector β dice values sorted into rank order.
|
Outputs:
|
|
Details:
|
βΈ You may assume that the input roll will be sorted by rank.
|
βΈ Both roll and counts are provided as inputs. You may use one or both to code this. If one is not used, replace the input variable with ~.
|
|
tf = is_straight([5 4 3 2 1], [1 1 1 1 1 0])
% returns tf = (logical) 1
tf = is_straight([6 5 4 3 2], [0 1 1 1 1 1])
% returns tf = (logical) 1
tf = is_straight([1 3 4 5 6], [1 0 1 1 1 1])
% returns tf = (logical) 0
is_fullhouse
Inputs:
|
roll (1 x 5) integer vector β dice values sorted into rank order.
|
Outputs:
|
|
Details:
|
βΈ You may assume that the input roll will be sorted by rank.
|
βΈ Both roll and counts are provided as inputs. You may use one or both to code this. If one is not used, replace the input variable with ~.
|
|
tf = is_fullhouse([5 5 5 1 1], [2 0 0 0 3 0])
% returns tf = (logical) 1
tf = is_fullhouse([1 1 1 1 6], [4 0 0 0 0 1])
% returns tf = (logical) 0
is_between
Inputs:
|
list (1 x N) double vector β values to check.
a (1x1) double β lower bound.
b (1x1) double β upper bound.
|
Outputs:
|
tf (1x1) logical β true if every value of list is between \(a\) & \(b\) OR is empty; false otherwise.
|
Details:
|
βΈ You may use logical vectors to code this.
|
tf = is_between([2 3 4], 1, 5)
% returns tf = (logical) 1
tf = is_between([0 2 4], 1, 5)
% returns tf = (logical) 0
tf = is_between([], 1, 5)
% returns tf = (logical) 1
get_winner
Inputs:
|
d_rank (1x1) integer β dealerβs rank ID (1-8).
|
d_roll (1 x 5) integer vector β dealerβs roll sorted by rank order.
|
|
p_rank (1x1) integer β playerβs rank ID (1-8).
|
|
p_roll (1 x 5) integer vector β playerβs roll sorted by rank order.
|
|
Outputs:
|
|
Details:
|
βΈ A lower rank ID wins.
|
βΈ If all five dice are equal, return \(0\) (tie).
|
% Example 1: rank decides
p_roll = [3 3 3 4 1]; % three-of-a-kind
p_rank = 5;
d_roll = [2 2 1 1 5]; % two-pair
d_rank = 6;
w = get_winner(d_rank, d_roll, p_rank, p_roll)
% returns w = 1 (player wins)
% Example 2: tie-break by dice values
p_roll = [5 5 4 2 1]; % one-pair, highest single: 4
p_rank = 7;
d_roll = [5 5 6 2 1]; % one-pair, highest single: 6
d_rank = 7;
w = get_winner(d_rank, d_roll, p_rank, p_roll)
% returns w = 2 (dealer wins)
% Example 3: exact tie
p_roll = [4 4 4 6 6]; % full-house, 4's over 6's
p_rank = 3;
d_roll = [4 4 4 6 6]; % full-house, 4's over 6's
d_rank = 3;
w = get_winner(d_rank, d_roll, p_rank, p_roll)
% returns w = 0 (tie)
get_player_reroll
Inputs:
|
None (reads from user input).
|
Outputs:
|
sel (1 x M) integer vector β indices (1-5) of dice to reroll. If the player keeps all dice or the input is invalid, returns [].
|
Details:
|
βΈ Get the userβs selection using the command:
|
| |
βΈ As long as the player entered numbers separated by spaces or commas, you can convert the input to a numerical vector using str2num.
|
|
βΈ If non-number text was entered then str2num will return empty. If this happens, the player will keep all dice by default and the function should return empty.
|
|
βΈ Next, validate the selection by calling your is_between helper function. If validation fails, print a brief message and set sel to empty (keep all). Otherwise, do nothing since sel is already set.
|
|
Helpers:
|
is_between
|
% Example 1: player keeps all
sel = get_player_reroll()
% (Player presses ENTER)
% returns: sel = []
% Example 2: player rerolls dice in position 2 and 5
sel = get_player_reroll()
% (Player types: 2 5)
% returns: sel = 2 5
% Example 3: invalid entry (e.g., "0 6" or "hello")
sel = get_player_reroll()
% prints: "Invalid selection. Keeping all."
% returns: sel = []
get_dealer_reroll
Inputs:
|
rank (1x1) integer β dealerβs rank ID (1-8) from the initial roll.
|
roll (1 x 5) integer vector β dealerβs initial roll sorted by rank order.
|
|
Outputs:
|
|
Details:
|
βΈ Use the rank to determine if the dealer has a good roll and should keep all dice.
|
βΈ Otherwise, return the locations of all the singles.
|
% Example 1: straight => keep all
rank = 4;
roll = [6 5 4 3 2];
counts = [0 1 1 1 1 1];
sel = get_dealer_reroll(rank, roll, counts)
% returns sel = []
% Example 2: three-of-a-kind => reroll singles in positions 4 & 5
rank = 5;
roll = [5 5 5 4 1];
counts = [1 0 0 1 3 0];
sel = get_dealer_reroll(rank, roll, counts)
% returns sel = 4 5
% Example 3: singles => reroll 1 in position 5
rank = 8;
roll = [6 4 3 2 1];
counts = [1 1 1 1 0 1];
sel = get_dealer_reroll(rank, roll, counts)
% returns sel = 5