July 29, 2015 Log No. 302

And on the Second Day of a/A, My True Love Gave to Me…

2 projects in one day! Tiring! But at least we managed to get through both the Sudoku checker and the connect-4 game by 6pm. Connect-4 had some not-very-pretty methods, though we refactored the diagonal_winner checker to be manageable. Our straight_line checker may have been a little more convoluted to understand, but I do like that it was operationally less exhaustive, aka more “optimized” (Instead of considering all possible 4-space slices, it only looks at what is actually in the board.)

But to make our diagonal_winner method the best it could be, I’m still trying to figure out a way for Ruby to dynamically select operators based on some condition–basically generate expressions on the fly. In this case, I’m passing in these parameters:

[row, col, row_modifier, col_modifier]

I want to run this block of code:

(0..3).each { |i| diagonal << grid[r +/- i][c +/- i] }

where the + operator is used if row_modifier is a positive number or - if row_modifier is a negative number, and same for c/c_modifier. Haven't found the solution yet, though would be cool right? Pretty meta functionality. Excel had something like that with the INDIRECT() function, so if Excel can do it, I'm sure Ruby has it somewhere.


Leave a Reply

Your email address will not be published. Required fields are marked *

*