Prep 12: JavaScript Classes
Textbook Reading
- CSS
- 7.5 CSS Effects
- JS
- 10.2 Prototypes, Classes, and Modules
Simple Scrabble
Here is a CS159-style assignment for you to do, but in JavaScript!
Introduction
Scrabble is a word game where players take turns placing letter tiles on a board to form words. Each letter has a point value. Players earn points based on the words they create. The game ends when all tiles are used or no more moves can be made. The player with the most points wins.
Getting Started
Download the following files:
The following class containing all the game logic is provided:
classDiagram
class Game {
+tileBag : Array~Tile~
+board : Board
+players : Array~Player~
+currentPlayerIndex : Number
+constructor()
+createTileBag() Array~Tile~
+shuffle(Array) Array$
+init()
+currentPlayer : Player
+playTurn(tileIndex : Number, row : Number, col : Number)
+nextTurn()
+updateUI()
}
Tip
The source code for the Game class can be used as a reference for writing your own classes.
UML Diagram
Implement the following classes in a new file named scrabble-core.js:
classDiagram
class Tile {
+letter : String
+points : Number
+constructor(letter : String, points : Number)
}
class Board {
+rows : Number
+cols : Number
+grid : Array~Array~Tile | null~~
+constructor(rows : Number, cols : Number)
+display()
+placeTile(row : Number, col : Number, tile : Tile) Boolean
}
class Player {
+name : String
+hand : Array~Tile~
+score : Number
+constructor(name : String)
+drawTiles(tileBag : Array~Tile~, count : Number)
+playTile(tileIndex : Number, board : Board, row : Number, col : Number) Boolean
}
Tile <--Player : uses
Board --> Tile : contains
Tip
Notice that line 1 of scrabble-game.js imports the three classes.
That means you will have to export each class in scrabble-core.js.
The Tile class
- Not much to do here; just write the constructor!
The Board class
- The
constructorshould initialize all three attributes- Create a 2D array of
nullvalues for the given number ofrowsandcols
- Create a 2D array of
display()should get theboardelement and replace its HTML contents:- Create a
divfor each row - Create a
spanfor each cell (on each row) - Set the cell’s
textContentto thetile’s letter (or_if null) - Set the cell’s padding to
4px. - Append each
spanto thediv; append eachdivto the board
- Create a
placeTile()should setthis.grid[row][col]to theTileand returntrue- But only if the cell is
null; if the cell is notnull, don’t update and returnfalse
- But only if the cell is
The Player class
- The
constructorshould initialize all three attributes- The name is provided; the hand is an empty array; the score is 0
drawTiles()is used for a Player to draw (take) tiles from the bag- Write a loop to run for
counttimes- If the tile bag is not empty
- Pop the next tile from the bag
- Push the tile into the Player’s hand
- If the tile bag is not empty
- Write a loop to run for
playTile()should returntrueon success orfalseon failure- If there is a tile at
tileIndexand you can place the tile on the board- Add the tile’s points to the player’s score
- Remove the tile from the player’s hand (Hint: use
hand.splice())
- If there is a tile at
Submission
Optional:
Before you submit, add JSDoc comments to your code in scrabble-core.js.
If you’d like help, paste your code into an AI tool with a prompt like:
Add JSDoc comments to all classes and methods in this JavaScript file.
Review the generated comments against your code. An inaccurate comment can reveal a bug you missed!
Required:
Submit your scrabble-core.js file to Gradescope.