Mikke.Learns

Coding Project: Bingo. Part 3

May 1st, 2019

I was happy to get back to my coding. I cleaned up some comments and prettified the code.

Today’s first challenge was to clear the centerpiece of the board. On the version I am making, the center is always “free”.

b: (5) [7, 1, 9, 3, 5] i: (5) [30, 27, 29, 25, 23] n: (5) [44, 45, 47, 32, 33] g: (5) [48, 51, 49, 58, 50] o: (5) [64, 67, 65, 72, 74] In this example, the number 47 should be cleared. I am not sure how to indicate that the numbers on the board are drawn yet, but I start with zeroing them out.

This was pretty simple. I inserted this function inside the fillPlayerBoard described in the last post.

// A function to fill a row in the player board const fillPlayerBoard = function (rowInPlayerBoard, number) { // Create a random number randomNum = randomNumber(number); counter = 0; // While loop to make sure that there are no duplicate numbers while (counter < 5) { if (rowInPlayerBoard.indexOf(randomNum) == -1) { rowInPlayerBoard.push(randomNum); counter++ } else { randomNum = randomNumber(number); } } // Make the middle of the player board "free" if (rowInPlayerBoard == playerBoard.n) { playerBoard.n[2] = 0; } } I check to see if the middle row of the player board is passed into the function. If so, I tell the function to turn that number into a zero.

It seems to be working:

b: (5) [13, 2, 7, 10, 9] i: (5) [21, 24, 27, 23, 18] n: (5) [34, 45, 0, 43, 41] g: (5) [52, 51, 60, 57, 46] o: (5) [66, 70, 64, 63, 71] Check for match Then came a tougher task. I needed to check if the number drawn matched any of the numbers on the player board.

I figured this had to be done inside the drawNumber function I made last time:

// Draw a random number from numbersToDraw const drawNumber = function () { // Pick a random number from numbersToDraw let randomNumber = Math.floor((Math.random() * numbersToDraw.length) + 1); // We need to get the first item in the array we splice from numbersToDraw controllBoard.push(numbersToDraw.splice(randomNumber, 1)[0]); } First I made a new const “entriesInPlayerBoard” for the values in the playerBoard object. I then made a for loop that iterates through these values, looking for a match with .indexOf. If there is a match, then turn that number on the player board into a zero. // Check player board for number for (var i = 0; i < entriesInPlayerBoard.length; i++) { if (entriesInPlayerBoard[i].indexOf(numbersToDraw[randomNumber]) != -1) { console.log('Match ' + entriesInPlayerBoard[i][entriesInPlayerBoard[i].indexOf(numbersToDraw[randomNumber])]); entriesInPlayerBoard[i][entriesInPlayerBoard[i].indexOf(numbersToDraw[randomNumber])] = 0; } } // We need to get the first item in the array we splice from numbersToDraw controllBoard.push(numbersToDraw.splice(randomNumber, 1)[0]); This is how the complete function looks like now: // Draw a random number from numbersToDraw const drawNumber = function () { // Pick a random number from numbersToDraw let randomNumber = Math.floor((Math.random() * numbersToDraw.length) + 1); const entriesInPlayerBoard = Object.values(playerBoard); // Check player board for number for (var i = 0; i < entriesInPlayerBoard.length; i++) { if (entriesInPlayerBoard[i].indexOf(numbersToDraw[randomNumber]) != -1) { console.log('Match ' + entriesInPlayerBoard[i][entriesInPlayerBoard[i].indexOf(numbersToDraw[randomNumber])]); entriesInPlayerBoard[i][entriesInPlayerBoard[i].indexOf(numbersToDraw[randomNumber])] = 0; } } // We need to get the first item in the array we splice from numbersToDraw controllBoard.push(numbersToDraw.splice(randomNumber, 1)[0]); } That .indexOf method doesn’t look too nice, and the line gets too long. It’s difficult to read. I need to try and find a better solution for it, but right now it’s doing the job.