Skip to content

Commit df2e549

Browse files
committed
Wordle.
1 parent 075cfed commit df2e549

72 files changed

Lines changed: 10392 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Wordle/Grid.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Grid {
2+
constructor() {
3+
this.rows = this.createRows()
4+
this.rows[0].isCurrentRow=true
5+
this.currentRowIdx=0
6+
}
7+
8+
createRows() {
9+
return new Array(6).fill(null).map((_,i)=> (new Row(i)))
10+
}
11+
12+
changeCurrentRow() {
13+
if(this.rows[this.currentRowIdx].currentLetterIdx<5) {return}
14+
this.rows[this.currentRowIdx].isCurrentRow=true
15+
this.rows[this.currentRowIdx].checkLetters()
16+
this.currentRowIdx++
17+
}
18+
19+
draw() {
20+
this.rows.forEach(row=> {
21+
row.draw()
22+
})
23+
}
24+
}

Wordle/Letter.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Letter {
2+
constructor(i, x=0, y=0, letter="", c=color(12, 12, 13)) {
3+
this.letterNum = i
4+
this.letter = letter
5+
this.c = c
6+
this.x = x
7+
this.y = y
8+
}
9+
draw() {
10+
//Box
11+
push()
12+
fill(this.c)
13+
stroke(58, 58, 60)
14+
strokeWeight(5)
15+
rect(this.x, this.y, letterW, letterH)
16+
pop()
17+
18+
//Letter
19+
push()
20+
fill(255, 255, 255)
21+
textSize(65)
22+
textAlign(CENTER)
23+
text(this.letter, this.x+(letterW/2), this.y+((letterH/4)*3)+5)
24+
pop()
25+
}
26+
}

Wordle/Row.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

Wordle/Wordle.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width" />
6+
<title>Wordle</title>
7+
<script type="text/javascript" src="wordle.js"></script>
8+
</head>
9+
<body>
10+
</body>
11+
</html>

Wordle/Wordle.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const wordBank = [
2+
"HELLO", "SLATE", "CRANE", "RIGOR", "CODES", "MONTH"
3+
]
4+
5+
let word
6+
7+
let grid
8+
9+
let letterW = 75
10+
let letterH = 75
11+
12+
let gameState="PLAYING"
13+
14+
function setup() {
15+
createCanvas(windowWidth, windowHeight);
16+
background(100);
17+
18+
//Choose the word
19+
word = chooseRandomWord()
20+
// word="CODES"
21+
console.log(word)
22+
23+
//Create the grid
24+
grid = new Grid()
25+
}
26+
27+
function chooseRandomWord() {
28+
return random(wordBank)
29+
}
30+
31+
function keyPressed() {
32+
if(keyCode===ENTER) {grid.changeCurrentRow()}
33+
else if(keyCode==BACKSPACE) {grid.rows[grid.currentRowIdx].backspace()}
34+
else {
35+
grid.rows[grid.currentRowIdx].changeLetter(key)
36+
}
37+
}
38+
39+
function draw() {
40+
background(12,12,13)
41+
42+
if(gameState=="PLAYING") {
43+
//Title text
44+
push()
45+
fill(255,255,255)
46+
textSize(50)
47+
textAlign(CENTER)
48+
text("Wordle", windowWidth/2, 70)
49+
pop()
50+
51+
grid.draw()
52+
}
53+
54+
if(gameState=="END") {
55+
push()
56+
fill(255,255,255)
57+
textSize(50)
58+
textAlign(CENTER)
59+
text("Correct!", windowWidth/2, 70)
60+
text("The word was "+word+"!", windowWidth/2, 130)
61+
text("The word was "+word+"!", windowWidth/2, 130)
62+
pop()
63+
}
64+
}

0 commit comments

Comments
 (0)