-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.rb
89 lines (79 loc) · 1.79 KB
/
board.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
require_relative 'square.rb'
require 'pp'
class Board
def initialize
@squares = []
@square_positions = []
@board = [
['', '', ''],
['', '', ''],
['', '', '']
]
end
def is_finished?
done = true
@board.flatten.each do |x|
done = false if x == ''
end
return done
end
def is_square_taken?(square)
@board[square.position.row_i][square.position.col_i] != ''
end
def add_new_square(square)
@squares << square
@square_positions << square.position
@board[square.position.row_i][square.position.col_i] = square.type.to_s
end
def position_filled?(square)
@square_positions.include? square.position ? True : False
end
def to_s
%(
------------
#{@board[0][0]} | #{@board[0][1]} | #{@board[0][2]}
------------
#{@board[1][0]} | #{@board[1][1]} | #{@board[1][2]}
------------
#{@board[2][0]} | #{@board[2][1]} | #{@board[2][2]}
------------
)
end
end
class ScoreBoard
def initialize
@score = {
top: 0,
middle: 0,
bottom: 0,
left: 0,
center: 0,
right: 0,
left_diaganal: 0,
right_diaganal: 0
}
end
def player_1_move(position)
@score[position.row.to_sym] += 1
@score[position.col.to_sym] += 1
position.left_diaganal? { @score[:left_diaganal] += 1 }
position.right_diaganal? { @score[:right_diaganal] += 1 }
end
def player_2_move(position)
@score[position.row.to_sym] -= 1
@score[position.col.to_sym] -= 1
position.left_diaganal? { @score[:left_diaganal] -= 1 }
position.right_diaganal? { @score[:right_diaganal] -= 1 }
end
def winner?
win = false
@score.each do |_k, v|
if v == 3
win = 0
elsif v == -3
win = 1
end
end
return win
end
end