Merge branch 'card-class'
continuous-integration/drone/push Build is passing Details

pull/59/head^2
Bastien JACQUELIN 2 years ago
commit 3d45e10f1b

@ -15,12 +15,6 @@
<script src="../Model/Exceptions.js"></script> <script src="../Model/Exceptions.js"></script>
<script src="../Model/Card.js"></script> <script src="../Model/Card.js"></script>
<script src="../algo.js"></script> <script src="../algo.js"></script>
<script src="../Model/Card4WithoutColor.js"></script>
<script src="../Model/Card4WithoutFilling.js"></script>
<script src="../Model/Card4WithoutNumber.js"></script>
<script src="../Model/Card4WithoutOutline.js"></script>
<script src="../Model/Card4WithoutShape.js"></script>
<script src="../Model/Card5.js"></script>
<script src="../Model/Const.js"></script> <script src="../Model/Const.js"></script>
<script src="../Model/Factory.js"></script> <script src="../Model/Factory.js"></script>
<script src="../Model/Deck.js"></script> <script src="../Model/Deck.js"></script>

@ -66,4 +66,4 @@ const list2 = [1, 1, 1, 1];
console.log(checkList(list2)); // Retourne 1 console.log(checkList(list2)); // Retourne 1
const list3 = [1, 2, 3, 1]; const list3 = [1, 2, 3, 1];
console.log(checkList(list3)); // Retourne 2 console.log(checkList(list3)); // Retourne 2

@ -1,15 +1,34 @@
class Card{ class Card{
constructor(){} /**
*
* @param {*} attributes : dictionnary of attributes : key : name of the attributes and value : value of the attributes
*/
constructor(attributes){
this.attributes=attributes;
}
/** /**
* *
* @returns all attributes of a card * @returns all attributes of a card
*/ */
getAttributes(){} getAttributes(){//working✅
let att=[];
Object.entries(this.attributes).forEach(function([key, value]) {
att.push(value);
});
return att;
}
/** /**
* *
* @param {*} card card to be compared with the current obj * @param {*} card card to be compared with the current obj
* @returns boolean * @returns boolean
*/ */
equals(card){} equals(card){//working✅
let bool=true;
}//export {Card} Object.entries(this.attributes).forEach(function([key, value]) {
if(card.attributes[key]!=value){
bool=false;
}
});
return bool;
}
}

@ -1,27 +0,0 @@
class Card4WithoutColor extends Card{
constructor(number, shape, filling, outline){
super();
if(number==''){
throw new EmptyParamaterException('Number');
}
if(shape==''){
throw new EmptyParamaterException('Shape');
}
if(filling==''){
throw new EmptyParamaterException('Filling');
}
if(outline==''){
throw new EmptyParamaterException('Outline');
}
this.number=number;
this.shape=shape;
this.filling=filling;
this.outline=outline;
}
getAttributes(){
return [this.number,this.shape,this.filling,this.outline];
}
equals(card){
return this.number===card.number && this.shape===card.shape && this.filling===card.filling && this.outline===card.outline ;
}
}

@ -1,27 +0,0 @@
class Card4WithoutFilling extends Card{
constructor(color,number, shape, outline){
super();
if(number==''){
throw new EmptyParamaterException('Number');
}
if(shape==''){
throw new EmptyParamaterException('Shape');
}
if(color==''){
throw new EmptyParamaterException('Color');
}
if(outline==''){
throw new EmptyParamaterException('Outline');
}
this.number=number;
this.shape=shape;
this.color=color;
this.outline=outline;
}
getAttributes(){
return [this.number,this.shape,this.color,this.outline];
}
equals(card){
return this.number===card.number && this.shape===card.shape && this.color===card.color && this.outline===card.outline ;
}
}

@ -1,27 +0,0 @@
class Card4WithoutNumber extends Card{
constructor(color, shape, filling, outline){
super();
if(color==''){
throw new EmptyParamaterException('Color');
}
if(shape==''){
throw new EmptyParamaterException('Shape');
}
if(filling==''){
throw new EmptyParamaterException('Filling');
}
if(outline==''){
throw new EmptyParamaterException('Outline');
}
this.color=color;
this.shape=shape;
this.filling=filling;
this.outline=outline;
}
getAttributes(){
return [this.color,this.shape,this.filling,this.outline];
}
equals(card){
return this.color===card.color && this.shape===card.shape && this.filling===card.filling && this.outline===card.outline ;
}
}

