function validateForth(input) 346 { 347 let vals = []; 348 let array = []; 349 if (input.length < 8) 350 { 351 console.log("Not long enough"); 352 $("#forthText").text("Input must be at least 8 characters long."); 353 return false; 354 } 355 //Find all question marks first 356 console.log("Finding question marks in " + input); 357 let found = false; 358 for (let i = 0; i < input.length; ++i) 359 { 360 if (input[i] === '?') 361 { 362 found = true; 363 console.log("Question mark found at " + i); 364 vals.push(i); 365 array.push('r'); 366 console.log("vals now equals"); 367 console.log(vals); 368 console.log("array now equals"); 369 console.log(array); 370 } 371 else 372 { 373 console.log("Adding " + input[i]); 374 array.push(input[i]); 375 console.log("array now equals"); 376 console.log(array); 377 } 378 } 379 if (!found) 380 { 381 console.log("No question marks found."); 382 $("#forthText").text("Input must include at least one question mark."); 383 return false; 384 } 385 let total = Math.pow(4, vals.length); 386 for (let i = 0; i < total; ++i) 387 { 388 if (increase(array, vals)) 389 { 390 return true; 391 } 392 } 393 $("#forthText").text("Not possible."); 394 return false; 395 } 396 397 function testMaize(input) 398 { 399 let x = 0, y = 0, cells = document.getElementById("maize").getElementsByTagName("td"); 400 fillMaize("White"); 401 cells[0].style.backgroundColor = "Red"; 402 let r = 255; 403 let g = 0; 404 let diff = 255 / input.length; 405 for (let i = 0; i < input.length; ++i) 406 { 407 switch (input[i]) 408 { 409 case 'r': 410 ++x; 411 break; 412 case 'l': 413 --x; 414 break; 415 case 'u': 416 ++y; 417 break; 418 case 'd': 419 --y; 420 break; 421 default: 422 return false; 423 } 424 if ((x > 4) || (x < 0) || (y > 0) || (y < -4)) 425 { 426 return false; 427 } 428 if (cells[x + (5 * Math.abs(y))].style.backgroundColor !== "white") 429 { 430 return false; 431 } 432 r -= diff; 433 g += diff; 434 cells[x + (5 * Math.abs(y))].style.backgroundColor = "rgb(" + r + "," + g + "," + 0 + ")"; 435 } 436 return (x === 4) && (y === -4); 437 } 438 439 function increase(array, vals) 440 { 441 switch (array[vals[0]]) 442 { 443 case 'r': 444 array[vals[0]] = 'l'; 445 break; 446 case 'l': 447 array[vals[0]] = 'u'; 448 break; 449 case 'u': 450 array[vals[0]] = 'd'; 451 break; 452 case 'd': 453 array[vals[0]] = 'r'; 454 break; 455 } 456 if ((array[vals[0]] === 'r') && (vals.length > 1)) 457 { 458 increase(array, vals.slice(1)) 459 } 460 return testMaize(array); 461 }