Repository navigation
Expand file tree
/
Copy pathmove.rb
More file actions
109 lines (82 loc) · 2.43 KB
/
Copy pathmove.rb
File metadata and controls
109 lines (82 loc) · 2.43 KB
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
class Move
NEIGHBOR_INCREMENTS = [[1,1], [1,0], [1,-1], [0,1], [0,-1],
[-1,1], [-1,0], [-1,-1]]
attr_accessor :move_type, :move_coords, :board, :dimension,
:dimension_range, :board_arr
def initialize(move_input, board, dimension)
self.move_type = move_input.first
self.move_coords = move_input[1]
self.board = board
self.board_arr = board.board
self.dimension = dimension
self.dimension_range = (0...dimension).to_a
if move_type == "r"
self.reveal
elsif move_type == "f"
self.flag
else
raise("Invalid move type in Move class")
end
end
def reveal
row, col = move_coords
current_square = board_arr[row][col]
return 'lose' if current_square == 'm' #for now
# leave this for now, must do a check through all neighboring squares
if current_square == '*'
adj_mine_count = adjacent_mines
if adj_mine_count == 0
board_arr[row][col] = '_'
reveal_empty_neighbors
else
board_arr[row][col] = adj_mine_count # to_s?
end
end
end
def adjacent_mines
row, col = move_coords
current_square = board_arr[row][col]
neighbors = adjacent_squares
adj_mine_count = 0
neighbors.each do |neighbor|
new_row, new_col = neighbor.first, neighbor.last
check_square = board_arr[new_row][new_col]
if check_square == 'm' || check_square == 'x'
adj_mine_count += 1
end
end
adj_mine_count
end
def adjacent_squares
row, col = move_coords
current_square = board_arr[row][col]
adjacent_squares = []
NEIGHBOR_INCREMENTS.each do |neighbor|
new_row, new_col = row + neighbor.first, col + neighbor.last
next unless dimension_range.include?(new_row)
next unless dimension_range.include?(new_col)# here
adjacent_squares << [new_row, new_col]
end
adjacent_squares
end
def reveal_empty_neighbors
neighbors = adjacent_squares
neighbors.each do |neighbor|
# reveal this neighbor
move_args = ["r", neighbor]
new_move = Move.new(move_args, board, dimension)
end
end
def flag
row, col = move_coords
if board_arr[row][col] == "*"
board_arr[row][col] = "F"
elsif board_arr[row][col] == "F"
board_arr[row][col] = "*"
elsif board_arr[row][col] == 'm'
board_arr[row][col] = "x"
elsif board_arr[row][col] == 'x'
board_arr[row][col] = 'm'
end
end
end