@ -1,27 +0,0 @@
class Card4WithoutOutline extends Card{
constructor(color, number, shape, filling){
super();
if(color==''){
throw new EmptyParamaterException('Color');
}
if(number==''){
throw new EmptyParamaterException('Number');
}
if(shape==''){
throw new EmptyParamaterException('Shape');
}
if(filling==''){
throw new EmptyParamaterException('Filling');
}
this.color=color;
this.number=number;
this.shape=shape;
this.filling=filling;
}
getAttributes(){
return [this.color,this.number,this.shape,this.filling];
}
equals(card){
return this.number===card.number && this.shape===card.shape && this.filling===card.filling && this.color===card.color;
}
}

@ -1,27 +0,0 @@
class Card4WithoutShape extends Card{
constructor(color,number, filling, outline){
super();
if(color==''){
throw new EmptyParamaterException('Color');
}
if(number==''){
throw new EmptyParamaterException('Number');
}
if(filling==''){
throw new EmptyParamaterException('Filling');
}
if(outline==''){
throw new EmptyParamaterException('Outline');
}
this.color=color;
this.number=number;
this.filling=filling;
this.outline=outline;
}
getAttributes(){
return [this.number,this.color,this.filling,this.outline];
}
equals(card){
return this.number===card.number && this.color===card.color && this.filling===card.filling && this.outline===card.outline ;
}
}

@ -1,43 +0,0 @@
// import('.Card');
class Card5 extends Card {
constructor(color, number, shape, filling, outline){
super();
if(color==''){
throw new EmptyParamaterException('Color');
}
if(number==''){
throw new EmptyParamaterException('Number');
}
if(shape==''){
throw new EmptyParamaterException('Shape');
}
if(filling==''){
throw new EmptyParamaterException('Filling');
}
if(outline==''){
throw new EmptyParamaterException('Outline');
}
this.color=color;
this.number=number;
this.shape=shape;
this.filling=filling;
this.outline=outline;
}
/**
* @returns array of all attributes :
idx 1 : color
idx 2 : number
idx 3 : shape
idx 4 : filling
idx 5 : outline
* @author Bastien Jacquelin
*/
getAttributes(){
return [this.color,this.number,this.shape,this.filling,this.outline];
}
equals(card){
return this.color===card.color && this.number===card.number && this.shape===card.shape && this.filling===card.filling && this.outline===card.outline ;
}
}
// export {Card5};

@ -4,4 +4,4 @@ const TAB_SHAPE = ['diamond','oval','wave','star','triangle'];
const TAB_FILLING = ['empty','stripped','full','pointed','squared']; const TAB_FILLING = ['empty','stripped','full','pointed','squared'];
const TAB_OUTLINE = ['continuous','dotted','hyphen','cloudy','sharpy']; const TAB_OUTLINE = ['continuous','dotted','hyphen','cloudy','sharpy'];
const ATTRIBUTES=[TAB_COLOR,TAB_NUMBER,TAB_SHAPE,TAB_FILLING,TAB_OUTLINE]; const ATTRIBUTES=[TAB_COLOR,TAB_NUMBER,TAB_SHAPE,TAB_FILLING,TAB_OUTLINE];
const IDX_ATTRIBUTES=["color","number","shape","filling","outline"]; const IDX_ATTRIBUTES=["color","number","shape","filling","outline"];

