Skip to main content

Section Homework 4

Before you begin ...

Permitted MATLAB Functions & Commands for Homework 4.

The following built-in MATLAB functions and commands are permitted for this assignment.
Vector/Matrix
Operations:
round β€’ mod β€’ floor β€’ ceil β€’ sum β€’ max β€’ min
Size/Dimensions:
length β€’ numel β€’ size β€’ height β€’ width
Creation:
zeros β€’ ones β€’ true β€’ false
Logical:
all β€’ any
Flow Control
Conditional:
if β€’ switch-case β€’ try-catch
Loops:
for β€’ while β€’ continue β€’ break β€’ return
Strings and Character Arrays
Operations:
join β€’ replace
Conversion:
num2str β€’ str2num β€’ str2double β€’ string β€’ char
Other
Printing Text:
fprintf β€’ disp β€’ display β€’ input β€’ assert
Random Generators:
rand β€’ randi β€’ rng
Special Variables:
nargin
Type Detection:
isnan β€’ isfield β€’ isempty
Points will be deducted from any programs using functions outside this list.

Summary.

The goal of this assignment is to write the last few helper functions for Dice Poker and bring them all together to create a fully working game.

Subsection init_game

This helper defines and initializes the default values for the fields of a Dice Poker game structure. The glyphs and maxRounds fields will remain static throughout the game, while the others may change as the game is played.
Inputs:
N (1x1) integer β€” maximum number of rounds to play (default 1)
Outputs:
game (1x1) struct with fields:
  • round (1x1) integer β€” starting round (1)
  • maxRounds (1x1) integer β€” should be N or (1)
  • glyphs (1x6) string vector β€” die faces \(βš€\)-\(βš…\) (for display)
  • dWins (1x1) integer β€” numbers of rounds won by the dealer (0)
  • pWins (1x1) integer β€” numbers of rounds won by the player (0)
  • dHand (1x1) struct β€” dealer dice hand (struct())
  • pHand (1x1) struct β€” player dice hand (struct())
Note:
The values in parentheses are default values for each field.
Details:
β–Έ Except for possibly maxRounds, the values in the output structure should match the default values.
β–Έ The dice icons (i.e., glyphs) we will use to visualize the dice are
["βš€" "⚁" "βš‚" "βšƒ" "βš„" "βš…"]
These will be used in your display_game_area helper.
Examples:
g = init_game(3)
% 
% struct with fields:
%   	round: 1
% 		maxRounds: 3
%       glyphs: ["βš€"  "⚁"  "βš‚"  "βšƒ"  "βš„"  "βš…"]
%   	dWins: 0
%   	pWins: 0
%       dHand: [1Γ—1 struct]
%       pHand: [1Γ—1 struct]
g.dHand
% 
%   struct with no fields.

Subsection init_hand

This helper defines and initializes the default values for the fields of a single Dice Poker hand structure.
Inputs:
None
Outputs:
hand (1x1) struct with fields:
  • roll1 (1x5) integer β€” initial roll ([])
  • roll2 (1x5) integer β€” reroll ([])
  • counts (1x6) integer β€” current roll face counts ([])
  • rid (1x1) integer β€” integer rank ID (0)
  • rank1 (1x1) string β€” text rank for roll1 ("")
  • rank2 (1x1) string β€” text rank for roll2 ("")
Note:
The values in parentheses are the default values for each field. These values will be updated later by your set_hand_struct helper.
Details:
β–Έ This function sets up a clean Dice Poker hand with the default values listed above.
Examples:
aHand = init_hand()
% struct with fields:
% 
%         roll1: []
%         roll2: []
%        counts: []
%           rid: 0
%         rank1: ""
%         rank2: ""
aHand.rid
% returns:  0

Subsection get_rank

Given a roll and counts vector, this function return the rank name and ID.
Inputs:
roll (1 x 5) integer vector β€” dealer’s initial roll sorted by rank order.
counts (1 x 6) integer vector β€” counts of each die face in roll.
Outputs:
id (1x1) integer β€” rank ID.
name (1x1) string β€” rank name.
Details:
β–Έ To get full-credit on the test cases, you must spell each rank name exactly as it reads in the Dice Rank Table.
β–Έ You can assume that roll and counts will be valid numeric vectors.
Helpers:
is_1pair, is_2pair, ... Basically all of your is_* helpers.
Examples:
% Example 1
roll = [3 3 3 4 1];
counts = [1 0 3 1 0 0];
[id,name] = get_rank(roll, counts)
% returns  
%    id  =  5
%    name  =  "three-of-a-kind"
% Example 2
roll = [4 4 4 6 6];
counts = [0 0 0 3 0 2];
[id,name] = get_rank(roll, counts)
% returns  
%    id  =  3
%    name  =  "full-house"

