Made single-level evaluation

This commit is contained in:
2021-06-23 04:30:58 +02:00
parent d8db645b9a
commit f65ccebdd2
2 changed files with 292 additions and 42 deletions
+1 -1
View File
@@ -26,7 +26,7 @@
</main-panel> </main-panel>
<main-panel> <main-panel>
<button onclick="reset();">Reset</button> <button onclick="reset();">Reset</button>
<button onclick="document.getElementById('result').innerHTML = getPatterns();">Analyze</button> <input type="checkbox" onclick="toggleAutoDraw(this)" id="autoDrawToggle" /><label for="autoDrawToggle">Auto-Draw</label>
<br> <br>
<div style="margin-top: 1.4em;" id="result">?</div> <div style="margin-top: 1.4em;" id="result">?</div>
</main-panel> </main-panel>
+288 -38
View File
@@ -1,21 +1,44 @@
var deck = {}; var maxNumber = 8; // the maximum number on cards (cards always start at one)
var hand = {}; var colors = [ "red", "blue", "yellow" ]; // the available numbers
var handSize = (colors.length * 2) - 1; // the maximum hand size
var seriesLength = colors.length; // the minimum required length of series
var pointsMultiplier = 10; // useless "big number feel" multiplier
var sameNumberExtra = 1; // extra points given for sets of same numbers
var sameColorExtra = 4; // extra points given for sets of the same color if they are in a series
var autoDraw = false; // whether the script should automatically draw on start and on discard
var autoRecommend = true; // whether the script should automatically recommend on change
var allCards = undefined;
var deck = undefined;
var hand = undefined;
var recommendations = undefined;
var deckElement = document.getElementById('deck'); var deckElement = document.getElementById('deck');
var handElement = document.getElementById('hand'); var handElement = document.getElementById('hand');
var recommendElement = document.getElementById('result');
var colors = [ "red", "blue", "yellow" ]; reset();
var handSize = 5;
var maxNumber = 8;
fillDeck(); function toggleAutoDraw(element) {
updateUI(); if (element.checked) {
autoDraw = true;
fillHand();
}
else {
autoDraw = false;
}
}
function cardToId(card) { function cardToId(card) {
return card.color + "_" + card.number.toString(); return card.color + "_" + card.number.toString();
} }
function cardToElement(card, forDeck = false) { function idToCard(cardId) {
return allCards[cardId];
}
function cardToElement(card, forDeck = false, drawn = false) {
var element = document.createElement("okey-card"); var element = document.createElement("okey-card");
element.innerHTML = card.number.toString(); element.innerHTML = card.number.toString();
@@ -29,7 +52,7 @@ function cardToElement(card, forDeck = false) {
element.setAttribute("onclick", "discard('" + cardToId(card) + "')"); element.setAttribute("onclick", "discard('" + cardToId(card) + "')");
} }
if (forDeck && card.drawn) { if (forDeck && drawn) {
element.classList.add("drawn-" + card.color); element.classList.add("drawn-" + card.color);
} }
else { else {
@@ -40,82 +63,309 @@ function cardToElement(card, forDeck = false) {
return element.outerHTML; return element.outerHTML;
} }
function isHandFull() { function isHandFull(thisHand = null) {
if (thisHand == null) {
thisHand = hand;
}
return Object.entries(hand).length >= handSize; return Object.entries(hand).length >= handSize;
} }
function fillDeck() { function generateCards() {
for (let color of colors) for (let color of colors)
{ {
for (let i = 1; i <= maxNumber; i++) for (let i = 1; i <= maxNumber; i++)
{ {
let card = { let card = {
color: color, color: color,
number: i, number: i
drawn: false
}; };
card.id = cardToId(card);
deck[cardToId(card)] = card; allCards[card.id] = card;
} }
} }
} }
function discard(cardId) { function fillDeck() {
delete hand[cardId]; for (let cardId of Object.keys(allCards)) {
updateUI(); deck.push(cardId);
}
} }
function drawCard(cardId) { function fillHand() {
if (!deck[cardId].drawn && !isHandFull()) { let draws = handSize - hand.length;
hand[cardId] = deck[cardId]; for (let i = 0; i < draws; i++) {
deck[cardId].drawn = true; let deckPos = Math.floor(Math.random()*deck.length);
drawCard(deck[deckPos]);
}
}
function discard(cardId, thisHand = null) {
let handGiven = true;
if (thisHand == null) {
thisHand = hand;
handGiven = false;
}
if (thisHand.indexOf(cardId) !== -1) {
thisHand.splice(thisHand.indexOf(cardId), 1);
}
if (autoDraw) {
fillHand();
}
if (!handGiven) {
updateUI(); updateUI();
} }
} }
function drawCard(cardId, thisHand = null, thisDeck = null) {
let handGiven = true;
if (thisHand == null) {
thisHand = hand;
handGiven = false;
}
if (thisDeck == null) {
thisDeck = deck;
handGiven = false;
}
if (!isHandFull(thisHand) && thisDeck.includes(cardId)) {
thisDeck.splice(thisDeck.indexOf(cardId), 1);
thisHand.push(cardId);
}
if (!handGiven) {
updateUI();
}
}
function recommend() {
recommendOfDepth();
updateUI(true);
}
function getPatterns(thisHand = null) { function getPatterns(thisHand = null) {
if (thisHand == null) { if (thisHand == null) {
thisHand = hand; thisHand = hand;
} }
let patterns = []; let patterns = {};
for (let i = 1; i <= maxNumber; i++) { cardsByNumbers = [];
sameNumber = 0; cardColorsByNumbers = [];
for (let card of Object.values(thisHand)) {
if (card.number == i) { // set up empty arrays for each number
sameNumber++; for (let i = 0; i < maxNumber; i++) {
cardsByNumbers.push([]);
cardColorsByNumbers.push([]);
}
// fill them with the current hand's cards
for (let cardId of thisHand) {
let card = idToCard(cardId);
cardColorsByNumbers[card.number - 1].push(card.color);
cardsByNumbers[card.number - 1].push(card);
}
// check if there are "color" amount of any
for (let i = 0; i < maxNumber; i++) {
if (cardsByNumbers[i].length >= colors.length) {
let patternName = (i + 1).toString();
for (let j = 1; j < colors.length; j++) {
patternName += "-" + (i + 1).toString();
}
// "i" is lagging behind, adjustment needed
let points = (i + 1 + sameNumberExtra)
patterns[patternName] = {};
patterns[patternName].pattern = patternName;
patterns[patternName].points = points;
} }
} }
if (sameNumber >= colors.length) { // check if there are series present
let patternName = i + "-" + i + "-" + i; for (let i = 0; i <= (maxNumber - seriesLength); i++) {
patterns[patternName] = (i + 1) * 10; let isSeries = true;
let theseColors = colors;
for (let numberOffset = 0; numberOffset < seriesLength; numberOffset++) {
if (cardColorsByNumbers[i + numberOffset] < 1) {
isSeries = false;
break;
}
// intersect
theseColors = [theseColors, cardColorsByNumbers[i + numberOffset]].reduce((a, c) => a.filter(i => c.includes(i)));
}
if (isSeries) {
let patternName = (i + 1).toString();
for (let numberOffset = 1; numberOffset < seriesLength; numberOffset++) {
patternName += "-" + (i + numberOffset + 1).toString();
}
// "i" is lagging behind, adjustment needed
let points = (i + 1);
// check if they are of the same color
if (theseColors.length > 0) {
points = ((i + 1) + sameColorExtra);
patternName += "*";
}
patterns[patternName] = {};
patterns[patternName].pattern = patternName;
patterns[patternName].points = points;
} }
} }
console.log(Object.keys(patterns)); let points = Object.values(patterns).map(pattern => pattern.points);
let maxPoints = Math.max(...points);
patterns = patterns.sort((a,b) => (a.last_nom > b.last_nom) ? -1 : ((b.last_nom > a.last_nom) ? 1 : 0)); let selectedPatterns = Object.values(patterns).filter(pattern => pattern.points == maxPoints);
return Object.keys(patterns); return selectedPatterns;
} }
function updateUI() { function getHandPoints(hand = null) {
let patterns = getPatterns(hand);
if (patterns.length > 0) {
return patterns[0].points;
}
return 0;
}
function recommendOfDepth(depth = 4) {
var t0 = performance.now();
recommendations = [];
recommendThrowaway(depth);
let current = getHandPoints();
if (current > 0) {
let choice = {
burnedCard: "CASH OUT",
points: current,
chance: 1
};
recommendations.unshift(choice);
}
var t1 = performance.now();
console.log("Took " + (t1 - t0) + " milliseconds.");
}
function recommendThrowaway(depth = 0, thisHand = null, thisDeck = null, chance = 1, startingFunction = true) {
if (thisHand == null) {
thisHand = hand;
}
if (thisDeck == null) {
thisDeck = deck;
}
if (Object.keys(thisHand).length < 1) {
return [0, "nothing", 0];
}
let decks = [];
let hands = [];
let choices = [];
let loopDuration = Math.min(handSize, thisHand.length);
for (let i = 0; i < loopDuration; i++) {
for (let deckIndex = 0; deckIndex < thisDeck.length; deckIndex++) {
let newHand = [...thisHand];
let newDeck = [...thisDeck];
choices.push({ burnedCard: newHand[i], index: i });
newHand.splice(i, 1);
newHand.push(newDeck[deckIndex]);
newDeck.splice(deckIndex, 1);
hands.push(newHand);
decks.push(newDeck);
}
}
if (startingFunction) {
console.log(choices);
for (let choice of choices) {
let points = getHandPoints(hands[choice.index]);
if (points > 0) {
choice.points = points;
let thisChance = 1;
if (thisDeck.length > 0) {
thisChance = chance * (1/thisDeck.length);
}
choice.chance = thisChance
recommendations.push(choice);
}
}
}
}
function getRecommendationText(rec) {
if (Object.keys(allCards).includes(rec.burnedCard)) {
return "Burn <b><u>" +
rec.burnedCard.replace("_", " ") +
"</u></b> and earn potentially " +
(rec.points * pointsMultiplier) +
" points. (" +
(rec.chance * 100).toFixed(2) +
"% chance)";
}
return "<b><u>Cash out</u></b> and get " +
(rec.points * pointsMultiplier) +
" points instantly!";
}
function updateUI(skipAuto = false) {
if (autoRecommend && !skipAuto) {
recommend();
}
recommendationArray = [];
for (let recommendation of recommendations) {
recommendationArray.push(getRecommendationText(recommendation));
}
recommendationArray = [...new Set(recommendationArray)];
recommendElement.innerHTML = recommendationArray.join("<br>");
handElement.innerHTML = ""; handElement.innerHTML = "";
for (let card of Object.values(hand)) { for (let cardId of hand) {
let card = idToCard(cardId);
handElement.innerHTML += cardToElement(card); handElement.innerHTML += cardToElement(card);
} }
deckElement.innerHTML = ""; deckElement.innerHTML = "";
for (let card of Object.values(deck)) { for (let cardId of Object.keys(allCards)) {
let card = idToCard(cardId);
if (deck.includes(cardId)) {
deckElement.innerHTML += cardToElement(card, true); deckElement.innerHTML += cardToElement(card, true);
} }
else {
deckElement.innerHTML += cardToElement(card, true, true);
}
}
} }
function reset() { function reset() {
deck = {}; allCards = {};
hand = {}; deck = [];
hand = [];
recommendations = [];
generateCards();
fillDeck(); fillDeck();
if (autoDraw) {
fillHand();
}
updateUI(); updateUI();
} }