15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/chapter_1.js:
--------------------------------------------------------------------------------
1 | // //Write a script to greet your visitors using js alert box
2 | function javascript_question1 (){
3 |
4 | alert("hello visitors");
5 | }
6 |
7 | javascript_question1();
8 |
9 |
10 | // question 2)write a script to display the following message in your broswer
11 | function javascript_question2(){
12 |
13 | alert("Error! Please enter a valid password");}
14 |
15 | javascript_question2();
16 |
17 |
18 | // question 3) write a script to display the following message in your broswer;
19 | function javasript_question3(){
20 | alert("Welcome to JS land...\n Happy Coding!");
21 | }
22 | javasript_question3();
23 |
24 |
25 | // QUESTION 4) write a scipt to display the following message in sequence in your broswer
26 | function javascript_question4(){
27 | alert("Welcome to Js land...")
28 | }
29 | function javascript_question4b(){
30 | alert("Happy coding! \n Prevent this page from Creating Additional Dialogs.")
31 | }
32 | javascript_question4();
33 | javascript_question4b();
34 |
35 |
36 | // here i made a todolist
37 |
38 | let input_element=document.getElementById("todoinput");
39 |
40 | let input_addbutton=document.getElementById("btn");
41 |
42 | let input_tasklist=document.getElementById("tasklist");
43 |
44 | input_addbutton.addEventListener('click',()=>{
45 | const task_text=input_element.value.trim();
46 |
47 | if(task_text===''){
48 |
49 | alert("task cant be empty");
50 | return;
51 | }
52 | const listitem=document.createElement("li");
53 |
54 | const taskspan=document.createElement('span');
55 |
56 | taskspan.textContent=task_text;
57 |
58 | taskspan.addEventListener('click',()=>{
59 |
60 | taskspan.classList.toggle("completed");
61 |
62 | });
63 | const delete_button=document.createElement("button");
64 |
65 | delete_button.textContent="delete";
66 |
67 | delete_button.style.marginLeft="10px";
68 |
69 | delete_button.addEventListener('click',()=>{
70 |
71 | listitem.remove();
72 | });
73 | listitem.appendChild(taskspan);
74 |
75 | listitem.appendChild(delete_button);
76 |
77 | input_tasklist.appendChild(listitem);
78 |
79 | input_element.value='';
80 |
81 | })
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
--------------------------------------------------------------------------------
/currency/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | font-family: 'Arial', sans-serif;
3 | background: linear-gradient(120deg, #000000, #32CD32);
4 | color: #333333;
5 | margin: 0;
6 | padding: 0;
7 | display: flex;
8 | justify-content: center;
9 | align-items: center;
10 | height: 100vh;
11 | }
12 |
13 | .container {
14 | display: flex;
15 | flex-direction: column;
16 | background: linear-gradient(120deg, #000000, #32CD32);
17 | padding: 30px;
18 | border-radius: 16px;
19 | box-shadow: 0 12px 24px rgba(0, 0, 0, 0.4);
20 | width: 350px;
21 | text-align: center;
22 | transition: background 1s ease;
23 | }
24 | .container:hover{
25 | background: linear-gradient(120deg,#000000, #32CD32);
26 | }
27 |
28 | h2 {
29 | color: #FFD700;
30 | text-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
31 | }
32 |
33 | h3 {
34 | color: #32CD32;
35 | text-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
36 | }
37 |
38 | form {
39 | display: flex;
40 | flex-direction: column;
41 | gap: 1.5rem;
42 | }
43 |
44 | .input {
45 | display: flex;
46 | flex-direction: column;
47 | }
48 |
49 | label {
50 | margin-bottom: 6px;
51 | color: #C1C1C1;
52 | font-weight: bold;
53 | }
54 |
55 | input,
56 | select {
57 | background-color: #0d1013;
58 | color: #FFFFFF;
59 | border: 1px solid #7F8C8D;
60 | border-radius: 10px;
61 | padding: 14px;
62 | transition: border-color 0.3s ease, transform 0.3s ease;
63 | }
64 |
65 | select {
66 | width: 100%;
67 | }
68 |
69 | input:focus,
70 | select:focus {
71 | border-color:#32CD32;
72 | outline: none;
73 | box-shadow: 0 0 10px rgb(38, 255, 0);
74 | transform: scale(1.05);
75 | }
76 |
77 | button {
78 | background: #0d1013;
79 | color: #FFFFFF;
80 | font-weight: bold;
81 | border: none;
82 | padding: 14px;
83 | border-radius: 12px;
84 | cursor: pointer;
85 | transition: background 0.4s ease, transform 0.3s ease;
86 | }
87 |
88 | button:hover {
89 | background: linear-gradient(145deg, #010605, #32CD32);
90 | transform: scale(1.1);
91 | }
92 |
93 | .result {
94 | margin-top: 20px;
95 | font-size: 1.2rem;
96 | color: #E8E8E8;
97 | }
98 |
99 | .error {
100 | color: #E74C3C;
101 | font-weight: bold;
102 | }
103 |
104 | .currency-selector {
105 | display: flex;
106 | align-items: center;
107 | gap: 15px;
108 | }
109 |
110 | .currency-flag {
111 | width: 28px;
112 | height: 28px;
113 | border-radius: 6px;
114 | box-shadow: 0 3px 6px rgba(0,0,0,0.3);
115 | }
--------------------------------------------------------------------------------
/currency/script2.js:
--------------------------------------------------------------------------------
1 | let form = document.querySelector("form");
2 |
3 | let resultValue = document.querySelector("#result-value");
4 |
5 | let countryOptions = document.querySelectorAll("select");
6 |
7 | countryOptions.forEach(selectOption => {
8 |
9 | for (currCode in countryList) {
10 | let option = document.createElement("option");
11 | option.innerHTML = currCode;
12 | option.value = currCode;
13 | selectOption.append(option);
14 |
15 | if (selectOption.id === "from-currency" && currCode === "CNY") {
16 | option.selected = "selected";
17 | } else if (selectOption.id === "to-currency" && currCode === "PKR") {
18 | option.selected = "selected";
19 | }
20 | };
21 | selectOption.addEventListener("change", (e) => {
22 | countryFlag(e.target);
23 | });
24 |
25 | });
26 |
27 |
28 | async function countryFlag(countryOption) {
29 | let countryCode = countryList[countryOption.value];
30 | let URL = `https://flagsapi.com/${countryCode}/flat/64.png`
31 | let img = countryOption.parentElement.querySelector("IMG");
32 | img.src = URL;
33 | }
34 |
35 | form.addEventListener("submit", async (e) => {
36 | e.preventDefault();
37 | let input = document.querySelector("input");
38 | let inputVal = Number(input.value);
39 | if (inputVal <= 0 || isNaN(inputVal)) {
40 | resultValue.innerText = "INVALID INPUT, TRY AGAIN."
41 | resultValue.classList.add("error")
42 | input.value = "";
43 | return;
44 | }
45 | else {
46 | fetchData(inputVal);
47 | }
48 | });
49 |
50 |
51 | async function fetchData(inputVal) {
52 | let fromCurrency = document.querySelector("#from-currency").value.toLowerCase();
53 | let toCurrency = document.querySelector("#to-currency").value.toLowerCase();
54 |
55 | try {
56 | let URL = `https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${fromCurrency}.json`
57 | let response = await fetch(URL);
58 | let data = await response.json();
59 |
60 | let exchangeRate = data[fromCurrency][toCurrency];
61 | if (!exchangeRate) {
62 | resultValue.innerText = "Exchange rate not available.";
63 | resultValue.classList.add("error");
64 | return;
65 | }
66 |
67 | let convertedAmount = (inputVal * exchangeRate).toFixed(2);
68 | resultValue.innerText = `${inputVal} ${fromCurrency.toUpperCase()} = ${convertedAmount} ${toCurrency.toUpperCase()}`;
69 | resultValue.classList.remove("error");
70 |
71 | }
72 |
73 | catch (error) {
74 | resultValue.innerText = "Failed to fetch exchange rates. Please try again later."
75 | resultValue.classList.add("error");
76 | console.log("error", error);
77 |
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/currency/script.js:
--------------------------------------------------------------------------------
1 | const countryList = {
2 | AED: "AE",
3 | AFN: "AF",
4 | XCD: "AG",
5 | ALL: "AL",
6 | AMD: "AM",
7 | ANG: "AN",
8 | AOA: "AO",
9 | AQD: "AQ",
10 | ARS: "AR",
11 | AUD: "AU",
12 | AZN: "AZ",
13 | BAM: "BA",
14 | BBD: "BB",
15 | BDT: "BD",
16 | XOF: "BE",
17 | BGN: "BG",
18 | BHD: "BH",
19 | BIF: "BI",
20 | BMD: "BM",
21 | BND: "BN",
22 | BOB: "BO",
23 | BRL: "BR",
24 | BSD: "BS",
25 | NOK: "BV",
26 | BWP: "BW",
27 | BYR: "BY",
28 | BZD: "BZ",
29 | CAD: "CA",
30 | CDF: "CD",
31 | XAF: "CF",
32 | CHF: "CH",
33 | CLP: "CL",
34 | CNY: "CN",
35 | COP: "CO",
36 | CRC: "CR",
37 | CUP: "CU",
38 | CVE: "CV",
39 | CYP: "CY",
40 | CZK: "CZ",
41 | DJF: "DJ",
42 | DKK: "DK",
43 | DOP: "DO",
44 | DZD: "DZ",
45 | ECS: "EC",
46 | EEK: "EE",
47 | EGP: "EG",
48 | ETB: "ET",
49 | EUR: "FR",
50 | FJD: "FJ",
51 | FKP: "FK",
52 | GBP: "GB",
53 | GEL: "GE",
54 | GGP: "GG",
55 | GHS: "GH",
56 | GIP: "GI",
57 | GMD: "GM",
58 | GNF: "GN",
59 | GTQ: "GT",
60 | GYD: "GY",
61 | HKD: "HK",
62 | HNL: "HN",
63 | HRK: "HR",
64 | HTG: "HT",
65 | HUF: "HU",
66 | IDR: "ID",
67 | ILS: "IL",
68 | INR: "IN",
69 | IQD: "IQ",
70 | IRR: "IR",
71 | ISK: "IS",
72 | JMD: "JM",
73 | JOD: "JO",
74 | JPY: "JP",
75 | KES: "KE",
76 | KGS: "KG",
77 | KHR: "KH",
78 | KMF: "KM",
79 | KPW: "KP",
80 | KRW: "KR",
81 | KWD: "KW",
82 | KYD: "KY",
83 | KZT: "KZ",
84 | LAK: "LA",
85 | LBP: "LB",
86 | LKR: "LK",
87 | LRD: "LR",
88 | LSL: "LS",
89 | LTL: "LT",
90 | LVL: "LV",
91 | LYD: "LY",
92 | MAD: "MA",
93 | MDL: "MD",
94 | MGA: "MG",
95 | MKD: "MK",
96 | MMK: "MM",
97 | MNT: "MN",
98 | MOP: "MO",
99 | MRO: "MR",
100 | MTL: "MT",
101 | MUR: "MU",
102 | MVR: "MV",
103 | MWK: "MW",
104 | MXN: "MX",
105 | MYR: "MY",
106 | MZN: "MZ",
107 | NAD: "NA",
108 | XPF: "NC",
109 | NGN: "NG",
110 | NIO: "NI",
111 | NPR: "NP",
112 | NZD: "NZ",
113 | OMR: "OM",
114 | PAB: "PA",
115 | PEN: "PE",
116 | PGK: "PG",
117 | PHP: "PH",
118 | PKR: "PK",
119 | PLN: "PL",
120 | PYG: "PY",
121 | QAR: "QA",
122 | RON: "RO",
123 | RSD: "RS",
124 | RUB: "RU",
125 | RWF: "RW",
126 | SAR: "SA",
127 | SBD: "SB",
128 | SCR: "SC",
129 | SDG: "SD",
130 | SEK: "SE",
131 | SGD: "SG",
132 | SKK: "SK",
133 | SLL: "SL",
134 | SOS: "SO",
135 | SRD: "SR",
136 | STD: "ST",
137 | SVC: "SV",
138 | SYP: "SY",
139 | SZL: "SZ",
140 | THB: "TH",
141 | TJS: "TJ",
142 | TMT: "TM",
143 | TND: "TN",
144 | TOP: "TO",
145 | TRY: "TR",
146 | TTD: "TT",
147 | TWD: "TW",
148 | TZS: "TZ",
149 | UAH: "UA",
150 | UGX: "UG",
151 | USD: "US",
152 | UYU: "UY",
153 | UZS: "UZ",
154 | VEF: "VE",
155 | VND: "VN",
156 | VUV: "VU",
157 | YER: "YE",
158 | ZAR: "ZA",
159 | ZMK: "ZM",
160 | ZWD: "ZW",
161 | };
--------------------------------------------------------------------------------
/Calculator/Calculator/style.css:
--------------------------------------------------------------------------------
1 | * {
2 | margin: 0;
3 | padding: 0;
4 | box-sizing: border-box;
5 | }
6 |
7 | @keyframes Gradient {
8 | 0% {
9 | background: linear-gradient(120deg, #a1c4fd, #c2e9fb);
10 | /* background-size: 400% 400%; */
11 | /* animation: Gradient 15s ease infinite; */
12 | }
13 |
14 | 50% {
15 | background: linear-gradient(120deg, #fbc2eb, #a6c1ee);
16 | /* background-size: 400% 400%; */
17 | /* animation: Gradient 15s ease infinite; */
18 | }
19 |
20 | 100% {
21 | background: linear-gradient(120deg, #a1c4fd, #c2e9fb);
22 | /* background-size: 400% 400%; */
23 | /* animation: Gradient 15s ease infinite; */
24 | }
25 | }
26 |
27 | /* body {
28 | font-family: 'Arial', sans-serif;
29 | background-color: #f4f8fb;
30 | color: #333;
31 | } */
32 |
33 | .container {
34 | display: flex;
35 | justify-content: center;
36 | align-items: center;
37 | height: 100vh;
38 | width: 100%;
39 | background: linear-gradient(120deg, #a1c4fd, #c2e9fb);
40 | animation: Gradient 8s ease infinite;
41 | overflow: hidden;
42 | }
43 |
44 | .calculator {
45 | display: flex;
46 | flex-direction: column;
47 | justify-content: center;
48 | align-items: center;
49 | padding: 20px;
50 | border: 1px solid #000;
51 | border-radius: 15px;
52 | background-color: #3a4452;
53 | box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.2), inset 0px 0px 10px rgba(255, 255, 255, 0.1);
54 | }
55 |
56 | /* .calculator_keys {
57 | display: grid;
58 | grid-template-columns: repeat(4, 1fr);
59 | gap: 15px;
60 | } */
61 |
62 | .calculator_keys button {
63 | border: 0;
64 | outline: 0;
65 | width: 60px;
66 | height: 60px;
67 | border-radius: 10px;
68 | box-shadow: -8px -8px 15px rgba(255, 255, 255, 0.1), 5px 5px 15px rgba(0, 0, 0, 0.3);
69 | font-size: 1rem;
70 | margin: 10px;
71 | background-color: transparent;
72 | color: white;
73 | cursor: pointer;
74 | }
75 |
76 | .calculator_keys button:hover {
77 | font-size: 1.1rem;
78 | background-color: #6b7076;
79 | transform: scale(1.05);
80 | }
81 |
82 | .calculator_display {
83 | display: flex;
84 | justify-items: flex-end;
85 | width: 100%;
86 | margin: 20px 0;
87 | }
88 |
89 | .calculator_input {
90 | border: 0;
91 | outline: 0;
92 | padding: 15px;
93 | margin: 10px;
94 | text-align: right;
95 | font-size: 1.5rem;
96 | width: 100%;
97 | height: 60px;
98 | border-radius: 10px;
99 | box-shadow: -8px -8px 15px rgba(255, 255, 255, 0.1), 5px 5px 15px rgba(0, 0, 0, 0.3);
100 | background-color: transparent;
101 | color: white;
102 | cursor: pointer;
103 | }
104 |
105 | .calculator_keys .equals {
106 | background-color: #ff7b66;
107 | color: #3a4452;
108 | font-size: 1.5rem;
109 | width: 145px;
110 | cursor: pointer;
111 | }
112 |
113 | .calculator_keys .equals:hover {
114 | background-color: #ff9a8b;
115 | font-size: 1.6rem;
116 | transform: scale(1.05);
117 | }
118 |
119 | @media (max-width: 600px) {
120 |
121 | .calculator {
122 | width: 60%;
123 | }
124 |
125 | .calculator_input {
126 | font-size: 1rem;
127 | }
128 |
129 | .calculator_keys button {
130 | font-size: 1rem;
131 | padding: 12px;
132 | width: 50px;
133 | height: 50px;
134 | }
135 |
136 | .calculator_keys .equals {
137 | width: 120px;
138 | }
139 | }
--------------------------------------------------------------------------------
/group_assignment/style.css:
--------------------------------------------------------------------------------
1 | * {
2 | box-sizing: border-box;
3 | margin: 0;
4 | padding: 0;
5 | }
6 |
7 | body {
8 | font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
9 | height: 100vh;
10 | background: radial-gradient(circle at top left, #0f2027, #203a43, #2c5364, #1e3c72);
11 | background-size: 600% 600%;
12 | animation: bgAnimation 25s ease infinite;
13 | display: flex;
14 | justify-content: center;
15 | align-items: center;
16 | overflow: hidden;
17 | color: #ffffff;
18 | position: relative;
19 | }
20 |
21 | @keyframes bgAnimation {
22 | 0% { background-position: 0% 50%; }
23 | 50% { background-position: 100% 50%; }
24 | 100% { background-position: 0% 50%; }
25 | }
26 |
27 | .wall {
28 | position: absolute;
29 | width: 100%;
30 | height: 100vh;
31 | background-image:
32 | linear-gradient(90deg, rgba(0, 255, 255, 0.07) 1px, transparent 1px),
33 | linear-gradient(rgba(0, 255, 255, 0.07) 1px, transparent 1px);
34 | background-size: 60px 60px;
35 | z-index: 0;
36 | animation: moveWall 40s linear infinite;
37 | }
38 |
39 | @keyframes moveWall {
40 | 0% { background-position: 0 0; }
41 | 100% { background-position: 300px 300px; }
42 | }
43 |
44 | #app {
45 | position: relative;
46 | z-index: 2;
47 | padding: 60px 50px;
48 | background: rgba(0, 0, 0, 0.55);
49 | border-radius: 30px;
50 | backdrop-filter: blur(20px);
51 | box-shadow: 0 0 50px #00fff7, 0 0 100px #00fff7;
52 | text-align: center;
53 | animation: fadeZoom 1.5s ease-in-out;
54 | }
55 |
56 | @keyframes fadeZoom {
57 | 0% { opacity: 0; transform: scale(0.8); }
58 | 100% { opacity: 1; transform: scale(1); }
59 | }
60 |
61 | h1 {
62 | font-size: 48px;
63 | font-weight: bold;
64 | color: #00fff7;
65 | margin-bottom: 30px;
66 | text-shadow: 0 0 15px #00fff7, 0 0 35px #00fff7;
67 | animation: pulseGlow 3s ease-in-out infinite;
68 | }
69 |
70 | @keyframes pulseGlow {
71 | 0% { text-shadow: 0 0 15px #00fff7; }
72 | 50% { text-shadow: 0 0 35px #00fff7, 0 0 15px #00fff7; }
73 | 100% { text-shadow: 0 0 15px #00fff7; }
74 | }
75 |
76 | canvas {
77 | border: 6px solid;
78 | border-image: linear-gradient(45deg, #00f2fe, #4facfe) 1;
79 | border-radius: 25px;
80 | background-color: #000;
81 | margin-bottom: 30px;
82 | box-shadow: 0 0 30px #00ffff;
83 | transition: transform 0.5s ease, box-shadow 0.5s ease;
84 | }
85 |
86 | canvas:hover {
87 | transform: scale(1.08) rotate(1deg);
88 | box-shadow: 0 0 50px #00ffff, 0 0 20px #00ffff inset;
89 | }
90 |
91 | .controls {
92 | display: flex;
93 | flex-wrap: wrap;
94 | justify-content: center;
95 | gap: 25px;
96 | margin-top: 30px;
97 | }
98 |
99 | button {
100 | padding: 14px 40px;
101 | font-size: 20px;
102 | font-weight: bold;
103 | border: none;
104 | border-radius: 50px;
105 | background: transparent;
106 | color: #00fff7;
107 | border: 2px solid #00fff7;
108 | box-shadow: 0 0 20px #00fff7;
109 | cursor: pointer;
110 | position: relative;
111 | overflow: hidden;
112 | z-index: 1;
113 | transition: all 0.4s ease;
114 | }
115 |
116 | button::before {
117 | content: '';
118 | position: absolute;
119 | top: -100%;
120 | left: -100%;
121 | width: 300%;
122 | height: 300%;
123 | background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
124 | animation: shinyEffect 6s linear infinite;
125 | z-index: 0;
126 | opacity: 0.5;
127 | }
128 |
129 | @keyframes shinyEffect {
130 | 0% { transform: translate(0%, 0%); }
131 | 100% { transform: translate(100%, 100%); }
132 | }
133 |
134 | button:hover {
135 | transform: scale(1.15);
136 | background-color: rgba(0, 255, 255, 0.2);
137 | color: #ffffff;
138 | box-shadow: 0 0 30px #00ffff, 0 0 10px #00ffff inset;
139 | border-color: #00ffff;
140 | }
141 |
142 |
143 |
--------------------------------------------------------------------------------
/group_assignment/hs.css:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | /* Global Styles */
19 | body {
20 | font-family: 'Segoe UI', sans-serif;
21 | background: linear-gradient(to right, #0f2027, #203a43, #2c5364);
22 | color: #fff;
23 | margin: 0;
24 | padding: 0;
25 | text-align: center;
26 | animation: fadeIn 2s ease-in-out; /* Fade-in effect */
27 | }
28 |
29 | /* Header Styles */
30 | header {
31 | padding: 30px 10px;
32 | background-color: #111;
33 | border-bottom: 2px solid #555;
34 | animation: slideInFromTop 1s ease-out; /* Slide-in animation */
35 | }
36 |
37 | h1 {
38 | font-size: 2.5rem;
39 | margin-bottom: 10px;
40 | color: #00ffd5;
41 | text-shadow: 0 0 10px #00ffd5;
42 | animation: glow 1.5s infinite alternate; /* Glowing effect */
43 | }
44 |
45 | header p {
46 | font-size: 1.2rem;
47 | color: #ddd;
48 | }
49 |
50 | /* Main Section Styles */
51 | main {
52 | padding: 30px 15px;
53 | }
54 |
55 | .controls {
56 | margin-bottom: 20px;
57 | }
58 |
59 | button {
60 | padding: 12px 25px;
61 | margin: 10px;
62 | font-size: 16px;
63 | font-weight: bold;
64 | border: none;
65 | border-radius: 8px;
66 | cursor: pointer;
67 | background: #00ffd5;
68 | color: #000;
69 | box-shadow: 0 0 15px #00ffd5, 0 0 30px #00ffd5 inset;
70 | transition: all 0.3s ease;
71 | animation: pulseButton 1s infinite alternate; /* Pulse effect */
72 | }
73 |
74 | button:hover {
75 | background: #00bfa6;
76 | box-shadow: 0 0 25px #00ffd5, 0 0 35px #00ffd5 inset;
77 | transform: scale(1.05);
78 | }
79 |
80 | /* Canvas Container */
81 | .canvas-container {
82 | display: flex;
83 | justify-content: center;
84 | align-items: center;
85 | margin-bottom: 30px;
86 | animation: fadeIn 2s ease-in-out;
87 | }
88 |
89 | canvas {
90 | border: 3px solid #00ffd5;
91 | background-color: #fff;
92 | border-radius: 10px;
93 | box-shadow: 0 0 20px #00ffd5;
94 | transition: transform 0.3s ease;
95 | }
96 |
97 | canvas:hover {
98 | transform: scale(1.05);
99 | }
100 |
101 | /* Instructions Section */
102 | .instructions {
103 | max-width: 600px;
104 | margin: 0 auto;
105 | text-align: left;
106 | background: #222;
107 | padding: 20px;
108 | border-radius: 10px;
109 | box-shadow: 0 0 20px #00ffd5;
110 | animation: slideInFromBottom 1s ease-out; /* Slide-in from bottom */
111 | }
112 |
113 | .instructions h2 {
114 | color: #00ffd5;
115 | margin-bottom: 10px;
116 | }
117 |
118 | .instructions ul {
119 | padding-left: 20px;
120 | }
121 |
122 | /* Footer Styles */
123 | footer {
124 | background: #111;
125 | padding: 15px;
126 | border-top: 2px solid #555;
127 | font-size: 14px;
128 | color: #aaa;
129 | animation: fadeIn 3s ease-in-out; /* Fade-in effect */
130 | }
131 |
132 | /* Keyframes for Animations */
133 | @keyframes fadeIn {
134 | 0% {
135 | opacity: 0;
136 | }
137 | 100% {
138 | opacity: 1;
139 | }
140 | }
141 |
142 | @keyframes slideInFromTop {
143 | 0% {
144 | transform: translateY(-100%);
145 | opacity: 0;
146 | }
147 | 100% {
148 | transform: translateY(0);
149 | opacity: 1;
150 | }
151 | }
152 |
153 | @keyframes slideInFromBottom {
154 | 0% {
155 | transform: translateY(100%);
156 | opacity: 0;
157 | }
158 | 100% {
159 | transform: translateY(0);
160 | opacity: 1;
161 | }
162 | }
163 |
164 | @keyframes glow {
165 | 0% {
166 | text-shadow: 0 0 10px #00ffd5;
167 | }
168 | 100% {
169 | text-shadow: 0 0 20px #00ffd5, 0 0 30px #00ffd5;
170 | }
171 | }
172 |
173 | @keyframes pulseButton {
174 | 0% {
175 | transform: scale(1);
176 | }
177 | 100% {
178 | transform: scale(1.1);
179 | }
180 | }
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
--------------------------------------------------------------------------------
/group_assignment/maze.css:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | /* Global Styles */
34 | body {
35 | font-family: 'Segoe UI', sans-serif;
36 | background: linear-gradient(to right, #0f2027, #203a43, #2c5364);
37 | color: #fff;
38 | margin: 0;
39 | padding: 0;
40 | text-align: center;
41 | animation: fadeIn 2s ease-in-out; /* Fade-in effect */
42 | }
43 |
44 | /* Header Styles */
45 | header {
46 | padding: 30px 10px;
47 | background-color: #111;
48 | border-bottom: 2px solid #555;
49 | animation: slideInFromTop 1s ease-out; /* Slide-in animation */
50 | }
51 |
52 | h1 {
53 | font-size: 2.5rem;
54 | margin-bottom: 10px;
55 | color: #00ffd5;
56 | text-shadow: 0 0 10px #00ffd5;
57 | animation: glow 1.5s infinite alternate; /* Glowing effect */
58 | }
59 |
60 | header p {
61 | font-size: 1.2rem;
62 | color: #ddd;
63 | }
64 |
65 | /* Main Section Styles */
66 | main {
67 | padding: 30px 15px;
68 | }
69 |
70 | .controls {
71 | margin-bottom: 20px;
72 | }
73 |
74 | button {
75 | padding: 12px 25px;
76 | margin: 10px;
77 | font-size: 16px;
78 | font-weight: bold;
79 | border: none;
80 | border-radius: 8px;
81 | cursor: pointer;
82 | background: #00ffd5;
83 | color: #000;
84 | box-shadow: 0 0 15px #00ffd5, 0 0 30px #00ffd5 inset;
85 | transition: all 0.3s ease;
86 | animation: pulseButton 1s infinite alternate; /* Pulse effect */
87 | }
88 |
89 | button:hover {
90 | background: #00bfa6;
91 | box-shadow: 0 0 25px #00ffd5, 0 0 35px #00ffd5 inset;
92 | transform: scale(1.05);
93 | }
94 |
95 | /* Canvas Container */
96 | .canvas-container {
97 | display: flex;
98 | justify-content: center;
99 | align-items: center;
100 | margin-bottom: 30px;
101 | animation: fadeIn 2s ease-in-out;
102 | }
103 |
104 | canvas {
105 | border: 3px solid #00ffd5;
106 | background-color: #fff;
107 | border-radius: 10px;
108 | box-shadow: 0 0 20px #00ffd5;
109 | transition: transform 0.3s ease;
110 | }
111 |
112 | canvas:hover {
113 | transform: scale(1.05);
114 | }
115 |
116 | /* Instructions Section */
117 | .instructions {
118 | max-width: 600px;
119 | margin: 0 auto;
120 | text-align: left;
121 | background: #222;
122 | padding: 20px;
123 | border-radius: 10px;
124 | box-shadow: 0 0 20px #00ffd5;
125 | animation: slideInFromBottom 1s ease-out; /* Slide-in from bottom */
126 | }
127 |
128 | .instructions h2 {
129 | color: #00ffd5;
130 | margin-bottom: 10px;
131 | }
132 |
133 | .instructions ul {
134 | padding-left: 20px;
135 | }
136 |
137 | /* Footer Styles */
138 | footer {
139 | background: #111;
140 | padding: 15px;
141 | border-top: 2px solid #555;
142 | font-size: 14px;
143 | color: #aaa;
144 | animation: fadeIn 3s ease-in-out; /* Fade-in effect */
145 | }
146 |
147 | /* Keyframes for Animations */
148 | @keyframes fadeIn {
149 | 0% {
150 | opacity: 0;
151 | }
152 | 100% {
153 | opacity: 1;
154 | }
155 | }
156 |
157 | @keyframes slideInFromTop {
158 | 0% {
159 | transform: translateY(-100%);
160 | opacity: 0;
161 | }
162 | 100% {
163 | transform: translateY(0);
164 | opacity: 1;
165 | }
166 | }
167 |
168 | @keyframes slideInFromBottom {
169 | 0% {
170 | transform: translateY(100%);
171 | opacity: 0;
172 | }
173 | 100% {
174 | transform: translateY(0);
175 | opacity: 1;
176 | }
177 | }
178 |
179 | @keyframes glow {
180 | 0% {
181 | text-shadow: 0 0 10px #00ffd5;
182 | }
183 | 100% {
184 | text-shadow: 0 0 20px #00ffd5, 0 0 30px #00ffd5;
185 | }
186 | }
187 |
188 | @keyframes pulseButton {
189 | 0% {
190 | transform: scale(1);
191 | }
192 | 100% {
193 | transform: scale(1.1);
194 | }
195 | }
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
--------------------------------------------------------------------------------
/group_assignment/new.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Maze Game
7 |
8 |
9 |
10 |
11 |
12 |
13 | Generating Maze...
14 |
15 |
16 |
Maze Game
17 |
18 |
19 |
20 |
21 |
22 |
25 |
26 |
29 |
30 |
199 |
200 |
201 |
202 |
--------------------------------------------------------------------------------
/group_assignment/script.js:
--------------------------------------------------------------------------------
1 | const canvas = document.getElementById("mazeCanvas");
2 | const ctx = canvas.getContext("2d");
3 |
4 | const rows = 20;
5 | const cols = 20;
6 | const size = canvas.width / cols;
7 | let grid = [];
8 | let stack = [];
9 | let player;
10 |
11 | class Cell {
12 | constructor(i, j) {
13 | this.i = i;
14 | this.j = j;
15 | this.walls = [true, true, true, true]; // top, right, bottom, left
16 | this.visited = false;
17 | this.previous = null;
18 | }
19 |
20 | draw() {
21 | const x = this.j * size;
22 | const y = this.i * size;
23 | ctx.strokeStyle = "black";
24 | ctx.lineWidth = 2;
25 |
26 | if (this.walls[0]) drawLine(x, y, x + size, y); // top
27 | if (this.walls[1]) drawLine(x + size, y, x + size, y + size); // right
28 | if (this.walls[2]) drawLine(x + size, y + size, x, y + size); // bottom
29 | if (this.walls[3]) drawLine(x, y + size, x, y); // left
30 |
31 | if (this.visited) {
32 | ctx.fillStyle = "#eee";
33 | ctx.fillRect(x, y, size, size);
34 | }
35 | }
36 |
37 | highlight(color) {
38 | const x = this.j * size;
39 | const y = this.i * size;
40 | ctx.fillStyle = color;
41 | ctx.fillRect(x, y, size, size);
42 | }
43 |
44 | getUnvisitedNeighbors() {
45 | let neighbors = [];
46 |
47 | const top = grid[index(this.i - 1, this.j)];
48 | const right = grid[index(this.i, this.j + 1)];
49 | const bottom = grid[index(this.i + 1, this.j)];
50 | const left = grid[index(this.i, this.j - 1)];
51 |
52 | if (top && !top.visited) neighbors.push(top);
53 | if (right && !right.visited) neighbors.push(right);
54 | if (bottom && !bottom.visited) neighbors.push(bottom);
55 | if (left && !left.visited) neighbors.push(left);
56 |
57 | return neighbors;
58 | }
59 | }
60 |
61 | function index(i, j) {
62 | if (i < 0 || j < 0 || i >= rows || j >= cols) return -1;
63 | return i * cols + j;
64 | }
65 |
66 | function drawLine(x1, y1, x2, y2) {
67 | ctx.beginPath();
68 | ctx.moveTo(x1, y1);
69 | ctx.lineTo(x2, y2);
70 | ctx.stroke();
71 | }
72 |
73 | function setupGrid() {
74 | grid = [];
75 | for (let i = 0; i < rows; i++) {
76 | for (let j = 0; j < cols; j++) {
77 | grid.push(new Cell(i, j));
78 | }
79 | }
80 | }
81 |
82 | function removeWalls(a, b) {
83 | let x = a.j - b.j;
84 | let y = a.i - b.i;
85 |
86 | if (x === 1) {
87 | a.walls[3] = false;
88 | b.walls[1] = false;
89 | } else if (x === -1) {
90 | a.walls[1] = false;
91 | b.walls[3] = false;
92 | }
93 |
94 | if (y === 1) {
95 | a.walls[0] = false;
96 | b.walls[2] = false;
97 | } else if (y === -1) {
98 | a.walls[2] = false;
99 | b.walls[0] = false;
100 | }
101 | }
102 |
103 | function drawGrid() {
104 | ctx.clearRect(0, 0, canvas.width, canvas.height);
105 | grid.forEach(cell => cell.draw());
106 | }
107 |
108 | async function generateMaze() {
109 | setupGrid();
110 | let current = grid[0];
111 | current.visited = true;
112 | stack = [];
113 |
114 | while (true) {
115 | drawGrid();
116 | current.highlight("green");
117 |
118 | let neighbors = current.getUnvisitedNeighbors();
119 | if (neighbors.length > 0) {
120 | let next = neighbors[Math.floor(Math.random() * neighbors.length)];
121 | stack.push(current);
122 | removeWalls(current, next);
123 | current = next;
124 | current.visited = true;
125 | } else if (stack.length > 0) {
126 | current = stack.pop();
127 | } else {
128 | break;
129 | }
130 |
131 | await new Promise(r => setTimeout(r, 10));
132 | }
133 |
134 | player = grid[0];
135 | drawGrid();
136 | highlightGoal();
137 | drawPlayer();
138 | }
139 |
140 | function drawPlayer() {
141 | const x = player.j * size + size / 4;
142 | const y = player.i * size + size / 4;
143 | ctx.fillStyle = "red";
144 | ctx.beginPath();
145 | ctx.arc(x + size / 4, y + size / 4, size / 4, 0, Math.PI * 2);
146 | ctx.fill();
147 | }
148 |
149 | function highlightGoal() {
150 | const goal = grid[grid.length - 1];
151 | const x = goal.j * size;
152 | const y = goal.i * size;
153 | ctx.fillStyle = "gold";
154 | ctx.fillRect(x, y, size, size);
155 | }
156 |
157 | function resetVisited() {
158 | grid.forEach(cell => {
159 | cell.visited = false;
160 | cell.previous = null;
161 | });
162 | }
163 |
164 | async function solveDFS() {
165 | resetVisited();
166 | let stack = [grid[0]];
167 | grid[0].visited = true;
168 |
169 | while (stack.length > 0) {
170 | drawGrid();
171 | let current = stack.pop();
172 | current.highlight("lightgreen");
173 |
174 | if (current === grid[grid.length - 1]) {
175 | await showPath(current);
176 | return;
177 | }
178 |
179 | let neighbors = getAvailableNeighbors(current);
180 | for (let neighbor of neighbors) {
181 | if (!neighbor.visited) {
182 | neighbor.visited = true;
183 | neighbor.previous = current;
184 | stack.push(neighbor);
185 | }
186 | }
187 |
188 | await new Promise(r => setTimeout(r, 20));
189 | }
190 | }
191 |
192 | async function solveBFS() {
193 | resetVisited();
194 | let queue = [grid[0]];
195 | grid[0].visited = true;
196 |
197 | while (queue.length > 0) {
198 | drawGrid();
199 | let current = queue.shift();
200 | current.highlight("skyblue");
201 |
202 | if (current === grid[grid.length - 1]) {
203 | await showPath(current);
204 | return;
205 | }
206 |
207 | let neighbors = getAvailableNeighbors(current);
208 | for (let neighbor of neighbors) {
209 | if (!neighbor.visited) {
210 | neighbor.visited = true;
211 | neighbor.previous = current;
212 | queue.push(neighbor);
213 | }
214 | }
215 |
216 | await new Promise(r => setTimeout(r, 20));
217 | }
218 | }
219 |
220 | function getAvailableNeighbors(cell) {
221 | let neighbors = [];
222 |
223 | const top = grid[index(cell.i - 1, cell.j)];
224 | const right = grid[index(cell.i, cell.j + 1)];
225 | const bottom = grid[index(cell.i + 1, cell.j)];
226 | const left = grid[index(cell.i, cell.j - 1)];
227 |
228 | if (top && !cell.walls[0]) neighbors.push(top);
229 | if (right && !cell.walls[1]) neighbors.push(right);
230 | if (bottom && !cell.walls[2]) neighbors.push(bottom);
231 | if (left && !cell.walls[3]) neighbors.push(left);
232 |
233 | return neighbors;
234 | }
235 |
236 | async function showPath(cell) {
237 | let path = [];
238 | while (cell) {
239 | path.push(cell);
240 | cell = cell.previous;
241 | }
242 | path.reverse();
243 |
244 | for (let step of path) {
245 | player = step;
246 | drawGrid();
247 | highlightGoal();
248 | drawPlayer();
249 | await new Promise(r => setTimeout(r, 100));
250 | }
251 |
252 | setTimeout(() => alert("🎉 Maze Solved Automatically!"), 100);
253 | }
254 |
255 | document.addEventListener("keydown", e => {
256 | let moved = false;
257 | if (e.key === "ArrowUp" || e.key === "w") {
258 | if (!player.walls[0]) {
259 | player = grid[index(player.i - 1, player.j)];
260 | moved = true;
261 | }
262 | } else if (e.key === "ArrowRight" || e.key === "d") {
263 | if (!player.walls[1]) {
264 | player = grid[index(player.i, player.j + 1)];
265 | moved = true;
266 | }
267 | } else if (e.key === "ArrowDown" || e.key === "s") {
268 | if (!player.walls[2]) {
269 | player = grid[index(player.i + 1, player.j)];
270 | moved = true;
271 | }
272 | } else if (e.key === "ArrowLeft" || e.key === "a") {
273 | if (!player.walls[3]) {
274 | player = grid[index(player.i, player.j - 1)];
275 | moved = true;
276 | }
277 | }
278 |
279 | if (moved) {
280 | drawGrid();
281 | highlightGoal();
282 | drawPlayer();
283 |
284 | if (player === grid[grid.length - 1]) {
285 | setTimeout(() => alert("🎉 You reached the goal!"), 100);
286 | }
287 | }
288 | });
289 |
--------------------------------------------------------------------------------
/group_assignment/hs.js:
--------------------------------------------------------------------------------
1 | // const canvas = document.getElementById('mazeCanvas');
2 | // const ctx = canvas.getContext('2d');
3 |
4 | // const cols = 20;
5 | // const rows = 20;
6 | // const cellSize = canvas.width / cols;
7 |
8 | // let grid = [];
9 | // let stack = [];
10 | // let current;
11 |
12 | // let solving = false;
13 |
14 | // // Cell constructor
15 | // class Cell {
16 | // constructor(i, j) {
17 | // this.i = i;
18 | // this.j = j;
19 | // this.walls = [true, true, true, true]; // top, right, bottom, left
20 | // this.visited = false;
21 | // this.solutionVisited = false;
22 | // }
23 |
24 | // draw() {
25 | // const x = this.i * cellSize;
26 | // const y = this.j * cellSize;
27 |
28 | // ctx.strokeStyle = '#222';
29 | // ctx.lineWidth = 2;
30 |
31 | // if (this.walls[0]) line(x, y, x + cellSize, y); // top
32 | // if (this.walls[1]) line(x + cellSize, y, x + cellSize, y + cellSize); // right
33 | // if (this.walls[2]) line(x + cellSize, y + cellSize, x, y + cellSize); // bottom
34 | // if (this.walls[3]) line(x, y + cellSize, x, y); // left
35 |
36 | // if (this.visited) {
37 | // ctx.fillStyle = '#b2fefa';
38 | // ctx.fillRect(x, y, cellSize, cellSize);
39 | // }
40 | // }
41 |
42 | // highlight(color) {
43 | // const x = this.i * cellSize;
44 | // const y = this.j * cellSize;
45 | // ctx.fillStyle = color;
46 | // ctx.fillRect(x, y, cellSize, cellSize);
47 | // }
48 |
49 | // getNeighbors() {
50 | // const neighbors = [];
51 |
52 | // const top = grid[index(this.i, this.j - 1)];
53 | // const right = grid[index(this.i + 1, this.j)];
54 | // const bottom = grid[index(this.i, this.j + 1)];
55 | // const left = grid[index(this.i - 1, this.j)];
56 |
57 | // if (top && !top.visited) neighbors.push(top);
58 | // if (right && !right.visited) neighbors.push(right);
59 | // if (bottom && !bottom.visited) neighbors.push(bottom);
60 | // if (left && !left.visited) neighbors.push(left);
61 |
62 | // return neighbors;
63 | // }
64 |
65 | // getUnblockedNeighbors() {
66 | // const neighbors = [];
67 |
68 | // const top = grid[index(this.i, this.j - 1)];
69 | // const right = grid[index(this.i + 1, this.j)];
70 | // const bottom = grid[index(this.i, this.j + 1)];
71 | // const left = grid[index(this.i - 1, this.j)];
72 |
73 | // if (top && !this.walls[0]) neighbors.push(top);
74 | // if (right && !this.walls[1]) neighbors.push(right);
75 | // if (bottom && !this.walls[2]) neighbors.push(bottom);
76 | // if (left && !this.walls[3]) neighbors.push(left);
77 |
78 | // return neighbors;
79 | // }
80 | // }
81 |
82 | // function index(i, j) {
83 | // if (i < 0 || j < 0 || i >= cols || j >= rows) return -1;
84 | // return i + j * cols;
85 | // }
86 |
87 | // function line(x1, y1, x2, y2) {
88 | // ctx.beginPath();
89 | // ctx.moveTo(x1, y1);
90 | // ctx.lineTo(x2, y2);
91 | // ctx.stroke();
92 | // }
93 |
94 | // function setup() {
95 | // grid = [];
96 | // stack = [];
97 | // for (let j = 0; j < rows; j++) {
98 | // for (let i = 0; i < cols; i++) {
99 | // grid.push(new Cell(i, j));
100 | // }
101 | // }
102 | // current = grid[0];
103 | // current.visited = true;
104 | // stack.push(current);
105 | // generate();
106 | // }
107 |
108 | // function generate() {
109 | // const interval = setInterval(() => {
110 | // if (stack.length > 0) {
111 | // const current = stack[stack.length - 1];
112 | // current.visited = true;
113 |
114 | // const neighbors = current.getNeighbors();
115 | // if (neighbors.length > 0) {
116 | // const next = neighbors[Math.floor(Math.random() * neighbors.length)];
117 | // removeWalls(current, next);
118 | // next.visited = true;
119 | // stack.push(next);
120 | // } else {
121 | // stack.pop();
122 | // }
123 |
124 | // drawGrid();
125 | // } else {
126 | // clearInterval(interval);
127 | // drawGrid();
128 | // }
129 | // }, 10);
130 | // }
131 |
132 | // function removeWalls(a, b) {
133 | // const x = a.i - b.i;
134 | // const y = a.j - b.j;
135 |
136 | // if (x === 1) {
137 | // a.walls[3] = false;
138 | // b.walls[1] = false;
139 | // } else if (x === -1) {
140 | // a.walls[1] = false;
141 | // b.walls[3] = false;
142 | // }
143 |
144 | // if (y === 1) {
145 | // a.walls[0] = false;
146 | // b.walls[2] = false;
147 | // } else if (y === -1) {
148 | // a.walls[2] = false;
149 | // b.walls[0] = false;
150 | // }
151 | // }
152 |
153 | // function drawGrid() {
154 | // ctx.clearRect(0, 0, canvas.width, canvas.height);
155 | // for (let cell of grid) {
156 | // cell.draw();
157 | // }
158 |
159 | // // start
160 | // grid[0].highlight('#0f0');
161 | // // end
162 | // grid[grid.length - 1].highlight('#f00');
163 | // }
164 |
165 | // function solveMaze() {
166 | // if (solving) return;
167 |
168 | // solving = true;
169 | // let path = [];
170 | // let visited = new Set();
171 |
172 | // const start = grid[0];
173 | // const end = grid[grid.length - 1];
174 |
175 | // function dfs(cell) {
176 | // path.push(cell);
177 | // cell.solutionVisited = true;
178 | // drawSolution(path);
179 |
180 | // if (cell === end) {
181 | // solving = false;
182 | // return true;
183 | // }
184 |
185 | // const neighbors = cell.getUnblockedNeighbors();
186 | // for (let neighbor of neighbors) {
187 | // if (!visited.has(neighbor)) {
188 | // visited.add(neighbor);
189 | // if (dfs(neighbor)) return true;
190 | // }
191 | // }
192 |
193 | // path.pop();
194 | // drawSolution(path);
195 | // return false;
196 | // }
197 |
198 | // dfs(start);
199 | // }
200 |
201 | // function drawSolution(path) {
202 | // setTimeout(() => {
203 | // drawGrid();
204 | // for (let cell of path) {
205 | // cell.highlight('rgba(255,255,0,0.6)');
206 | // }
207 | // }, 10);
208 | // }
209 |
210 | // function generateMaze() {
211 | // setup();
212 | // }
213 |
214 | // function resetMaze() {
215 | // setup();
216 | // drawGrid();
217 | // }
218 |
219 | // // Initialize
220 | // setup();
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 | let grid = [];
239 | let solving = false;
240 |
241 | function generateMaze() {
242 | grid = [];
243 | for (let row = 0; row < 20; row++) {
244 | let newRow = [];
245 | for (let col = 0; col < 20; col++) {
246 | newRow.push(new Cell(row, col));
247 | }
248 | grid.push(newRow);
249 | }
250 | addWalls();
251 | drawGrid();
252 | }
253 |
254 | function addWalls() {
255 | // Add random walls to the grid
256 | for (let row = 0; row < 20; row++) {
257 | for (let col = 0; col < 20; col++) {
258 | if (Math.random() < 0.2) {
259 | grid[row][col].blocked = true;
260 | }
261 | }
262 | }
263 | // Make sure the start and end are not blocked
264 | grid[0][0].blocked = false;
265 | grid[19][19].blocked = false;
266 | }
267 |
268 | function drawGrid() {
269 | let gridElement = document.getElementById("grid");
270 | gridElement.innerHTML = "";
271 | for (let row = 0; row < grid.length; row++) {
272 | for (let col = 0; col < grid[row].length; col++) {
273 | let cell = grid[row][col];
274 | let cellElement = document.createElement("div");
275 | cellElement.classList.add("cell");
276 | if (cell.blocked) {
277 | cellElement.classList.add("blocked");
278 | }
279 | if (cell.solutionVisited) {
280 | cellElement.classList.add("visited");
281 | }
282 | if (cell === grid[0][0]) {
283 | cellElement.classList.add("start");
284 | }
285 | if (cell === grid[19][19]) {
286 | cellElement.classList.add("end");
287 | }
288 | gridElement.appendChild(cellElement);
289 | }
290 | }
291 | }
292 |
293 | function solveMaze(isBFS = false) {
294 | if (solving) return;
295 |
296 | solving = true;
297 | let path = [];
298 | let visited = new Set();
299 | let queue = [];
300 |
301 | const start = grid[0][0];
302 | const end = grid[19][19];
303 |
304 | // BFS implementation
305 | if (isBFS) {
306 | queue.push(start);
307 | visited.add(start);
308 | let parent = new Map(); // To track the path
309 |
310 | while (queue.length > 0) {
311 | let current = queue.shift(); // dequeue
312 | path.push(current);
313 | drawSolution(path);
314 |
315 | if (current === end) {
316 | solving = false;
317 | break;
318 | }
319 |
320 | const neighbors = current.getUnblockedNeighbors();
321 | for (let neighbor of neighbors) {
322 | if (!visited.has(neighbor)) {
323 | visited.add(neighbor);
324 | queue.push(neighbor); // enqueue
325 | parent.set(neighbor, current); // Track path
326 | }
327 | }
328 | }
329 |
330 | // Trace back the path
331 | let current = end;
332 | let finalPath = [];
333 | while (parent.has(current)) {
334 | finalPath.push(current);
335 | current = parent.get(current);
336 | }
337 | finalPath.reverse();
338 | path = finalPath;
339 | } else {
340 | // DFS implementation
341 | function dfs(cell) {
342 | path.push(cell);
343 | cell.solutionVisited = true;
344 | drawSolution(path);
345 |
346 | if (cell === end) {
347 | solving = false;
348 | return true;
349 | }
350 |
351 | const neighbors = cell.getUnblockedNeighbors();
352 | for (let neighbor of neighbors) {
353 | if (!visited.has(neighbor)) {
354 | visited.add(neighbor);
355 | if (dfs(neighbor)) return true;
356 | }
357 | }
358 |
359 | path.pop();
360 | drawSolution(path);
361 | return false;
362 | }
363 |
364 | dfs(start);
365 | }
366 | }
367 |
368 | function drawSolution(path) {
369 | setTimeout(() => {
370 | drawGrid();
371 | for (let cell of path) {
372 | cell.highlight('rgba(255,255,0,0.6)');
373 | }
374 | }, 10);
375 | }
376 |
377 | class Cell {
378 | constructor(row, col) {
379 | this.row = row;
380 | this.col = col;
381 | this.blocked = false;
382 | this.solutionVisited = false;
383 | }
384 |
385 | getUnblockedNeighbors() {
386 | let neighbors = [];
387 | if (this.row > 0 && !grid[this.row - 1][this.col].blocked) neighbors.push(grid[this.row - 1][this.col]); // Up
388 | if (this.row < grid.length - 1 && !grid[this.row + 1][this.col].blocked) neighbors.push(grid[this.row + 1][this.col]); // Down
389 | if (this.col > 0 && !grid[this.row][this.col - 1].blocked) neighbors.push(grid[this.row][this.col - 1]); // Left
390 | if (this.col < grid[0].length - 1 && !grid[this.row][this.col + 1].blocked) neighbors.push(grid[this.row][this.col + 1]); // Right
391 | return neighbors;
392 | }
393 |
394 | highlight(color) {
395 | const gridElement = document.getElementById("grid");
396 | const index = this.row * grid[0].length + this.col;
397 | const cellElement = gridElement.children[index];
398 | cellElement.style.backgroundColor = color;
399 | }
400 | }
401 |
402 | // HTML buttons functionality
403 | document.getElementById("generate").addEventListener("click", () => {
404 | generateMaze();
405 | });
406 |
407 | document.getElementById("solveDFS").addEventListener("click", () => {
408 | solveMaze(false); // DFS
409 | });
410 |
411 | document.getElementById("solveBFS").addEventListener("click", () => {
412 | solveMaze(true); // BFS
413 | });
414 |
415 | generateMaze();
416 |
--------------------------------------------------------------------------------
/group_assignment/Style1.css:
--------------------------------------------------------------------------------
1 | /* * {
2 | box-sizing: border-box;
3 | padding: 0;
4 | margin: 0;
5 | }
6 |
7 | body {
8 | font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
9 | display: flex;
10 | justify-content: center;
11 | align-items: center;
12 | height: 100vh;
13 | background: linear-gradient(-45deg, #0f2027, #203a43, #2c5364, #1e3c72);
14 | background-size: 400% 400%;
15 | animation: bgAnimation 20s ease infinite;
16 | color: #f0f0f0;
17 | overflow: hidden;
18 | }
19 |
20 | @keyframes bgAnimation {
21 | 0% { background-position: 0% 50%; }
22 | 50% { background-position: 100% 50%; }
23 | 100% { background-position: 0% 50%; }
24 | }
25 |
26 | #app {
27 | text-align: center;
28 | padding: 40px;
29 | background-color: rgba(0, 0, 0, 0.4);
30 | border-radius: 25px;
31 | backdrop-filter: blur(10px);
32 | box-shadow: 0 0 40px rgba(0, 255, 255, 0.3);
33 | animation: fadeIn 1.5s ease-in-out;
34 | transition: all 0.4s ease-in-out;
35 | }
36 |
37 | @keyframes fadeIn {
38 | 0% { opacity: 0; transform: scale(0.9); }
39 | 100% { opacity: 1; transform: scale(1); }
40 | }
41 |
42 | canvas {
43 | border: 4px solid #00ffcc;
44 | border-radius: 20px;
45 | margin-bottom: 25px;
46 | box-shadow: 0 0 30px #00ffcc;
47 | background-color: #000;
48 | transition: box-shadow 0.4s ease, transform 0.4s ease;
49 | }
50 |
51 | canvas:hover {
52 | box-shadow: 0 0 40px #00ffff, 0 0 10px #00ffff inset;
53 | transform: scale(1.02);
54 | }
55 |
56 | .controls {
57 | display: flex;
58 | justify-content: center;
59 | gap: 20px;
60 | flex-wrap: wrap;
61 | margin-top: 20px;
62 | }
63 |
64 | button {
65 | padding: 14px 30px;
66 | font-size: 18px;
67 | font-weight: bold;
68 | cursor: pointer;
69 | border: none;
70 | border-radius: 35px;
71 | background: linear-gradient(135deg, #1e90ff, #00ffff);
72 | color: #000;
73 | box-shadow: 0 0 20px #00ffff;
74 | transition: all 0.4s ease-in-out;
75 | position: relative;
76 | overflow: hidden;
77 | }
78 |
79 | button::before {
80 | content: "";
81 | position: absolute;
82 | width: 300%;
83 | height: 300%;
84 | top: -100%;
85 | left: -100%;
86 | background: radial-gradient(circle, rgba(255,255,255,0.3) 0%, transparent 70%);
87 | animation: shine 3s infinite linear;
88 | opacity: 0.4;
89 | }
90 |
91 | @keyframes shine {
92 | 0% {
93 | transform: translate(0, 0);
94 | }
95 | 100% {
96 | transform: translate(100%, 100%);
97 | }
98 | }
99 |
100 | button:hover {
101 | background: linear-gradient(135deg, #00ffff, #1e90ff);
102 | transform: scale(1.08);
103 | box-shadow: 0 0 30px #00ffff;
104 | }
105 |
106 | h1 {
107 | font-size: 36px;
108 | font-weight: bold;
109 | color: #00ffff;
110 | text-shadow: 0 0 10px #00ffff, 0 0 20px #00ffff;
111 | margin-bottom: 20px;
112 | animation: glow 2s ease-in-out infinite alternate;
113 | }
114 |
115 | @keyframes glow {
116 | 0% {
117 | text-shadow: 0 0 10px #00ffff, 0 0 20px #00ffff;
118 | }
119 | 100% {
120 | text-shadow: 0 0 20px #00ffff, 0 0 30px #00ffff;
121 | }
122 | }
123 |
124 | .wall {
125 | position: absolute;
126 | top: 0;
127 | left: 0;
128 | width: 100%;
129 | height: 100%;
130 | background-image:
131 | linear-gradient(90deg, rgba(0, 255, 255, 0.1) 1px, transparent 1px),
132 | linear-gradient(rgba(0, 255, 255, 0.1) 1px, transparent 1px);
133 | background-size: 40px 40px;
134 | z-index: 0;
135 | animation: moveWall 20s linear infinite;
136 | }
137 |
138 | @keyframes moveWall {
139 | 0% {
140 | background-position: 0 0;
141 | }
142 | 100% {
143 | background-position: 200px 200px;
144 | }
145 | }
146 |
147 |
148 |
149 | */
150 | */
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 | /* * {
163 | box-sizing: border-box;
164 | margin: 0;
165 | padding: 0;
166 | }
167 |
168 | body {
169 | font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
170 | height: 100vh;
171 | background: radial-gradient(circle at top left, #0f2027, #203a43, #2c5364, #1e3c72);
172 | background-size: 600% 600%;
173 | animation: bgAnimation 25s ease infinite;
174 | display: flex;
175 | justify-content: center;
176 | align-items: center;
177 | overflow: hidden;
178 | color: #ffffff;
179 | position: relative;
180 | }
181 |
182 | @keyframes bgAnimation {
183 | 0% { background-position: 0% 50%; }
184 | 50% { background-position: 100% 50%; }
185 | 100% { background-position: 0% 50%; }
186 | }
187 |
188 | .wall {
189 | position: absolute;
190 | width: 100%;
191 | height: 100%;
192 | background-image:
193 | linear-gradient(90deg, rgba(0, 255, 255, 0.07) 1px, transparent 1px),
194 | linear-gradient(rgba(0, 255, 255, 0.07) 1px, transparent 1px);
195 | background-size: 60px 60px;
196 | z-index: 0;
197 | animation: moveWall 40s linear infinite;
198 | }
199 |
200 | @keyframes moveWall {
201 | 0% { background-position: 0 0; }
202 | 100% { background-position: 300px 300px; }
203 | }
204 |
205 | #app {
206 | position: relative;
207 | z-index: 2;
208 | padding: 60px 50px;
209 | background: rgba(0, 0, 0, 0.55);
210 | border-radius: 30px;
211 | backdrop-filter: blur(20px);
212 | box-shadow: 0 0 50px #00fff7, 0 0 100px #00fff7;
213 | text-align: center;
214 | animation: fadeZoom 1.5s ease-in-out;
215 | }
216 |
217 | @keyframes fadeZoom {
218 | 0% { opacity: 0; transform: scale(0.8); }
219 | 100% { opacity: 1; transform: scale(1); }
220 | }
221 |
222 | h1 {
223 | font-size: 48px;
224 | font-weight: bold;
225 | color: #00fff7;
226 | margin-bottom: 30px;
227 | text-shadow: 0 0 15px #00fff7, 0 0 35px #00fff7;
228 | animation: pulseGlow 3s ease-in-out infinite;
229 | }
230 |
231 | @keyframes pulseGlow {
232 | 0% { text-shadow: 0 0 15px #00fff7; }
233 | 50% { text-shadow: 0 0 35px #00fff7, 0 0 15px #00fff7; }
234 | 100% { text-shadow: 0 0 15px #00fff7; }
235 | }
236 |
237 | canvas {
238 | border: 6px solid;
239 | border-image: linear-gradient(45deg, #00f2fe, #4facfe) 1;
240 | border-radius: 25px;
241 | background-color: #000;
242 | margin-bottom: 30px;
243 | box-shadow: 0 0 30px #00ffff;
244 | transition: transform 0.5s ease, box-shadow 0.5s ease;
245 | }
246 |
247 | canvas:hover {
248 | transform: scale(1.08) rotate(1deg);
249 | box-shadow: 0 0 50px #00ffff, 0 0 20px #00ffff inset;
250 | }
251 |
252 | .controls {
253 | display: flex;
254 | flex-wrap: wrap;
255 | justify-content: center;
256 | gap: 25px;
257 | margin-top: 30px;
258 | }
259 |
260 | button {
261 | padding: 14px 40px;
262 | font-size: 20px;
263 | font-weight: bold;
264 | border: none;
265 | border-radius: 50px;
266 | background: transparent;
267 | color: #00fff7;
268 | border: 2px solid #00fff7;
269 | box-shadow: 0 0 20px #00fff7;
270 | cursor: pointer;
271 | position: relative;
272 | overflow: hidden;
273 | z-index: 1;
274 | transition: all 0.4s ease;
275 | }
276 |
277 | button::before {
278 | content: '';
279 | position: absolute;
280 | top: -100%;
281 | left: -100%;
282 | width: 300%;
283 | height: 300%;
284 | background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
285 | animation: shinyEffect 6s linear infinite;
286 | z-index: 0;
287 | opacity: 0.5;
288 | }
289 |
290 | @keyframes shinyEffect {
291 | 0% { transform: translate(0%, 0%); }
292 | 100% { transform: translate(100%, 100%); }
293 | }
294 |
295 | button:hover {
296 | transform: scale(1.15);
297 | background-color: rgba(0, 255, 255, 0.2);
298 | color: #ffffff;
299 | box-shadow: 0 0 30px #00ffff, 0 0 10px #00ffff inset;
300 | border-color: #00ffff;
301 | }
302 | */ */
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 | /* * {
322 | box-sizing: border-box;
323 | margin: 0;
324 | padding: 0;
325 | }
326 |
327 | body {
328 | font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
329 | height: 100vh;
330 | background: linear-gradient(-45deg, #0f2027, #203a43, #2c5364, #1e3c72);
331 | background-size: 400% 400%;
332 | animation: bgAnimation 20s ease infinite;
333 | color: #f0f0f0;
334 | overflow: hidden;
335 | display: flex;
336 | justify-content: center;
337 | align-items: center;
338 | perspective: 1000px;
339 | }
340 |
341 | @keyframes bgAnimation {
342 | 0% { background-position: 0% 50%; }
343 | 50% { background-position: 100% 50%; }
344 | 100% { background-position: 0% 50%; }
345 | }
346 |
347 | #app {
348 | text-align: center;
349 | padding: 50px;
350 | background-color: rgba(0, 0, 0, 0.5);
351 | border-radius: 30px;
352 | backdrop-filter: blur(12px);
353 | box-shadow: 0 0 60px rgba(0, 255, 255, 0.4);
354 | transform: rotateX(10deg) rotateY(10deg);
355 | animation: popUp 1s ease forwards;
356 | transition: all 0.5s ease-in-out;
357 | }
358 |
359 | @keyframes popUp {
360 | 0% { opacity: 0; transform: scale(0.8) rotateX(20deg); }
361 | 100% { opacity: 1; transform: scale(1) rotateX(10deg); }
362 | }
363 |
364 | h1 {
365 | font-size: 42px;
366 | font-weight: bold;
367 | color: #00ffff;
368 | text-shadow: 0 0 15px #00ffff, 0 0 30px #00ffff;
369 | animation: floatText 3s ease-in-out infinite;
370 | margin-bottom: 25px;
371 | }
372 |
373 | @keyframes floatText {
374 | 0%, 100% { transform: translateY(0); }
375 | 50% { transform: translateY(-10px); }
376 | }
377 |
378 | canvas {
379 | border: 5px solid #00ffcc;
380 | border-radius: 25px;
381 | box-shadow: 0 0 25px #00ffcc;
382 | margin-bottom: 30px;
383 | background-color: #000;
384 | transition: transform 0.4s, box-shadow 0.4s;
385 | }
386 |
387 | canvas:hover {
388 | transform: scale(1.05) rotateZ(1deg);
389 | box-shadow: 0 0 40px #00ffff, 0 0 15px #00ffff inset;
390 | }
391 |
392 | .controls {
393 | display: flex;
394 | flex-wrap: wrap;
395 | justify-content: center;
396 | gap: 25px;
397 | margin-top: 30px;
398 | }
399 |
400 | button {
401 | padding: 14px 32px;
402 | font-size: 18px;
403 | font-weight: bold;
404 | border: none;
405 | border-radius: 50px;
406 | background: linear-gradient(135deg, #1e90ff, #00ffff);
407 | color: #000;
408 | cursor: pointer;
409 | box-shadow: 0 0 25px #00ffff;
410 | transition: transform 0.3s ease, box-shadow 0.3s ease;
411 | position: relative;
412 | overflow: hidden;
413 | }
414 |
415 | button:hover {
416 | transform: scale(1.1) rotate(-1deg);
417 | box-shadow: 0 0 40px #00ffff, 0 0 20px #00ffff inset;
418 | }
419 |
420 | button::before {
421 | content: "";
422 | position: absolute;
423 | top: -150%;
424 | left: -150%;
425 | width: 400%;
426 | height: 400%;
427 | background: radial-gradient(circle, rgba(255,255,255,0.4) 0%, transparent 60%);
428 | animation: sparkle 5s linear infinite;
429 | }
430 |
431 | @keyframes sparkle {
432 | 0% { transform: translate(0, 0); }
433 | 100% { transform: translate(150%, 150%); }
434 | }
435 |
436 | .wall {
437 | position: absolute;
438 | width: 100%;
439 | height: 100%;
440 | top: 0;
441 | left: 0;
442 | background-image:
443 | linear-gradient(90deg, rgba(0, 255, 255, 0.1) 1px, transparent 1px),
444 | linear-gradient(rgba(0, 255, 255, 0.1) 1px, transparent 1px);
445 | background-size: 40px 40px;
446 | z-index: 0;
447 | animation: moveWall 20s linear infinite;
448 | }
449 |
450 | @keyframes moveWall {
451 | 0% { background-position: 0 0; }
452 | 100% { background-position: 200px 200px; }
453 | } */
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
--------------------------------------------------------------------------------
/group_assignment/maze.js:
--------------------------------------------------------------------------------
1 |
2 | const canvas = document.getElementById("mazeCanvas");
3 | const ctx = canvas.getContext("2d");
4 |
5 | const rows = 20;
6 | const cols = 20;
7 | const size = canvas.width / cols;
8 | let grid = [];
9 | let stack = [];
10 | let player;
11 | let solvingMethod = 'DFS';
12 |
13 | class Cell {
14 | constructor(i, j) {
15 | this.i = i;
16 | this.j = j;
17 | this.walls = [true, true, true, true];
18 | this.visited = false;
19 | this.previous = null;
20 | }
21 |
22 | draw() {
23 | const x = this.j * size;
24 | const y = this.i * size;
25 | ctx.strokeStyle = "black";
26 | ctx.lineWidth = 2;
27 |
28 | if (this.walls[0]) drawLine(x, y, x + size, y);
29 | if (this.walls[1]) drawLine(x + size, y, x + size, y + size);
30 | if (this.walls[2]) drawLine(x + size, y + size, x, y + size);
31 | if (this.walls[3]) drawLine(x, y + size, x, y);
32 |
33 | if (this.visited) {
34 | ctx.fillStyle = "#eee";
35 | ctx.fillRect(x, y, size, size);
36 | }
37 | }
38 |
39 | highlight(color) {
40 | const x = this.j * size;
41 | const y = this.i * size;
42 | ctx.fillStyle = color;
43 | ctx.fillRect(x, y, size, size);
44 | }
45 |
46 | getUnvisitedNeighbors() {
47 | let neighbors = [];
48 |
49 | const top = grid[index(this.i - 1, this.j)];
50 | const right = grid[index(this.i, this.j + 1)];
51 | const bottom = grid[index(this.i + 1, this.j)];
52 | const left = grid[index(this.i, this.j - 1)];
53 |
54 | if (top && !top.visited) neighbors.push(top);
55 | if (right && !right.visited) neighbors.push(right);
56 | if (bottom && !bottom.visited) neighbors.push(bottom);
57 | if (left && !left.visited) neighbors.push(left);
58 |
59 | return neighbors;
60 | }
61 | }
62 |
63 | function index(i, j) {
64 | if (i < 0 || j < 0 || i >= rows || j >= cols) return -1;
65 | return i * cols + j;
66 | }
67 |
68 | function drawLine(x1, y1, x2, y2) {
69 | ctx.beginPath();
70 | ctx.moveTo(x1, y1);
71 | ctx.lineTo(x2, y2);
72 | ctx.stroke();
73 | }
74 |
75 | function setupGrid() {
76 | grid = [];
77 | for (let i = 0; i < rows; i++) {
78 | for (let j = 0; j < cols; j++) {
79 | grid.push(new Cell(i, j));
80 | }
81 | }
82 | }
83 |
84 | function removeWalls(a, b) {
85 | let x = a.j - b.j;
86 | let y = a.i - b.i;
87 |
88 | if (x === 1) {
89 | a.walls[3] = false;
90 | b.walls[1] = false;
91 | }
92 | else if (x === -1) {
93 | a.walls[1] = false;
94 | b.walls[3] = false;
95 | }
96 |
97 | if (y === 1) {
98 | a.walls[0] = false;
99 | b.walls[2] = false;
100 | }
101 | else if (y === -1) {
102 | a.walls[2] = false;
103 | b.walls[0] = false;
104 | }
105 | }
106 |
107 | function drawGrid() {
108 | ctx.clearRect(0, 0, canvas.width, canvas.height);
109 | grid.forEach(cell => cell.draw());
110 | }
111 |
112 | async function generateMaze() {
113 | setupGrid();
114 | let current = grid[0];
115 | current.visited = true;
116 | stack = [];
117 |
118 | while (true) {
119 | drawGrid();
120 | current.highlight("green");
121 |
122 | let neighbors = current.getUnvisitedNeighbors();
123 | if (neighbors.length > 0) {
124 | let next = neighbors[Math.floor(Math.random() * neighbors.length)];
125 | stack.push(current);
126 | removeWalls(current, next);
127 | current = next;
128 | current.visited = true;
129 | }
130 | else if (stack.length > 0) {
131 | current = stack.pop();
132 | }
133 | else {
134 | break;
135 | }
136 |
137 | await new Promise(r => setTimeout(r, 10));
138 | }
139 |
140 | player = grid[0];
141 | drawGrid();
142 | highlightGoal();
143 | drawPlayer();
144 | }
145 |
146 | function drawPlayer() {
147 | const x = player.j * size + size / 4;
148 | const y = player.i * size + size / 4;
149 | ctx.fillStyle = "red";
150 | ctx.beginPath();
151 | ctx.arc(x + size / 4, y + size / 4, size / 4, 0, Math.PI * 2);
152 | ctx.fill();
153 | }
154 |
155 | function highlightGoal() {
156 | const goal = grid[grid.length - 1];
157 | const x = goal.j * size;
158 | const y = goal.i * size;
159 | ctx.fillStyle = "gold";
160 | ctx.fillRect(x, y, size, size);
161 | }
162 |
163 | function resetVisited() {
164 | grid.forEach(cell => {
165 | cell.visited = false;
166 | cell.previous = null;
167 | });
168 | }
169 |
170 | async function solveDFS() {
171 | solvingMethod = 'DFS';
172 | resetVisited();
173 | let stack = [grid[0]];
174 | grid[0].visited = true;
175 |
176 | while (stack.length > 0) {
177 | drawGrid();
178 | let current = stack.pop();
179 | current.highlight("lightgreen");
180 |
181 | if (current === grid[grid.length - 1]) {
182 | await showPath(current);
183 | return;
184 | }
185 |
186 | let neighbors = getAvailableNeighbors(current);
187 | for (let neighbor of neighbors) {
188 | if (!neighbor.visited) {
189 | neighbor.visited = true;
190 | neighbor.previous = current;
191 | stack.push(neighbor);
192 | }
193 | }
194 |
195 | await new Promise(r => setTimeout(r, 20));
196 | }
197 | }
198 |
199 | async function solveBFS() {
200 | solvingMethod = 'BFS';
201 | resetVisited();
202 | let queue = [grid[0]];
203 | grid[0].visited = true;
204 |
205 | while (queue.length > 0) {
206 | drawGrid();
207 | let current = queue.shift();
208 | current.highlight("skyblue");
209 |
210 | if (current === grid[grid.length - 1]) {
211 | await showPath(current);
212 | return;
213 | }
214 |
215 | let neighbors = getAvailableNeighbors(current);
216 | for (let neighbor of neighbors) {
217 | if (!neighbor.visited) {
218 | neighbor.visited = true;
219 | neighbor.previous = current;
220 | queue.push(neighbor);
221 | }
222 | }
223 |
224 | await new Promise(r => setTimeout(r, 20));
225 | }
226 | }
227 |
228 | function getAvailableNeighbors(cell) {
229 | let neighbors = [];
230 |
231 | const top = grid[index(cell.i - 1, cell.j)];
232 | const right = grid[index(cell.i, cell.j + 1)];
233 | const bottom = grid[index(cell.i + 1, cell.j)];
234 | const left = grid[index(cell.i, cell.j - 1)];
235 |
236 | if (top && !cell.walls[0]) neighbors.push(top);
237 | if (right && !cell.walls[1]) neighbors.push(right);
238 | if (bottom && !cell.walls[2]) neighbors.push(bottom);
239 | if (left && !cell.walls[3]) neighbors.push(left);
240 |
241 | return neighbors;
242 | }
243 |
244 | async function showPath(cell) {
245 | let path = [];
246 | while (cell) {
247 | path.push(cell);
248 | cell = cell.previous;
249 | }
250 | path.reverse();
251 |
252 | for (let step of path) {
253 | player = step;
254 | drawGrid();
255 | highlightGoal();
256 | drawPlayer();
257 | await new Promise(r => setTimeout(r, 100));
258 | }
259 |
260 | setTimeout(() => alert(" Maze Solved Automatically!"), 100);
261 | }
262 |
263 | document.getElementById("generateBtn").addEventListener("click", generateMaze);
264 | document.getElementById("dfsBtn").addEventListener("click", solveDFS);
265 | document.getElementById("bfsBtn").addEventListener("click", solveBFS);
266 | document.getElementById("resetBtn").addEventListener("click", generateMaze);
267 |
268 |
269 | document.addEventListener("keydown", e => {
270 | let moved = false;
271 | if (e.key === "ArrowUp" || e.key === "w") {
272 | if (!player.walls[0]) {
273 | player = grid[index(player.i - 1, player.j)];
274 | moved = true;
275 | }
276 | }
277 | else if (e.key === "ArrowRight" || e.key === "d") {
278 | if (!player.walls[1]) {
279 | player = grid[index(player.i, player.j + 1)];
280 | moved = true;
281 | }
282 | }
283 | else if (e.key === "ArrowDown" || e.key === "s") {
284 | if (!player.walls[2]) {
285 | player = grid[index(player.i + 1, player.j)];
286 | moved = true;
287 | }
288 | }
289 | else if (e.key === "ArrowLeft" || e.key === "a") {
290 | if (!player.walls[3]) {
291 | player = grid[index(player.i, player.j - 1)];
292 | moved = true;
293 | }
294 | }
295 |
296 | if (moved) {
297 | drawGrid();
298 | highlightGoal();
299 | drawPlayer();
300 |
301 | if (player === grid[grid.length - 1]) {
302 | setTimeout(() => alert(" You reached the goal"), 100);
303 | }
304 | }
305 | });
306 |
307 | const canvas = document.getElementById("mazeCanvas-box");
308 | const ctx = canvas.getContext("2d");
309 |
310 | const rows = 20;
311 | const cols = 20;
312 | const size = canvas.width / cols;
313 | let grid = [];
314 | let stack = [];
315 | let player;
316 | let solvingMethod = 'DFS';
317 |
318 | class Cell {
319 | constructor(i, j) {
320 | this.i = i;
321 | this.j = j;
322 | this.walls = [true, true, true, true];
323 | this.visited = false;
324 | this.previous = null;
325 | }
326 |
327 | draw() {
328 | const x = this.j * size;
329 | const y = this.i * size;
330 | ctx.strokeStyle = "black";
331 | ctx.lineWidth = 2;
332 |
333 | if (this.walls[0]) drawLine(x, y, x + size, y);
334 | if (this.walls[1]) drawLine(x + size, y, x + size, y + size);
335 | if (this.walls[2]) drawLine(x + size, y + size, x, y + size);
336 | if (this.walls[3]) drawLine(x, y + size, x, y);
337 |
338 | if (this.visited) {
339 | ctx.fillStyle = "#eee";
340 | ctx.fillRect(x, y, size, size);
341 | }
342 | }
343 |
344 | highlight(color) {
345 | const x = this.j * size;
346 | const y = this.i * size;
347 | ctx.fillStyle = color;
348 | ctx.fillRect(x, y, size, size);
349 | }
350 |
351 | getUnvisitedNeighbors() {
352 | let neighbors = [];
353 |
354 | const top = grid[index(this.i - 1, this.j)];
355 | const right = grid[index(this.i, this.j + 1)];
356 | const bottom = grid[index(this.i + 1, this.j)];
357 | const left = grid[index(this.i, this.j - 1)];
358 |
359 | if (top && !top.visited) neighbors.push(top);
360 | if (right && !right.visited) neighbors.push(right);
361 | if (bottom && !bottom.visited) neighbors.push(bottom);
362 | if (left && !left.visited) neighbors.push(left);
363 |
364 | return neighbors;
365 | }
366 | }
367 |
368 | function index(i, j) {
369 | if (i < 0 || j < 0 || i >= rows || j >= cols) return -1;
370 | return i * cols + j;
371 | }
372 |
373 | function drawLine(x1, y1, x2, y2) {
374 | ctx.beginPath();
375 | ctx.moveTo(x1, y1);
376 | ctx.lineTo(x2, y2);
377 | ctx.stroke();
378 | }
379 |
380 | function setupGrid() {
381 | grid = [];
382 | for (let i = 0; i < rows; i++) {
383 | for (let j = 0; j < cols; j++) {
384 | grid.push(new Cell(i, j));
385 | }
386 | }
387 | }
388 |
389 | function removeWalls(a, b) {
390 | let x = a.j - b.j;
391 | let y = a.i - b.i;
392 |
393 | if (x === 1) {
394 | a.walls[3] = false;
395 | b.walls[1] = false;
396 | }
397 | else if (x === -1) {
398 | a.walls[1] = false;
399 | b.walls[3] = false;
400 | }
401 |
402 | if (y === 1) {
403 | a.walls[0] = false;
404 | b.walls[2] = false;
405 | } else if (y === -1) {
406 | a.walls[2] = false;
407 | b.walls[0] = false;
408 | }
409 | }
410 |
411 | function drawGrid() {
412 | ctx.clearRect(0, 0, canvas.width, canvas.height);
413 | grid.forEach(cell => cell.draw());
414 | }
415 |
416 | async function generateMaze() {
417 | setupGrid();
418 | let current = grid[0];
419 | current.visited = true;
420 | stack = [];
421 |
422 | while (true) {
423 | drawGrid();
424 | current.highlight("green");
425 |
426 | let neighbors = current.getUnvisitedNeighbors();
427 | if (neighbors.length > 0) {
428 | let next = neighbors[Math.floor(Math.random() * neighbors.length)];
429 | stack.push(current);
430 | removeWalls(current, next);
431 | current = next;
432 | current.visited = true;
433 | } else if (stack.length > 0) {
434 | current = stack.pop();
435 | } else {
436 | break;
437 | }
438 |
439 | await new Promise(r => setTimeout(r, 10));
440 | }
441 |
442 | player = grid[0];
443 | drawGrid();
444 | highlightGoal();
445 | drawPlayer();
446 | }
447 |
448 | function drawPlayer() {
449 | const x = player.j * size + size / 4;
450 | const y = player.i * size + size / 4;
451 | ctx.fillStyle = "red";
452 | ctx.beginPath();
453 | ctx.arc(x + size / 4, y + size / 4, size / 4, 0, Math.PI * 2);
454 | ctx.fill();
455 | }
456 |
457 | function highlightGoal() {
458 | const goal = grid[grid.length - 1];
459 | const x = goal.j * size;
460 | const y = goal.i * size;
461 | ctx.fillStyle = "gold";
462 | ctx.fillRect(x, y, size, size);
463 | }
464 |
465 | function resetVisited() {
466 | grid.forEach(cell => {
467 | cell.visited = false;
468 | cell.previous = null;
469 | });
470 | }
471 |
472 | async function solveDFS() {
473 | solvingMethod = 'DFS';
474 | resetVisited();
475 | let stack = [grid[0]];
476 | grid[0].visited = true;
477 |
478 | while (stack.length > 0) {
479 | drawGrid();
480 | let current = stack.pop();
481 | current.highlight("lightgreen");
482 |
483 | if (current === grid[grid.length - 1]) {
484 | await showPath(current);
485 | return;
486 | }
487 |
488 | let neighbors = getAvailableNeighbors(current);
489 | for (let neighbor of neighbors) {
490 | if (!neighbor.visited) {
491 | neighbor.visited = true;
492 | neighbor.previous = current;
493 | stack.push(neighbor);
494 | }
495 | }
496 |
497 | await new Promise(r => setTimeout(r, 20));
498 | }
499 | }
500 |
501 | async function solveBFS() {
502 | solvingMethod = 'BFS';
503 | resetVisited();
504 | let queue = [grid[0]];
505 | grid[0].visited = true;
506 |
507 | while (queue.length > 0) {
508 | drawGrid();
509 | let current = queue.shift();
510 | current.highlight("skyblue");
511 |
512 | if (current === grid[grid.length - 1]) {
513 | await showPath(current);
514 | return;
515 | }
516 |
517 | let neighbors = getAvailableNeighbors(current);
518 | for (let neighbor of neighbors) {
519 | if (!neighbor.visited) {
520 | neighbor.visited = true;
521 | neighbor.previous = current;
522 | queue.push(neighbor);
523 | }
524 | }
525 |
526 | await new Promise(r => setTimeout(r, 20));
527 | }
528 | }
529 |
530 | function getAvailableNeighbors(cell) {
531 | let neighbors = [];
532 |
533 | const top = grid[index(cell.i - 1, cell.j)];
534 | const right = grid[index(cell.i, cell.j + 1)];
535 | const bottom = grid[index(cell.i + 1, cell.j)];
536 | const left = grid[index(cell.i, cell.j - 1)];
537 |
538 | if (top && !cell.walls[0]) neighbors.push(top);
539 | if (right && !cell.walls[1]) neighbors.push(right);
540 | if (bottom && !cell.walls[2]) neighbors.push(bottom);
541 | if (left && !cell.walls[3]) neighbors.push(left);
542 |
543 | return neighbors;
544 | }
545 |
546 | async function showPath(cell) {
547 | let path = [];
548 | while (cell) {
549 | path.push(cell);
550 | cell = cell.previous;
551 | }
552 | path.reverse();
553 |
554 | for (let step of path) {
555 | player = step;
556 | drawGrid();
557 | highlightGoal();
558 | drawPlayer();
559 | await new Promise(r => setTimeout(r, 100));
560 | }
561 |
562 | setTimeout(() => alert(" Maze Solved Automatically!"), 100);
563 | }
564 |
565 | document.getElementById("generateBtn").addEventListener("click", generateMaze);
566 | document.getElementById("dfsBtn").addEventListener("click", solveDFS);
567 | document.getElementById("bfsBtn").addEventListener("click", solveBFS);
568 | document.getElementById("resetBtn").addEventListener("click", generateMaze);
569 |
570 | document.addEventListener("keydown", e => {
571 | let moved = false;
572 | if (e.key === "ArrowUp" || e.key === "w") {
573 | if (!player.walls[0]) {
574 | player = grid[index(player.i - 1, player.j)];
575 | moved = true;
576 | }
577 | }
578 | else if (e.key === "ArrowRight" || e.key === "d") {
579 | if (!player.walls[1]) {
580 | player = grid[index(player.i, player.j + 1)];
581 | moved = true;
582 | }
583 | }
584 | else if (e.key === "ArrowDown" || e.key === "s") {
585 | if (!player.walls[2]) {
586 | player = grid[index(player.i + 1, player.j)];
587 | moved = true;
588 | }
589 | }
590 | else if (e.key === "ArrowLeft" || e.key === "a") {
591 | if (!player.walls[3]) {
592 | player = grid[index(player.i, player.j - 1)];
593 | moved = true;
594 | }
595 | }
596 |
597 | if (moved) {
598 | drawGrid();
599 | highlightGoal();
600 | drawPlayer();
601 |
602 | if (player === grid[grid.length - 1]) {
603 | setTimeout(() => alert(" You reached the goal!"), 100);
604 | }
605 | }
606 | });
607 |
608 |
609 |
610 |
611 |