Compare your project to our solution code and share your project below! Your solution might not look exactly like ours, and that’s okay! The most important thing right now is to get your code working as it should (you can always refactor more later). There are multiple ways to complete these projects and you should exercise your creative abilities in doing so.
This is a safe space for you to ask questions about any sample solution code and share your work with others! Simply reply to this thread to get the conversation started. Feedback is a vital component in getting better with coding and all ability levels are welcome here, so don’t be shy!
About community guidelines: This is a supportive and kind community of people learning and developing their skills. All comments here are expected to keep to our community guidelines
How do I share my own solutions?
If you completed the project off-platform, you can upload your project to your own GitHub and share the public link on the relevant project topic.
If you completed the project in the Codecademy learning environment, use the share code link at the bottom of your code editor to create a gist, and then share that link here.
Do I really need to get set up on GitHub?
Yes! Both of these sharing methods require you to get set up on GitHub, and trust us, it’s worth your time. Here’s why:
Once you have your project in GitHub, you’ll be able to share proof of your work with potential employers, and link out to it on your CV.
It’s a great opportunity to get your feet wet using a development tool that tech workers use on the job, every day.
Not sure how to get started? We’ve got you covered - read this article for the easiest way to get set up on GitHub.
Best practices for asking questions about the sample solution
Be specific! Reference exact line numbers and syntax so others are able to identify the area of the code you have questions about.
First of all I want to thank you for your program. Its a great way to learn programming and web development.
But this time I have to say some words:
First of all the Part 10 BROWSER COMPATIBILITY AND TRANSPILATION in javascript introduction is not well explained as in other parts.
While doing it I had impression to copy and past without understand well like in other lessons.
This project is far more difficult than all projects that I have done. (And I have done all challenges that gave me the path until this project).
Even if I downloaded the solution I still don’t understand how it works…
Is there any way that some of your experimented developper makes a video to explain step by step how to build this game please ?
I think this project is really important and I don’t want to skip it. I managed to do it until step 3.
Than after step 3 its quit hard to understand how to start where to start what to do…
I think this project would be more helpful for people if there are more explanations and steps or a video that build it.
Where fieldW is the width of the field and fieldH is the height of the field.
If you want to run a check on the hat location you can use something similar to what you used to check if the player’s location is in bounds in the main Field class.
I agree that the directions are very hard to understand. I didn’t quite understand what I should do until I started, and then I thought, “ohhh, that’s what the directions meant!”.
Basically, when main.js is initiated it does these 3 things:
create an array with Field.generateField()
stores the array from 1) in an object with const var = new Field(array)
console.log the starting field with .print()
At this point, what the player sees is the starting field, while 1-3 was done “behind the scenes” by the program. The next steps are a loop requiring player interaction. Basically the program:
asks for input
takes the input and do stuff
calls myHatGame.print() to print out the updated field
repeat 4-6 until the player wins or loses.
program ends
I spent most of my time making the two functions, generateField() and print(), work properly. You’ll know they work properly when generateField() makes something like this:
Once I got these two done, the rest kinda finished itself. The loop took a bit of trial and error, but it’s surprisingly simple once I got the hang of it! It’s step 1-3 that really made me my pull my hair out It’s totally doable though, so don’t be surprised if steps 1-3 took a couple of hours. It’s where you’ll spend 80% of the project time.
Not too bad of a project. The maze validation took a few hours of troubleshooting. Unfortunately it seems like an unintended consequence is that most of the random fields that pass validation are very easy to traverses.
It is hard to avoid impossible games.
So I didn’t…
const prompt = require('prompt-sync')({ sigint: true });
const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';
class Field {
constructor(height, width, percentage = 25) {
this._height = height;
this._width = width;
this._percentage = percentage;
this._field = Field.generateField(height, width, percentage);
}
print() {
for (let row of this._field) {
console.log(row.join(" "))
}
}
play() {
let posY = 0;
let posX = 0;
let userMove;
let allowedMoves = this.allowedMoves(posY, posX);
while (!allowedMoves) {
this._field = Field.generateField(this._height, this._width, this._percentage);
}
while (this._field[posY][posX] !== hat && this._field[posY][posX] !== hole) {
console.clear();
this._field[posY][posX] = pathCharacter;
this.print()
allowedMoves = this.allowedMoves(posY, posX);
if (!allowedMoves) {
console.log("You can't move anymore. Maybe there was no path?")
}
userMove = prompt("Where do you want to go? Type U for Up, R for Right, D for Down, L for Left: ");
while (!allowedMoves.includes(userMove.toUpperCase())) {
userMove = prompt("The move you entered is invalid or forbidden. Please, try again: ");
}
switch (userMove.toUpperCase()) {
case "U":
posY -= 1;
break;
case "R":
posX += 1;
break;
case "D":
posY += 1;
break;
case "L":
posX -= 1;
break;
}
}
if (this._field[posY][posX] === hat) {
console.log("Yeah! You found your hat!")
} else if (this._field[posY][posX] === hole) {
console.log("Did you just fall in a hole?")
}
}
allowedMoves(posY, posX) {
const allowedMoves = [];
// Up:
if (posY && this._field[posY - 1][posX] !== pathCharacter) { allowedMoves.push("U"); }
// Right:
if (posX < this._field[0].length - 1 && this._field[posY][posX + 1] !== pathCharacter) { allowedMoves.push("R"); }
// Down :
if (posY < this._field.length - 1 && this._field[posY + 1][posX] !== pathCharacter) { allowedMoves.push("D"); }
// Left:
if (posX && this._field[posY][posX - 1] !== pathCharacter) { allowedMoves.push("L"); }
return allowedMoves;
}
static generateField(height, width, percentage = 25) {
const field = [] // Creating the field.
if (percentage > 50) {
percentage = 35;
console.log("The percentage of holes, being too high, has been set to 35%")
}
let numberOfHoles = Math.floor(height * width * percentage / 100) // Defining the number of holes.
// The position of the hat is generated randomly. It can't be [0][0].
let hatY = 0;
let hatX = 0;
while (!hatX && !hatY) {
hatY = Math.floor(Math.random() * height)
hatX = Math.floor(Math.random() * width)
}
for (let row = 0; row < height; row++) {
const rowArray = []
for (let column = 0; column < width; column++) {
// The position [0][0] is the starting point.
if (row === 0 & column === 0) {
rowArray.push(pathCharacter);
continue;
}
// The hat is added if the position corresponds to its.
if (row === hatY && column === hatX) {
rowArray.push(hat);
continue;
}
// Every other position is filled with a field character.
rowArray.push(fieldCharacter);
}
field.push(rowArray);
}
for (numberOfHoles; numberOfHoles > 0; numberOfHoles--) {
let holeY = 0;
let holeX = 0;
while (field[holeY][holeX] !== fieldCharacter) {
holeY = Math.floor(Math.random() * height);
holeX = Math.floor(Math.random() * width);
}
field[holeY][holeX] = hole;
}
return field;
}
}
playGround = new Field(15, 25);
console.log(playGround.play())
I tried to use different kind of things: while for switch
Etc…
Any idea why this method I put in is giving me a syntax error?
checkWin() {
if (this._field[this.locationY][this.locationX] === hole) {
console.log('Oops! You lost by falling into the hole!');
return 'lose';
}
esle if (this._field[this.locationY][this.locationX] === hat) {
console.log('Congratulations! You found the hat!');
return 'win';
}
else {
return 'cont';
}
}
This took significantly longer then 2h to complete but i learnt a lot whilst doing it. I think it’s a significant jump from the JavaScript course but recommend anyone to attempt and insist on it.
The hardest bit for me was the static generateField method. Creating a method for custom 2D array was surprisingly challenging.
Choice of 10 search algorithms to help you find the hat if possible at all
AStarFinder,BestFirstFinder,BreadthFirstFinder,DijkstraFinder,IDAStarFinder,
JumpPointFinder,BiAStarFinder,BiBestFirstFinder,BiBreadthFirstFinder,BiDijkstraFinder
This also satisfies the ‘field validator’ extended criteria
Colorized hat,path,field,hole and help route
Hard mode : holes added on up or down only; easy mode : no holdes added
It creates a random grid with random start location. Width, height and percentage of grid being holes can be set up. Easy mode - no added holes. Hard mode - can set up level of difficulty, the higher the level, the more likely a new hole added after a move. Before it lets a player start the game, it checks if the grid can be solved at easy level and if not, it forces creation of a new grid… I used Las Vegas algorithm that returns false if the solution is not found within permitted time (time being a function of squared grid size).