### Mastermind ðŸŽ°

How about a game of Bulls and cows?
en.wikipedia.org/wiki/bulls_and_cows

#game

```const codeOptions = 6; //min=4 max=16 step=1 (0 to 3, 0 to 4, 0 1 2 3 4 and 5, 0 to 6, 0 to 7, 0 to 8, 0 to 9, 0 to A, 0 to B, 0 to C, 0 to D, 0 to E, 0 to F)
const codeLength = 4; //min=3 max=6 step=1
const guess = ''; //type=string

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);

const guesses = [guess.split(' ')].map(g => Array.from({length: 10}).map((e,i) => g[i] || '')).pop().map(g => g.toUpperCase().trim());
//const guesses = [guess1, guess2, guess3, guess4, guess5, guess6, guess7, guess8, guess9, guess10].map(v => v.trim());

const seed = (new Date().toDateString())+' '+codeOptions+' '+codeLength;
// Seedable random number generator by David Bau: http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html
!function(a,b,c,d,e,f,g,h,i){function j(a){var b,c=a.length,e=this,f=0,g=e.i=e.j=0,h=e.S=[];for(c||(a=[c++]);d>f;)h[f]=f++;for(f=0;d>f;f++)h[f]=h[g=s&g+a[f%c]+(b=h[f])],h[g]=b;(e.g=function(a){for(var b,c=0,f=e.i,g=e.j,h=e.S;a--;)b=h[f=s&f+1],c=c*d+h[s&(h[f]=h[g=s&g+b])+(h[g]=b)];return e.i=f,e.j=g,c})(d)}function k(a,b){var c,d=[],e=typeof a;if(b&&"object"==e)for(c in a)try{d.push(k(a[c],b-1))}catch(f){}return d.length?d:"string"==e?a:a+"\0"}function l(a,b){for(var c,d=a+"",e=0;e<d.length;)b[s&e]=s&(c^=19*b[s&e])+d.charCodeAt(e++);return n(b)}function m(c){try{return o?n(o.randomBytes(d)):(a.crypto.getRandomValues(c=new Uint8Array(d)),n(c))}catch(e){return[+new Date,a,(c=a.navigator)&&c.plugins,a.screen,n(b)]}}function n(a){return String.fromCharCode.apply(0,a)}var o,p=c.pow(d,e),q=c.pow(2,f),r=2*q,s=d-1,t=c["seed"+i]=function(a,f,g){var h=[];f=1==f?{entropy:!0}:f||{};var o=l(k(f.entropy?[a,n(b)]:null==a?m():a,3),h),s=new j(h);return l(n(s.S),b),(f.pass||g||function(a,b,d){return d?(c[i]=a,b):a})(function(){for(var a=s.g(e),b=p,c=0;q>a;)a=(a+c)*d,b*=d,c=s.g(1);for(;a>=r;)a/=2,b/=2,c>>>=1;return(a+c)/b},o,"global"in f?f.global:this==c)};if(l(c[i](),b),g&&g.exports){g.exports=t;try{o=require("crypto")}catch(u){}}else h&&h.amd&&h(function(){return t})}(this,[],Math,256,6,52,"object"==typeof module&&module,"function"==typeof define&&define,"random");
Math.seedrandom(''+seed);
// Add a seed in seedrandom, then Math.random will use this seed

// Global code will be evaluated once.
init();
const turtle = new Turtle();
const polygons = new Polygons();

const fonts = {
};

const logoPaths = [ //Paths converted to absolute SVG with InkScape from https://www.svgrepo.com/svg/482779/brain-illustration-12
'M 410.34,153.043 C 409.062,151.75 407.628,150.566 406.052,149.516 L 404.437,149.179 C 403.591,148.811 402.917,148.207 402.477,147.477 399.953,146.286 397.311,145.518 394.623,145.134 392.302,144.812 390.672,142.657 391.009,140.32 391.322,137.984 393.493,136.354 395.814,136.675 398.472,137.067 401.136,137.742 403.692,138.745 404.107,136.879 404.342,134.974 404.342,133.054 404.334,128.852 403.347,124.611 401.246,120.621 398.847,116.059 395.328,112.5 391.212,110.071 387.606,107.939 383.53,106.669 379.344,106.371 379.039,110.188 378.13,113.99 376.546,117.674 375.605,119.837 373.105,120.833 370.934,119.9 368.77,118.975 367.767,116.467 368.7,114.303 370.22,110.753 370.934,107.1 370.95,103.478 L 370.895,101.934 C 370.605,96.917 368.943,92.073 366.113,87.942 363.283,83.818 359.317,80.44 354.379,78.3 350.836,76.787 347.167,76.074 343.553,76.074 336.631,76.074 329.929,78.74 324.866,83.442 325.814,86.664 326.355,90.058 326.355,93.586 326.355,95.938 324.427,97.85 322.075,97.85 319.723,97.85 317.803,95.938 317.803,93.586 317.803,90.129 317.168,86.852 316.008,83.803 H 316.016 C 314.048,78.669 310.568,74.248 306.108,71.128 301.663,68.009 296.278,66.182 290.415,66.182 285.328,66.182 280.585,67.57 276.509,69.968 273.248,71.896 270.434,74.499 268.224,77.556 272.543,83.474 275.098,90.788 275.098,98.674 275.098,101.025 273.178,102.93 270.818,102.93 268.466,102.93 266.546,101.025 266.546,98.674 266.546,91.745 263.998,85.442 259.758,80.621 254.726,74.875 247.381,71.278 239.15,71.278 233.436,71.278 228.176,73.01 223.778,75.989 219.396,78.968 215.908,83.185 213.846,88.139 212.514,91.376 211.753,94.943 211.753,98.674 211.753,101.025 209.848,102.93 207.489,102.93 205.129,102.93 203.225,101.025 203.225,98.674 203.225,95.209 203.727,91.87 204.636,88.687 201.422,87.386 197.942,86.665 194.368,86.665 192.338,86.665 190.3,86.9 188.23,87.363 182.147,88.766 177.06,92.043 173.336,96.479 170.655,99.678 168.742,103.479 167.747,107.563 176.566,107.719 185.376,111.129 192.181,117.792 193.859,119.446 193.89,122.143 192.244,123.82 190.59,125.513 187.893,125.529 186.208,123.89 180.862,118.678 173.972,116.067 167.042,116.059 165.804,116.059 164.565,116.145 163.326,116.302 H 163.311 163.303 C 157.495,117.109 151.898,119.758 147.453,124.29 142.232,129.62 139.645,136.503 139.638,143.448 139.638,147.407 140.5,151.396 142.224,155.065 143.564,157.957 145.446,160.654 147.876,163.021 V 163.028 C 150.62,165.725 153.787,167.716 157.166,169.017 159.352,169.872 160.458,172.341 159.596,174.535 158.742,176.746 156.28,177.844 154.078,176.981 149.665,175.272 145.495,172.655 141.888,169.119 140.313,167.567 138.909,165.913 137.664,164.158 134.442,168.775 132.694,174.3 132.694,179.929 132.694,184.288 133.737,188.685 135.939,192.816 138.416,197.441 141.999,201.047 146.216,203.508 149.547,205.46 153.271,206.691 157.112,207.122 L 157.104,206.863 C 157.104,204.605 157.316,202.395 157.7,200.247 158.139,197.927 160.373,196.413 162.678,196.829 164.998,197.26 166.527,199.486 166.111,201.806 165.805,203.444 165.641,205.145 165.641,206.862 L 165.711,208.846 165.938,210.845 V 210.868 L 165.946,210.907 C 167.906,224.296 179.476,234.305 193.021,234.305 L 194.989,234.227 H 194.997 C 201.001,233.796 206.394,231.475 210.674,227.862 213.676,225.33 216.137,222.156 217.823,218.58 218.818,216.44 221.381,215.532 223.506,216.542 225.638,217.546 226.548,220.085 225.544,222.218 223.718,226.067 221.225,229.539 218.231,232.494 219.908,237.652 223.059,242.081 227.175,245.373 231.879,249.144 237.812,251.37 244.224,251.37 L 246.2,251.292 H 246.208 C 250.268,251.002 254.023,249.834 257.355,248.015 256.837,245.562 256.54,243.054 256.54,240.514 256.54,235.277 257.708,229.939 260.122,224.922 261.149,222.798 263.696,221.912 265.813,222.939 267.937,223.95 268.839,226.506 267.812,228.63 265.954,232.479 265.084,236.524 265.084,240.514 265.084,243.304 265.515,246.087 266.346,248.728 H 266.354 C 268.541,255.697 273.48,261.788 280.589,265.205 284.43,267.063 288.475,267.933 292.457,267.933 298.783,267.933 304.968,265.722 309.875,261.717 L 309.891,261.709 C 312.87,259.255 315.363,256.151 317.157,252.443 L 317.165,252.427 C 318.835,248.963 319.713,245.334 319.861,241.712 319.971,239.36 321.954,237.526 324.321,237.636 326.673,237.738 328.491,239.728 328.389,242.08 328.185,246.83 327.033,251.627 324.846,256.15 V 256.142 C 323.09,259.803 320.778,263.025 318.066,265.776 323.098,273.003 331.47,277.558 340.524,277.55 342.201,277.55 343.91,277.394 345.627,277.064 352.219,275.826 357.777,272.337 361.728,267.54 365.671,262.734 367.968,256.628 367.968,250.184 L 367.96,249.605 V 249.573 C 367.929,248.123 367.78,246.603 367.498,245.073 366.816,241.444 365.428,238.151 363.524,235.243 360.765,232.813 357.7,229.639 354.808,226.111 351.727,222.349 348.89,218.233 346.985,214.087 345.731,211.312 344.837,208.506 344.821,205.551 344.821,204.242 345.001,202.886 345.495,201.585 345.973,200.26 346.757,199.006 347.807,197.971 349.469,196.317 352.165,196.317 353.835,197.971 355.497,199.648 355.497,202.36 353.835,204.015 L 353.513,204.525 353.349,205.552 C 353.333,206.626 353.78,208.445 354.744,210.537 355.7,212.623 357.119,214.966 358.789,217.278 362.097,221.911 366.385,226.458 369.614,229.232 L 370.053,229.609 370.375,230.087 C 373.017,234.022 374.953,238.553 375.885,243.499 V 243.507 L 376.191,245.623 377.469,245.678 C 382.878,245.67 388.325,244.071 393.115,240.708 396.925,238.027 399.825,234.586 401.785,230.721 403.752,226.849 404.779,222.569 404.779,218.258 404.779,214.221 403.869,210.168 402.051,206.398 400.365,206.939 398.656,207.37 396.924,207.668 394.604,208.083 392.401,206.507 391.986,204.188 391.593,201.86 393.162,199.665 395.482,199.257 403.038,197.956 409.96,193.551 414.24,186.48 416.952,182.036 418.23,177.144 418.23,172.308 418.23,165.182 415.478,158.214 410.352,153.048 Z M 214.788,120.481 C 218.864,115.66 224.273,112.234 230.089,109.992 235.929,107.75 242.184,106.676 248.189,106.676 254.029,106.684 259.641,107.672 264.407,109.883 266.539,110.854 267.472,113.402 266.484,115.542 265.504,117.675 262.972,118.615 260.817,117.628 257.525,116.084 253.017,115.198 248.189,115.198 243.219,115.198 237.921,116.114 233.162,117.965 228.404,119.791 224.226,122.519 221.286,125.984 219.781,127.787 217.077,128.022 215.274,126.486 213.479,124.965 213.252,122.276 214.788,120.481 Z M 311.925,221.396 C 306.963,221.396 302.463,220.832 298.356,219.884 298.466,221.059 298.544,222.368 298.544,223.756 298.544,226.539 298.238,229.72 297.368,233.06 296.49,236.391 295.009,239.895 292.673,243.258 291.333,245.202 288.675,245.68 286.739,244.339 284.795,242.998 284.325,240.349 285.649,238.397 287.373,235.912 288.447,233.372 289.106,230.872 289.765,228.372 290.008,225.941 290.008,223.755 290.016,220.737 289.553,218.252 289.224,216.833 287.139,215.892 285.218,214.834 283.424,213.65 276.275,209.025 271.494,202.919 268.491,197.11 266.477,193.214 265.253,189.452 264.595,186.183 264.219,184.317 264.031,182.625 264.031,181.087 264.031,178.72 265.944,176.807 268.303,176.807 270.662,176.807 272.575,178.72 272.575,181.087 272.567,182.325 272.834,184.403 273.508,186.763 274.182,189.13 275.256,191.819 276.808,194.523 279.92,199.963 284.866,205.411 292.501,208.947 297.604,211.314 303.953,212.85 311.925,212.85 314.284,212.85 316.197,214.763 316.197,217.122 316.197,219.484 314.285,221.396 311.925,221.396 Z M 341.979,190.229 C 341.101,192.409 338.616,193.475 336.421,192.604 334.234,191.726 333.184,189.257 334.046,187.07 V 187.062 L 334.07,186.999 334.196,186.623 334.635,185.087 C 334.957,183.739 335.286,181.834 335.286,179.741 L 335.278,179.443 C 329.415,181.434 323.975,182.327 318.966,182.327 311.394,182.343 304.841,180.266 299.51,177.115 294.172,173.965 290.018,169.794 286.913,165.553 283.386,160.748 278.761,156.437 273.462,153.388 268.148,150.33 262.206,148.512 255.896,148.496 250.37,148.512 244.491,149.891 238.353,153.262 232.529,156.46 228.782,160.716 226.36,165.388 223.954,170.068 222.935,175.178 222.942,179.835 222.926,184.146 223.836,188.049 225.019,190.511 226.03,192.643 225.113,195.183 222.989,196.194 220.865,197.206 218.317,196.288 217.306,194.164 215.448,190.228 214.414,185.274 214.398,179.835 214.406,174.426 215.48,168.515 218.122,162.864 212.824,164.064 207.901,164.597 203.417,164.597 200.995,164.597 198.713,164.44 196.542,164.158 L 195.75,165.757 195.742,165.772 195.679,165.836 195.452,166.126 194.542,167.364 C 193.766,168.478 192.692,170.1 191.524,172.169 189.196,176.269 186.531,182.054 185.237,188.551 184.775,190.864 182.533,192.377 180.22,191.906 177.908,191.444 176.402,189.202 176.881,186.874 178.417,179.168 181.452,172.615 184.093,167.951 185.472,165.537 186.735,163.625 187.659,162.323 184.179,161.281 181.169,160.003 178.708,158.701 173.088,155.714 170.163,152.72 169.913,152.477 168.282,150.776 168.329,148.079 170.022,146.441 171.723,144.811 174.412,144.858 176.05,146.535 L 176.098,146.582 176.348,146.825 177.571,147.851 C 178.683,148.729 180.401,149.937 182.697,151.159 187.306,153.589 194.189,156.051 203.415,156.067 209.897,156.067 217.54,154.829 226.421,151.411 228.671,149.318 231.265,147.406 234.244,145.775 241.55,141.77 248.91,139.967 255.894,139.967 263.874,139.967 271.297,142.288 277.725,145.987 284.153,149.703 289.601,154.79 293.794,160.512 296.342,163.968 299.689,167.316 303.836,169.753 308.006,172.199 312.936,173.791 318.964,173.798 323.095,173.798 327.751,173.03 333.019,171.18 331.734,169.118 329.821,167.127 326.67,165.324 324.616,164.156 323.887,161.554 325.047,159.508 326.215,157.446 328.81,156.74 330.863,157.885 335.911,160.731 339.29,164.501 341.249,168.444 343.232,172.371 343.82,176.353 343.82,179.74 343.797,185.652 342.096,189.924 341.979,190.229 Z M 340.38,137.938 C 330.409,137.938 321.685,135.249 314.63,131.181 313.635,132.561 312.302,134.293 310.585,136.174 306.721,140.415 301.045,145.393 293.45,148.12 291.216,148.904 288.786,147.76 287.994,145.534 287.202,143.307 288.37,140.87 290.573,140.086 296.178,138.087 300.865,134.16 304.111,130.609 305.616,128.986 306.784,127.45 307.631,126.274 303.421,122.716 300.098,118.663 297.817,114.493 295.654,110.495 294.399,106.372 294.384,102.366 294.384,100.015 296.297,98.102 298.656,98.102 301.008,98.102 302.92,100.015 302.92,102.366 302.904,104.522 303.657,107.399 305.318,110.417 306.956,113.442 309.449,116.593 312.71,119.416 319.232,125.068 328.646,129.394 340.381,129.394 342.74,129.394 344.645,131.315 344.645,133.674 344.644,136.025 342.739,137.938 340.38,137.938 Z M 374.023,198.037 C 372.526,199.84 369.829,200.106 368.011,198.593 366.193,197.096 365.942,194.4 367.431,192.589 372.087,186.937 373.804,181.246 373.819,175.799 373.835,170.837 372.314,166.047 369.931,162.011 367.579,157.981 364.327,154.752 361.199,152.957 359.098,151.75 357.123,151.209 355.555,151.209 353.195,151.209 351.291,149.305 351.291,146.945 351.291,144.586 353.196,142.673 355.555,142.673 358.149,142.673 360.689,143.308 363.104,144.374 365.51,145.425 367.784,146.922 369.9,148.749 374.125,152.401 377.715,157.371 379.973,163.196 381.462,167.083 382.371,171.348 382.371,175.8 382.379,183.12 379.879,190.958 374.023,198.037 Z',
'M 428.699,66.574 C 396.943,29.442 349.183,4.155 287.986,0.462 283.008,0.156 278.054,0 273.147,0 221.678,0 174.332,16.712 139.623,47.393 104.89,77.995 83.263,122.801 83.341,176.465 83.341,177.68 83.349,178.895 83.372,180.118 L 83.318,186.592 39.139,264.61 C 36.764,268.819 35.564,273.515 35.564,278.211 35.564,282.129 36.396,286.065 38.08,289.733 41.647,297.517 48.662,303.114 56.971,305.042 L 56.955,305.112 77.273,310.709 85.3,388.132 85.363,388.124 C 85.928,399.404 90.96,409.994 99.505,417.425 107.101,424.041 116.797,427.631 126.768,427.631 127.778,427.631 128.774,427.537 129.778,427.467 L 152.8,429.293 153.953,429.168 154.321,429.152 C 155.206,429.152 156.03,429.466 156.665,430.038 L 156.703,430.078 C 157.433,430.736 157.864,431.7 157.864,432.688 V 512 H 372.824 V 499.96 C 372.824,499.96 372.824,467.344 372.824,454.332 372.8,449.095 373.843,438.741 375.897,428.754 376.916,423.746 378.177,418.752 379.605,414.434 381.016,410.123 382.647,406.454 384.01,404.314 398.308,381.268 418.524,362.362 437.038,335.624 455.53,308.91 471.293,274.255 475.612,221.673 476.161,215.081 476.436,208.488 476.436,201.928 476.436,152.079 460.508,103.674 428.699,66.574 Z M 451.611,219.664 C 447.433,268.539 433.762,297.784 417.206,321.967 400.675,346.126 380.232,365.119 363.59,391.544 360.69,396.216 358.565,401.39 356.739,406.892 354.027,415.131 352.091,424.114 350.766,432.478 349.449,440.865 348.752,448.469 348.744,454.332 348.744,462.688 348.744,475.324 348.744,487.92 H 181.944 V 432.689 C 181.944,424.842 178.605,417.372 172.756,412.129 167.653,407.543 161.069,405.066 154.32,405.066 153.826,405.066 153.348,405.12 152.862,405.152 L 129.864,403.318 128.735,403.443 126.768,403.553 C 122.582,403.553 118.506,402.04 115.315,399.265 111.631,396.059 109.451,391.45 109.31,386.551 L 109.303,386.097 99.536,291.876 62.929,281.78 62.49,281.694 C 61.377,281.474 60.445,280.737 59.966,279.702 L 59.644,278.213 60.099,276.473 107.35,193.03 107.452,179.994 V 179.821 C 107.429,178.693 107.421,177.579 107.421,176.467 107.5,129.216 125.865,91.809 155.574,65.432 185.314,39.117 226.844,24.082 273.147,24.082 277.56,24.082 282.013,24.223 286.497,24.498 C 341.947,27.986 382.756,50.107 410.418,82.253 438.034,114.423 452.356,157.151 452.356,201.927 452.356,207.812 452.113,213.73 451.611,219.664'
];
// an array of [fill, Path]
const logoPathSets = logoPaths.flatMap(ps => new PathSet(ps).reverse().map((e,i,a) => [i==a.length-1, e]));

// The walk function will be called until it returns false.
function walk(i) {
if(!guesses.some(g => g.trim() != '')) {
displayLogo([0,-60], .3);

const logoText = new Text(fonts.logo);
let sizeInfo = logoText.size(turtle, 'Mastermind', 2);
turtle.jump(-sizeInfo.width/2, 0);
logoText.print(turtle, 'Mastermind', 2);

let height = sizeInfo.height;

let txt = 'A set of new games every day!';
const text = new Text(fonts.normal);
sizeInfo = text.size(turtle, txt, .8);
turtle.jump(-sizeInfo.width/2, height);
height += sizeInfo.height + 5;
text.print(turtle, txt, .8);

txt = `To start a game enter characters in 'guess'. Separate
entries with a space, e.g. '1234 5678 1278'.

