Clear Filters
Clear Filters

Yatzee compare most ocurred values with new values from throw?

1 view (last 30 days)
Im looking for help on a specifc scenario while simulating how many throws it takes to get yatzee. My problem I want to solve is for an example if I get 2 fours on the first throw and if I get 3 ones on the second throw then I want to save those values instead. I have a function that saves the most ocurred value from one throw and it looks like this:
function [counting, mostOcurredDice] = mostCommon(Elementvector)
bestDice = vectorElements(Elementvector);
[counting, mostOcurredDice] = max(bestDice);
end
%2.4 Dice from last function gets saved into a list
function saveBestDice = diceNotTosave(Elementvector)
[count, mostOcurredDice] = mostCommon(Elementvector);
saveBestDice = zeros(1, count);
for i = 1:count
saveBestDice(i) = mostOcurredDice;
end
end

Answers (2)

arushi
arushi on 5 Oct 2023
Hi Andreas,
I understand that you want to solve the problem of saving the most occurred value from each throw while simulating Yahtzee, you can modify the existing code to keep track of the most occurred value in each throw. Here is the code for your reference:
function saveBestDice = saveMostOccurredValue(throws)
numThrows = length(throws);
saveBestDice = zeros(1, numThrows);
for i = 1:numThrows
[count, mostOccurredDice] = mostCommon(throws{i});
saveBestDice(i) = mostOccurredDice;
end
end
function [counting, mostOccurredDice] = mostCommon(elementVector)
uniqueElements = unique(elementVector);
counts = histc(elementVector, uniqueElements);
[~, maxIndex] = max(counts);
mostOccurredDice = uniqueElements(maxIndex);
counting = counts(maxIndex);
end
In the saveMostOccurredValue function, it takes in a cell array of throws where each element represents the dice values obtained in a single throw. The function initializes an empty vector saveBestDice to store the most occurred value from each throw.
The for loop iterates through each throw and calls the mostCommon function to find the most occurred value. The result is then stored in the corresponding index of saveBestDice.
The mostCommon function is the same as the one you provided, which finds the most occurred value in a given elementVector using the unique and “histc” functions.
By using this modified code, you can simulate multiple throws of Yahtzee and save the most occurred value from each throw in the saveBestDice vector.
For example, below is an example:
throws = { [2, 4, 4, 1, 3], [1, 1, 1, 2, 6], [5, 5, 5, 5, 5] };
saveBestDice = saveMostOccurredValue(throws);
disp(saveBestDice);
The output is:
4 1 5
Hope this answers your question.

Steven Lord
Steven Lord on 5 Oct 2023
The mode function returns the most frequently occurring value in an array. You may want to use the second and third outputs from mode as well.
x = [1 2 1 2 1];
[value, frequency] = mode(x)
value = 1
frequency = 3
locations = value == x
locations = 1×5 logical array
1 0 1 0 1

Categories

Find more on Word games in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!