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/Card.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/Factory.js"></script>
<script src="../Model/Deck.js"></script>

@ -66,4 +66,4 @@ const list2 = [1, 1, 1, 1];
console.log(checkList(list2)); // Retourne 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{
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
*/
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
* @returns boolean
*/
equals(card){}
}//export {Card}
equals(card){//working✅
let bool=true;
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_OUTLINE = ['continuous','dotted','hyphen','cloudy','sharpy'];
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
*/
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.remainingCards=[]
this.nbCards=nbCards;
this.remainingCards=this.remainingCards.concat(this.allCards);// cards in the stack
this.remainingCards=[];//init tab null
this.remainingCards=this.remainingCards.concat(this.allCards);// cards in the stack, init = all before creation of deck -> remove
this.outputCards=[];// 12 cards lay on the table
this.setMade=[];// array of array with all the set already mades (array of set)
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
*/
createDeck(nbCards){
if(this.remainingCards.length==0){
createCards(attributes){//working✅
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");
return;
}
@ -30,7 +38,11 @@ class Deck{
this.outputCards.push(this.remainingCards[rand]);
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)
}
}
@ -41,41 +53,32 @@ class Deck{
* @returns random number in range of the array size
* @author Bastien Jacquelin
*/
getRandCard(){
getRandCard(){//working✅
const random = Math.floor(Math.random() * this.remainingCards.length);
return random;
}
/**
*
* @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
* @brief verification of the validity of the set selected, call removeFromoutputCards when set is confirmed
* @param {*} selectedCards array of cards : set
* @author Bastien Jacquelin
*/
checkSet(selectedCards){
if(true){//isSet(selectedCards)){
checkSet(selectedCards){//toTest⌛
if(true){//isSet(selectedCards)){// is a set
if(this.outputCards.length==0){
console.log("C'est win")
return;
return 2;
}
else{
this.removeFromoutputCards(selectedCards);
return 1;
}
}
else if(this.remainingCards.length==0){
else if(this.remainingCards.length<this.nbCards){
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
* @author Bastien Jacquelin
*/
removeFromoutputCards(selectedCards){//better check of card type more opti
removeFromoutputCards(selectedCards){//working✅
let set=[];
selectedCards.forEach(element => {
for(let i=0; i<this.outputCards.length;i++){
@ -94,7 +97,6 @@ class Deck{
}
}
});
if(set.length<1){
throw new UnFoundCardException(selectedCards);
}

@ -1,7 +1,46 @@
class Factory{
constructor(arrayOfAttributes){
let length=arrayOfAttributes.length
this.product=this.concreteCardCreation(arrayOfAttributes,length);
constructor(arrayOfAttributes, nbAttributes){
this.nbAttr=nbAttributes;
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
@ -9,7 +48,7 @@ class Factory{
* @param {*} arrayOfAttributes array
* @returns boolean
*/
inArray(i,arrayOfAttributes){
inArray(i,arrayOfAttributes){//toTest⌛
let finded=false;
for (let j=0;j<arrayOfAttributes.length;j++){
if(i==arrayOfAttributes[j]){
@ -17,77 +56,70 @@ class Factory{
}
}
return finded
}
}
/**
* @brief create a matrix with the attributes of the cards : if attributes not defined : value 0
* @param {*} arrayOfAttributes
* @param {*} length
* @returns matrix of attributes
*
* @param {*} dico dictionnary of attributes
* @returns array of the key : name of the attributes
*/
attributesRequiredFun(arrayOfAttributes){
let attributesRequiredTmp=[];
let nullArray=[0,0,0,0,0];
for(let i=0;i<5;i++){
if(!this.inArray(i,arrayOfAttributes)){
attributesRequiredTmp.push(nullArray);
}
else{
attributesRequiredTmp.push(ATTRIBUTES[i]);
}
}
return attributesRequiredTmp;
attributesName(dico){//working✅
let attributes=[]
Object.entries(dico).forEach(function([key, value]) {
attributes.push(key);
});
return attributes
}
/**
* @biref create the right cards : 3,4,5 attributes
* @brief create the right cards : 3,4,5 attributes
* @param {*} arrayOfAttributes
* @param {*} length
* @returns array of all cards
*/
concreteCardCreation(arrayOfAttributes, length){
concreteCardCreation(){//working✅
let tabOfAllCards=[];
let attributes=this.attributesRequiredFun(arrayOfAttributes);
let nbAttributes=length
if(nbAttributes==3){
for (let c=0; c<nbAttributes-1; c++){
for (let n=0; n<nbAttributes-1; n++){
for (let s=0; s<nbAttributes-1; s++){
tabOfAllCards.push(new Card3(ATTRIBUTES[0][c],ATTRIBUTES[1][n],ATTRIBUTES[2][s]));
let dicoAttributes=this.dicoAttributes
let attributes=this.attributesName(dicoAttributes);
let nbAttributes=this.nbAttr;
if(attributes.length==3){
for (let c=0; c<nbAttributes; c++){
for (let n=0; n<nbAttributes; n++){
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){
for (let n=0; n<nbAttributes-1; n++){
for (let s=0; s<nbAttributes-1; s++){
for (let f=0; f<nbAttributes-1; f++){
for (let o=0; o<nbAttributes-1; o++){
if(attributes[0][0]===0){
tabOfAllCards.push(new Card4WithoutColor(attributes[1][n],attributes[2][s],attributes[3][f],attributes[4][o]));
}
else if(attributes[1][0]===0){
tabOfAllCards.push(new Card4WithoutNumber(attributes[0][n],attributes[2][s],attributes[3][f],attributes[4][o]));
}
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(attributes.length==4){
for (let a=0; a<nbAttributes; a++){
for (let b=0; b<nbAttributes; b++){
for (let c=0; c<nbAttributes; c++){
for (let d=0; d<nbAttributes; d++){
let attribDic = {};
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]
tabOfAllCards.push(new Card(attribDic))
}
}
}
}
}
else if(nbAttributes==5){
for (let c=0; c<nbAttributes-1; c++){
for (let n=0; n<nbAttributes-1; n++){
for (let s=0; s<nbAttributes-1; s++){
for (let f=0; f<nbAttributes-1; f++){
for (let o=0; o<nbAttributes-1; o++){
tabOfAllCards.push(new Card5(attributes[0][c],attributes[1][n],attributes[2][s],attributes[3][f],attributes[4][o]));
else if(attributes.length==5){
for (let a=0; a<nbAttributes; a++){
for (let b=0; b<nbAttributes; b++){
for (let c=0; c<nbAttributes; c++){
for (let d=0; d<nbAttributes; d++){
for (let e=0; e<nbAttributes; e++){
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(k=j+1;k<deck.length;k++){
if(isSet([deck[i],deck[j],deck[k]])){
//console.log(deck[i],deck[j],deck[k])
res += 1
}
}
@ -81,7 +82,8 @@ function numberOfSets4(deck){
for(j = i+1 ; j < deck.length - 2; j++){
for(k = j+1 ; k < deck.length - 1 ; k++){
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
}
}
@ -98,7 +100,8 @@ function numberOfSets5(deck){
for(k = j+1 ; k < deck.length - 2 ; k++){
for(l = k + 1 ; l < deck.length - 1;l++){
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
}
}

Loading…
Cancel
Save