Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. var quicklist = ['TRUTH', 'TOOLS','TWIST', 'TOPAZ', 'TRICK', 'TRIES', 'TRACK', 'TRIPS', 'TWINS', 'TOUCH','TRUCK', 'TRUST', 'TELLS', 'TARTS', 'THINK', 'TROPE', 'TIPSY', 'TOWER']; // word list
  2. var input = document.getElementById('guess'); // the input box
  3. var button = document.getElementById('button'); // the button
  4. var guess;
  5.  
  6. // change css class
  7. var changeClass = function(cng, old, newClass){
  8. cng.className = cng.className.replace(old, newClass);
  9. }
  10.  
  11. // game loop
  12. var gameloop = function(){
  13. // pick a random word
  14. var rand = quicklist[Math.floor(Math.random() * quicklist.length)];
  15. var hasDuplicates = (/([a-zA-Z]).*?\1/).test(rand); // if multiple insances of a letter in the word
  16.  
  17. var pressn = 1; // turn number
  18.  
  19. // get all indexes of a given value in an array
  20. var getAllIndexes = function(arr, val) {
  21. var indexes = [], i;
  22. for(i = 0; i < arr.length; i++)
  23. if (arr[i] === val)
  24. indexes.push(i);
  25. return indexes;
  26. }
  27.  
  28. // give first letter
  29. document.getElementById("row1").firstElementChild.innerHTML=rand[0];
  30.  
  31. // guess event
  32. input.onkeypress = function(event) {
  33. if (event.key == "Enter" || event.keyCode == 13) {
  34. document.getElementById('smallMsg').innerHTML = "Green = correct letter, Yellow = wrong place"; // reset message
  35. guess = input.value.toUpperCase();
  36.  
  37. var current = "row" + pressn;
  38. // current row
  39. var childDivs = document.getElementById(current).getElementsByTagName('div');
  40. var c = 0; // correct count
  41.  
  42. // If not right number of letters
  43. if(guess.length !== 5){
  44. document.getElementById('smallMsg').innerHTML = "Guesses must be 5 letters!";
  45. if(pressn===5){
  46. end("Sorry, you lost.", "Correct word: " + rand);
  47. }
  48. pressn++;
  49. document.getElementById(current).firstElementChild.innerHTML=rand[0];
  50. return; // move down
  51. }
  52.  
  53. // check for correctness
  54. for(var i=0; i<childDivs.length; i++) {
  55. childDivs[i].innerHTML = guess[i];
  56.  
  57. // if letter match in right place
  58. if(guess[i] == rand[i]){
  59. changeClass(childDivs[i], 'default', 'correct');
  60. c++;
  61. }
  62. // if exists but is in the wrong place
  63. else if(rand.indexOf(guess[i])!=-1){
  64. if(hasDuplicates === false && childDivs[rand.indexOf(guess[i])].className != "square correct"){
  65. changeClass(childDivs[i], 'default', 'wrongplace');
  66. } //if
  67. // if there are duplicate letters
  68. else if(hasDuplicates === true){
  69. var ind = getAllIndexes(rand, guess[i]);
  70. if (ind.length > 1){
  71. for (var j=0; j<ind.length;j++){
  72. if(childDivs[ind[j]].className != "square correct" && childDivs[i].className != "square wrongplace"){
  73. changeClass(childDivs[i], 'default', 'wrongplace');
  74. } //if
  75. } //for
  76. } //if
  77. else if (childDivs[rand.indexOf(guess[i])].className != "square correct"){
  78. changeClass(childDivs[i], 'default', 'wrongplace');
  79. } //else if
  80. } //else if(hasDuplicates === true)
  81. } //else if
  82.  
  83. input.value = ""; // clear input box
  84.  
  85. if(c===5) { // if they have all the correct letters
  86. end("Congrats, you won!", "Play Again?");
  87. } //if
  88. else if (pressn === 5){ // if they're out of tries
  89. end("Sorry, you lost.", "Correct word: " + rand);
  90. } //else if
  91. } //for (check for correctness loop)
  92. pressn++; // inc number of guesses
  93. } //if (key = 'enter')
  94. } //input
  95. } //gameloop
  96.  
  97. // endgame
  98. var end = function(msg, smallmsg){
  99. document.getElementById('msgBox').innerHTML = msg;
  100. document.getElementById('smallMsg').innerHTML = smallmsg;
  101. changeClass(button, "invisible", "visible");
  102. document.getElementById('guess').readOnly = true;
  103. }
  104.  
  105. // reset
  106. var playagain = function(){
  107. document.getElementById('msgBox').innerHTML="Guess the Word!"; // main message
  108. document.getElementById('smallMsg').innerHTML = "Green = correct letter, Yellow = wrong place"; // small message
  109. document.getElementById('guess').readOnly = false;
  110. changeClass(button, "visible", "invisible");
  111.  
  112. // clean boxes
  113. for(var i=1;i<6;i++){
  114. var resets = document.getElementById('row'+i).getElementsByTagName('div');
  115. for(var j=0;j<5;j++){
  116. resets[j].innerHTML="";
  117. if(resets[j].className == "square correct" || resets[j].className == "square wrongplace"){
  118. changeClass(resets[j], "correct", "default");
  119. changeClass(resets[j], "wrongplace", "default");
  120. } //if
  121. } //for
  122. } //for
  123. // restart the loop
  124. gameloop();
  125. };
  126.  
  127. // start loop
  128. gameloop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement