-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shibi Suriya
committed
Sep 25, 2023
1 parent
aa32e9b
commit db48d9e
Showing
8 changed files
with
203 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// in px (pixels) | ||
const GRID_WIDTH = 1500; | ||
const GRID_HEIGHT = 600; | ||
const CELL_DIMENSION = 30; | ||
|
||
const NUMBER_OF_ROWS = GRID_HEIGHT / CELL_DIMENSION; | ||
const NUMBER_OF_COLUMNS = GRID_WIDTH / CELL_DIMENSION; | ||
|
||
export { | ||
GRID_HEIGHT, | ||
GRID_WIDTH, | ||
CELL_DIMENSION, | ||
NUMBER_OF_COLUMNS, | ||
NUMBER_OF_ROWS, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { generateKey } from './utils.js'; | ||
export default class OrderedHash { | ||
constructor({ list = [], columns, rows } = {}) { | ||
this.columns = columns; | ||
this.rows = rows; | ||
this.orderedHash = {}; | ||
this.keys = []; | ||
list.forEach((item) => { | ||
this.push(...item); | ||
}); | ||
} | ||
push(x, y) { | ||
const key = generateKey(x, y, this.columns, this.rows); | ||
if (!this.keys.includes(key)) { | ||
this.keys.push(key); | ||
} else { | ||
throw new Error("Error: The key already exists in the 'OrderedHash'."); | ||
} | ||
this.orderedHash[key] = [x, y]; | ||
} | ||
/** | ||
* Adds an element to the beginning of the ordered hash. | ||
* @param {any} x - Data to be added to the ordered hash. | ||
* @param {any} y - Data to be added to the ordered hash. | ||
* @returns {void} | ||
*/ | ||
unshift(x, y) { | ||
const key = generateKey(x, y, this.columns, this.rows); | ||
if (!this.keys.includes(key)) { | ||
this.keys.unshift(key); | ||
} else { | ||
throw new Error("Error: The key already exists in the 'OrderedHash'."); | ||
} | ||
this.orderedHash[key] = [x, y]; | ||
} | ||
remove(x, y) { | ||
const key = generateKey(x, y, this.columns, this.rows); | ||
if (this.keys.includes(key)) { | ||
this.keys.splice(this.keys.indexOf(key), 1); | ||
delete this.orderedHash[key]; | ||
} else { | ||
throw new Error("Error: The key doesn't exists in the 'OrderedHash'."); | ||
} | ||
} | ||
count() { | ||
return this.keys.length; | ||
} | ||
get(x, y) { | ||
const key = generateKey(x, y, this.columns, this.rows); | ||
return this.orderedHash[key]; | ||
} | ||
getOrderedHash() { | ||
return this.orderedHash; | ||
} | ||
getKeys() { | ||
return this.keys; | ||
} | ||
getArray() { | ||
return this.keys.map((key) => this.orderedHash[key]); | ||
} | ||
getHead() { | ||
const [head] = this.keys; | ||
return this.orderedHash[head]; | ||
} | ||
getBody() { | ||
const [head, ...body] = this.keys; | ||
return body.map((key) => this.orderedHash[key]); | ||
} | ||
getTail() { | ||
const key = this.keys[this.keys.length - 1]; | ||
return this.orderedHash[key]; | ||
} | ||
getNeck() { | ||
return this.orderedHash[this.keys[1]]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useState } from "react"; | ||
import { generateKey } from "../utils"; | ||
|
||
const useOrderedHash = (list = []) => { | ||
const hash = {}; | ||
const keys = []; | ||
|
||
list.forEach(([x, y]) => { | ||
const key = generateKey(x, y); | ||
if (!keys.includes(key)) { | ||
keys.push(key); | ||
} else { | ||
throw new Error("Error: The key already exists in the 'OrderedHash'."); | ||
} | ||
hash[key] = [x, y]; | ||
}); | ||
|
||
|
||
const getHead = () => { | ||
const key = keys[0]; | ||
return { key, value: hash[key] }; | ||
}; | ||
|
||
const getTail = () => { | ||
return hash[keys[keys.length - 1]]; | ||
}; | ||
|
||
const getNeck = () => { | ||
// TODO: | ||
// Note: A snake should have atleast two cells, a head and a neck... Snake with only one | ||
// cell (only head doesn't make sense). | ||
return hash[keys[keys[1]]]; | ||
}; | ||
|
||
const moveForward = () => { | ||
console.log("move forward invoked -> ", Date.now()); | ||
}; | ||
|
||
return { list, hash, moveForward, getHead, getTail, getNeck }; | ||
}; | ||
|
||
export default useOrderedHash; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NUMBER_OF_COLUMNS, NUMBER_OF_ROWS } from "./constants"; | ||
export const generateKey = (i, j) => { | ||
if (i > NUMBER_OF_ROWS || j > NUMBER_OF_COLUMNS) { | ||
throw new Error(`Invalid cordinate, (${i}, ${j})!`); | ||
} | ||
return `${i}-${j}`; | ||
}; |