103 lines
2.1 KiB
JavaScript
103 lines
2.1 KiB
JavaScript
// @ts-check
|
|
|
|
/**
|
|
* Retrieve card from cards array at the 0-based position
|
|
*
|
|
* @param {number[]} cards
|
|
* @param {number} position
|
|
*
|
|
* @returns {number} the card
|
|
*/
|
|
export function getItem(cards, position) {
|
|
return cards[position];
|
|
}
|
|
|
|
/**
|
|
* Exchange card with replacementCard at the 0-based position
|
|
*
|
|
* @param {number[]} cards
|
|
* @param {number} position
|
|
* @param {number} replacementCard
|
|
*
|
|
* @returns {number[]} the cards with the change applied
|
|
*/
|
|
export function setItem(cards, position, replacementCard) {
|
|
cards[position] = replacementCard;
|
|
return cards;
|
|
}
|
|
|
|
/**
|
|
* Insert newCard at the end of the cards array
|
|
*
|
|
* @param {number[]} cards
|
|
* @param {number} newCard
|
|
*
|
|
* @returns {number[]} the cards with the newCard applied
|
|
*/
|
|
export function insertItemAtTop(cards, newCard) {
|
|
cards.push(newCard);
|
|
return cards;
|
|
}
|
|
|
|
/**
|
|
* Remove the card at the 0-based position
|
|
*
|
|
* @param {number[]} cards
|
|
* @param {number} position
|
|
*
|
|
* @returns {number[]} the cards without the removed card
|
|
*/
|
|
export function removeItem(cards, position) {
|
|
cards.splice(position, 1);
|
|
return cards;
|
|
}
|
|
|
|
/**
|
|
* Remove card from the end of the cards array
|
|
*
|
|
* @param {number[]} cards
|
|
*
|
|
* @returns {number[]} the cards without the removed card
|
|
*/
|
|
export function removeItemFromTop(cards) {
|
|
cards.pop();
|
|
return cards;
|
|
}
|
|
|
|
/**
|
|
* Insert newCard at beginning of the cards array
|
|
*
|
|
* @param {number[]} cards
|
|
* @param {number} newCard
|
|
*
|
|
* @returns {number[]} the cards including the new card
|
|
*/
|
|
export function insertItemAtBottom(cards, newCard) {
|
|
cards.splice(0, 0, newCard);
|
|
return cards;
|
|
}
|
|
|
|
/**
|
|
* Remove card from the beginning of the cards
|
|
*
|
|
* @param {number[]} cards
|
|
*
|
|
* @returns {number[]} the cards without the removed card
|
|
*/
|
|
export function removeItemAtBottom(cards) {
|
|
cards.splice(0, 1);
|
|
return cards;
|
|
}
|
|
|
|
/**
|
|
* Compare the number of cards with the given stackSize
|
|
*
|
|
* @param {number[]} cards
|
|
* @param {number} stackSize
|
|
*
|
|
* @returns {boolean} true if there are exactly stackSize number of cards, false otherwise
|
|
*/
|
|
export function checkSizeOfStack(cards, stackSize) {
|
|
return(cards.length == stackSize);
|
|
}
|