|
| 1 | +class Row { |
| 2 | + constructor(i) { |
| 3 | + this.rowNum = i |
| 4 | + this.gap = 20 |
| 5 | + this.letters = this.createLetters() |
| 6 | + this.currentLetterIdx=0 |
| 7 | + this.isCurrentRow = false |
| 8 | + } |
| 9 | + |
| 10 | + createLetters() { |
| 11 | + let xOffset = (windowWidth/2) - (((letterW*5)+(this.gap*4))/2) |
| 12 | + let yOffset = 100 |
| 13 | + return new Array(5).fill(null).map((_,i)=> (new Letter(i, xOffset+(i*(letterW+this.gap)), (this.rowNum*(letterH+this.gap))+yOffset))) |
| 14 | + } |
| 15 | + |
| 16 | + backspace() { |
| 17 | + if(this.currentLetterIdx>0) {this.currentLetterIdx-=1} |
| 18 | + this.letters[this.currentLetterIdx].letter="" |
| 19 | + } |
| 20 | + |
| 21 | + changeLetter(l) { |
| 22 | + if(this.currentLetterIdx>4) {return} |
| 23 | + this.letters[this.currentLetterIdx].letter=l.toUpperCase() |
| 24 | + this.currentLetterIdx+=1 |
| 25 | + } |
| 26 | + |
| 27 | + getWord() { |
| 28 | + let word="" |
| 29 | + this.letters.forEach(letter=>{word+=letter.letter}) |
| 30 | + return word |
| 31 | + } |
| 32 | + |
| 33 | + checkLetters() { |
| 34 | + let counts={} |
| 35 | + |
| 36 | + const s = this.getWord() |
| 37 | + |
| 38 | + //Correct word |
| 39 | + if(s==word) {gameState="END"} |
| 40 | + |
| 41 | + for(let i=0; i<s.length; i++) { |
| 42 | + //Letter has already been checked a sufficient amount of times |
| 43 | + if(s[i] in counts && counts[s[i]]>word.split(s[i]).length-2) {this.letters[i].c=color(142, 142, 142)} |
| 44 | + |
| 45 | + //Incorrect letter |
| 46 | + else if(!word.includes(s[i])) {this.letters[i].c=color(142, 142, 142)} |
| 47 | + |
| 48 | + //Correct letter and placement |
| 49 | + else if(s[i]==word[i]) {this.letters[i].c=color(0,255,0)} |
| 50 | + |
| 51 | + //Correct letter but incorrect placement |
| 52 | + else if(word.includes(s[i])) {this.letters[i].c=color(232, 197, 1)} |
| 53 | + |
| 54 | + if(!(s[i] in counts)) {counts[s[i]]=0} |
| 55 | + counts[s[i]]++ |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + draw() { |
| 60 | + this.letters.forEach(letter=> { |
| 61 | + letter.draw() |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
0 commit comments