@ -5,22 +5,30 @@ class Deck{
* @author Bastien Jacquelin * @author Bastien Jacquelin
*/ */
constructor(attributes,nbCards){ constructor(attributes,nbCards){
//console.log(attributes); this.nbCards=nbCards;// number of card to do a set
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=[];//init tab null
this.nbCards=nbCards; this.remainingCards=this.remainingCards.concat(this.allCards);// cards in the stack, init = all before creation of deck -> remove
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 of array with all the set already mades (array of set) this.setMade=[];// array of array with all the set already mades (array of set)
this.createDeck(12); this.createDeck(12);
} }
/** /**
* @brief creation of the deck : 12 cards lay in front of the player *
* @param attributes : index of the attributes used
* @returns all cards: 81 in case of 4 attributes and 1024 if 5 attributes
* @author Bastien Jacquelin * @author Bastien Jacquelin
*/ */
createDeck(nbCards){ createCards(attributes){//working✅
if(this.remainingCards.length==0){ let factory = new Factory(attributes,this.nbCards);
return factory.product;
}
/**
* @brief creation of the deck : 12 random cards lay in front of the playe and remove card from the remainingCard array
* @author Bastien Jacquelin
*/
createDeck(nbCards){//toTest⌛
if(this.remainingCards.length<this.nbCards){// no more cards
console.log("PLUS DE CARTES"); console.log("PLUS DE CARTES");
return; return;
} }
@ -30,7 +38,11 @@ class Deck{
this.outputCards.push(this.remainingCards[rand]); this.outputCards.push(this.remainingCards[rand]);
this.remainingCards.splice(rand,1); this.remainingCards.splice(rand,1);
} }
if(setsCounter(this.outputCards,this.nbCards)==0){ let nbSets=setsCounter(this.outputCards,this.nbCards);
console.log("nbSets",nbSets);
console.log("outputCards.length",this.outputCards.length);
console.log("remainingCards.length",this.remainingCards.length);
if(nbSets==0){
this.createDeck(this.nbCards) this.createDeck(this.nbCards)
} }
} }
@ -41,41 +53,32 @@ class Deck{
* @returns random number in range of the array size * @returns random number in range of the array size
* @author Bastien Jacquelin * @author Bastien Jacquelin
*/ */
getRandCard(){ getRandCard(){//working✅
const random = Math.floor(Math.random() * this.remainingCards.length); const random = Math.floor(Math.random() * this.remainingCards.length);
return random; return random;
} }
/** /**
* * @brief verification of the validity of the set selected, call removeFromoutputCards when set is confirmed
* @param attributes : index of the attributes used
* @returns all cards: 81 in case of 4 attributes and 1024 if 5 attributes
* @author Bastien Jacquelin
*/
createCards(attributes){
let factory = new Factory(attributes)
return factory.product
}
/**
* @brief verification of the validity of the set selected
* @param {*} selectedCards array of cards : set * @param {*} selectedCards array of cards : set
* @author Bastien Jacquelin * @author Bastien Jacquelin
*/ */
checkSet(selectedCards){ checkSet(selectedCards){//toTest⌛
if(true){//isSet(selectedCards)){ if(true){//isSet(selectedCards)){// is a set
if(this.outputCards.length==0){ if(this.outputCards.length==0){
console.log("C'est win") console.log("C'est win")
return; return 2;
} }
else{ else{
this.removeFromoutputCards(selectedCards); this.removeFromoutputCards(selectedCards);
return 1;
} }
} }
else if(this.remainingCards.length==0){ else if(this.remainingCards.length<this.nbCards){
console.log("C'est win") console.log("C'est win")
return; return 2;
} }
return 0;
} }
/** /**
@ -83,7 +86,7 @@ class Deck{
* @param {*} selectedCards cards which need to be removed from the outputcards * @param {*} selectedCards cards which need to be removed from the outputcards
* @author Bastien Jacquelin * @author Bastien Jacquelin
*/ */
removeFromoutputCards(selectedCards){//better check of card type more opti removeFromoutputCards(selectedCards){//working✅
let set=[]; let set=[];
selectedCards.forEach(element => { selectedCards.forEach(element => {
for(let i=0; i<this.outputCards.length;i++){ for(let i=0; i<this.outputCards.length;i++){
@ -94,7 +97,6 @@ class Deck{
} }
} }
}); });
if(set.length<1){ if(set.length<1){
throw new UnFoundCardException(selectedCards); throw new UnFoundCardException(selectedCards);
} }