To the right of each code entry, empty circles will show
for each correct character. Fully black circles are
shown for each correct character in a correct location.

You can change the setup of a game: codeOptions sets
the characters to use and codeLength defines the
number of characters in a code.`;
sizeInfo = text.size(turtle, txt, .6);
turtle.jump(-sizeInfo.width/2, height);
height += sizeInfo.height;
text.print(turtle, txt, .6);

return false;
}
try {
const mm = new MasterMind(new Text(fonts.game), codeOptions, codeLength, guesses);
if(mm.hasWon()) {
displayLogo([0,-60], .3);

const logoText = new Text(fonts.logo);
let sizeInfo = logoText.size(turtle, 'Mastermind', 2);
turtle.jump(-sizeInfo.width/2, 0);
logoText.print(turtle, 'Mastermind', 2);

let height = sizeInfo.height + 5;

let txt = 'You won!';
sizeInfo = logoText.size(turtle, txt, 2.5);
turtle.jump(-sizeInfo.width/2, height);
logoText.print(turtle, txt, 2.5);
height += sizeInfo.height + 5;

txt = 'Game id: ' + seed;
sizeInfo = logoText.size(turtle, txt, .6);
turtle.jump(-sizeInfo.width/2, 90);
logoText.print(turtle, txt, .6);
height += sizeInfo.height + 5;

mm.drawGuess(turtle, [0, 50], 25, mm.code.join(''), 1);

return false;
}
return mm.drawBoard(turtle, [0, 0], 195);
} catch(e) {
const text = new Text(fonts.normal);
turtle.jump(-95, 0);
text.print(turtle, e.message, .8);
return false;
}
}

function displayLogo(position, scale = 1) {
for(let i = 0; i < logoPathSets.length; i++) {
const [hatch, path] = logoPathSets[i];
const steps = path.length() | 0;

const p = polygons.create();
for(let j = 0; j <= steps; j++) {
}
polygons.draw(turtle, p);
}
}

function MasterMind(text, codeOptions, codeLength, guesses) {
class MasterMind {
constructor(codeOptions, codeLength, guesses) {
this.codeCharacters = '0123456789ABCDEF'.substring(0, codeOptions);
this.codeLength = codeLength;
this.guesses = guesses;
this.text = text;

this.code = Array.from({length: codeLength}, () => this.codeCharacters[Math.random() * this.codeCharacters.length | 0]);

//Uncomment the next line to cheat
//console.log(this.code);

this.calibrationSize = null;
this.calibratedDims = null;

guesses.forEach((g, gIdx) => g.split('').forEach(c => {
if(this.codeCharacters.indexOf(c) === -1) throw new Error('Invalid character input in guess '+ gIdx +'.\nThe ' + c + ' in ' + g + ' is not one of the posibilities.\nPossibilities to choose from: '+this.codeCharacters+'');
}))
}

hasWon() {
return this.guesses.some(g => this.getGuessResult(g).correctLocation == this.codeLength);
}

drawBoard(turtle, position, height, padding = 2) {
const heightPerGuess = (height - (this.guesses.length - 1) * padding) / this.guesses.length;
for(let i = 0; i < this.guesses.length; i++) {
[0, i * (heightPerGuess + padding)]
), heightPerGuess, this.guesses[i], padding / 2);
}
}

calibrateCharacterDimensions(height) {
if(this.calibratedSize === height) return;

const maxWHat1Scale = Array.from({length: this.codeCharacters.length}).map((e, i) => this.text.dimensions(1, this.codeCharacters[i])).reduce((a, c) => [Math.max(c.width, a[0]), Math.max(c.height, a[1])], [0,0]);
const useScale = height / Math.max(...maxWHat1Scale);

const dimensionsPerCharacter = Array.from({length: this.codeCharacters.length}).map((e, i) => this.text.dimensions(useScale, this.codeCharacters[i]));
this.calibratedDims = {
scale: useScale,
perCharacter: dimensionsPerCharacter,
maxWidthHeight: dimensionsPerCharacter.reduce((a, c) => [Math.max(c.width, a[0]), Math.max(c.height, a[1])], [0,0])
}
this.calibrationSize = height;
}

drawGuess(turtle, position, height, guess, padding = 1) {
this.drawRectangle(turtle, position, this.codeLength * height - (this.codeLength - 1) * padding + answerWidth, height);

this.calibrateCharacterDimensions(height);

for(let i = 0; i < this.codeLength; i++) {
this.drawRectangle(
turtle,
characterPosition,
);
if(i < guess.length) {
const cIndex = this.codeCharacters.indexOf(guess[i]);
turtle.jump(V.add(characterPosition, [this.calibratedDims.perCharacter[cIndex].width / -4, this.calibratedDims.perCharacter[cIndex].height / 4]));
this.text.print(turtle, guess[i])
}
}
turtle.jump(V.add(position, [(this.codeLength / 2 - .5) * (height - padding), height / -2 + 2 * padding]));

}

getGuessResult(guess) {
const codeClone = [...this.code];
const guessClone = guess.split('');

const correctCharLocation = guessClone.map((c, i) => codeClone[i] == c);
codeClone.forEach((c, i) => codeClone[i] = correctCharLocation[i]? '_': codeClone[i]);
guessClone.forEach((c, i) => guessClone[i] = correctCharLocation[i]? '_': guessClone[i]);
let correctChar = 0;
for(let i = 0; i < guessClone.length; i++) {
if(guessClone[i] == '_') continue;
const cIdx = codeClone.indexOf(guessClone[i]);
if(cIdx === -1) continue;
codeClone[cIdx] = '_';
correctChar++;
}

return {correctLocation: correctCharLocation.reduce((a, c) => a + (c?1:0), 0), correctCharacters: correctChar}
}

this.drawRectangle(turtle, position, height);

const result = [this.getGuessResult(guess)].map(gr => Array.from({length: this.codeLength}).map((e,i) => guess.length < this.codeLength? 0: (i < gr.correctLocation? 2: (i < gr.correctLocation + gr.correctCharacters? 1: 0)))).pop();

const pinHoles = [
[-1/4,-1/4], [0,-1/4], [1/4,-1/4],
[-1/4,   0], [0,   0], [1/4,   0],
[-1/4, 1/4], [0, 1/4], [1/4, 1/4]
];
const useHoles = [
[0, 4, 8],
[0, 2, 6, 8],
[0, 2, 4, 6, 8],
[0, 1, 2, 6, 7, 8]
];

useHoles[this.codeLength - 3].map(e => V.add(position, V.scale(pinHoles[e], height)))
.forEach((e, i) => {
const h = height * (result[i] == 0? .02: .09);
for(let j = h; j > .075; j -= .15) {
turtle.jump(e[0], e[1] - j);
turtle.circle(j);
if(result[i] < 2) break;
}
});
}

drawRectangle(turtle, position, width, height = width) {
[[-.5,-.5],[.5,-.5],[.5,.5],[-.5,.5]].map(ref => [ref[0]*width+position[0], ref[1]*height+position[1]])
.forEach((e,i,a) => {
turtle.jump(e);
turtle.goto(a[(i+1)%a.length]);
});
}
}
return new MasterMind(codeOptions, codeLength, guesses);
}

////////////////////////////////////////////////////////////////
// Text utility code. Created by Reinder Nijhoff 2024
// https://turtletoy.net/turtle/0f84fd3ae4
// Modifications by Jurgen Westerhof 2024: https://turtletoy.net/turtle/9aef87b45e
// Usage:
// const text = new Text(fontData);               // fontData can be found in https://turtletoy.net/turtle/0f84fd3ae4
// text.print(turtle, str, scale = 1);            // print the text str using location and heading of turtle
// text.bboxPrint(turtle, str, scale = 1);        // same as text.print() but also returns bounding box with absolute coordinates
// text.size(turtle, str, scale = 1)              // returns properties of text str to be printed
// text.bbox(turtle, str, scale = 1)              // returns bounding box of text str to be printed
// text.dimensions(scale = 1, standardChar = 'x') // returns properties of fontData for scale
////////////////////////////////////////////////////////////////
function Text(e){let t=e=>{let t=atob(e),s=new Int16Array(t.length/2),r={};for(let n=0;n<t.length;n+=2)s[n/2|0]=t.charCodeAt(n)|t.charCodeAt(n+1)<<8;for(let i=0;i<s.length;i++){let h=String.fromCharCode(s[i++]),o=s[i++],a=[],g=[];for(;-16384!==s[i];i++)16384===s[i]?(a.push(g),g=[]):g.push(s[i]);r[h]={x:o,d:a}}return r},s=(e,t,s)=>[Math.cos(s)*e[0]-Math.sin(s)*e[1]+t[0],Math.cos(s)*e[1]+Math.sin(s)*e[0]+t[1]],r=Object.fromEntries(Object.entries(e).map(([e,s])=>[e,"glyphs"===e?t(s):s]));class n extends Turtle{constructor(e,t){super(e,t),this.reset()}goto(e,t){super.goto(e,t);let[s,r]=this.pos();this.rangeX=[Math.min(s,this.rangeX?this.rangeX[0]:s),Math.max(s,this.rangeX?this.rangeX[1]:s)],this.rangeY=[Math.min(r,this.rangeY?this.rangeY[0]:r),Math.max(r,this.rangeY?this.rangeY[1]:r)]}reset(){let[e,t]=this.pos();this.rangeX=[e,e],this.rangeY=[t,t]}bbox(){return[[this.rangeX[0],this.rangeY[0]],[this.rangeX[1],this.rangeY[1]]]}}return new class e{print(e,t,n=1){let i=2*Math.PI/e._fullCircle,h=e&&e.isdown(),o=e?[e.x(),e.y()]:[0,0],a=e?e.h():0,g=o,l=n/r.unitsPerEm;return t.split("").map(t=>{if("\n"==t){o=g=s([0,10*n],g,a*i);return}let b=r.glyphs[t]||r.glyphs[" "],_=b.d;_.forEach((t,r)=>{e&&e.up();for(let n=0;n<t.length;n+=2)e&&e.goto(s([t[n]*l,t[n+1]*l],o,a*i)),e&&h&&e.down()}),o=s([b.x*l,0],o,a*i)}),h&&e.down(),s([0,10*n],o,a*i)}bboxPrint(e,t,s=1){let r=new n(e.pos());return r.degrees(e._fullCircle),r.seth(e.h()),r.up(),this.print(r,t,s),this.print(e,t,s),r.bbox()}size(e,t,s=1){let r=new n;return r.up(),this.print(r,t,s),{width:r.rangeX[1]-r.rangeX[0],height:r.rangeY[1]-r.rangeY[0],bbox:0===e.h()?r.bbox():this.bbox(e,t,s)}}bbox(e,t,s=1){let r=new n;return r.degrees(e._fullCircle),r.seth(e.h()),r.up(),this.print(r,t,s),r.bbox()}dimensions(e=1,t="x"){let s=new Turtle,r=this.size(s,t,e),n=this.size(s,"abcdefghijklmnopqrstuvwxyz",e),i=this.size(s,"ABCDEFGHIJKLMNOPQRSTUVWXYZ",e);return{height:r.height,width:r.width,ascenders:Math.abs(n.bbox[0][1])-r.height,descenders:Math.abs(n.bbox[1][1]),capitalHeight:Math.abs(i.bbox[0][1]),capitalDescenders:Math.abs(i.bbox[1][1])}}}}

function init() {
///////////////////////////////////////////////////////
// Vector functions - Created by Jurgen Westerhof 2024
///////////////////////////////////////////////////////
class Vector {
static add  (a,b) { return a.map((v,i)=>v+b[i]); }
static sub  (a,b) { return a.map((v,i)=>v-b[i]); }
static mul  (a,b) { return a.map((v,i)=>v*b[i]); }
static div  (a,b) { return a.map((v,i)=>v/b[i]); }
static scale(a,s) { return a.map(v=>v*s); }

static det(m)                { return m.length == 1? m[0][0]: m.length == 2 ? m[0][0]*m[1][1]-m[0][1]*m[1][0]: m[0].reduce((r,e,i) => r+(-1)**(i+2)*e*this.det(m.slice(1).map(c => c.filter((_,j) => i != j))),0); }
static angle(a)              { return Math.PI - Math.atan2(a[1], -a[0]); } //compatible with turtletoy heading
static rot2d(angle)          { return [[Math.cos(angle), -Math.sin(angle)], [Math.sin(angle), Math.cos(angle)]]; }
static rot3d(yaw,pitch,roll) { return [[Math.cos(yaw)*Math.cos(pitch), Math.cos(yaw)*Math.sin(pitch)*Math.sin(roll)-Math.sin(yaw)*Math.cos(roll), Math.cos(yaw)*Math.sin(pitch)*Math.cos(roll)+Math.sin(yaw)*Math.sin(roll)],[Math.sin(yaw)*Math.cos(pitch), Math.sin(yaw)*Math.sin(pitch)*Math.sin(roll)+Math.cos(yaw)*Math.cos(roll), Math.sin(yaw)*Math.sin(pitch)*Math.cos(roll)-Math.cos(yaw)*Math.sin(roll)],[-Math.sin(pitch), Math.cos(pitch)*Math.sin(roll), Math.cos(pitch)*Math.cos(roll)]]; }
static trans(matrix,a)       { return a.map((v,i) => a.reduce((acc, cur, ci) => acc + cur * matrix[ci][i], 0)); }
//Mirror vector a in a ray through [0,0] with direction mirror
static mirror2d(a,mirror)    { return [Math.atan2(...mirror)].map(angle => this.trans(this.rot2d(angle), this.mul([-1,1], this.trans(this.rot2d(-angle), a)))).pop(); }

static approx(a,b,p) { return this.len(this.sub(a,b)) < (p === undefined? .001: p); }
static norm  (a)     { return this.scale(a,1/this.len(a)); }
static len   (a)     { return Math.hypot(...a); }
static lenSq (a)     { return a.reduce((a,c)=>a+c**2,0); }
static lerp  (a,b,t) { return a.map((v, i) => v*(1-t) + b[i]*t); }
static dist  (a,b)   { return Math.hypot(...this.sub(a,b)); }

static dot  (a,b)   { return a.reduce((a,c,i) => a+c*b[i], 0); }
static cross(...ab) { return ab[0].map((e, i) => ab.map(v => v.filter((ee, ii) => ii != i))).map((m,i) => (i%2==0?-1:1)*this.det(m)); }
}
this.V = Vector;

class Intersection2D {
//a-start, a-direction, b-start, b-direction
//returns false on no intersection or [[intersection:x,y], scalar a-direction, scalar b-direction
static info(as, ad, bs, bd) { const d = V.sub(bs, as), det = -V.det([bd, ad]); if(det === 0) return false; const res = [V.det([d, bd]) / det, V.det([d, ad]) / det]; return [V.add(as, V.scale(ad, res[0])), ...res]; }
static ray(a, b, c, d) { return this.info(a, b, c, d); }
static segment(a,b,c,d, inclusiveStart = true, inclusiveEnd = true) { const i = this.info(a, V.sub(b, a), c, V.sub(d, c)); return i === false? false: ( (inclusiveStart? 0<=i[1] && 0<=i[2]: 0<i[1] && 0<i[2]) && (inclusiveEnd?   i[1]<=1 && i[2]<=1: i[1]<1 && i[2]<1) )?i[0]:false;}
static tour(tour, segmentStart, segmentDirection) { return tour.map((e, i, a) => [i, this.info(e, V.sub(a[(i+1)%a.length], e), segmentStart, segmentDirection)]).filter(e => e[1] !== false && 0 <= e[1][1] && e[1][1] <= 1).filter(e => 0 <= e[1][2]).map(e => ({position: e[1][0],tourIndex: e[0],tourSegmentPortion: e[1][1],segmentPortion: e[1][2],}));}
static inside(tour, pt) { return tour.map((e,i,a) => this.segment(e, a[(i+1)%a.length], pt, [Number.MAX_SAFE_INTEGER, 0], true, false)).filter(e => e !== false).length % 2 == 1; }
}
this.Intersection = Intersection2D;

class PathTools {
static bezier(p1, cp1, cp2, p2, steps = null) {steps = (steps === null? (V.len(V.sub(cp1, p1)) + V.len(V.sub(cp2, cp1)) + V.len(V.sub(p2, cp2))) | 0: steps) - 1;return Array.from({length: steps + 1}).map((v, i, a, f = i/steps) => [[V.lerp(p1, cp1, f),V.lerp(cp1, cp2, f),V.lerp(cp2, p2, f)]].map(v => V.lerp(V.lerp(v[0], v[1], f), V.lerp(v[1], v[2], f), f))[0]);}
// https://stackoverflow.com/questions/18655135/divide-bezier-curve-into-two-equal-halves#18681336
static splitBezier(p1, cp1, cp2, p2, t=.5) {const e = V.lerp(p1, cp1, t);const f = V.lerp(cp1, cp2, t);const g = V.lerp(cp2, p2, t);const h = V.lerp(e, f, t);const j = V.lerp(f, g, t);const k = V.lerp(h, j, t);return [[p1, e, h, k], [k, j, g, p2]];}
static circle(r){return this.circular(r,Math.max(12, r*2*Math.PI|0));}
static arc(radius, extend = 2 * Math.PI, clockWiseStart = 0, steps = null, includeLast = false) { return [steps == null? (radius*extend+1)|0: steps].map(steps => Array.from({length: steps}).map((v, i, a) => [radius * Math.cos(clockWiseStart + extend*i/(a.length-(includeLast?1:0))), radius * Math.sin(clockWiseStart + extend*i/(a.length-(includeLast?1:0)))])).pop(); }
static draw(turtle, path) {path.forEach((pt, i) => turtle[i==0?'jump':'goto'](pt));}
static drawTour(turtle, path) {this.draw(turtle, path.concat([path[0]]));}
static drawPoint(turtle, pt, r = .1) {this.drawTour(turtle, this.circle(r).map(e => V.add(e, pt)));}
}

this.PT = PathTools;

class Complex {
static sub(a,b)     { return V.sub(a,b); }
static scale(a,s)   { return V.scale(a,s); }
static mult(a,b)    { return [a[0]*b[0]-a[1]*b[1],a[0]*b[1]+a[1]*b[0]]; }
static sqrt(a)      { return [[Math.hypot(...a)**.5, Math.atan2(...a.reverse()) / 2]].map(ra => [ra[0]*Math.cos(ra[1]), ra[0]*Math.sin(ra[1])]).pop(); }
}
this.C = Complex;

class Numbers {
static approx(a,b,p)        { return Math.abs(a-b) < (p === undefined? .001: p); }
static clamp(a, min, max)   { return Math.min(Math.max(a, min), max); }
}
this.N = Numbers;
}

function PathSet(svg) {
return svg.split(/z/gi).map(s => (s.trim() == '')? false: new Path(s)).filter(p => p);
}

////////////////////////////////////////////////////////////////
// Path utility code. Created by Reinder Nijhoff 2023
// Parses a single SVG path (only M, C and L statements are
// supported). The p-method will return
// [...position, ...derivative] for a normalized point t.
//
//
// Modifications by Jurgen Westerhof 2024 (support for InkScape absolute export)
// Added support for H and V statements
// Added support for multiple sets of parameter(s) for one statement
////////////////////////////////////////////////////////////////
function Path(svg) {
class MoveTo {
constructor(p) { this.p0 = p; }
p(t, s) { return [...this.p0, 1, 0]; }
length() { return 0; }
}
class LineTo {
constructor(p0, p1) { this.p0 = p0, this.p1 = p1; }
p(t, s = 1) {
const nt = 1 - t, p0 = this.p0, p1 = this.p1;
return [
nt*p0[0] + t*p1[0],
nt*p0[1] + t*p1[1],
(p1[0] - p0[0]) * s,
(p1[1] - p0[1]) * s,
];
}
length() {
const p0 = this.p0, p1 = this.p1;
return Math.hypot(p0[0]-p1[0], p0[1]-p1[1]);
}
}
class BezierTo {
constructor(p0, c0, c1, p1) { this.p0 = p0, this.c0 = c0, this.c1 = c1, this.p1 = p1; }
p(t, s = 1) {
const nt = 1 - t, p0 = this.p0, c0 = this.c0, c1 = this.c1, p1 = this.p1;
return [
nt*nt*nt*p0[0] + 3*t*nt*nt*c0[0] + 3*t*t*nt*c1[0] + t*t*t*p1[0],
nt*nt*nt*p0[1] + 3*t*nt*nt*c0[1] + 3*t*t*nt*c1[1] + t*t*t*p1[1],
(3*nt*nt*(c0[0]-p0[0]) + 6*t*nt*(c1[0]-c0[0]) + 3*t*t*(p1[0]-c1[0])) * s,
(3*nt*nt*(c0[1]-p0[1]) + 6*t*nt*(c1[1]-c0[1]) + 3*t*t*(p1[1]-c1[1])) * s,
];
}
length() {
return this._length || (
this._length = Array.from({length:25}, (x, i) => this.p(i/25)).reduce(
(a,c,i,v) => i > 0 ? a + Math.hypot(c[0]-v[i-1][0], c[1]-v[i-1][1]) : a, 0));
}
}
class Path {
constructor(svg) {
this.segments = [];
this.parsePath(svg);
}
parsePath(svg) {
const knownStatements = 'MLCHV';
const t = svg.match(/([0-9.-]+|[MLCHV])/g); // Turtletoy does not support 'new Regex()'. If it did, MLCHV would not have to be repeated here
let st = t[0];
for (let s, i=0; i<t.length;) {
const a = t[i++];
if(knownStatements.indexOf(a) > -1) st = a; else i--;
switch (st) {
break;
break;
case 'C': this.add(new BezierTo(s, [t[i++],t[i++]], [t[i++],t[i++]], s=[t[i++],t[i++]]));
break;
break;
break;
default:  i++;
}
}
}
this.segments.push(segment);
this._length = 0;
}
length() {
return this._length || (this._length = this.segments.reduce((a,c) => a + c.length(), 0));
}
p(t) {
t = Math.max(Math.min(t, 1), 0) * this.length();
for (let l=0, i=0, sl=0; i<this.segments.length; i++, l+=sl) {
sl = this.segments[i].length();
if (t > l && t <= l + sl) {
return this.segments[i].p((t-l)/sl, sl/this.length());
}
}
return this.segments[Math.min(1, this.segments.length-1)].p(0);
}
}
return new Path(svg);
}

////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// (Polygon binning by Lionel Lemarie 2021) https://turtletoy.net/turtle/95f33bd383
// (Delegated Hatching by Jurgen Westerhof 2024) https://turtletoy.net/turtle/d068ad6040
// (Deferred Polygon Drawing by Jurgen Westerhof 2024) https://turtletoy.net/turtle/6f3d2bc0b5
// https://turtletoy.net/turtle/a5befa1f8d
//
// const polygons = new Polygons();
// const p = polygons.create();
// polygons.draw(turtle, p);
// polygons.list();
// polygons.startDeferSession();
// polygons.stopDeferring();
// polygons.finalizeDeferSession(turtle);
//