@@ -41,77 +41,71 @@ pub struct Grid {
41
41
pub goal : Point ,
42
42
}
43
43
44
- fn get_as_usize ( grid : & Grid , point : & Point ) -> Space {
45
- grid. spaces
46
- . get ( grid. width * point. y + point. x )
47
- . unwrap ( )
48
- . to_owned ( )
49
- }
44
+ impl Grid {
45
+ fn idx ( & self , point : & Point ) -> usize {
46
+ self . width * point. y + point. x
47
+ }
50
48
51
- fn get_as_usize_offset_row ( grid : & Grid , point : & Point , y : isize ) -> Space {
52
- let new_y = point. y as isize + y;
53
- grid. spaces
54
- . get ( grid. width * ( new_y as usize ) + point. x )
55
- . unwrap ( )
56
- . to_owned ( )
57
- }
49
+ fn get_by_row ( & self , point : & Point , y : isize ) -> & Space {
50
+ let new_y = point. y as isize + y;
51
+ self . spaces
52
+ . get ( self . width * ( new_y as usize ) + point. x )
53
+ . unwrap ( )
54
+ }
58
55
59
- fn get_as_usize_offset_col ( grid : & Grid , point : & Point , x : isize ) -> Space {
60
- let new_x = point. x as isize + x;
61
- grid. spaces
62
- . get ( grid. width * point. y + new_x as usize )
63
- . unwrap ( )
64
- . to_owned ( )
65
- }
56
+ fn get_by_col ( & self , point : & Point , x : isize ) -> & Space {
57
+ let new_x = point. x as isize + x;
58
+ self . spaces
59
+ . get ( self . width * point. y + new_x as usize )
60
+ . unwrap ( )
61
+ }
66
62
67
- impl Grid {
68
-
69
- pub fn valid_neighbors ( & self , point : & Point ) -> Vec < ( Point , usize ) > {
70
- let mut valid = Vec :: new ( ) ;
71
- let cur = get_as_usize ( self , point) ;
72
- // up
73
- if point. y >= 1 {
74
- let up = get_as_usize_offset_row ( self , point, -1 ) ;
75
- if cur. can_move ( & up) {
76
- valid. push ( Point {
77
- x : point. x ,
78
- y : point. y - 1 ,
79
- } ) ;
63
+ pub fn valid_neighbors ( & self , point : & Point ) -> Vec < ( Point , usize ) > {
64
+ let mut valid = Vec :: new ( ) ;
65
+ let cur = self . spaces . get ( self . idx ( point) ) . unwrap ( ) ;
66
+ // up
67
+ if point. y >= 1 {
68
+ let up = self . get_by_row ( point, -1 ) ;
69
+ if cur. can_move ( & up) {
70
+ valid. push ( Point {
71
+ x : point. x ,
72
+ y : point. y - 1 ,
73
+ } ) ;
74
+ }
80
75
}
81
- }
82
76
83
- // right
84
- if point. x + 1 < self . width {
85
- let right = get_as_usize_offset_col ( self , point, 1 ) ;
86
- if cur. can_move ( & right) {
87
- valid. push ( Point {
88
- x : point. x + 1 ,
89
- y : point. y ,
90
- } ) ;
77
+ // right
78
+ if point. x + 1 < self . width {
79
+ let right = self . get_by_col ( point, 1 ) ;
80
+ if cur. can_move ( & right) {
81
+ valid. push ( Point {
82
+ x : point. x + 1 ,
83
+ y : point. y ,
84
+ } ) ;
85
+ }
91
86
}
92
- }
93
87
94
- // down
95
- if point. y + 1 < self . height {
96
- let down = get_as_usize_offset_row ( self , point, 1 ) ;
97
- if cur. can_move ( & down) {
98
- valid. push ( Point {
99
- x : point. x ,
100
- y : point. y + 1 ,
101
- } ) ;
88
+ // down
89
+ if point. y + 1 < self . height {
90
+ let down = self . get_by_row ( point, 1 ) ;
91
+ if cur. can_move ( & down) {
92
+ valid. push ( Point {
93
+ x : point. x ,
94
+ y : point. y + 1 ,
95
+ } ) ;
96
+ }
102
97
}
103
- }
104
98
105
- // left
106
- if point. x >= 1 {
107
- let left = get_as_usize_offset_col ( self , point, -1 ) ;
108
- if cur. can_move ( & left) {
109
- valid. push ( Point {
110
- x : point. x - 1 ,
111
- y : point. y ,
112
- } ) ;
99
+ // left
100
+ if point. x >= 1 {
101
+ let left = self . get_by_col ( point, -1 ) ;
102
+ if cur. can_move ( & left) {
103
+ valid. push ( Point {
104
+ x : point. x - 1 ,
105
+ y : point. y ,
106
+ } ) ;
107
+ }
113
108
}
109
+ valid. into_iter ( ) . map ( |p| ( p, 1 ) ) . collect ( )
114
110
}
115
- valid. into_iter ( ) . map ( |p| ( p, 1 ) ) . collect ( )
116
- }
117
111
}
0 commit comments