Published
Edited
Jul 23, 2022
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
handChecker("5m-5m-5m-6p-6p-4s-5s-6s-7s-8s-9s-6z-6z-6z")
Insert cell
handChecker("1m-1m-3m-4m-5m-1p-1p-1p-5s-7s-8s-9s-1z-2z")
Insert cell
handChecker("3m-3m-4m-4m-5m-5m-6m-6m-3p-5p-9s-9s-9s-4p")
Insert cell
handChecker("7m-7m-1p-2s-3s-6s-7s-8s-5z-6z-6z-7z-7z-4s")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
handChecker("1m-1m-2m-2m-2p-2p-7p-7p-1z-1z-5z-5z-7z-7z")
Insert cell
Insert cell
handChecker("1m-9m-1p-9p-1s-9s-1z-2z-3z-4z-5z-6z-7z-9p")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vec = handStrToVec(exHand)
Insert cell
printVec(vec)
Insert cell
Insert cell
Insert cell
Insert cell
printVec(allActions[48].vec)
Insert cell
Insert cell
printVec(allActions[32].vec)
Insert cell
Insert cell
applyAction = (vec, action) => {
return vec.map((m, i) => m - action.vec[i]);
}
Insert cell
Insert cell
printVec(applyAction(vec, allActions[48]))
Insert cell
Insert cell
mh(vecToHandStr(applyAction(vec, allActions[48])))
Insert cell
Insert cell
possibleActions = (vec, actions) => {
return actions.filter((action) => {
const s = applyAction(vec, action);
const x = s.filter((m) => m < 0).length;
return x === 0;
});
}
Insert cell
Insert cell
availableActions = possibleActions(vec, allActions)
Insert cell
printActions(availableActions)
Insert cell
Insert cell
mh(vecToHandStr(applyAction(vec,availableActions[0])))
Insert cell
mh(vecToHandStr(applyAction(vec,availableActions[1])))
Insert cell
mh(vecToHandStr(applyAction(vec,availableActions[2])))
Insert cell
Insert cell
Insert cell
availableHeadActions = possibleActions(vec, headActions)
Insert cell
printActions(availableHeadActions, 8)
Insert cell
Insert cell
mh(vecToHandStr(applyAction(vec,availableHeadActions[0])))
Insert cell
mh(vecToHandStr(applyAction(vec,availableHeadActions[1])))
Insert cell
mh(vecToHandStr(applyAction(vec,availableHeadActions[2])))
Insert cell
mh(vecToHandStr(applyAction(vec, availableHeadActions[3])))
Insert cell
Insert cell
Insert cell
function checkHandValid(vec, actions, headActions, appliedActions = []) {
/** No more tiles to process. We‘ve got a valid hand! **/
if (isVecEmpty(vec)) {
return [true, appliedActions];
}

const availActions = possibleActions(vec, actions);
const availHeadActions =
headActions != null ? possibleActions(vec, headActions) : [];

for (let i = 0; i < availActions.length; i++) {
const action = availActions[i];
const aplVec = applyAction(vec, action);
const [res, resAppliedActions] = checkHandValid(
aplVec,
actions,
headActions,
[...appliedActions, action]
);
if (res) {
return [res, resAppliedActions];
}
}

// TODO: we could probably reduce the code repetition.

for (let i = 0; i < availHeadActions.length; i++) {
const action = availHeadActions[i];
const aplVec = applyAction(vec, action);
/**
* When we check the next level, we no longer pass `headActions`
* since once it is applied, we can no longer check it.
*/
const [res, resAppliedActions] = checkHandValid(aplVec, actions, null, [
...appliedActions,
action
]);
if (res) {
return [res, resAppliedActions];
}
}

return [false, appliedActions];
}
Insert cell
Insert cell
exResult = checkHandValid(vec, allActions, headActions)
Insert cell
exResult[0] ? md`### This is a valid hand!` : md`### This is NOT a valid hand.`
Insert cell
Insert cell
exResult[0] ? printActions(exResult[1]) : null
Insert cell
checkStandard = (vec) => checkHandValid(vec, allActions, headActions);
Insert cell
checkThirteenOrphans = (vec) => checkHandValid(vec, thirteenOrphanActions, thirteenOrphanHeadActions);
Insert cell
checkSevenPairs = (vec) => checkHandValid(vec, sevenPairsActions)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
printActions(allActions)
Insert cell
Insert cell
Insert cell
Insert cell
printActions(headActions)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
printActions(thirteenOrphanActions)
Insert cell
Insert cell
printActions(thirteenOrphanHeadActions)
Insert cell
Insert cell
Insert cell
Insert cell
printActions(sevenPairsActions)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more