-2

I'm looking for an explanation why this code in Javascript works.

Idea looks as follow:

             *     273                 84
             *        \               /
             *          1 |   2 |   4  = 7
             *       -----+-----+-----
             *          8 |  16 |  32  = 56
             *       -----+-----+-----
             *         64 | 128 | 256  = 448
             *       =================
             *         73   146   292

Every time a player sets his figure, the number of the field is added to his score. The numbers around the field are the wins.

Now there is this checkup:

wins = [7, 56, 448, 73, 146, 292, 273, 84],
win = function (score) {
    var i;
    for (i = 0; i < wins.length; i += 1) {
        if ((wins[i] & score) === wins[i]) {
            return true;
        }
    }
    return false;
},

What I don't understand now: If a player sets a figure in fields (numbers in the fields, order matters) 1,16,4,2, then he has a score of 23. How does the code knows that he has 3 in a row even though he does not have score 7? (which is the row on top) Because the code only compares the score with the wins, and 23 is not a win!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • Why would you want to do this in PHP? PHP is server side language. The only way I can see PHP being used here is for a multiplayer version but the actually interface would be built in Javascript. – Peter Dempsey Nov 03 '14 at 20:56
  • I AM trying to make a multiplayer version (score, players and whos turn it is is saved in a mysql db) And I want everything to be on PHP because I dont want to use ajax in this project! – TheElbenreich Nov 03 '14 at 21:01
  • Then you should have made that clear in your question. The easiest way to do this would be to use Ajax, Sockets (I would use sockets if I was being honest, take a look into Node.JS it is really cool) or dare I say it... IFrames. – Peter Dempsey Nov 03 '14 at 21:06

2 Answers2

2

The algorithm isn't just using simple math to figure out a win or not. It is using bitwise arithmetic which performs bitmasking to check for a win.

Each square has a different bit value (starting at the top right corner of the board):

1 = 000000001
2 = 000000010
4 = 000000100
...
9 = 100000000

You then add the winning squares to get the winning combinations:

7   = 000000111
56  = 000111000
448 = 111000000
...

Then we can use your example of 1, 16, 4, 2 being set:

23 = 0000001111

And when we use the bitwise and operation to check against the winning combination that it has:

  000001111 (23)
& 000000111 (7)
  ---------
  000000111 (7)

You can see the result is the same as the winning combination, which is what your algorithm is checking for.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

It's not comparing to a number, it's comparing which bits are set. The line wins[i] & score === wins[i] means that it is comparing any common bits between wins[i] and score with wins[i].

Here's an example using the values you had in your question, 1,16,4,2.

1 = 1
2 = 10
4 = 100
7 = 111
16 = 10000

Therefore if you picked all of these fields it is 10111 in binary, comparing that to 7, the common bits that are flipped is 111 (7), so it knows that there is a win because those 3 are are all set.

Shriike
  • 1,351
  • 9
  • 20