MATLAB Programming Applications

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

MATLAB Programming

Applications

MATLAB GAMES
Heads or Tails
Roll Dice
Beat the Dealer
Rock-Paper Scissors
Blackjack
Craps
Due Thursday, February 28th (in class
exam)

Random Number Generator


The command in MATLAB is rand for values between (0,1)
R=rand(N)
%NxN matrix of random #s
R=rand(M,N)
%NxN matrix of random #s
R=randi(imax)
%imax is the max value of the integer
rand
%scalar
To make the numbers generated be truly random add this command
to the top of each of your programs:
rand(state, sum(100*clock)) %resets to a different state each time

To change the range of your


random numbers generated
rand automatically gives values between (0,1)
But you can change that range
Multiply by a value
Add a value for a different starting point
Use round, floor, ceil to get values in the
format you would like
Random numbers distributed between (a,b)
(b-a)*rand+a

Simulate A Coin Flip


heads=0;
tails=0;
for i=1:1000000
x=rand;
if x<0.5
heads=heads+1;
else
tails=tails+1;
end
end
disp(heads)
disp(tails)

Images

Use the internet to find images

Use comments to indicate the source (URL)

Store images as .jpg

You can use other formats as well


Keep images in the same directory as the
programs

IMREAD: Read image from graphics file


I = imread(FILENAME)

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.

Activity: Rolling Dice

What about a 6 sided die?


Random numbers distributed between
(1,6)
randi(6)
Write a program that runs 1000 times
generating random numbers between
1-6
Count how many are in each range

HW Game: Rolling Dice

Offer the user a menu


1, 2, 3, 4, or 5 dice
Quit
Show an image of die that is rolled

HW Game: Rolling Dice


Hint:
Use Subplot
SUBPLOT(m, n, p)
breaks the Figure
window into an m-by-n
matrix of small plots,
selects the p-th axes for the current plot.
The axes are counted along the top row of the Figure
window, then the second row, etc.
SUBPLOT(2,1,1), image(I)
SUBPLOT(2,1,2), image(I)
plots x vs. y on the top half of the window and x vs. z on the
bottom half.

HW: Beat the Dealer


Offer the user a menu
Pick a card
Quit
Show your card and show the dealers card
Determine the winner
Keep score
Hints: Need to organize a method to keep track
of cards
Values
Suit

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 ..

Main Program Structure


Requirements
Menu for games
Each game as a separate function
Betting options
Keep track of money

Main Program Pseudocode


%Display header
%Ask user if they are playing for money or not
%If betting, gain user input on how much to start with
%Outer loop to keep playing
%Menu to determine what game to play
%Switch-Case on game chosen
%Function for Heads or Tails
%Function for Roll the Dice
%Function for Beat the Dealer
%Function for RPS
%Function for Blackjack
%Function for Craps
otheriwse quit
%End
%Display running money total if betting, etc.
This must include the logic for if they are out of $

Function Program Pseudocode


%function header
function [$]=function_game_name($)
%Use display statements to explain the game
%Load images
%Outer loop to keep playing
%Menu for user input
%Logic of the game
%Keep track of wins, losses, money (display to the
command window)
%If money drops to $0 go back to the main program
return
%Ask user to play again
%end

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.

Program Example: The Game of Craps


Craps is a dice game played with 2 die
The player begins by making first roll:
If the roll is a 7 or 11

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

Player continues rolling until the first roll is repeated or a 7 is rolled.


Player can start game with any amount of money, and can bet any
amount (<= total) for each game, until out of money

Initial Breakdown of Program


1. Simulate a random roll of 2 die
2. Evaluate if player wins or loses after first roll
based on rules
3. If neither, generate a second roll
4. Evaluate if player wins or loses
5. Repeat 3 and 4 until player wins or loses
6. Output results
7. See if player wants to try again

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.

Modify Game to Allow Betting


Start program giving user a total of $20
Prior to each game, ask user how much they want to bet.
Make this a function called bet_amount with what user has as the function
input and what user wants to bet as function output
This function should tell the user how much (s)he has, ask how much to bet,
and check to make sure user does not bet more than they have.

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.

You might also like