In the matrix game, if a user chooses two balls of the same color, those two matching color patterns will be eliminated

I am currently working on a matrix game with the following condition:

  • When a user selects two balls of the same color, they will destroy the two patterns with the same color.

I have successfully implemented horizontal and vertical selection. However, I am facing difficulties with diagonal selection. It seems like there might be an issue in my code for the diagonal selection condition.

The problem lies in my coding where the diagonal selection does not match the same color pattern. In particular, I am unsure if the below condition for diagonal selection is correct:

    onCheckPattern: function(pPattern) {
    if (pPattern != null) {
        this.mPromptTimerTally = 0;
        this.mPromptMarkSpr.setPosition(-1000.0, -1000.0);

        if (this.mFirstCheckPattern === null) {
            this.mFirstCheckPattern = pPattern;
            this.mCheckMarkSpr.setPosition(this.mPatternsPos[this.mFirstCheckPattern.m_nRowIndex][this.mFirstCheckPattern.m_nColIndex]);
        } else {
            this.mSecondCheckPattern = pPattern;
            if (this.mSecondCheckPattern === this.mFirstCheckPattern) {
                //                    this.mSecondCheckPattern = null;
                //                    return;
            }

            var isAdjacent = false;

            //HORIZONTAL& VERTICAL
            if (this.mFirstCheckPattern.m_nRowIndex == this.mSecondCheckPattern.m_nRowIndex) {
                if (this.mFirstCheckPattern.m_nColIndex > 0 &&
                    this.mFirstCheckPattern.m_nColIndex - 1 == this.mSecondCheckPattern.m_nColIndex)
                    isAdjacent = true;

                else if (this.mFirstCheckPattern.m_nColIndex + 1 < this.m_nMatrixCol &&
                    this.mFirstCheckPattern.m_nColIndex + 1 == this.mSecondCheckPattern.m_nColIndex)
                    isAdjacent = true;
            } else if (this.mFirstCheckPattern.m_nColIndex == this.mSecondCheckPattern.m_nColIndex) {
                if (this.mFirstCheckPattern.m_nRowIndex > 0 &&
                    this.mFirstCheckPattern.m_nRowIndex - 1 == this.mSecondCheckPattern.m_nRowIndex)
                    isAdjacent = true;
                else if (this.mFirstCheckPattern.m_nRowIndex + 1 < this.m_nMatrixRow &&
                    this.mFirstCheckPattern.m_nRowIndex + 1 == this.mSecondCheckPattern.m_nRowIndex)
                    isAdjacent = true;

            }
            //       


            //DIAGONAL SELECTION
            else if (this.mFirstCheckPattern.m_nRowIndex + 1, this.mFirstCheckPattern.m_nColIndex - 1 && this.mSecondCheckPattern.m_nRowIndex, this.mSecondCheckPattern.m_nColIndex)

            {
                isAdjacent = true;
            } else if (this.mFirstCheckPattern.m_nRowIndex - 1 == this.mSecondCheckPattern.m_nRowIndex && this.mFirstCheckPattern.m_nColIndex - 1 == this.mSecondCheckPattern.m_nColIndex) {
                isAdjacent = true;
            }




            if (isAdjacent) {
                this.mCheckMarkSpr.setPosition(-1000.0, -1000.0);

                this.swapTwoPattern(this.mFirstCheckPattern, this.mSecondCheckPattern, false);
                this.mFirstCheckPattern = null;
                this.mSecondCheckPattern = null;
            } else {
                this.mCheckMarkSpr.setPosition(this.mPatternsPos[this.mSecondCheckPattern.m_nRowIndex][this.mSecondCheckPattern.m_nColIndex]);

                this.mFirstCheckPattern = this.mSecondCheckPattern;
                this.mSecondCheckPattern = null;
            }
        }
    }
},

Answer №1

This section of code raises suspicion.

            else if (this.mFirstCheckPattern.m_nRowIndex + 1, this.mFirstCheckPattern.m_nColIndex - 1 && this.mSecondCheckPattern.m_nRowIndex, this.mSecondCheckPattern.m_nColIndex)

The condensed version appears to be

            else if (a + 1, b - 1 && c, d)

Due to the operation of the JavaScript comma operator, it is interpreted as else if (d). Therefore, if d is not zero, isAdjacent will be set to true.

Consider using

            else if (this.mFirstCheckPattern.m_nRowIndex + 1 == this.mSecondCheckPattern.m_nRowIndex && this.mFirstCheckPattern.m_nColIndex - 1 == this.mSecondCheckPattern.m_nColIndex)

as an alternative approach.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Validate Prop Types in VueJS When Dealing With NULL and 'undefined' Values?

Despite what the official VueJS 2 documentation on prop validation says in a code example comment: // Basic type check (null and undefined values will pass any type validation) I encountered an error while testing this piece of code — could you explai ...

What could be causing the absence of req.body data in a specific route in my Node JS application?

