From 0e4f315901f214a2c3b7d42b440a993f27f1aba1 Mon Sep 17 00:00:00 2001 From: Kara Rondinelli Date: Thu, 8 Feb 2024 15:13:13 -0500 Subject: [PATCH] finished house cat simulator decision tree --- week-04/index.html | 85 +++++++++++++++++++++++++++ week-04/js/game-framework.js | 108 +++++++++++++++++++++++++++++++++++ week-04/js/main.js | 43 ++++++++++++++ week-04/test/test.example.js | 22 +++++++ week-04/tests.html | 14 +++++ 5 files changed, 272 insertions(+) create mode 100644 week-04/index.html create mode 100644 week-04/js/game-framework.js create mode 100644 week-04/js/main.js create mode 100644 week-04/test/test.example.js create mode 100644 week-04/tests.html diff --git a/week-04/index.html b/week-04/index.html new file mode 100644 index 0000000..6babd09 --- /dev/null +++ b/week-04/index.html @@ -0,0 +1,85 @@ + + + + + + Week 04 + + + + + + + +
+
+
+

House Cat Simulator

+

Being a house cat is a simple life of eat, sleep, and play. Take a moment out of your busy human life to experience + a day in the life of a house cat.

+
+
+ +
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/week-04/js/game-framework.js b/week-04/js/game-framework.js new file mode 100644 index 0000000..ebc865a --- /dev/null +++ b/week-04/js/game-framework.js @@ -0,0 +1,108 @@ +/** + * Game Framework + * It's safe to ignore this file. It's just a helper for the game. + * + */ + +// Get the templates +let confirmTmpl = document.querySelector('#confirm').content; +let promptTmpl = document.querySelector('#prompt').content; +let alertTmpl = document.querySelector('#alert').content; + + + +function Confirm(question = '', helpText='', appendTo = 'main') { + + if (typeof question == 'string' && question !== '') { + + return new Promise((resolve, reject) => { + + // documentFragment from string + let df = confirmTmpl.cloneNode(true); + + df.querySelector('.card-title').textContent = question; + df.querySelector('.card-text').textContent = helpText; + + + df.querySelector('.btn-true').addEventListener('click', (event) => { + console.log('Promise Resolved', true) + resolve(true) + }, {once: true}) + + df.querySelector('.btn-false').addEventListener('click', (event) => { + console.log('Promise Resolved', false) + resolve(false) + }, { once: true }) + + document.querySelector(appendTo).appendChild(df) + }); + } +} + + +function Alert(question = '', helpText = '', appendTo = 'main') { + + if (typeof question == 'string' && question !== '') { + + return new Promise((resolve, reject) => { + + // documentFragment from string + let df = alertTmpl.cloneNode(true); + + df.querySelector('.card-title').textContent = question; + df.querySelector('.card-text').textContent = helpText; + + df.querySelector('.btn-true').addEventListener('click', (event) => { + console.log('Promise Resolved', true) + resolve(true) + }, { once: true }) + + df.querySelector('.btn-false').addEventListener('click', (event) => { + console.log('Promise Resolved', false) + resolve(false) + }, { once: true }) + + document.querySelector(appendTo).appendChild(df) + }); + } +} + + +function Prompt(question = '', helpText = '', appendTo = 'main') { + + if (typeof question == 'string' && question !== '') { + + return new Promise((resolve, reject) => { + + // documentFragment from string + let df = promptTmpl.cloneNode(true); + + df.querySelector('.card-title').textContent = question; + // df.querySelector('.card-text').textContent = helpText; + let input = df.querySelector('input'); + + df.querySelector('.btn-true').addEventListener('click', (event) => { + console.log('Promise Resolved', input.value) + resolve(input.value) + }, { once: true }) + + df.querySelector('.btn-false').addEventListener('click', (event) => { + console.log('Promise Resolved', false) + resolve(false) + }, { once: true }) + + document.querySelector(appendTo).appendChild(df) + }); + } +} + + +// Autoscroll to the bottom of the page +const resizeObserver = new ResizeObserver(entries => + window.scrollTo(0, document.body.scrollHeight) +) + +// start observing a DOM node +resizeObserver.observe(document.body) + +export { Confirm, Alert, Prompt } \ No newline at end of file diff --git a/week-04/js/main.js b/week-04/js/main.js new file mode 100644 index 0000000..40a2e71 --- /dev/null +++ b/week-04/js/main.js @@ -0,0 +1,43 @@ +import { Alert, Confirm, Prompt } from "./game-framework.js"; +/** + * Week 04 - Control Flow + */ + + + +// Modify the text for the introduction +await Alert('Welcome to House Cat Simulator. Make decisions to experience a day in the life of a house cat!') + +let name = await Prompt("What is your cat name?") + +// Level 1 +if(await Confirm(`Hi ${name}. Do you want to go to sleep?`)) { + + let decision = await Prompt('Do you want to sleep on the windowsill, bed, or in a box?') + + if (decision.toLowerCase() === "windowsill") { + await Alert('You find a spot to nap on the windowsill and fall asleep in the warmth of the sun.') + } else if (decision.toLowerCase() === "bed") { + await Alert('You find a cozy spot on the bed with your human and fall asleep snuggled next to them.') + } else if (decision.toLowerCase() === "box") { + await Alert('You find a cool box and fall asleep inside of it.') + } + + } else { + if (await Confirm('Do you want to play?')) { + if (await Confirm('Are there toys nearby?')) { + await Alert ('You bat around a toy and chase it around.') + } else { + await Alert('You play with a makeshift toy made out of crumpled paper.') + } + } else { + if (await Confirm('Are you hungry?')) { + await Alert('You eat some food from your bowl, licking your chops. You are happy.') + } else { + await Alert("Since you don't want to do anything and are bored, you go look for your human to give you some attention.") + } + } + +} + + window.location.reload() \ No newline at end of file diff --git a/week-04/test/test.example.js b/week-04/test/test.example.js new file mode 100644 index 0000000..d206d7e --- /dev/null +++ b/week-04/test/test.example.js @@ -0,0 +1,22 @@ +import { assert } from 'https://unpkg.com/chai/chai.js'; +import * as ctx from '../js/week-01.js'; + + +describe("Variables:", function () { + describe("firstName", function () { + it("should be a valid string", function () { + assert.typeOf(ctx.firstName, 'string'); + }); + }); + describe("age", function () { + it("should be a valid number", function () { + assert(Number.isInteger(ctx.age)); + assert.isNumber(ctx.age); + }); + }); + describe("whyImTakingThisCourse", function () { + it("should be a valid string", function () { + assert.typeOf(ctx.whyImTakingThisCourse, 'string'); + }); + }); +}); diff --git a/week-04/tests.html b/week-04/tests.html new file mode 100644 index 0000000..ff4d720 --- /dev/null +++ b/week-04/tests.html @@ -0,0 +1,14 @@ + + + + + + Test Results + + + + + + \ No newline at end of file