Subsection set_hand_struct

This function’s task is to convert a raw initial roll or reroll and update the dice hand for the player or dealer.
Inputs:
hand (1x1) struct containing the fields:
  • roll1 (1x5) integer β€” initial roll
  • roll2 (1x5) integer β€” reroll
  • counts (1x6) integer β€” current roll face counts
  • rid (1x1) integer β€” integer rank ID
  • rank1 (1x1) string β€” text rank for roll1
  • rank2 (1x1) string β€” text rank for roll2
roll (1 x 5) integer vector β€” new set of dice values (faces 1-6).
Note:
hand is a generic input name. In practice, this will be either the dHand or pHand structures initialized in init_game.
Outputs:
new_hand (1x1) struct β€” updated roll with new roll, rank ID, rank name, and face counts.
Details:
β–Έ The input roll will represent the initial roll or reroll from either the dealer or player (depending on whose hand was sent in as the input).
β–Έ Use roll to update counts and rid.
β–Έ Also update either:
roll1 & rank1
roll2 & rank2.
Helpers:
get_face_counts, sort_by_rank, get_rank
Examples:
% Example 1: Before and After initial roll
d = init_hand()
% d before:
% d = 
%   struct with fields:
%      roll1: []
%      roll2: []
%      rank1: ''
%      rank2: ''
%     counts: []
%        rid: 0				
d = set_hand_struct(d, [5 2 4 1 5])
% d after:
% d = 
%   struct with fields:
%      roll1: [5 5 4 2 1]
%      roll2: []
%      rank1: 'one-pair'
%      rank2: ''
%     counts: [1 1 0 1 2 0]
%        rid: 7
% Example 2: After reroll
d = set_hand_struct(d, [3 3 5 5 5])
% returns:
% d = 
%   struct with fields:
%      roll1: [5 5 4 2 1]
%      roll2: [5 5 5 3 3]
%      rank1: 'one-pair'
%      rank2: 'full-house'
%     counts: [0 0 2 0 3 0]
%        rid: 3

Subsection display_game_area

This helper displays the current state of a Dice Poker game as ASCII text, showing dealer and player rolls, rerolls, and roll ranks.
Inputs:
g (1x1) struct β€” current game structure. Same fields that were initialized in init_game.
phaseMsg (1x1) string β€” describes the current phase of the game.
actionMsg (1x1) string β€” describes actions or results of a phase.
Outputs:
None
Details:
β–Έ Create a template for the game area. For example:
gameArea = [ ...
" DICE POKER (Best of MAXRDS)          ROUND: RD"; ...
"==============================================="; ...
" DEALER: D-WINS                                "; ...
"       ROLL: D-ROLL1    D-RANK1                "; ...
"     REROLL: D-ROLL2    D-RANK2                "; ...
"==============================================="; ...
" PLAYER: P-WINS                                "; ...
"       ROLL: P-ROLL1    P-RANK1                "; ...
"     REROLL: P-ROLL2    P-RANK2                "; ...
"==============================================="; ...
"PHASEMSG                                       "; ...
"ACTIONMSG                                      "; ...
];
β–Έ The goal is to replace all the generic labels, below, with the current values from the game inside g.
  • Round Info: MAXRDS, RD
  • Dealer Info: D-WINS, D-ROLL1, D-RANK1, D-ROLL2, D-RANK2
  • Player Info: P-WINS, P-ROLL1, P-RANK1, P-ROLL2, P-RANK2
  • Game Narration: PHASEMSG, ACTIONMSG
β–Έ You can use MATLAB’s replace function to find & replace the generic labels in gameArea with their corresponding game values.
β–Έ Any game value that is a string in g can replace its generic label directly. Numeric values should be converted to a string with num2str.
β–Έ Converting a roll (1x5 integer vector) requires a two step process:
  1. Create a 1x5 string vector of dice icons matching the roll values:
    [6 6 5 3 1]
    ➜
    ["βš…" "βš…" "βš„" "βš‚" "βš€"]
  2. Next, use MATLAB’s join to convert a 1x5 string to a 1x1 string:
    ["βš…" "βš…" "βš„" "βš‚" "βš€"]
    ➜
    "βš… βš… βš„ βš‚ βš€"