Hello, I am encountering an issue with my Node JS application: When attempting to authenticate, I am receiving an "undefined" value for "req.body.username" upon sending a POST request to that specific route. This problem seems to only occur on this parti ...

Assistance needed in extracting the body content utilizing javascript or jquery

I am looking to swap out the body content of one page with that of another. How can I retrieve the body content from the response text of the second page in order to make this replacement? Please assist me with this. Thank you in advance, Raja ...

How can I display different divs based on a selected option?

I'm encountering difficulties while attempting to develop the code for this specific task. My goal is to display or hide divs based on the selection made in a select option, with a total of 4 options available. There are 2 select elements <select ...

Error encountered in AngularJS when utilizing the Flickr API with the parameter "nojsoncallback=1": Unexpected token Syntax

My AngularJS application is trying to access the Flickr API. I need the data in RAW JSON format without any function wrapper, following the guidelines provided in the documentation by using &nojsoncallback=1. However, I keep encountering a console er ...

A guide to crafting a fresh WordPress post with the help of the wpapi library for Node.js

After attempting to use the wpapi module to generate a post in WordPress, I encountered a puzzling issue. Despite receiving a 200 Success response, the request body was empty and no post was actually created. var wp = new WPAPI({ endpoint: 'http:/ ...

Having trouble with adding the copy to clipboard feature

I'm having trouble implementing a copy to clipboard option on my color selector. The goal is to display the selected color on an h3 tag and create a background color generator. Each time a color is chosen, it should appear on screen for us to easily c ...

Exploring Methods for Retrieving offsetHeight and scrollHeight in AngularJS

I am currently developing a directive that requires three specific values: scrollTop offsetHeight scrollHeight projectModule.directive('scroller', function ($window) { return { restrict: 'A', link: function (scope, elem, attrs) { ...

Nextjs is having trouble loading the Infogram script

Struggling to insert an Infogram into my project by pasting the script but it's not working as expected. All other scripts in _app.js are functioning properly, however, this particular script isn't loading the graphic even though it appears when ...

Can you explain the distinction between browser.sleep() and browser.wait() functions?

Encountering timing problems with my protractor tests. Occasionally, test cases fail because of network or performance-related issues. I managed to resolve these issues using browser.sleep(), but recently discovered browser.wait(). What sets them apart an ...

Unpredictable order of replies retrieved using $http.get

I am struggling to create a function that sends multiple requests to the server based on the number of Ids in the idArray. The issue I am encountering is that the data being pushed into the dataArray does not align with the corresponding Ids of the idArr ...

Harnessing the power of lazysizes with WebP

I've been working on optimizing our site through the Google Lighthouse audit, and it's clear that images are a major focus. Right now, my main goal is to address the issue of 'Deter offscreen images' highlighted in the audit. To tackle ...

AngularJS to validate image dimensions (width and height) on upload

Hello, I am new to working with Angular JS. I have been experimenting with a login form that includes a file upload field for the user picture. The input field code looks like this: <input type="file" file-model="userPic"/> When I submit the form i ...

What potential issues arise from utilizing useRef alongside useSelector?

Although I have the capability to access the store by using thunks and/or global stores, I avoid binding my component to the Redux store. This is because the component will be utilized with various stores both inside and outside of the project. The compone ...

Is there a way to create a reusable HTTP response handler for Node.js applications?

As I work on creating a rest api for a node application, I often find myself repeating the same code structure: function(req, res, next) { databaseCall() .then( (results) => { if (results != null) { res.status(200).send(results); } el ...

Ways to authenticate custom kinds in JSON schema?

When working with JSON Schema, one of the challenges I have faced is the limitation on supporting ‘double’ and ‘float’ types for numeric values. While using AJV in JavaScript to validate a schema, it fails due to this restriction. Is there a way to ...

Utilizing custom form fields with JavaScript in Symfony2

Here is my form field template: {% block test_question_widget %} {% spaceless %} <div {{ block('widget_container_attributes') }}> {% set type = type|default('hidden') %} <input type="{{ typ ...

"Trying to access jQuery .slide and .slideUp features, but unfortunately they are

I've created this script: $("#comments .comment .links").hide(); $("#comments .comment").hover( function() { $(".links", this).stop(true).slideDown(300); }, function() { $(".links", this).stop(true).slideUp(300); } ); However, I'm facin ...

Exploring Iconography in Amcharts

Is there a simple way to include images/icons within a chart using x and y coordinates? I'm new to javascript and amcharts (am4charts) so any assistance would be greatly appreciated! I've searched for solutions on stackoverflow, like this one o ...

What could be causing my ASP.Net MVC script bundles to load on every page view?

I'm a bit puzzled. The _layout.cshtml page I have below contains several bundles of .css and .js files. Upon the initial site load, each file in the bundles is processed, which makes sense. However, every time a new view is loaded, each line of code f ...