Skip to content

Commit

Permalink
added a grid_map hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Shibi Suriya committed Sep 30, 2023
1 parent a757371 commit 6f8c4eb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/game-client/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useState, useEffect, useRef } from 'react';
import { generateKey, getOppositeDirection } from './utils';
import { DIRECTIONS, NUMBER_OF_COLUMNS, NUMBER_OF_ROWS, DEFAULT_DIRECTION } from './constants';
import { GRID_MAP } from './computed';
console.log(GRID_MAP);
import Grid from './Grid';

const SPEED = 1 * 100;
Expand Down
15 changes: 15 additions & 0 deletions packages/game-client/src/computed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NUMBER_OF_COLUMNS, NUMBER_OF_ROWS } from './constants';
import { generateKey, generateValue } from './utils';

const generateGridMap = () => {
const hash = {};
for (let i = 0; i < NUMBER_OF_ROWS; i++) {
for (let j = 0; j < NUMBER_OF_COLUMNS; j++) {
hash[generateKey(i, j)] = generateValue(i, j);
}
}
return hash;
};

const GRID_MAP = generateGridMap();
export { GRID_MAP };
13 changes: 12 additions & 1 deletion packages/game-client/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { NUMBER_OF_COLUMNS, NUMBER_OF_ROWS, DIRECTIONS } from './constants';

const isCellValid = (i, j) => {
return !(i > NUMBER_OF_ROWS || j > NUMBER_OF_COLUMNS);
};

export const generateKey = (i, j) => {
if (i > NUMBER_OF_ROWS || j > NUMBER_OF_COLUMNS) {
if (!isCellValid(i, j)) {
throw new Error(`Invalid coordinates! ${i} ${j}`);
}
return `${i}-${j}`;
};

export const generateValue = (x, y) => {
if (!isCellValid(x, y)) {
throw new Error(`Invalid coordinates! ${i} ${j}`);
}
return { x, y };
};

export const getOppositeDirection = (direction) => {
switch (direction) {
case DIRECTIONS.DOWN:
Expand Down

0 comments on commit 6f8c4eb

Please sign in to comment.