Skip to content

Commit

Permalink
fix(Home): Swap works as intended.
Browse files Browse the repository at this point in the history
We ensure the item being moved can be placed in the new location without overlapping other items.

We ensure the item being swapped is completely removed from its original location and placed in the new location.
  • Loading branch information
SobieskiCodes committed Aug 7, 2024
1 parent 2c120b2 commit 83d7e95
Showing 1 changed file with 69 additions and 58 deletions.
127 changes: 69 additions & 58 deletions src/features/aquariumshop/aquariumSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,57 +77,50 @@ const aquariumSlice = createSlice({
const { item, index } = action.payload;
const { rows, cols } = item.size;
const gridSize = Math.sqrt(state.gridItems.length);

// Remove the item from its previous position
const previousIndex = state.gridItems.findIndex(gridItem => gridItem && gridItem.id === item.id);
if (previousIndex !== -1) {
const previousRow = Math.floor(previousIndex / gridSize);
const previousCol = previousIndex % gridSize;
for (let r = 0; r < item.size.rows; r++) {
for (let c = 0; c < item.size.cols; c++) {
const targetIndex = (previousRow + r) * gridSize + (previousCol + c);
state.gridItems[targetIndex] = null;

// Helper function to clear an item's space in the grid
const clearItemSpace = (item) => {
const itemIndex = state.gridItems.findIndex(gridItem => gridItem && gridItem.id === item.id);
if (itemIndex !== -1) {
const itemRow = Math.floor(itemIndex / gridSize);
const itemCol = itemIndex % gridSize;
for (let r = 0; r < item.size.rows; r++) {
for (let c = 0; c < item.size.cols; c++) {
const targetIndex = (itemRow + r) * gridSize + (itemCol + c);
if (targetIndex >= 0 && targetIndex < state.gridItems.length) {
state.gridItems[targetIndex] = null;
}
}
}
}
}

// Check if the item can be placed without overlapping
let canPlace = true;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const targetIndex = index + r * gridSize + c;
if (targetIndex >= state.gridItems.length || state.gridItems[targetIndex] !== null) {
canPlace = false;
break;
}
};

// Helper function to check if an item can be placed at a specific index
const canPlaceItemAt = (item, index) => {
const { rows, cols } = item.size;
const startRow = Math.floor(index / gridSize);
const startCol = index % gridSize;

if (startCol + cols > gridSize || startRow + rows > gridSize) {
return false;
}
if (!canPlace) break;
}

// Place the item if there's no overlap, otherwise swap
if (canPlace) {

for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const targetIndex = index + r * gridSize + c;
if (targetIndex >= 0 && targetIndex < state.gridItems.length) {
state.gridItems[targetIndex] = item;
if (targetIndex >= state.gridItems.length || state.gridItems[targetIndex] !== null) {
return false;
}
}
}
} else {
// Handle swapping logic if the space is occupied
const occupiedItem = state.gridItems[index];
const occupiedSize = occupiedItem.size;

// Clear the space of the occupied item
for (let r = 0; r < occupiedSize.rows; r++) {
for (let c = 0; c < occupiedSize.cols; c++) {
const targetIndex = index + r * gridSize + c;
state.gridItems[targetIndex] = null;
}
}

// Place the moving item
return true;
};

// Remove the item from its previous position
clearItemSpace(item);

// Check if the item can be placed without overlapping
if (canPlaceItemAt(item, index)) {
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const targetIndex = index + r * gridSize + c;
Expand All @@ -136,27 +129,45 @@ const aquariumSlice = createSlice({
}
}
}

// Find the first empty spot to place the swapped item
const emptyIndex = state.gridItems.findIndex((gridItem) => gridItem === null);
if (emptyIndex !== -1) {
const emptyRow = Math.floor(emptyIndex / gridSize);
const emptyCol = emptyIndex % gridSize;
let canPlace = true;
for (let r = 0; r < occupiedSize.rows; r++) {
for (let c = 0; c < occupiedSize.cols; c++) {
const targetIndex = emptyIndex + r * gridSize + c;
if (targetIndex >= state.gridItems.length || state.gridItems[targetIndex] !== null) {
canPlace = false;
} else {
// Handle swapping logic if the space is occupied
const occupiedItem = state.gridItems[index];
if (occupiedItem) {
const occupiedSize = occupiedItem.size;

// Clear the space of the occupied item
clearItemSpace(occupiedItem);

// Place the moving item
if (canPlaceItemAt(item, index)) {
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const targetIndex = index + r * gridSize + c;
if (targetIndex >= 0 && targetIndex < state.gridItems.length) {
state.gridItems[targetIndex] = item;
}
}
}

// Find the first empty spot to place the swapped item
for (let i = 0; i < state.gridItems.length; i++) {
if (canPlaceItemAt(occupiedItem, i)) {
for (let r = 0; r < occupiedSize.rows; r++) {
for (let c = 0; c < occupiedSize.cols; c++) {
const targetIndex = i + r * gridSize + c;
if (targetIndex >= 0 && targetIndex < state.gridItems.length) {
state.gridItems[targetIndex] = occupiedItem;
}
}
}
break;
}
}
if (!canPlace) break;
}
if (canPlace) {
} else {
// If the item cannot be placed back in the original position, restore the original item
for (let r = 0; r < occupiedSize.rows; r++) {
for (let c = 0; c < occupiedSize.cols; c++) {
const targetIndex = emptyIndex + r * gridSize + c;
const targetIndex = index + r * gridSize + c;
if (targetIndex >= 0 && targetIndex < state.gridItems.length) {
state.gridItems[targetIndex] = occupiedItem;
}
Expand Down

0 comments on commit 83d7e95

Please sign in to comment.