Skip to content

Commit 94cee66

Browse files
authoredSep 25, 2016
Merge pull request #14 from ilasorsa/master
Add player class, draft specs for it, add two error classes and refactor project directory structure
2 parents db8e712 + 089e299 commit 94cee66

File tree

10 files changed

+99
-31
lines changed

10 files changed

+99
-31
lines changed
 

‎lib/errors/column_full_error.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module RubyConnect
2+
class ColumnFullError < StandardError; end
3+
end

‎lib/errors/game_over_error.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module RubyConnect
2+
class GameOverError < StandardError; end
3+
end

‎lib/game.rb

-13
This file was deleted.

‎lib/board.rb renamed to ‎lib/models/board.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'errors/column_full_error'
2+
13
module RubyConnect
24
class Board
35
attr_reader :grid
@@ -16,6 +18,10 @@ def full?
1618
slots.all?
1719
end
1820

21+
def moves_done
22+
slots.compact.count
23+
end
24+
1925
def insert_into_column(column, color)
2026
column -= 1
2127
row = first_open_slot column
@@ -25,7 +31,7 @@ def insert_into_column(column, color)
2531
private
2632

2733
def first_open_slot(column)
28-
grid[column].index(nil) || raise(ArgumentError, "Column full")
34+
grid[column].index(nil) || raise(ColumnFullError, "Column full")
2935
end
3036

3137
def slots

‎lib/models/game.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'errors/game_over_error'
2+
3+
module RubyConnect
4+
class Game
5+
attr_accessor :board, :winning_move
6+
7+
def initialize(player1, player2)
8+
@board = Board.new
9+
@winning_move = false
10+
@player1 = player1
11+
@player2 = player2
12+
end
13+
14+
def done?
15+
board.full? || winning_move
16+
end
17+
18+
def make_move(column, color)
19+
raise GameOverError if done?
20+
board.insert_into_column column, color
21+
winning_move = false # TODO: winning_move?
22+
end
23+
end
24+
end

‎lib/models/player.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module RubyConnect
2+
class Player
3+
attr_reader :game, :color
4+
5+
def initialize(info = {})
6+
@name = info[:name]
7+
@phone_number = info[:phone_number]
8+
@color = info[:color]
9+
@game = info[:game]
10+
end
11+
12+
def make_move(column)
13+
game.make_move(column, color)
14+
end
15+
end
16+
end

‎spec/game_spec.rb

-15
This file was deleted.

‎spec/board_spec.rb renamed to ‎spec/models/board_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'board'
1+
require 'models/board'
22

33
module RubyConnect
44
describe Board do
@@ -31,7 +31,7 @@ module RubyConnect
3131
end
3232

3333
it 'raises an error if you try to select a column' do
34-
expect{ full_board.insert_into_column 1, :red }.to raise_error(ArgumentError)
34+
expect{ full_board.insert_into_column 1, :red }.to raise_error(ColumnFullError)
3535
end
3636
end
3737
end

‎spec/models/game_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'models/game'
2+
require 'models/board'
3+
4+
module RubyConnect
5+
describe Game do
6+
let(:player1) { Player.new }
7+
let(:player2) { Player.new }
8+
let(:game) { Game.new(player1, player2) }
9+
10+
it 'can add one piece to the board' do
11+
expect{ game.make_move(1, :red) }.to change{ game.board.moves_done }.from(0).to(1)
12+
end
13+
14+
context 'when the grid is full' do
15+
before do
16+
(1..Board::COLUMNS).each do |column|
17+
Board::ROWS.times { game.make_move column, :red }
18+
end
19+
end
20+
21+
it 'then the game is done' do
22+
expect(game).to be_done
23+
end
24+
end
25+
26+
it 'switches player turns after one move'
27+
28+
describe '#winning_move?' do
29+
it 'in a vertical direction'
30+
it 'in a horizontal direction'
31+
end
32+
end
33+
end

‎spec/models/player_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'models/player'
2+
3+
module RubyConnect
4+
describe Player do
5+
let(:player) { Player.new color: :red, name: "John", game: 1 }
6+
7+
describe '#make_move' do
8+
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)
Please sign in to comment.