Matching Pairs Puzzle

Hi there,

Do you know if there is an example of a matching pairs type puzzle. This is where you have a grid of pairs of items and as pairs are matched up, they puzzle is gradually solved until all pairs are matched.

Thanks

Rod

Here is a simplified form of logic required to build any memory card matching game.

class MatchGameLogic 
{
    constructor() {
        this.allowClick_Bool = true;
        this.SESSION_RESET_INTERVAL = 1500; // MS
        this.TOTAL_LEVELS = 1;
        this.level_Num = 0;
        this.random_Arr = [];
        this.cardsClicked_Arr = [];
        this.match_Arr = [];
        this.myTimer = new Timer(1000, 2);
        this.win_Bool = false;
    }

    initialize(gridPoint_param_arr, parentContainer_param_mc, cardInstance0_param_arr, cardInstance1_param_arr, cardsPerGame_param_num) {
        this.parentContainer_Mc = parentContainer_param_mc;
        this.cardInstance0_Arr = cardInstance0_param_arr;
        this.cardInstance1_Arr = cardInstance1_param_arr;
        this.gridPoint_Arr = gridPoint_param_arr;
        this.cardsPerGame_Num = cardsPerGame_param_num;
        console.log(this.cardsPerGame_Num);
        this.startGame();
    }

    onComplete(e) {
       
    }

   
    startGame() {
        let i = 0;
        this.myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onComplete.bind(this));
        this.place_Arr = [];

        for (i = 0; i < 12; i++) {
            this.place_Arr[i] = this.gridPoint_Arr[i];
        }

        this.level_Num++;

        if (this.level_Num === 1) {
            this.card0_Arr = this.cardInstance0_Arr.slice(0, this.cardsPerGame_Num);
            this.card1_Arr = this.cardInstance1_Arr.slice(0, this.cardsPerGame_Num);
        }

        console.log(this.card0_Arr.length);

        this.session_Num = 0;
        this.limit = this.card0_Arr.length + this.card1_Arr.length;
        this.win_Bool = true;

        for (i = 0; i < this.limit; i++) {
            this.random_Arr.push(i);
        }

        for (i = 0; i < this.card0_Arr.length; i++) {
            this.card0_Arr[i].grp = this.card1_Arr[i].grp = i;
            let random_num;
            let j;

            random_num = Math.floor(Math.random() * this.random_Arr.length);
            j = this.random_Arr[random_num];
            this.card0_Arr[i].x = this.place_Arr[j].x;
            this.card0_Arr[i].y = this.place_Arr[j].y;

            this.parentContainer_Mc.addChild(this.card0_Arr[i]);

            this.random_Arr.splice(random_num, 1);
            random_num = Math.floor(Math.random() * this.random_Arr.length);
            j = this.random_Arr[random_num];
            this.card1_Arr[i].x = this.place_Arr[j].x;
            this.card1_Arr[i].y = this.place_Arr[j].y;
            console.log(this.card1_Arr[i].x, this.card1_Arr[i].y);
            this.parentContainer_Mc.addChild(this.card1_Arr[i]);

            this.random_Arr.splice(random_num, 1);

            this.card0_Arr[i].addEventListener(MouseEvent.CLICK, this.card_Arr_CLICK.bind(this));
            this.card1_Arr[i].addEventListener(MouseEvent.CLICK, this.card_Arr_CLICK.bind(this));

            this.card0_Arr[i].addEventListener("onSide1OverComplete", this.onSide1OverComplete.bind(this));
            this.card1_Arr[i].addEventListener("onSide1OverComplete", this.onSide1OverComplete.bind(this));

            this.card0_Arr[i].buttonMode = true;
            this.card1_Arr[i].buttonMode = true;
        }
    }

    card_Arr_CLICK(e) {
        console.log("card_Arr_CLICK", this.allowClick_Bool);

        if (!this.allowClick_Bool) {
            return;
        }
        if (this.cardsClicked_Arr.indexOf(e.currentTarget) >= 0) {
            return;
        }

       

        this.session_Num++;

        TweenFlip(e.currentTarget).side0Over();

        if (this.session_Num === 1) {
            this.cardsClicked_Arr[0] = e.currentTarget;
        } else if (this.session_Num === 2) {
            this.allowClick_Bool = false;
            this.cardsClicked_Arr[1] = e.currentTarget;
            this.session_Num = -1;
            this.judge();
        }
    }

    judge() {
        if (this.cardsClicked_Arr[0].grp === this.cardsClicked_Arr[1].grp) {
            this.win_Bool = true;
            this.match_Arr.push(this.cardsClicked_Arr[0]);
            this.match_Arr.push(this.cardsClicked_Arr[1]);
            this.myTimer.start();
        } else {
            this.win_Bool = false;
        }
        setTimeout(this.sessionReset.bind(this), this.SESSION_RESET_INTERVAL);
    }

    sessionReset() {
        if (this.win_Bool) {
            this.onSide1OverComplete();

            this.cardsClicked_Arr[0].removeEventListener(MouseEvent.CLICK, this.card_Arr_CLICK.bind(this));
            this.cardsClicked_Arr[1].removeEventListener(MouseEvent.CLICK, this.card_Arr_CLICK.bind(this));

            this.cardsClicked_Arr[0].removeEventListener("onSide1OverComplete", this.onSide1OverComplete.bind(this));
            this.cardsClicked_Arr[1].removeEventListener("onSide1OverComplete", this.onSide1OverComplete.bind(this));

            
        } else {
            this.cardsClicked_Arr[0].side1Over();
            this.cardsClicked_Arr[1].side1Over();
            
        }

        this.cardsClicked_Arr = [];
        this.win_Bool = false;
        this.session_Num = 0;

        if (this.match_Arr.length >= this.card0_Arr.length + this.card1_Arr.length) {
            this.parentContainer_Mc.wellDone.visible = true;
            setTimeout(this.nextLevel.bind(this), 2000);
        }
    }

    onSide1OverComplete(e = null) {
        if (!this.allowClick_Bool) {
            this.allowClick_Bool = true;
        }
    }

    nextLevel() {
        for (let i = 0; i < this.card0_Arr.length; i++) {
            this.card0_Arr[i].removeEventListener(MouseEvent.CLICK, this.card_Arr_CLICK.bind(this));
            this.card1_Arr[i].removeEventListener(MouseEvent.CLICK, this.card_Arr_CLICK.bind(this));
            this.card0_Arr[i].side1Over();
            this.card1_Arr[i].side1Over();
        }

        this.match_Arr = [];

        if (this.cardsPerGame_Num * this.level_Num > this.cardInstance0_Arr.length) {
            this.level_Num = 0;
            this.myTimer.start();
        }

        this.parentContainer_Mc.wellDone.visible = false;

        console.log(">>>", this.level_Num, this.cardsPerGame_Num * this.level_Num, (this.cardsPerGame_Num) * (this.level_Num + 1) - 1);

        this.card0_Arr = this.cardInstance0_Arr.slice(this.cardsPerGame_Num * this.level_Num, (this.cardsPerGame_Num) * (this.level_Num + 1) - 1);
        this.card1_Arr = this.cardInstance1_Arr.slice(this.cardsPerGame_Num * this.level_Num, (this.cardsPerGame_Num) * (this.level_Num + 1) - 1);

        this.startGame();
    }
}

Thank you for this .... it will take me a while to sift through it. I appreciate your help.

Regards
Rod

One of these types of games? ZIM Matching Game

Yesssssss........ That is what I need. Once again, many thanks.
Rod

1 Like