Week 2, Day 2: Chess (My Favorite Game, Coincidentally)
Today we began to build a chess game. Anyone who knows me, knows that I'm nuts for chess. I'm enjoying this assignment a bunch. My partner Mark and I really wanted to make the code modular and well-organized. The past two days, we haven't been learning new concepts so much as honing our programming style. Assignments like Minesweeper and Chess really force us to consider how domain objects relate to each other, and it gives us great practice in decomposing methods and grouping inherited functionality.
Learned a cool trick with parallel assignment today: you can define multiple variables on a single line to point to multiple items in an array.
diapers = ["huggies","pampers"]
my_diaper, your_diaper = diapers
In the snippet above, I'd be wearing huggies, and you pampers.
Another great thing to do is to overwrite the [ ] method when constantly dealing with multi-dimensional arrays.
def [ ] (position)
self[position[0]][position[1]]
end
Now instead of asking for a position by querying two nested arrays, you can just pass in a single array and let the [ ] function take care of separating your request and delegating it to the sub arrays.
location_of_my_pacifier =
[["SF", "CA"], ["SOMA", "160 Folsom"]]
pacifier_thief = Thief.new(location = ["SF", "160 Folsom"])
puts "Run away! Don't take my binkie!" if
pacifier_thief.location == pacifier[location_of_my_pacifier]
I hope to refactor our code a bit with Mark and add in these ideas as we continue building our chess game tomorrow. This project is our first to span multiple days and is shaping up to be pretty beastly. I'm cognizant, though, that it'll be nothing compared to what we'll put out in a few weeks time.