Using the program Matlab: Create a GUI for a Yahtzee game that has the following
ID: 3831013 • Letter: U
Question
Using the program Matlab: Create a GUI for a Yahtzee game that has the following: the ability to role dice and a scoring system (ones, twos, threes, fours, fives, sixes, full house, chance, and Yahtzee). Please show all work. Thanks! Using the program Matlab: Create a GUI for a Yahtzee game that has the following: the ability to role dice and a scoring system (ones, twos, threes, fours, fives, sixes, full house, chance, and Yahtzee). Please show all work. Thanks! Using the program Matlab: Create a GUI for a Yahtzee game that has the following: the ability to role dice and a scoring system (ones, twos, threes, fours, fives, sixes, full house, chance, and Yahtzee). Please show all work. Thanks! Create a GUI for a Yahtzee game that has the following: the ability to role dice and a scoring system (ones, twos, threes, fours, fives, sixes, full house, chance, and Yahtzee). Please show all work. Thanks!Explanation / Answer
YAHTZEE:
Yahtzee is a dice game made by Milton Bradley (now owned by Hasbro), which was first marketed as "Yatzie" by the National Association Service of Toledo.
The object of the game is to score points by rolling five dice to make certain combinations. The dice can be rolled up to three times in a turn to try to make various scoring combinations. A game consists of thirteen rounds. After each round the player chooses which scoring category is to be used for that round. Once a category has been used in the game, it cannot be used again. The scoring categories have varying point values, some of which are fixed values and others where the score depends on the value of the dice. A Yahtzee is five-of-a-kind and scores 50 points; the highest of any category. The winner is the player who scores the most points.
Rules
The game consists of a number of rounds. In each round, a player gets three rolls of the dice, although they can choose to end their turn after one or two rolls. After the first roll the player can save any dice they want and re-roll the other dice. This procedure is repeated after the second roll. The player has complete choice as to which dice to roll. They can re-roll a die for the third roll that was not rolled on the second roll.
The Yahtzee scorecard contains 13 different category boxes and in each round, after the third roll, the player must choose one of these categories. The score entered in the box depends on how well the five dice match the scoring rule for the category. Details of the scoring rules for each category are given below. As an example, one of the categories is called Three of a Kind. The scoring rule for this category means that a player only scores if at least three of the five dice are the same value. The game is completed after 13 rounds by each player, with each of the 13 boxes filled. The total score is calculated by summing all thirteen boxes, together with any bonuses.
The Yahtzee scorecard contains 13 scoring boxes divided between two sections: the upper section and the lower section.
The Dice
The dice class inherits from System.Windows.Forms. User Control so that it is easier to do the drawing of the object. The control knows how to draw itself and also what the next number to draw is. The latter is done using the Random class, which originally posed some problems, like how do you make sure that two or more dice are not initialized with the same seed.
public void InitializeRandomRoll( int nSeed )
{
DateTime aTime = new DateTime(1000);
aTime = DateTime.Now;
nSeed += (int)(aTime.Millisecond);
RandomPick = new Random(nSeed);
}
We need to be able to hold a dice, so that it does not roll when the roll button is pressed. So, we add a variable to hold the state of the dice.
private bool m_bHoldState = false;
public bool HoldState
{
get { return m_bHoldState; }
set { m_bHoldState = value; }
}
Now, we need to make sure that if the dice is held, it does not get rolled. This is simply a matter of adding an if statement to the Roll() method.
public void Roll()
{
// If the dice is not held, roll it
if( !HoldState )
{
RollNumber = RandomPick.Next(1,7);
this.Invalidate();
}
}
The following method draws a single dot at a given point:
public void DrawDot( Graphics g, Point p )
{
SolidBrush myBrush;
if( HoldState )
{
myBrush = new SolidBrush( Color.Red );
}
else
{
myBrush = new SolidBrush( Color.Black );
}
g.FillEllipse( myBrush, p.X, p.Y, dotWidth, dotWidth );
myBrush.Dispose();
}
Calculating The Score
To calculate the scores, a class was created. The class has many functions that calculate the individual scores, it also keeps a tally of the total, upper and lower scores.
The algorithm used to add up the total scores of five dice when the same number is needed is shown below. The total of all of the dice that have the same number is returned to the form, or zero if none of the dice are the same as that needed.
public int AddUpDice( int DieNumber, Dice[] myDice )
{
int Sum = 0;
for( int i = 0; i < 5; i++ )
{
if( myDice[i].RollNumber == DieNumber )
{
Sum += DieNumber;
}
}
return Sum;
}
Two loops are used to determine if indeed three or four of the dice are the same.
public int CalculateThreeOfAKind( Dice[] myDice )
{
int Sum = 0;
bool ThreeOfAKind = false;
for( int i = 1; i <= 6; i++ )
{
int Count = 0;
for( int j = 0; j < 5; j++ )
{
if( myDice[j].RollNumber == i )
Count++;
if( Count > 2 )
ThreeOfAKind = true;
}
}
if( ThreeOfAKind )
{
for( int k = 0; k < 5; k++ )
{
Sum += myDice[k].RollNumber;
}
}
return Sum;
}
Instead of returning the sum of the dice, we return a fixed score, which is 25.
public int CalculateFullHouse( Dice[] myDice )
{
int Sum = 0;
int[] i = new int[5];
i[0] = myDice[0].RollNumber;
i[1] = myDice[1].RollNumber;
i[2] = myDice[2].RollNumber;
i[3] = myDice[3].RollNumber;
i[4] = myDice[4].RollNumber;
Array.Sort(i);
if( (((i[0] == i[1]) && (i[1] == i[2])) && // Three of a Kind
(i[3] == i[4]) && // Two of a Kind
(i[2] != i[3])) ||
((i[0] == i[1]) && // Two of a Kind
((i[2] == i[3]) && (i[3] == i[4])) && // Three of a Kind
(i[1] != i[2])) )
{
Sum = 25;
}
return Sum;
}
Again, instead of returning the sum of the dice, we return a fixed score, which is 40.
public int CalculateLargeStraight( Dice[] myDice )
{
int Sum = 0;
int[] i = new int[5];
i[0] = myDice[0].RollNumber;
i[1] = myDice[1].RollNumber;
i[2] = myDice[2].RollNumber;
i[3] = myDice[3].RollNumber;
i[4] = myDice[4].RollNumber;
Array.Sort(i);
if( ((i[0] == 1) &&
(i[1] == 2) &&
(i[2] == 3) &&
(i[3] == 4) &&
(i[4] == 5)) ||
((i[0] == 2) &&
(i[1] == 3) &&
(i[2] == 4) &&
(i[3] == 5) &&
(i[4] == 6)) )
{
Sum = 40;
}
return Sum;
}
Again, instead of returning the sum of the dice, we return a fixed score, which is 30.
public int CalculateSmallStraight( Dice[] myDice )
{
int Sum = 0;
int[] i = new int[5];
i[0] = myDice[0].RollNumber;
i[1] = myDice[1].RollNumber;
i[2] = myDice[2].RollNumber;
i[3] = myDice[3].RollNumber;
i[4] = myDice[4].RollNumber;
Array.Sort(i);
// Problem can arise hear, if there is more than one of the same number, so
// we must move any doubles to the end
for( int j = 0; j < 4; j++ )
{
int temp = 0;
if( i[j] == i[j+1] )
{
temp = i[j];
for( int k = j; k < 4; k++ )
{
i[k] = i[k+1];
}
i[4] = temp;
}
}
if( ((i[0] == 1) && (i[1] == 2) && (i[2] == 3) && (i[3] == 4)) ||
((i[0] == 2) && (i[1] == 3) && (i[2] == 4) && (i[3] == 5)) ||
((i[0] == 3) && (i[1] == 4) && (i[2] == 5) && (i[3] == 6)) ||
((i[1] == 1) && (i[2] == 2) && (i[3] == 3) && (i[4] == 4)) ||
((i[1] == 2) && (i[2] == 3) && (i[3] == 4) && (i[4] == 5)) ||
((i[1] == 3) && (i[2] == 4) && (i[3] == 5) && (i[4] == 6)) )
{
Sum = 30;
}
return Sum;
}
Counting the scores
private int ScoreCount = 0;
if( ScoreCount == 14 )
{
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show( "Your Score is " + TotalScore.Text + ".
Would You like to play again?",
"End Of Game",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question );
if( result == DialogResult.Yes )
{
// Reset Everything
. . .
}
else
{
this.Close();
}
}
CODE:
function varargout = scoring(dice)
%Yahtzee scoring
% dice must be a 1x5 vector of integer values representing the face value
% of the die. Returned is a cell array.
if ~nargin
error('Matlab:scoring','no inputs!')
end
if any(find(dice == -1))
varargout{1} = num2cell(zeros(1,13));
return
end
if length(dice) ~= 5
beep
sprintf('Doh! You called scoring with wrong number of dice')
return
end
dice = sort(dice);
points = num2cell(zeros(1,13));
%points cell array:
% 1-6 number of dice
% do Upper 6 first
for i = 1:6
% get number of like numbered dice
counts(i) = size(find(dice == i), 2);
% calculate points for first 6
points{i} = i*counts(i);
end
% 7 Three of a Kind
if any(find(counts >= 3))
points{7} = sum(dice);
end
% 8 Four of a Kind
if any(find(counts >= 4))
points{8} = sum(dice);
end
% 9 Full House
if any(find(counts == 3)) & any(find(counts == 2))
points{9} = 25;
end
% 10 Small Straight
dif = dice(2:end) - dice(1:end-1);
if (size(find(dif==1), 2) >= 3)
if (dif(2) ~= 2) & (dif(3) ~= 2)
points{10} = 30;
end
end
% 11 Large Straight
if (size(find(dif==1), 2) == 4)
points{11} = 40;
end
% 12 Yahtzee
if any(find(counts == 5))
points{12} = 50;
end
% 13 Chance
points{13} = sum(dice);
if nargout
varargout{1} = points;
if nargout >=2
varargout{2} = counts;
end
end
OR
diceValues = randi(6,[1 5])
for k=2:3
diceIndexesToReRoll = input('Which dice would you like to reroll?')
diceValues(diceIndexesToReRoll) = randi(6,[1 numel(diceIndexesToReRoll)])
end
points=zeros(1,13);
% upper
for i = 1:6
counts(i) = sum(find(diceValues == i), 2);
points(i) = i*counts(i)
end
upperpoints=points(i)
% lower
% Three of a Kind
if any(find(counts >= 3))
points(7) = sum(diceValues)
end
% Four of a Kind
if any(find(counts >= 4))
points(8) = sum(diceValues)
end
% Full House
if any(find(counts == 3)) & any(find(counts == 2))
points(9) = 25
end
% Small Straight
dif = diceValues(2:end) - diceValues(1:end-1);
if (sum(find(dif==1), 2) >= 3)
if (dif(2) ~= 2) & (dif(3) ~= 2)
points(10) = 30
end
end
% Large Straight
if (sum(find(dif==1), 2) == 4)
points(11) = 40
end
% Yahtzee
if any(find(counts == 5))
points(12) = 50
end
% Chance
points(13) = sum(diceValues)
lowerpoints=sum(points(7)+points(8)+points(9)+points(10)+points(11)+points(12)+points(13))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.