Skip to content

Commit

Permalink
fix handleFoundationDrop
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaanahmed committed Sep 13, 2023
1 parent 26655a1 commit 5812296
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 57 deletions.
2 changes: 1 addition & 1 deletion backend/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# pylint: disable=import-outside-toplevel
def main():
"""Main start Django function"""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.production')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
81 changes: 25 additions & 56 deletions frontend/components/solitaire/SolitaireGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ class SolitaireGame extends React.Component {
};

setDragState = (stackName, cardIndex) => {
console.log("Setting drag state: ", stackName, cardIndex);
this.setState(
{dragState: {stackName, cardIndex }}
);
};

clearDragState = () => { console.log("Clearing drag state"); this.setDragState('', -1); };
clearDragState = () => this.setDragState('', -1);

/*
Only call this when the stock stack is clicked
Expand All @@ -180,9 +179,12 @@ class SolitaireGame extends React.Component {

handleTableauDrop = (destTableauIndex) => {
const destStackName = 'tableau' + destTableauIndex.toString();
const destStack = this.state[destStackName];

const srcStackName = this.state.dragState.stackName;
const srcStack = this.state[srcStackName];
const destStack = this.state[destStackName];

if (srcStack === destStack) return;

const transferCard =
srcStackName.startsWith('tableau')
Expand All @@ -191,11 +193,6 @@ class SolitaireGame extends React.Component {

this.clearDragState();

if (srcStack === destStack) {
// If source and destination stacks are the same, no further action needed
return;
}


let isValidTransfer = false;
if (destStack.length === 0) {
Expand All @@ -208,10 +205,10 @@ class SolitaireGame extends React.Component {
compareSuits(destStackTopCard["suit"], transferCard["suit"]);
}

if (isValidTransfer) {
const cardsToTransfer = srcStack.splice(this.state.dragState.cardIndex);
destStack.push(...cardsToTransfer);
}
if (!isValidTransfer) return;

const cardsToTransfer = srcStack.splice(this.state.dragState.cardIndex);
destStack.push(...cardsToTransfer);

if (srcStack.length > 0) {
const srcStackNextCard = srcStack[srcStack.length-1];
Expand All @@ -220,78 +217,56 @@ class SolitaireGame extends React.Component {

var stacks = {...this.state};
stacks[destStackName] = destStack;
this.setState({
stacks: stacks
});

// For testing purposes
// console.log(stackName + " gained a card");
// console.log(stackName + ": " + this.state[stackName]);
// console.log(this.state);
this.setState({ stacks });
};


handleFoundationDrop = (suit) => {
const srcStackName = this.state.dragState.stackName;
const srcStack = this.state[srcStackName];

// can't drop multiple cards on to a foundation
if (srcStack.length-1 !== this.state.dragState.cardIndex) return;

const destStackName = 'foundation' + suit;
const destStack = this.state[destStackName];
const transferCard = srcStack.pop();

this.clearDragState();
if (srcStack === destStack) return;

if (srcStack === destStack) {
srcStack.push(transferCard);
if (srcStackName.startsWith('tableau') && srcStack.length-1 !== this.state.dragState.cardIndex) {
// can't drop multiple cards on to a foundation
return;
}

// Card must be same suit as foundation
if (transferCard["suit"] !== suit) {
srcStack.push(transferCard);
return;
}
const transferCard = srcStack[srcStack.length-1];

this.clearDragState();

if (transferCard["suit"] !== suit) return;

let isValidTransfer = false;
if (destStack.length === 0) {
// Aces can only go on empty stacks
if (transferCard["card"] === "A") {
destStack.push(transferCard);
} else {
srcStack.push(transferCard);
return;
}
isValidTransfer = transferCard["card"] === "A";
} else {
const destStackTopCard = destStack[destStack.length-1];
if (!compareCards(transferCard["card"], destStackTopCard["card"])) {
srcStack.push(transferCard);
return;
}

destStack.push(transferCard);
isValidTransfer = compareCards(transferCard["card"], destStackTopCard["card"]);
}

if (!isValidTransfer) return;
destStack.push(srcStack.pop());

if (srcStack.length > 0) {
const srcStackNextCard = srcStack[srcStack.length-1];
srcStackNextCard.faceUp = true;
}

var stacks = {...this.state};
stacks[destStackName] = destStack;
this.setState({
stacks: stacks
});

this.setState({ stacks });
};

/*
Handle the stock pile being clicked (drawing a new card to the waste pile):
*/
handleStockClick = () => {
// console.log("Clicking stock");

var stock = this.state.stock;
var stockCard = stock.pop();
stockCard.faceUp = true;
Expand Down Expand Up @@ -335,12 +310,6 @@ class SolitaireGame extends React.Component {
// Reassign this.state to reflect the new changes
stacks: stacks
});
// console.log("Length of this.state.waste after refill: " + this.state.waste.length);

// Note (from Alyssa): there's a weird bug where the waste doesn't fully empty out and there's still
// one card left in the waste after refreshing the stock.
// I ran a bunch of print statements and it seems that the stack correctly fully empties out at the end of this function,
// but sometime during the rendering, one of the cards from the stock gets moved back to the waste
};

render() {
Expand Down

0 comments on commit 5812296

Please sign in to comment.