Over OR Under Seven Dice Game Code:
clc
clear
close all
fprintf(‘*****************************\n’)
fprintf(‘Over OR Under Seven Dice Game\n’)
fprintf(‘*****************************\n’)
%intialize the location of the over or under within the sprite
over_sprite = 19;
under_sprite = 20;
seven_sprite = 21;
dice_sprites = 11:16;
%background dice sprites has to equal the same amount of dice roll
background_dice_sprite = [7,7];
%generate the graphics with importing PNG file
game_screen = simpleGameEngine(‘retro_dice_with_OverUnder.png’, 16,16,15, [0,0,0]);
%load in png file for the game with appropriate dimensions and brightness
n_players = input(‘how many players are playing this game? : ‘);
%get the number of players playing
player_guess = zeros(1,n_players);
%array of zeros
seven = 2;
over = 1;%over will be represented by one in array
under = 0;%under will be represented by zero in array
for i=1:n_players
fprintf(‘\nPlayer %i, over or under than 7?’,i);
%draws two buttons, over, and under
drawScene(game_screen, [over_sprite,under_sprite,seven_sprite]);
%mouse input by user to choose if the roll will over or under 7
[row,col] = getMouseInput(game_screen);
%gets the user’s mouse in over’s block
if(col == 1)
player_guess(i) = over;
fprintf(‘\nPlayer %i, chose %s\n’,i,”over”)
elseif(col == 0)
player_guess(i) = under;
fprintf(‘\nPlayer %i, chose %s\n’,i,”under”)
else
player_guess(i) = seven;
fprintf(‘\nPlayer %i, chose %s\n’,i,”seven”)
end
end
%Rolling of the dice the two dice
VofRoll = randi(6,1,2);
%generating a vector of sprites, by looping through
for n_dice = 1:2
%creates a new array
roll = VofRoll(n_dice);
%stores the dice_sprite according to the roll
roll_sprites(n_dice) = dice_sprites(roll);
end
%illustrate the output of the dice
drawScene(game_screen, background_dice_sprite, roll_sprites);
%getting the total of the roll
sum_roll = sum(VofRoll);
%checking if the roll was greater than 7
if sum_roll > 7
outcome = over;
elseif sum_roll == 7
outcome = seven;
else
outcome = under;
end
%Command window printing who won and who lost!
for n = 1:n_players
if(player_guess(n) == outcome)
fprintf(‘\nPlayer %i Wins!’,n);
else
fprintf(‘\nPlayer %i Loses!’, n)
end
end
fprintf(‘\n’);