-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.rb
55 lines (53 loc) · 1.46 KB
/
validate.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
require 'json'
class Validate
attr_accessor :iboard
def initialize(jboard, move)
@iboard = nil
temp = JSON.parse(jboard, symbolize_names: true)
@gmatrix = temp[:grid] #2D List
@height = temp[:height]
@width = temp[:width]
@player =temp[:player]
mtemp = JSON.parse(move, symbolize_names: true)
@move = mtemp[:move]
validate
end
def validate
if @gmatrix.nil?
puts 'Error! Could not read JSON file!'
elsif @gmatrix.respond_to?('each')
@iboard = Board.new(@gmatrix, @move, @player)
@iboard.class.setcols(@width)
@iboard.class.setrows(@height)
i = 0
@gmatrix.each do |col|
if col.respond_to?('each')
j = 0
col.each do |row|
case row
when 2
@iboard.column_moves[i][2] += 1
@iboard.row_moves[j][2] += 1
@iboard.total_moves += 1
when 1
@iboard.column_moves[i][1] += 1
@iboard.row_moves[j][1] += 1
@iboard.total_moves += 1
when 0
nil # Do nothing
else
raise StandardError, 'Error! Invalid player number in grid!'
end
j += 1
end
else
raise StandardError, 'Error! Could not access column data in rows!'
end
i += 1
end
@iboard.make_move(@move, @player)
else
raise StandardError, 'Error! Could not access row data!'
end
end
end