Blogging Through a Problem at Flatiron School

So, I’m stuck since last night on a problem in the Flatiron School Community Based Program. I figured that it would be a good idea to use this blog as my rubber duck and work though this step by step. So, here’s a rubber duck for me to explain it to. For those thinking I’ve lost my mind, go here.

That’s right, Bat Duck is here to help me solve the problem!

So, let me lay out what I’m trying to do by rephrasing the issue. No copying and pasting!

The first method I need to build is one called won? that will take in all the possible ways of winning tic tac toe.

So, you can win by getting three in a row on each row as well as each column, as well as 3 in the diagonals from upper left to bottom right and from upper right to bottom left. That is 8 different ways to win.

Now, we have been using an array for this so each space in the tic tac board is numbered 0 to 8, with 0 at the top left hand corner down to 8 in the lower right hand corner. The first task is for me to make a nested array of all the combinations to win, utilizing the indexes of each space, and place them inside a constant. A constant is a variable type in Ruby that begins with a capital letter and is used for data that is unlikely to change. Since the ways to win are set in stone, this will be a constant. This is simple enough. Take a look:

This next part is where I got confused. So, the method should accept a board as an argument. So, something like this but updated with moves (X or O wherever the user moved):

And return false/ nil if there is no win combination present in the board, and if there is a winning combination of indexes, return that as an array. It was clear, however, that this method should not decide WHO won, ie whether X or O had the winning combination. This is a helper method to be used in another method in a bit.

OK, so let’s look back over the ways this whole lesson discussed enumerating through these arrays.

First is the Boolean Enumerables. Let’s start with all?.

all? will take an array of data and then decide if all the data meets the requirements. The example given in the lesson was passing in an array like [1,3,5] and asking Ruby if they were odd. Since ALL of the numbers were evaluated as true then it returned true.

The next Boolean Enumerable is none?. It is the opposite of all? in that it asks the opposite of all. So, if asked if [1,3,5] are even, it will say none are and return true.

Next up is any?. Any will go through an array like [1,2,3,5,7,9] and ask are ANY of these even. Since 2 is even it will say true, one of this list is even.

Next is include? which asks does this array of numbers, [34,56,78,98,345,43, 65] include a specific number, 72. The method will go through and if it finds the number it returns true. If not, its false.

So, would any of these work. I’m not so sure yet. I’m taking an array and asking it the same player token is in the 3 spots for each of each array in the WIN_COMBINATIONS constant. I can see where a few of these may work, but none honestly scream at me as the winner. Let’s look at the other enumerables…

These are search enumerables. Honestly, that alone makes me feel a little more comfortable as searching through the board to see if any are winning combinations is sort of what I need to do.

The first is select. Now, this looks promising. First, it will return a new array with the results that passed true. OK, that sounds promising. So, for example, if I have an array like this: [1,2,3,4,5,6,7,8] and tell it to select the even numbers it will return this: [2,4,6,8]. So, if I tell it to select the matching arrays that have X’s at 3 indexes, will it return those? OK, I want to try this.

To the IDE!

OK, as of 11:30PM I am still stuck… I will keep up this tomorrow for those wondering.

To sum up what I have discovered, including help from someone on a Slack channel related to Flatiron’s Learn platform,

I have this:

and the i inside the pipes is the single array inside the larger WIN_COMBINATIONS constant. I need to compare the values of board to the positions inside of i. I’m still trying to make this work, but I probably need sleep at this point to approach it with fresh eyes…

One thought on “Blogging Through a Problem at Flatiron School

Comments are closed.