add all class of 4 attributes cards, edit facotry

pull/32/head
Bastien JACQUELIN 2 years ago
parent 29e8e23c90
commit 99ed8b1233

@ -16,8 +16,14 @@
<script src="../Model/emptyParameterException.js"></script> <script src="../Model/emptyParameterException.js"></script>
<script src="../Model/unfoundCardException.js"></script> <script src="../Model/unfoundCardException.js"></script>
<script src="../Model/Card.js"></script> <script src="../Model/Card.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/Card5.js"></script>
<script src="../Model/Const.js"></script> <script src="../Model/Const.js"></script>
<script src="../Model/Factory.js"></script>
<script src="../Model/Deck.js"></script> <script src="../Model/Deck.js"></script>
<script src="main.js"></script> <script src="main.js"></script>
</body> </body>

@ -3,12 +3,14 @@
//import {Card} from '../Model/Card'; //import {Card} from '../Model/Card';
console.log("~#Test#~"); console.log("~#Test#~");
let card4 = new Card('red','2','losange','full'); let card4 = new Card4WithoutOutline('red','2','losange','full');
console.group('Carte 4 attributes'); console.group('Carte 4 attributes');
console.log(`carte de 4 elements : ${card4.color}`); console.log(`carte de 4 elements : ${card4.color}`);
console.groupEnd(); console.groupEnd();
let card5 = new Card5('blue','2','losange','full','pointillet'); let card5 = new Card5('blue','2','losange','full','pointillet');
console.group('Carte 5 attributes'); console.group('Carte 5 attributes');
console.log(`carte de 5 elements : ${card5.color}`);
console.log(`carte de 5 elements : ${card5.outline}`); console.log(`carte de 5 elements : ${card5.outline}`);
console.log(`carte de 5 éléments instance de 5: ${card5 instanceof Card5}`); console.log(`carte de 5 éléments instance de 5: ${card5 instanceof Card5}`);
console.log(`carte de 5 éléments accès par méthode idx 0: ${card5.getAttributes()[0]}`); console.log(`carte de 5 éléments accès par méthode idx 0: ${card5.getAttributes()[0]}`);
@ -26,19 +28,22 @@ try {
} }
} }
console.groupEnd(); console.groupEnd();
console.group('Deck');
let deck = new Deck([0,1,2,3],4);
console.group('Deck');
let deck = new Deck([0,1,2,3]);
console.log(`All cards with 4 attributes size ${deck.allCards.length}`); console.log(`All cards with 4 attributes size ${deck.allCards.length}`);
console.log(`size output ${deck.outputCards.length}`); console.log(`size output ${deck.outputCards.length}`);
// Display all cards //Display all cards
// deck.allCards.forEach(e => { console.log(`All cards`);
// console.log(e.color,e.number,e.shape,e.filling); console.log(deck.allCards)
// }); deck.allCards.forEach(e => {
console.log(`Output cards`);
deck.outputCards.forEach(e => {
console.log(e.color,e.number,e.shape,e.filling); console.log(e.color,e.number,e.shape,e.filling);
}); });
console.log(`Output cards`);
// deck.outputCards.forEach(e => {
// console.log(e.getAttributes());
// });
console.log(`set already made ${deck.setMade}`); console.log(`set already made ${deck.setMade}`);

@ -1,40 +1,15 @@
class Card{ class Card{
constructor(color, number, shape, filling, outline){ constructor(){}
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 * @returns all attributes of a card
idx 2 : number
idx 3 : shape
idx 4 : filling
idx 5 : null
* @author Bastien Jacquelin
*/ */
getAttributes(){ getAttributes(){}
return [this.color,this.number,this.shape,this.filling,this.outline]; /**
} *
equals(card){ * @param {*} card card to be compared with the current obj
return this.color===card.color && this.number===card.number && this.shape===card.shape && this.filling===card.filling && this.outline===card.outline ; * @returns boolean
} */
equals(card){}
}//export {Card} }//export {Card}

@ -0,0 +1,27 @@
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 ;
}
}

@ -0,0 +1,27 @@
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 ;
}
}

@ -0,0 +1,27 @@
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 ;
}
}

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

@ -0,0 +1,27 @@
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 ;
}
}

