Newer Win Conditions

 In my last post I went through the process of explaining the basic functioning of the small bingo simulator which I'm calling BitNGo. Now, I'm going to explain how each win condition works.

This probably won't be a long post.

Full Table

This probably was the easiest one to code. We just make a for loop that checks if every letter of the board has the complete 5 chips. If every letter is indeed full, a bingo is announced. For this win condition, we applied the very useful array.prototype.every() function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function wc_full_table()
{
    if(played_nums.length >= 24)
    {
        for(i=0; i<boards.length; i++)
        {
            if(boards[i][1].every(element => element === "X") && boards[i][2].every(element => element === "X") && boards[i][3].every(element => element === "X") && boards[i][4].every(element => element === "X") && boards[i][5].every(element => element === "X"))
            {
                console.log("Full table on board " + i);
            }
        }
    }
}

We also added an if() condition that checks that at least 24 chips have been played, since it would be impossible for a board to be full if the minimum amount of chips haven't been played. Although not very elegant, I did this for optimization purposes.

Four Corners

Another piece of cake to code. We just have to check 4 boxes on this one. This one is also very resource friendly.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function wc_four_corners()
{
    if(played_nums.length >= 4)
    {
        for(i=0; i<boards.length; i++)
        {
            if(boards[i][1][0] === "X" && boards[i][1][4] === "X" && boards[i][5][0] === "X" && boards[i][5][4] === "X")
            {
                console.log("Four corners on board " + i);
            }
        }
    }
}

There's nothing much to that one as you can see. Pretty simple.

Linked Four Corners

Finally, the linked four corners is just a frame around the board, covering the 16 outer boxes. This one is a little tricky because it just looks terrible to code. But whatever.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function wc_linked_four_corners()
{
    if(played_nums.length >= 16)
    {
        for(i=0; i<boards.length; i++)
        {
            if(boards[i][1].every(element => element === "X") && boards[i][5].every(element => element === "X") && boards[i][2][0] === "X" && boards[i][2][4] === "X" && boards[i][3][0] === "X" && boards[i][3][4] === "X" && boards[i][4][0] === "X" && boards[i][4][4] === "X")
            {
                console.log("Linked four corners on board " + i);
            }
        }
    }
}

As you can see, we check if the letter B and O are complete, and then we check the check the last 6 boxes separately. This illustration explains it a little better (the dark rectangles being the letters B and O, and the green circles being the 6 remaining letters).


As you can see, it is not a very elegant solution, but for now it will do just fine.




"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away."

-Antoine de Saint-Exupéry


Before I finish this post, I would like to make one thing clear: this are all probably terrible solutions for the problems that I'm trying to solve. See, even though it works, it is probably very slow, and the computing time for every action (like looking up numbers on boards or checking for bingos), might even increase exponentially if we have multiple boards in game.
What I'm trying to say is, in coding, almost never your first solution will be the best; so I will be changing these functions constantly, with the main goal of saving computer power.
So don't see these lines of code as definitive solutions, but rather as a time machine where you'll be able to see the evolution of my personal problem solving.

For the next post, I will try to optimize the chip lookup time by a wide margin. Thanks for reading!

Comments

Popular posts from this blog

An Audioguide about the Geology of Ecuador

Active Projects as of Today

Explaining Color-Driven Summation