remove the card from the output, need to fix createdeck too much card added

pull/47/head
Bastien JACQUELIN 2 years ago
parent 889c843aa7
commit 0a13894bc2

@ -30,8 +30,10 @@ try {
console.groupEnd(); console.groupEnd();
// CREATE DECK
console.group('Deck'); console.group('Deck');
let deck = new Deck([0,1,2,3]); let deck = new Deck([0,1,2,3],3);
console.log(`All cards : ${deck.allCards.length}`); console.log(`All cards : ${deck.allCards.length}`);
//Display all cards //Display all cards
console.log(`All cards display`); console.log(`All cards display`);
@ -54,23 +56,27 @@ console.log(`set already made ${deck.setMade}`);
// console.log(e.color,e.number,e.shape,e.filling); // console.log(e.color,e.number,e.shape,e.filling);
// }); // });
// CHECK SET
console.log("CHECKING SET")
console.log("Card to remove: ") console.log("Card to remove: ")
console.log(deck.outputCards[0].getAttributes(),deck.outputCards[1].getAttributes(),deck.outputCards[2].getAttributes()) console.log(deck.outputCards[0].getAttributes(),deck.outputCards[1].getAttributes(),deck.outputCards[2].getAttributes())
let customCard=[deck.outputCards[0],deck.outputCards[1],deck.outputCards[2]]; let customCard=[deck.outputCards[0],deck.outputCards[1],deck.outputCards[2]];
deck.checkSet(customCard); deck.checkSet(customCard);
console.log(`remaining cards : ${deck.remainingCards.length}`) console.log(`remaining cards : ${deck.remainingCards.length}`)
console.log(`All cards : ${deck.allCards.length}`) console.log(`All cards : ${deck.allCards.length}`)
console.log(`remaining cards : ${deck.remainingCards.length}`) console.log(`remaining cards : ${deck.remainingCards.length}`)
// deck.remainingCards.forEach(e => { // deck.remainingCards.forEach(e => {
// console.log(e.getAttributes()); // console.log(e.getAttributes());
// }); // });
console.log(`size output ${deck.outputCards.length}`); console.log(`size output ${deck.outputCards.length}`);
console.log(`Output cards`); console.group('Output cards');
deck.outputCards.forEach(e => { deck.outputCards.forEach(e => {
console.log(e.getAttributes()); console.log(e.getAttributes());
}); });
console.log(`nbCards : ${deck.nbCards}`);
console.groupEnd();
//let deck5 = new Deck([0,1,2,3,4]); //let deck5 = new Deck([0,1,2,3,4]);

@ -2,21 +2,24 @@ class Deck{
/** /**
* *
* @param {*} attributes : array with the attributes index for the cards * @param {*} attributes : array with the attributes index for the cards
* @author Bastien Jacquelin
*/ */
constructor(attributes){ constructor(attributes,nbCards){
//console.log(attributes); //console.log(attributes);
this.allCards=this.createCards(attributes);// All the cards in the game this.allCards=this.createCards(attributes);// All the cards in the game
this.remainingCards=[] this.remainingCards=[]
this.nbCards=nbCards;
this.remainingCards=this.remainingCards.concat(this.allCards);// cards in the stack this.remainingCards=this.remainingCards.concat(this.allCards);// cards in the stack
this.outputCards=[];// 12 cards lay on the table this.outputCards=[];// 12 cards lay on the table
this.setMade=[];// array with all the set already mades (array of set) this.setMade=[];// array with all the set already mades (array of set)
this.createDeck(); this.createDeck(12);
} }
/** /**
* @brief creation of the deck : call factory to create the good cards * @brief creation of the deck : 12 cards lay in front of the player
* @author Bastien Jacquelin
*/ */
createDeck(){ createDeck(nbCards){
for (let i=0; i<12; i++){ for (let i=0; i<nbCards; i++){
const rand = this.getRandCard(); const rand = this.getRandCard();
this.outputCards.push(this.remainingCards[rand]); this.outputCards.push(this.remainingCards[rand]);
this.remainingCards.splice(rand,1); this.remainingCards.splice(rand,1);
@ -25,6 +28,7 @@ class Deck{
/** /**
* *
* @returns random number in range of the array size * @returns random number in range of the array size
* @author Bastien Jacquelin
*/ */
getRandCard(){ getRandCard(){
const random = Math.floor(Math.random() * this.remainingCards.length); const random = Math.floor(Math.random() * this.remainingCards.length);
@ -34,11 +38,17 @@ class Deck{
* *
* @param attributes : index of the attributes used * @param attributes : index of the attributes used
* @returns all cards: 81 in case of 4 attributes and 1024 if 5 attributes * @returns all cards: 81 in case of 4 attributes and 1024 if 5 attributes
* @author Bastien Jacquelin
*/ */
createCards(attributes){ createCards(attributes){
let factory = new Factory(attributes) let factory = new Factory(attributes)
return factory.product return factory.product
} }
/**
* @brief verification of the validity of the set selected
* @param {*} selectedCards array of cards : set
* @author Bastien Jacquelin
*/
checkSet(selectedCards){ checkSet(selectedCards){
if(true){//isSet(selectedCards)){ if(true){//isSet(selectedCards)){
selectedCards.forEach(e => { selectedCards.forEach(e => {
@ -47,8 +57,9 @@ class Deck{
} }
} }
/** /**
* * @brief when a set is made, need to remove the card from the array remainingCards
* @param {*} selectedCards wehn a set is made, need to remove the card from the array remainingCards * @param {*} selectedCards cards which need to be removed from the outputcards
* @author Bastien Jacquelin
*/ */
removeFromoutputCards(selectedCards){//better check of card type more opti removeFromoutputCards(selectedCards){//better check of card type more opti
let set=[]; let set=[];
@ -65,6 +76,7 @@ class Deck{
} }
else{ else{
this.setMade.push(set); this.setMade.push(set);
this.createDeck(this.nbCards)
} }
} }
} }

@ -3,6 +3,12 @@ class Factory{
let length=arrayOfAttributes.length let length=arrayOfAttributes.length
this.product=this.concreteCardCreation(arrayOfAttributes,length); this.product=this.concreteCardCreation(arrayOfAttributes,length);
} }
/**
* @brief check if i in arrayOfAttributes
* @param {*} i value
* @param {*} arrayOfAttributes array
* @returns boolean
*/
inArray(i,arrayOfAttributes){ inArray(i,arrayOfAttributes){
let finded=false; let finded=false;
for (let j=0;j<arrayOfAttributes.length;j++){ for (let j=0;j<arrayOfAttributes.length;j++){
@ -12,7 +18,13 @@ class Factory{
} }
return finded return finded
} }
attributesRequiredFun(arrayOfAttributes,length){ /**
* @brief create a matrix with the attributes of the cards : if attributes not defined : value 0
* @param {*} arrayOfAttributes
* @param {*} length
* @returns matrix of attributes
*/
attributesRequiredFun(arrayOfAttributes){
let attributesRequiredTmp=[]; let attributesRequiredTmp=[];
let nullArray=[0,0,0,0,0]; let nullArray=[0,0,0,0,0];
for(let i=0;i<5;i++){ for(let i=0;i<5;i++){
@ -25,9 +37,15 @@ class Factory{
} }
return attributesRequiredTmp; return attributesRequiredTmp;
} }
/**
* @biref create the right cards : 3,4,5 attributes
* @param {*} arrayOfAttributes
* @param {*} length
* @returns array of all cards
*/
concreteCardCreation(arrayOfAttributes, length){ concreteCardCreation(arrayOfAttributes, length){
let tabOfAllCards=[]; let tabOfAllCards=[];
let attributes=this.attributesRequiredFun(arrayOfAttributes,length); let attributes=this.attributesRequiredFun(arrayOfAttributes);
let nbAttributes=length let nbAttributes=length
if(nbAttributes==3){ if(nbAttributes==3){
for (let c=0; c<nbAttributes-1; c++){ for (let c=0; c<nbAttributes-1; c++){

Loading…
Cancel
Save