β–Έ Any empty rolls ([]) should replace its generic label with the empty string "".
β–Έ Once the gameArea has the correct values, clear the Command Window with clc & display the game area with disp(char(gameArea)). Displaying the game area as a character array with char removes the " " characters from the sides of the game area.
Examples:
% Example 1
g = init_game(3);
g.dHand = init_hand();
g.pHand = init_hand();
g.dHand.roll1 = [6 6 5 3 2];
g.dHand.rank1 = 'one-pair';
display_game_area(g, ":: Dealer's Initial Roll ::", "");
% The commands above should clear the command window and display:
 DICE POKER (Best of 3)          ROUND: 1
===============================================
 DEALER: 0
       ROLL: βš… βš… βš„ βš‚ ⚁    one-pair
     REROLL:
===============================================
 PLAYER: 0 
       ROLL:
     REROLL:
===============================================
:: Dealer's Initial Roll ::

% There should be a blank line above since actionMsg = ""
% Example 2
g = init_game(3);
g.round = 2;
g.dWins = 1;
g.dHand = init_hand();
g.pHand = init_hand();
g.dHand.roll1 = [6 6 5 3 2];
g.dHand.rank1 = 'one-pair';
g.dHand.roll2 = [6 6 2 2 5];
g.dHand.rank2 = 'two-pair';
g.pHand.roll1 = [6 5 4 3 2];
g.pHand.rank1 = 'straight';
phase_msg = ":: Player's Selection ::";
action_msg = "Player keeps all Dice.";
display_game_area(g, phase_msg, action_msg);
% The commands above should clear the command window and display:
 DICE POKER (Best of 3)          ROUND: 2
===============================================
 DEALER: 1
       ROLL: βš… βš… βš„ βš‚ ⚁    one-pair                             
     REROLL: βš… βš… ⚁ ⚁ βš„    two-pair
===============================================
 PLAYER: 0 
       ROLL: βš… βš„ βšƒ βš‚ ⚁    straight
     REROLL:
===============================================
:: Player's Selection ::
Player keeps all Dice.

Subsection play_one_round

Plays one complete round of Dice Poker between the dealer and the player. This function manages all rolls, re-roll selections, game display, and winner determination.
Inputs:
game (1x1) struct β€” the current game state structure, as returned by init_game.
Outputs:
game (1x1) struct β€” updated game state structure.
Details:
β–Έ Before starting a round, reset the player and dealer’s hand structures to the default values.
β–Έ Then, this function executes the following phases:
  1. Dealer’s Initial roll
  2. Player’s Initial roll
  3. Dealer’s Selection
  4. Dealer’s Reroll
  5. Player’s Selection
  6. Player’s Reroll
  7. Determine Winner
See the resource β€œTypical Round” for more details.
β–Έ Each phase should update a field of the game structure followed by a call your display_game_area helper.
β–Έ Conclude each phase with a prompt for the player to press enter:
input("Press ENTER to continue.")
This pauses the code until ENTER is pressed and triggers a new phase to begin.
β–Έ You can see a full round in action by downloading this play_one_round demo and running it in MATLAB.
Helpers:
init_hand, roll_dice, set_hand_struct, get_dealer_reroll, get_player_reroll, display_game_area, get_winner
Examples:
game = init_game();  % One-round game initialization
game = play_one_round(game);
% Does it play without error?
% Does it mimic the provided demo?
% The input game struct enters as a blank slate. Are the fields of the
%     output game struct filled-in with the results of the game?

Subsection dice_poker

Runs a best-of-N-round game of Dice Poker between a player and a dealer.
Inputs:
None.
Outputs:
None.
Details:
β–Έ This is the entry point and main driver for your Dice Poker game.
β–Έ It can be broken down into three main sections:
  1. Initialization
    • Set the number of rounds to be played, and
    • Initialize the game structure.
  2. Game Loop (by round)
    • Use a loop for each round,
    • Update the game structure as necessary between rounds,
    • End the loop early if either the dealer or player was won more than half of the games.
  3. Print the Overall Results
    • Display the score (number of rounds each player won).
    • Display the overall winner.
β–Έ To see a full β€œBest-of-3” Dice Poker Game, download this dice_poker demo and run it in MATLAB.
β–Έ This should be a self-contained game, meaning all helper functions that it depends on must be included below the dice_poker function within your dice_poker.m file.
β–Έ Before turning this in, move your dice_poker.m file to a completely isolated folder on your computer and run it in MATLAB. If it runs without error and as you expect, then you should be good to go.
Helpers:
init_game, play_one_round
Examples:
% Start a game.
dice_poker()
% Does it play without error? Does it mimic the provided demo?