diff --git a/public/board.js b/public/board.js index e7237ff..51ff2a4 100644 --- a/public/board.js +++ b/public/board.js @@ -434,7 +434,7 @@ class Board{ // Draw deck count text // TODO: Center in the circle ctx.fillStyle = '#000'; - ctx.fillText(deckLength+1, textx, texty); + ctx.fillText(deckLength, textx, texty); } } @@ -874,13 +874,13 @@ class Board{ } } +// Run board commands here for testing +let board = new Board; + // TODO: TEMP!! Replace soon createDeckList(playerDeck, deckCount, 0); createDeckList(opponentDeck, deckCountOpponent, 1); -// Run board commands here for testing -let board = new Board; - // Play 4 shield from top (0) of each players deck for(let currentPlayer = 0; currentPlayer <= players-1; currentPlayer++){ board.playShield(0, 'deck', currentPlayer, 4); @@ -1113,37 +1113,39 @@ function createDeckList(deck, deckCount, playerId){ itemCount++; } - //TODO: shuffleDeck(0); Make shuffleDeck work, and call after created the deck (for now) + // Shuffle the deck straight away after generating/loading it in + shuffleDeck(playerId); } function shuffleDeck(playerId){ - let tempDeck = []; - for(let itemKey = 0; itemKey < item.length; itemKey++){ - // Check if item is in deck - // 'key' in 'object' - if(itemKey in boardElement && boardElement[itemKey] == 'deck'){ - // Check if item belongs to opponent - if(itemKey in player && player[itemKey] == playerId){ - // This will shuffle, but not from OG deck state. - // Will need to get the listPosition and order by that first - //console.log(itemKey); - tempDeck.push(itemKey); - } - } + + // Create a tempDeck array of same length of the player deck + let deckLength = getCurrentPositionAndLength('deck', playerId)[1]; + let tempDeck = Array.from(Array(deckLength).keys()) + + // Loop the tempDeck and shuffle + // https://stackoverflow.com/a/73603221 + for(let i = 0; i < deckLength; i++){ + // picks the random number between 0 and length of the deck + let shuffle = Math.floor(Math.random() * (tempDeck.length)); + // swap the current listPosition with a random one with the deck count + [ tempDeck[i], tempDeck[shuffle] ] = [ tempDeck[shuffle], tempDeck[i] ]; } - // Temporary shuffle until algo selected/written - let id, shuffledPile, i; - console.log(tempDeck.length); - for (i = tempDeck.length - 1; i > 0; i--) { - id = Math.floor(Math.random() * (i + 1)); - //shuffledPile = tempDeck[i]; - //tempDeck[i] = tempDeck[id]; - //tempDeck[id] = shuffledPile; - - // Change the position within the deck - listPosition[tempDeck[id]] = i; + console.log(getCurrentPositionAndLength('deck', playerId)[1]); + // For each item in the actual deck, set the listPosition to the random number from tempDeck + let items = board.ECSLoop('deck', playerId, null, null); + for(let item = 0; item < items.length; item++){ + let itemKey = items[item]; + console.log('ITEM KEY: '+itemKey); + console.log('OLD LIST POSITION: '+listPosition[itemKey]); + + listPosition[itemKey] = tempDeck[item]; + + console.log('NEW LIST POSITION: '+listPosition[itemKey]); } + console.log(getCurrentPositionAndLength('deck', playerId)[1]); + } @@ -1179,8 +1181,8 @@ function getCurrentPositionAndLength(elementName, playerId){ if(listPosition[itemKey] >= highestListPosition){ highestListPosition = listPosition[itemKey]; - length++; } + length++; }