1616 styleGrid [][]int
1717 lastMouseX , lastMouseY int
1818 mouseMoved bool
19+ hasFloor bool = true
1920)
2021
2122func HSVtoRGB (hue int ) (int32 , int32 , int32 ) {
@@ -50,40 +51,77 @@ func HSVtoRGB(hue int) (int32, int32, int32) {
5051 return int32 (r ), int32 (g ), int32 (b )
5152}
5253
54+ func BottomRowClear () bool {
55+ for x := range grid [len (grid )- 1 ]{
56+ if grid [len (grid )- 1 ][x ] != 0 {
57+ return false
58+ }
59+ }
60+ return true
61+ }
62+
63+ func resetGrid (s tcell.Screen ){
64+ grid = make ([][]int , screenHeight )
65+ styleGrid = make ([][]int , screenHeight )
66+
67+ for i := 0 ; i < screenHeight ; i ++ {
68+ grid [i ] = make ([]int , screenWidth )
69+ styleGrid [i ] = make ([]int , screenWidth )
70+ for x := 0 ; x < screenWidth ; x ++ {
71+ s .SetContent (x , i , ' ' , nil , tcell .StyleDefault )
72+ }
73+ }
74+ }
75+
5376func render (s tcell.Screen ) {
5477
78+ if ! hasFloor {
79+ isclear := true
80+ for x := range grid [len (grid )- 1 ]{
81+ if grid [len (grid )- 1 ][x ] != 0 {
82+ isclear = false ;
83+ }
84+ grid [len (grid )- 1 ][x ]= 0
85+ s .SetContent (x , screenHeight - 1 , ' ' , nil , tcell .StyleDefault )
86+ }
87+ if isclear {
88+ hasFloor = true
89+ resetGrid (s )
90+ return
91+ }
92+ }
5593 for y := screenHeight - 2 ; y >= 0 ; y -- {
5694 for x := 0 ; x < screenWidth ; x ++ {
5795 blockstyle := tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (grid [y ][x ])))
58- if grid [y ][x ] > 0 {
59- if grid [y + 1 ][x ] == 0 {
60- grid [y + 1 ][x ] = grid [y ][x ]
61- grid [y ][x ] = 0
62- s .SetContent (x , y + 1 , ' ' , nil , blockstyle )
63- styleGrid [y + 1 ][x ] = 1
64- } else if x > 0 && grid [y + 1 ][x - 1 ] == 0 {
65- grid [y + 1 ][x - 1 ] = grid [y ][x ]
66- grid [y ][x ] = 0
67- s .SetContent (x - 1 , y + 1 , ' ' , nil , blockstyle )
68- styleGrid [y + 1 ][x - 1 ] = 1
69- } else if x < screenWidth - 1 && grid [y + 1 ][x + 1 ] == 0 {
70- grid [y + 1 ][x + 1 ] = grid [y ][x ]
71- grid [y ][x ] = 0
72- s .SetContent (x + 1 , y + 1 , ' ' , nil , blockstyle )
73- styleGrid [y + 1 ][x + 1 ] = 1
74- }
75- } else {
76- style := tcell .StyleDefault
77- if y != 0 && grid [y - 1 ][x ] != 0 {
78- style = tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (grid [y - 1 ][x ])))
79- grid [y ][x ] = grid [y - 1 ][x ]
80- grid [y - 1 ][x ] = 0
81- styleGrid [y ][x ] = 1
82- } else {
83- styleGrid [y ][x ] = 0
84- }
85- s .SetContent (x , y , ' ' , nil , style )
86- }
96+ if grid [y ][x ] > 0 {
97+ if grid [y + 1 ][x ] == 0 {
98+ grid [y + 1 ][x ] = grid [y ][x ]
99+ grid [y ][x ] = 0
100+ s .SetContent (x , y + 1 , ' ' , nil , blockstyle )
101+ styleGrid [y + 1 ][x ] = 1
102+ } else if x > 0 && grid [y + 1 ][x - 1 ] == 0 {
103+ grid [y + 1 ][x - 1 ] = grid [y ][x ]
104+ grid [y ][x ] = 0
105+ s .SetContent (x - 1 , y + 1 , ' ' , nil , blockstyle )
106+ styleGrid [y + 1 ][x - 1 ] = 1
107+ } else if x < screenWidth - 1 && grid [y + 1 ][x + 1 ] == 0 {
108+ grid [y + 1 ][x + 1 ] = grid [y ][x ]
109+ grid [y ][x ] = 0
110+ s .SetContent (x + 1 , y + 1 , ' ' , nil , blockstyle )
111+ styleGrid [y + 1 ][x + 1 ] = 1
112+ }
113+ } else {
114+ style := tcell .StyleDefault
115+ if y != 0 && grid [y - 1 ][x ] != 0 {
116+ style = tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (grid [y - 1 ][x ])))
117+ grid [y ][x ] = grid [y - 1 ][x ]
118+ grid [y - 1 ][x ] = 0
119+ styleGrid [y ][x ] = 1
120+ } else {
121+ styleGrid [y ][x ] = 0
122+ }
123+ s .SetContent (x , y , ' ' , nil , style )
124+ }
87125 if grid [y ][x ] != 0 && styleGrid [y ][x ] == 0 {
88126 s .SetContent (x , y , ' ' , nil , tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (grid [y ][x ]))))
89127 }
@@ -105,13 +143,7 @@ func main() {
105143 s .EnableFocus ()
106144 s .EnableMouse ()
107145 screenWidth , screenHeight = s .Size ()
108- grid = make ([][]int , screenHeight )
109- styleGrid = make ([][]int , screenHeight )
110-
111- for i := 0 ; i < screenHeight ; i ++ {
112- grid [i ] = make ([]int , screenWidth )
113- styleGrid [i ] = make ([]int , screenWidth )
114- }
146+ resetGrid (s )
115147
116148 s .Clear ()
117149 colorNum := 0
@@ -136,20 +168,23 @@ func main() {
136168 switch ev := ev .(type ) {
137169 case * tcell.EventKey :
138170 switch ev .Key () {
139- case tcell .KeyCtrlQ :
171+ case tcell .KeyCtrlQ , tcell . KeyCtrlC :
140172 s .Fini ()
141173 os .Exit (0 )
174+ case tcell .KeyCtrlR :
175+ hasFloor = false
142176 }
143177 case * tcell.EventResize :
144178 screenWidth , screenHeight = s .Size ()
179+ resetGrid (s )
145180 s .Sync ()
146181 case * tcell.EventMouse :
147182 lastMouseX , lastMouseY = ev .Position ()
148183 mouseMoved = true
149184 }
150185 case <- ticker .C :
151186 // Add sand at the last known mouse position if the mouse has moved
152- if mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth && grid [lastMouseY ][lastMouseX ] == 0 {
187+ if hasFloor && mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth && grid [lastMouseY ][lastMouseX ] == 0 {
153188 grid [lastMouseY ][lastMouseX ] = colorNum
154189 rand1 := rand .Intn (4 )
155190 rand2 := rand .Intn (4 )
@@ -166,14 +201,15 @@ func main() {
166201 if (rand1 == 3 || rand2 == 3 ) && lastMouseX + 1 < screenWidth && grid [lastMouseY ][lastMouseX + 1 ] == 0 {
167202 grid [lastMouseY ][lastMouseX + 1 ] = colorNum
168203 }
204+ colorNum ++
205+ if colorNum == 360 {
206+ colorNum = 1
207+ }
169208 }
170209
171210 render (s )
172211 s .Show ()
173- colorNum ++
174- if colorNum == 360 {
175- colorNum = 1
176- }
212+
177213 }
178214 }
179215}
0 commit comments