Professional Documents
Culture Documents
MATLAB Programming Applications
MATLAB Programming Applications
MATLAB Programming Applications
Applications
MATLAB GAMES
Heads or Tails
Roll Dice
Beat the Dealer
Rock-Paper Scissors
Blackjack
Craps
Due Thursday, February 28th (in class
exam)
Images
Images
IMSHOW: Display image in Handle Graphics figure
imshow(I)
displays the grayscale image I
Hint: in some cases you may want to turn the
axis off so that there are no scale on the image
axis off
AXIS OFF turns off all axis labeling, tick marks and
background.
AXIS ON turns axis labeling, tick marks and background
back on.
Random Numbers
For some of the games you may want
to assign permutation random
numbers
randperm(52)
RANDPERM(n) is a random permutation
of the integers from 1 to n.
For example, RANDPERM(6) might be
[2 4 5 6 1 3]
Shuffling
When do you need to shuffle?
At the start of each game
Within a game, if more than 35 cards are
used you must shuffle
newdeck=randperm(52)
An array of 52 elements you must be able
to index through as you draw new cards
Card Mapping
Use a function to perform the mapping
Two potential approaches
(1) By Suit:
1-13 are Clubs
14-26 are Hearts
27-39 are Diamonds
40-52 are Spades
(2) By Number
1-4 are Twos
5-8 are Threes
9-12 are Fours ..
Rules of Blackjack
The dealer will deal himself two cards
One visible
One not visible (hole card)
Single-deck game so you have to keep track of which cards were already played (a
card cannot appear multiple times in one hand)
The deck be re-shuffled either:
After each hand
Or you can use the code you have for Beat the Dealer and reshuffle after 35 cards have
been dealt and the most recent hand is over
The highest total value without going over 21 wins.
Face cards are all valued at 10
Numbered cards are valued as their number
Aces can either be valued at 1 or 11, depending on which works best.
The player is dealt two cards face up and can:
Stand with just those cards
Hit as many times as wanted, but immediately loses if he goes over 21
The dealer must hit if his initial total is 16 or less, and must continue hitting until he
goes over 21 or reaches 16 or above.
RPS Rules
User against the computer
Choice to play the best of 3, 5, or 7 throws.
The users choice and the computers randomly chosen choice will be
represented with images of a hand in the rock, paper or scissors
ROCK crushes SCISSORS
SCISSORS cuts PAPER
PAPER covers ROCK
If the user is betting and wins, they win the amount betted (plus getting back
the bet). If the user loses, they lose their bet amount.
Player wins
If the roll is a 2, 3, or 12
Player loses
Otherwise the player continues rolling:
If after the first roll, the player rolls a 7 Player loses
If the player rolls the same total as the first roll Player wins
Craps Pseudocode
%Set up while loop to play again
%Create function which randomly generates two separate numbers between
1 and 6 and returns the sum (help rand)
%Call function for first roll
a. If sum = 7 | sum = 11, gameresult = 1 (win)
b. If sum = 2 | sum = 3 | sum = 12, gameresult = 2 (lose)
c. Else gameresult = 0, firstroll = sum
%Set up while loop to keep rolling until player wins or
loses (while gameresult = 0)
a. If sum = firstroll, gameresult = 1
b. If sum = 7, gameresult = 2
%Output result:
a. If gameresult = 1, print YOU WIN
b. If gameresult = 2, print SORRY, YOU LOSE
%Ask if user wants to play again.
If user wins game, add bet amount to total. If user loses game, subtract
bet amount from total.
Make this a function called bet_result with 3 inputs (total, bet amount,
game result) and total as the output
This function should adjust total based on result of game, then print out the
new total.