Teach Something to Learn Something
Although I can’t imagine anyone needing to learn something related to Ruby programming from me, my Launch Academy assignment is to write a technical blog entry … and so I shall.
Tic-Tac-Toe
The aforementioned assignment stated an elementary topic could be utilized (and I’m far from capable of addressing an advanced topic) so I’ve chosen to describe the process of creating a Ruby method that identifies a winning Tic-Tac-Toe board. Creation of such a method was one of the programming challenges provided as (part of) preparation for the Launch Academy course beginning November 10.
Identifying a Winning Board
My understanding of the challenge was that a multi-dimensional array (also known as an array of arrays) would be provided and that the Ruby method created must return the Ruby object true if that array contained a winning combination (of “x” or “o”). Conversely, the Ruby object false must be returned if no winning combination was contained (by the array).
I’m embarrassed to say that (as was the case with many of the challenges) I was uncertain how to solve the problem so I decided to simply address one row of a given board (relative to “x”). Indeed, I noticed during multiple challenges that attempting to solve one component of a problem resulted in solving numerous components (or even the entirety) of the problem. I might describe the phenomenon as “code begetting code” and it’s an approach that I suspect may be useful in resolving a variety of issues/problems.
In any case, I elected to use an “if” statement specifying respective array indices to determine whether each was equal to “x”. Once my testing suggested/determined that approach was viable, it was reasonably short work to identify whether a winning combination (for either “x” or “o”) was present horizontally, vertically or diagonally. The method that I submitted can be found below:
def winner?(board) if (board[0][0] == “x” && board[0][1] == “x” && board[0][2] == “x”) || \ (board[1][0] == “x” && board[1][1] == “x” && board[1][2] == “x”) || \ (board[2][0] == “x” && board[2][1] == “x” && board[2][2] == “x”) || \ (board[0][0] == “x” && board[1][0] == “x” && board[2][0] == “x”) || \ (board[0][1] == “x” && board[1][1] == “x” && board[2][1] == “x”) || \ (board[0][2] == “x” && board[1][2] == “x” && board[2][2] == “x”) || \ (board[0][0] == “x” && board[1][1] == “x” && board[2][2] == “x”) || \ (board[0][2] == “x” && board[1][1] == “x” && board[2][0] == “x”) true elsif (board[0][0] == “o” && board[0][1] == “o” && board[0][2] == “o”) || \ (board[1][0] == “o” && board[1][1] == “o” && board[1][2] == “o”) || \ (board[2][0] == “o” && board[2][1] == “o” && board[2][2] == “o”) || \ (board[0][0] == “o” && board[1][0] == “o” && board[2][0] == “o”) || \ (board[0][1] == “o” && board[1][1] == “o” && board[2][1] == “o”) || \ (board[0][2] == “o” && board[1][2] == “o” && board[2][2] == “o”) || \ (board[0][0] == “o” && board[1][1] == “o” && board[2][2] == “o”) || \ (board[0][2] == “o” && board[1][1] == “o” && board[2][0] == “o”) true else false end
end
Obviously, my approach was rudimentary (at best) and I was inclined to refactor the solution … but I tend to be reluctant to modify “functional code” (for fear of regression). Given that, I simply forged ahead with my remaining Launch Academy (i.e., Ignition) assignments.