-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_four_spec.rb
131 lines (113 loc) · 2.89 KB
/
connect_four_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'rspec/autorun'
require 'securerandom'
a = 'Hello, World'
regex = /llo/
outcome = regex.match?(a)
#puts outcome
regex = /lllo/
outcome = regex.match?(a)
#puts outcome
# 6 rows
# 7 cols
class ConnectFour
NUM_OF_ROWS = 6
NUM_OF_COLS = 7
attr_accessor :row, :board
def initialize
@board = []
NUM_OF_ROWS.times do |i|
@row = []
NUM_OF_COLS.times do |i|
#puts i
@row << ' '
end
@board << @row
end
end
def get_next_available_row(col)
j = 0
j < NUM_OF_ROWS
while j < NUM_OF_ROWS && @board[j][col] != ' '
j += 1
end
return j if j < NUM_OF_ROWS
return -1
end
def check_for_winner
x_regex = /XXXX/
o_regex = /OOOO/
NUM_OF_COLS.times do |i|
outcome = x_regex.match?(get_column(i).join)
return true,'X is winner' if outcome
outcome = o_regex.match?(get_column(i).join)
return true,'O is winner' if outcome
end
return false
end
class WinnerDetected < StandardError; end
def play_board(token, col)
i = get_next_available_row(col)
@board[i][col] = token if i >= 0
raise ArgumentError, "column overflow at col: #{col}" if i < 0
puts show_board
puts ""
sleep 1
outcome, winner = check_for_winner
raise WinnerDetected, "Winner detected - #{winner} is the winner!" if outcome
end
def get_column(col)
accumulator = []
NUM_OF_ROWS.times do |i|
accumulator << "#{@board[i][col]}"
end
return accumulator
end
def show_board
accumulator = []
NUM_OF_ROWS.times do |i|
reversed_i = NUM_OF_ROWS - i - 1
#puts reversed_i
accumulator << "#{@board[reversed_i]}"
end
return accumulator
end
end
RSpec.describe 'ConnectFour implementation with BDD' do
it 'should display a human viewable board(i.e inverted) without overflows' do
cf = ConnectFour.new
cf.play_board('X', 2)
cf.play_board('O', 2)
cf.play_board('X', 2)
cf.play_board('O', 2)
cf.play_board('O', 2)
cf.play_board('X', 2)
board = cf.show_board
puts board
expect(cf.get_column(2)).to eq(['X','O','X','O','O','X'])
#overflow here
expect {
cf.play_board('O', 2)}.to raise_error('column overflow at col: 2')
expect {
cf.play_board('X', 2)}.to raise_error('column overflow at col: 2')
end
it 'should play board with a random generator' do
puts ""
col_randomness_range = 7
row_randomness_range = 6
cf = ConnectFour.new
(ConnectFour::NUM_OF_ROWS * ConnectFour::NUM_OF_COLS).times do |i|
begin
col = SecureRandom.random_number(col_randomness_range)
cf.play_board('X', col)
col = SecureRandom.random_number(col_randomness_range)
cf.play_board('O', col)
rescue ArgumentError
# do nothing
rescue ConnectFour::WinnerDetected => message
puts "Hurray winner detected"
puts message
break
end
end
end
end