Search⌘ K
AI Features

Exercise on Classes

Explore the fundamentals of ES6 classes through practical exercises that involve creating player and non-player characters within a simulated 10x10 game field. Learn to implement class inheritance, model character movement directions, and handle interactions such as scoring and eliminating non-player characters. This lesson enhances understanding of object-oriented features in JavaScript using real coding challenges.

Exercise 1:

Create a PlayerCharacter and a NonPlayerCharacter with a common ancestor Character. The characters are located in a 10x10 game field. All characters appear at a random location. Create the three classes, and make sure you can query where each character is.

Node.js
class Character {
constructor( id, name, x, y ) {
//Write your code here
}
get position() {
//Write your code here
}
}
//Define Player Character and NonPlayerCharacter classes here
function createPlayer( id, name ) {
//Write your code here
}
function createNonPlayer( id, name ) {
//Write your code here
}

Explanation:

This exercise has many solutions. We just used ...