@ -1,7 +1,46 @@
class Factory{ class Factory{
constructor(arrayOfAttributes){ constructor(arrayOfAttributes, nbAttributes){
let length=arrayOfAttributes.length this.nbAttr=nbAttributes;
this.product=this.concreteCardCreation(arrayOfAttributes,length); this.dicoAttributes=this.attributesDictionnary(arrayOfAttributes,this.funArrayOfAttributes(arrayOfAttributes));
this.product=this.concreteCardCreation(arrayOfAttributes);
// console.log("arrayOfAttributes",this.funArrayOfAttributes(arrayOfAttributes));
// console.log("attributesDictionnary",this.dicoAttributes)
// console.log("attributesName",this.attributesName(this.dicoAttributes));
// console.log("allCards",this.product);
}
/**
*
* @param {*} arrayOfIdxAttributes index of attributes in ATTRIBUTES
* @returns array of all attributes
*/
funArrayOfAttributes(arrayOfIdxAttributes){//working✅
let attr=[];
let l=this.nbAttr;
arrayOfIdxAttributes.forEach(e => {
for (let i=0;i<l;i++){
attr.push(ATTRIBUTES[e][i]);
}
});
return attr;
}
/**
*
* @param {*} arrayOfIdxAttributes index of attributes in ATTRIBUTES
* @param {*} arrayOfAllAttributes array of all attributes
* @returns dictionnary with key : attribute and value : array of the possibilities of attributes
*/
attributesDictionnary(arrayOfIdxAttributes,arrayOfAllAttributes){//working✅
let l=arrayOfIdxAttributes.length;
let dico={};
for (let i=0;i<l;i++){
let tmp=[]
for(let j=0;j<this.nbAttr;j++){
tmp.push(arrayOfAllAttributes[(i*this.nbAttr)+j]);
}
dico[IDX_ATTRIBUTES[arrayOfIdxAttributes[i]]]=tmp;
}
return dico;
} }
/** /**
* @brief check if i in arrayOfAttributes * @brief check if i in arrayOfAttributes
@ -9,7 +48,7 @@ class Factory{
* @param {*} arrayOfAttributes array * @param {*} arrayOfAttributes array
* @returns boolean * @returns boolean
*/ */
inArray(i,arrayOfAttributes){ inArray(i,arrayOfAttributes){//toTest⌛
let finded=false; let finded=false;
for (let j=0;j<arrayOfAttributes.length;j++){ for (let j=0;j<arrayOfAttributes.length;j++){
if(i==arrayOfAttributes[j]){ if(i==arrayOfAttributes[j]){
@ -17,77 +56,70 @@ class Factory{
} }
} }
return finded return finded
} }
/** /**
* @brief create a matrix with the attributes of the cards : if attributes not defined : value 0 *
* @param {*} arrayOfAttributes * @param {*} dico dictionnary of attributes
* @param {*} length * @returns array of the key : name of the attributes
* @returns matrix of attributes
*/ */
attributesRequiredFun(arrayOfAttributes){ attributesName(dico){//working✅
let attributesRequiredTmp=[]; let attributes=[]
let nullArray=[0,0,0,0,0]; Object.entries(dico).forEach(function([key, value]) {
for(let i=0;i<5;i++){ attributes.push(key);
if(!this.inArray(i,arrayOfAttributes)){ });
attributesRequiredTmp.push(nullArray); return attributes
}
else{
attributesRequiredTmp.push(ATTRIBUTES[i]);
}
}
return attributesRequiredTmp;
} }
/** /**
* @biref create the right cards : 3,4,5 attributes * @brief create the right cards : 3,4,5 attributes
* @param {*} arrayOfAttributes * @param {*} arrayOfAttributes
* @param {*} length * @param {*} length
* @returns array of all cards * @returns array of all cards
*/ */
concreteCardCreation(arrayOfAttributes, length){ concreteCardCreation(){//working✅
let tabOfAllCards=[]; let tabOfAllCards=[];
let attributes=this.attributesRequiredFun(arrayOfAttributes); let dicoAttributes=this.dicoAttributes
let nbAttributes=length let attributes=this.attributesName(dicoAttributes);
if(nbAttributes==3){ let nbAttributes=this.nbAttr;
for (let c=0; c<nbAttributes-1; c++){ if(attributes.length==3){
for (let n=0; n<nbAttributes-1; n++){ for (let c=0; c<nbAttributes; c++){
for (let s=0; s<nbAttributes-1; s++){ for (let n=0; n<nbAttributes; n++){
tabOfAllCards.push(new Card3(ATTRIBUTES[0][c],ATTRIBUTES[1][n],ATTRIBUTES[2][s])); for (let s=0; s<nbAttributes; s++){
attribDic[attributes[0]]=dicoAttributes[attributes[0]][a]
attribDic[attributes[1]]=dicoAttributes[attributes[1]][b]
attribDic[attributes[2]]=dicoAttributes[attributes[2]][c]
tabOfAllCards.push(new Card(attribDic))
} }
} }
} }
} }
else if(nbAttributes==4){ else if(attributes.length==4){
for (let n=0; n<nbAttributes-1; n++){ for (let a=0; a<nbAttributes; a++){
for (let s=0; s<nbAttributes-1; s++){ for (let b=0; b<nbAttributes; b++){
for (let f=0; f<nbAttributes-1; f++){ for (let c=0; c<nbAttributes; c++){
for (let o=0; o<nbAttributes-1; o++){ for (let d=0; d<nbAttributes; d++){
if(attributes[0][0]===0){ let attribDic = {};
tabOfAllCards.push(new Card4WithoutColor(attributes[1][n],attributes[2][s],attributes[3][f],attributes[4][o])); attribDic[attributes[0]]=dicoAttributes[attributes[0]][a]
} attribDic[attributes[1]]=dicoAttributes[attributes[1]][b]
else if(attributes[1][0]===0){ attribDic[attributes[2]]=dicoAttributes[attributes[2]][c]
tabOfAllCards.push(new Card4WithoutNumber(attributes[0][n],attributes[2][s],attributes[3][f],attributes[4][o])); attribDic[attributes[3]]=dicoAttributes[attributes[3]][d]
} tabOfAllCards.push(new Card(attribDic))
else if(attributes[2][0]===0){
tabOfAllCards.push(new Card4WithoutShape(attributes[0][n],attributes[1][s],attributes[3][f],attributes[4][o]));
}
else if(attributes[3][0]===0){
tabOfAllCards.push(new Card4WithoutFilling(attributes[0][n],attributes[1][s],attributes[2][f],attributes[4][o]));
}
else if(attributes[4][0]===0){
tabOfAllCards.push(new Card4WithoutOutline(attributes[0][n],attributes[1][s],attributes[2][f],attributes[3][o]));
}
} }
} }
} }
} }
} }
else if(nbAttributes==5){ else if(attributes.length==5){
for (let c=0; c<nbAttributes-1; c++){ for (let a=0; a<nbAttributes; a++){
for (let n=0; n<nbAttributes-1; n++){ for (let b=0; b<nbAttributes; b++){
for (let s=0; s<nbAttributes-1; s++){ for (let c=0; c<nbAttributes; c++){
for (let f=0; f<nbAttributes-1; f++){ for (let d=0; d<nbAttributes; d++){
for (let o=0; o<nbAttributes-1; o++){ for (let e=0; e<nbAttributes; e++){
tabOfAllCards.push(new Card5(attributes[0][c],attributes[1][n],attributes[2][s],attributes[3][f],attributes[4][o])); attribDic[attributes[0]]=dicoAttributes[attributes[0]][a]
attribDic[attributes[1]]=dicoAttributes[attributes[1]][b]
attribDic[attributes[2]]=dicoAttributes[attributes[2]][c]
attribDic[attributes[3]]=dicoAttributes[attributes[3]][d]
attribDic[attributes[4]]=dicoAttributes[attributes[4]][e]
tabOfAllCards.push(new Card(attribDic))
} }
} }
} }

@ -67,6 +67,7 @@ function numberOfSets3(deck){
for(j=i+1;j<deck.length-1;j++){ for(j=i+1;j<deck.length-1;j++){
for(k=j+1;k<deck.length;k++){ for(k=j+1;k<deck.length;k++){
if(isSet([deck[i],deck[j],deck[k]])){ if(isSet([deck[i],deck[j],deck[k]])){
//console.log(deck[i],deck[j],deck[k])
res += 1 res += 1
} }
} }
@ -81,7 +82,8 @@ function numberOfSets4(deck){
for(j = i+1 ; j < deck.length - 2; j++){ for(j = i+1 ; j < deck.length - 2; j++){
for(k = j+1 ; k < deck.length - 1 ; k++){ for(k = j+1 ; k < deck.length - 1 ; k++){
for(l = k + 1 ; l < deck.length;l++){ for(l = k + 1 ; l < deck.length;l++){
if(isSet([deck[i],deck[j],deck[k],deck[l]])){ if(isSet([deck[i],deck[j],deck[k]])){
//console.log(deck[i],deck[j],deck[k],deck[l])
res += 1 res += 1
} }
} }
@ -98,7 +100,8 @@ function numberOfSets5(deck){
for(k = j+1 ; k < deck.length - 2 ; k++){ for(k = j+1 ; k < deck.length - 2 ; k++){
for(l = k + 1 ; l < deck.length - 1;l++){ for(l = k + 1 ; l < deck.length - 1;l++){
for(m = l + 1; m <deck.length;m++){ for(m = l + 1; m <deck.length;m++){
if(isSet([deck[i],deck[j],deck[k],deck[l],deck[m]])){ if(isSet([deck[i],deck[j],deck[k]])){
//console.log(deck[i],deck[j],deck[k],deck[l],deck[m])
res += 1 res += 1
} }
} }

Loading…
Cancel
Save