Week 2, day 2
This week's pair-programming exercises have been to recreate the Battleships game. My partner has some experience coding so it's gone very well. There is always a concern that a disparity in skill can lead to problems when pairing but by and large I haven't found this to be the case. If I'm a bit ahead of someone else I learn so much by explaining what I'm doing to them and when I've paired with someone that is ahead of me I learn so much from them, it's a win-win! My personal accomplishment for the day was initializing a hash to represent our game board.
1 def initialize
2 @grid = {}
3 ('A'..'J').each do |letter|
4 (1..10).each do |number|
5 @grid[(letter + number.to_s).to_s] = 'E'
6 end
7 end
8 end
9 # creating a 10 x 10 grid of hashes with lettered and numbered
10 # keys with changeable values to define status of the cells
We used the MVP (Minimum Viable Product) principle, focus on the smallest object, then make it smaller and smaller until it can't be any smaller then start writing tests and application code to pass those tests, and build from there. Much less of an emphasis on behaviour-driven design and large feature tests and more of an emphasis on lots of smaller, more useful unit tests. Our coach's mantra is YAGNI - You Ain't Gonna Need It!
The 'Ship' or ships were our MVPs and here are some of our unit tests, all passed of course! I'm beginning to develop a love-hate relation with Rubocop, I love that it forces you to write very compact, efficient and well layed out code. I hate when it just won't leave you alone! Rspec knowledge has been increasing dramatically, I'm addicted to writing and passing tests, those green dots in Ruby are magical.
1 require 'ship'
2
3 describe Ship do
4 it 'can be initiated with a length' do
5 expect(subject.length).not_to eq 0
6 end
7
8 it 'can be hit' do
9 subject.hit
10 expect(subject.hit_count).to eq 1
11 end
12
13 it 'can be sunk' do
14 subject.hit
15 expect(subject.hit).to eq 'SUNK!'
16 expect(subject).to be_sunk
17 end
18
19 it 'can have a size of 2, 3, 4 or 5 when created' do
20 ship2 = Ship.new 2
21 ship3 = Ship.new 3
22 ship4 = Ship.new 4
23 ship5 = Ship.new 5
24 expect(ship2.length).to eq 2
25 expect(ship3.length).to eq 3
26 expect(ship4.length).to eq 4
27 expect(ship5.length).to eq 5
28 end
29
30 it 'cannot be instantiated with a length of less than 2 or more than 5' do
31 expect { (Ship.new 1) }.to raise_error 'Invalid length'
32 expect { (Ship.new 6) }.to raise_error 'Invalid length'
33 end
34
35 xit 'can be placed horizontally on the board'
36
37 xit 'can be placed vertically on the board'
38 end