@ -2,10 +2,26 @@
class Card5 extends Card { class Card5 extends Card {
constructor(color, number, shape, filling, outline){ constructor(color, number, shape, filling, outline){
super(color,number,shape,filling); super();
if(!outline){ 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'); throw new EmptyParamaterException('Outline');
} }
this.color=color;
this.number=number;
this.shape=shape;
this.filling=filling;
this.outline=outline; this.outline=outline;
} }
/** /**
@ -18,8 +34,7 @@ class Card5 extends Card {
* @author Bastien Jacquelin * @author Bastien Jacquelin
*/ */
getAttributes(){ getAttributes(){
// return [this.color,this.number,this.shape,this.filling,this.outline]; return [this.color,this.number,this.shape,this.filling,this.outline];
return super.getAttributes().concat(this.outline);
} }
equals(card){ equals(card){
return this.color===card.color && this.number===card.number && this.shape===card.shape && this.filling===card.filling && this.outline===card.outline ; return this.color===card.color && this.number===card.number && this.shape===card.shape && this.filling===card.filling && this.outline===card.outline ;

@ -3,38 +3,17 @@ class Deck{
* *
* @param {*} attributes : array with the attributes index for the cards * @param {*} attributes : array with the attributes index for the cards
*/ */
constructor(attributes,nbAttributes){ constructor(attributes){
let attributesRequired=this.attributesRequiredFun(attributes); console.log(attributes);
attributesRequired.forEach(e=>{ this.allCards=this.createCards(attributes);// All the cards in the game
for(let i=0;i<e.length;i++){
console.log(e[i]);
}
console.log(`lenght: ${e.length}`);
console.log('------');
});
this.allCards=this.createCards(attributesRequired,nbAttributes);// All the cards in the game
this.remainingCards=this.allCards;// cards in the stack this.remainingCards=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();
}
attributesRequiredFun(attributes){
let attributesRequiredTmp=[];
let nullArray=[0,0,0,0,0];
for(let i=0;i<5;i++){
let find=false;
for (let j=0;j<attributes.length;j++){
if(i==attributes[j]){
attributesRequiredTmp.push(ATTRIBUTES[j]);
find=true;
}
}
if(!find){
attributesRequiredTmp.push(nullArray);
}
}
return attributesRequiredTmp;
} }
/**
* @brief creation of the deck : call factory to create the good cards
*/
createDeck(){ createDeck(){
for (let i=0; i<12; i++){ for (let i=0; i<12; i++){
const rand = this.getRandCard(); const rand = this.getRandCard();
@ -42,33 +21,22 @@ class Deck{
this.remainingCards.splice(rand,1); this.remainingCards.splice(rand,1);
} }
} }
/**
*
* @returns random number in range of the array size
*/
getRandCard(){ getRandCard(){
const random = Math.floor(Math.random() * this.remainingCards.length); const random = Math.floor(Math.random() * this.remainingCards.length);
return random; return random;
} }
/** /**
* *
* @param nbAttributes : attributes of the card, by default = 4 * @param attributes : index of the attributes used
* @returns all cards: 81 in case of 4 attributes and 1224 * @returns all cards: 81 in case of 4 attributes and 1024 if 5 attributes
*/ */
createCards(attributes,nbAttributes){ createCards(attributes){
let tabOfAllCards=[]; let factory = new Factory(attributes)
return factory.concreteCardCreation();
for (let c=0; c<this.lower(attributes[0].length,nbAttributes)-1; c++){
for (let n=0; n<this.lower(attributes[1].length,nbAttributes)-1; n++){
for (let s=0; s<this.lower(attributes[2].length,nbAttributes)-1; s++){
for (let f=0; f<this.lower(attributes[3].length,nbAttributes)-1; f++){
for (let o=0; o<this.lower(attributes[4].length,nbAttributes)-1; o++){
tabOfAllCards.push(new Card(attributes[0][c],attributes[1][n],attributes[2][s],attributes[3][f],attributes[4][o]));
}
}
}
}
}
return tabOfAllCards;
}
lower(length,nbAttributes){
return nbAttributes<length?nbAttributes:length;
} }
checkSet(selectedCards){ checkSet(selectedCards){
if(true){//isSet(selectedCards)){ if(true){//isSet(selectedCards)){
@ -77,6 +45,10 @@ class Deck{
}); });
} }
} }
/**
*
* @param {*} selectedCards wehn a set is made, need to remove the card from the array remainingCards
*/
removeFromRemainingCards(selectedCards){//better check of card type more opti removeFromRemainingCards(selectedCards){//better check of card type more opti
let set=[]; let set=[];
for(let i=0; i<this.allCards.length;i++){ for(let i=0; i<this.allCards.length;i++){

@ -0,0 +1,82 @@
class Factory{
constructor(arrayOfAttributes){
let length=arrayOfAttributes.length
this.product=this.concreteCardCreation(arrayOfAttributes,length);
}
attributesRequiredFun(arrayOfAttributes,length){
let attributesRequiredTmp=[];
let nullArray=[0,0,0,0,0];
for(let i=0;i<5;i++){
let find=false;
for (let j=0;j<length;j++){
if(i==arrayOfAttributes[j]){
attributesRequiredTmp.push(ATTRIBUTES[j]);
find=true;
}
}
if(!find){
attributesRequiredTmp.push(nullArray);
}
}
return attributesRequiredTmp;
}
concreteCardCreation(arrayOfAttributes, length){
let tabOfAllCards=[];
let attributes=this.attributesRequiredFun(arrayOfAttributes,length);
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]));
}
}
}
}
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){
console.log(attributes[0][n],attributes[1][s],attributes[2][f],attributes[4][o]);
tabOfAllCards.push(new Card4WithoutFilling(attributes[0][n],attributes[1][s],attributes[2][f],attributes[4][o]));
}
else if(attributes[4][0]===0){
console.log('rentre la');
console.log(attributes[0][n],attributes[1][s],attributes[2][f],attributes[3][o]);
tabOfAllCards.push(new Card4WithoutOutline(attributes[0][n],attributes[1][s],attributes[2][f],attributes[3][o]));
}
}
}
}
}
}
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{
//ERROR
}
return tabOfAllCards
}
}

@ -1,20 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>My awesome blog</title>
</head>
<body>
<h1>My awesome blog</h1>
</body>
<script src="./Model/Card.js"></script>
<script src="./algo.js"></script>
<script>
main();
</script>
</html>
Loading…
Cancel
Save