From 3121cd06646403389b4a21d5af1e4e52f7879184 Mon Sep 17 00:00:00 2001 From: plutov Date: Sun, 19 Jan 2025 21:05:20 +0100 Subject: [PATCH] 33 mines: fixes Signed-off-by: plutov --- .DS_Store | Bin 0 -> 10244 bytes 33-raylib-go-minesweeper/.DS_Store | Bin 0 -> 6148 bytes 33-raylib-go-minesweeper/main.go | 87 +++++++++++++++++++++-------- 3 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 .DS_Store create mode 100644 33-raylib-go-minesweeper/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..22b5902aa5550bed010d767f5d221276622be8ee GIT binary patch literal 10244 zcmeHNTW{P%82y~2%|^L2mlmo@RTuHXL$~BcFG4~g7okX8q;4q>T*7*Hn^^0$x4v}K zMnQRo|G;11m0!UlKZAFk;G7wIGc%haAtZ!|y|QQQopWa9d}lt}n}~?Fop;tm>LOCZ z&T?@MhYL1-&wXs0D6Xuc^ne#hWFYPRFmC(dSnKLSr9h=Xr9h=Xr9h>?e?bAZ9gL(x(Ifct=Lmlq{3R5r2t)e{BrGkP}zdd*b z)GJ;wJPpV9bv8%2Rg`DC)Nt`MT)d)ZuXsc8RPVsmRdeySbXgrM1u6x`72v!3w5;OO zkVEtRJ@)Yp7t@E>@BN`R)I=80b`7oDXx)$u$3R*V0Ub(Lzx$7{KV5)b2g;Wms8Bkn zKSS99I<>LhZ3nxAemBtmP-4XppwAAF8Fr3iAfdzXgTrtK44bNL7uXa$iRwL;A+83} zSL5iSo=6w-i_uqv>lyY~B8UBVhkY6B?*Ys4wdDnBm1`d5@&WwQ4=wX_H>+f zO;1I$As49WEf{JNb?&ti<@6;`(n4JaivH4l^)bp0Mu}AceHy6K7d@OM&Me_{i{}n_ zN+;2SWMF(kFyzIW$>FJX=}g5>m#MxTc4%+0bCmSh5E^m~>alT*J@CbF9b++&fy0AX zSUk(%q2oB)^HHA1vjYnsuqIBtQ1-W#ec!ga#bZPA0MjB>mgN`1{zMyPArI+5Re`N~fmO!{(;$6WcqqCIFCB1 z!Z!66wVB$?!Q#!6lZWf8ck2%}Hc#)?Pad(pxpDvWbf$LW?RVdQe9#N~QT#pbeAv9= zbw}p)`RE7@PD|AN`v@`R(-ggH{5_|9rt{!eOZ{uI zBcI8q@e?C~h4o19C|ie+Yvlp~o%4l%aeW>D^S8R%^G##|c$m#Peqsg*Mv zdy=G|O@?Xt7z*yl9#m|~p3@)oH(Sp@_dRH{E1#fzo#rOu3dX~X9k|h-!>~vBvL?$Q%~e3RsvM zcvp6FeEA%M=PpMv=xecz7Ex8H6u86+OzOGUXHo9G{_p>nSfOg_l>+~N1-z-<=I%D; zSw3vKQ|52&b?o0_=f-wtx>OJx)X(uK)z9%b;m`5!U}szY#;7*bw^%`Wrc2bY{p)`W UsNVmp_kaA4TKUX+8Q=f^1kw%$t^fc4 literal 0 HcmV?d00001 diff --git a/33-raylib-go-minesweeper/.DS_Store b/33-raylib-go-minesweeper/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c5d9eef33e17ce5fb4c1317bcee7470ea6e2c241 GIT binary patch literal 6148 zcmeHKOG*Pl5Um=cMzSzVH$tBv;0@Xk7p}d4#)KdwPA~+))$GNyctm~mG0Z1!MYIa4 zUU$!{?)2+0HBCf3-p($FhD6kd3l=8`drY2-cdYP{Lm=xMCC#pv<^6P7PCM)v_>Bt4 z*&We>N*d7$v3`E?r?`ZVKGgK*e{naorB&^$h5O#(9%IHkdZ0(ljTM?~K5wxHU<-Fn z8+t~Z(;EKN%6e^Oo$k-ViZ7UL-OMgzw{NxGpc@tngaV{kr7bc`qGWyi|U(uw`@ z!M-y44TW`e%%7A_oDCy}0--=tfdlIfrT$-ifB$bLMK2Tx1^$!*+?(7?#@LeA*3Ran s)+YEZTugYC;VuOYEydtUDL#X%VLV9#m>nxajKJbUKxK#!3jC-7pD0^VXaE2J literal 0 HcmV?d00001 diff --git a/33-raylib-go-minesweeper/main.go b/33-raylib-go-minesweeper/main.go index b52696d..04aeab5 100644 --- a/33-raylib-go-minesweeper/main.go +++ b/33-raylib-go-minesweeper/main.go @@ -15,7 +15,7 @@ const ( minRowsCols = 3 maxRowsCols = 20 defaultWinWidth = 300 - defaultWinHeight = 400 + defaultWinHeight = 450 ) type state struct { @@ -58,9 +58,6 @@ func (s *state) reset() { s.gameOver = false s.gameWon = false s.menu = true - s.rows = 9 - s.cols = 9 - s.mines = 10 } func (s *state) start() { @@ -97,14 +94,14 @@ func (s *state) start() { func (s *state) doForNeighbours(x, y int, do func(x, y int)) { // with diagonals - dx := []int{-1, -1, -1, 0, 0, 1, 1, 1} - dy := []int{-1, 0, 1, -1, 1, -1, 0, 1} + dx := []int{-1, 0, 1, -1, 1, -1, 0, 1} + dy := []int{-1, -1, -1, 0, 0, 1, 1, 1} for i := 0; i < len(dx); i++ { nx := x + dx[i] ny := y + dy[i] - if nx >= 0 && nx < int(s.cols) && ny >= 0 && ny < int(s.rows) { + if nx >= 0 && nx < int(s.rows) && ny >= 0 && ny < int(s.cols) { do(nx, ny) } } @@ -130,12 +127,13 @@ func (s *state) revealTile(x, y int) { return } + s.field[x][y].open = true + if s.field[x][y].hasMine { s.gameOver = true return } - s.field[x][y].open = true s.gameWon = s.checkIfGameWon() // No neighbors, reveal all adjacent tiles recursively. @@ -150,20 +148,41 @@ func (s *state) drawMenu() { w, h := defaultWinWidth, defaultWinHeight colw := float32(w / 2) var rowh float32 = 50 + var fontSize int32 = 20 + buttonWidth := float32(w - 2*padding) + rl.SetWindowSize(w, h) + rl.SetWindowPosition((rl.GetMonitorWidth(0)-int(w))/2, (rl.GetMonitorHeight(0)-int(h))/2) + + if clicked := gui.Button(rl.NewRectangle(padding, rowh, buttonWidth, size), "BEGINNER"); clicked { + s.rows = 9 + s.cols = 9 + s.mines = 10 + } + if clicked := gui.Button(rl.NewRectangle(padding, 2*rowh, buttonWidth, size), "INTERMEDIATE"); clicked { + s.rows = 16 + s.cols = 16 + s.mines = 40 + } + if clicked := gui.Button(rl.NewRectangle(padding, 3*rowh, buttonWidth, size), "EXPERT"); clicked { + s.rows = 30 + s.cols = 30 + s.mines = 99 + } - rl.DrawText("ROWS:", padding, int32(rowh), size, rl.White) - s.rows = gui.Spinner(rl.NewRectangle(colw, rowh, float32(colw-padding), size), "", &s.rows, minRowsCols, maxRowsCols, true) + rl.DrawText("ROWS:", padding, 4*int32(rowh)+5, fontSize, rl.White) + s.rows = gui.Spinner(rl.NewRectangle(colw, 4*rowh, float32(colw-padding), size), "", &s.rows, minRowsCols, maxRowsCols, true) - rl.DrawText("COLS:", padding, 2*int32(rowh), size, rl.White) - s.cols = gui.Spinner(rl.NewRectangle(colw, 2*rowh, float32(colw-padding), size), "", &s.cols, minRowsCols, maxRowsCols, true) + rl.DrawText("COLS:", padding, 5*int32(rowh)+5, fontSize, rl.White) + s.cols = gui.Spinner(rl.NewRectangle(colw, 5*rowh, float32(colw-padding), size), "", &s.cols, minRowsCols, maxRowsCols, true) - rl.DrawText("MINES:", padding, 3*int32(rowh), size, rl.White) - s.mines = gui.Spinner(rl.NewRectangle(colw, 3*rowh, float32(colw-padding), size), "", &s.mines, 1, int(s.rows)*int(s.cols), true) + rl.DrawText("MINES:", padding, 6*int32(rowh)+5, fontSize, rl.White) + s.mines = gui.Spinner(rl.NewRectangle(colw, 6*rowh, float32(colw-padding), size), "", &s.mines, 1, int(s.rows)*int(s.cols), true) - if clicked := gui.Button(rl.NewRectangle(padding, 4*rowh, float32(w-2*padding), size), "START"); clicked { + if clicked := gui.Button(rl.NewRectangle(padding, 7*rowh, buttonWidth, size), "START"); clicked { s.start() } + } func getTextColor(neighbors int) rl.Color { @@ -184,12 +203,33 @@ func (s *state) drawField() { h := float32(s.getHeight()) rl.SetWindowSize(int(w), int(h)) + rl.SetWindowPosition((rl.GetMonitorWidth(0)-int(w))/2, (rl.GetMonitorHeight(0)-int(h))/2) + gui.StatusBar(rl.NewRectangle(0, h-size, w, size), s.getStatus()) + if restart := gui.Button(rl.NewRectangle(w-65, h-size+5, 60, size-10), "RESTART"); restart { + s.reset() + return + } for x := range s.field { for y := range s.field[x] { rect := rl.NewRectangle(float32(padding+x*size), float32(padding+y*size), size, size) + if s.gameOver { + // reveal current state + if s.field[x][y].hasMine { + rl.DrawText("*", 5+padding+int32(x)*size, 5+padding+int32(y)*size, 20, rl.Red) + } else { + text := "" + if s.field[x][y].neighbours > 0 { + text = fmt.Sprintf("%d", s.field[x][y].neighbours) + } + + rl.DrawText(text, 5+padding+int32(x)*size, 5+padding+int32(y)*size, 20, getTextColor(s.field[x][y].neighbours)) + } + continue + } + // Mark on right mouse button if rl.IsMouseButtonPressed(rl.MouseButtonRight) { if rl.CheckCollisionPointRec(rl.GetMousePosition(), rect) { @@ -217,26 +257,27 @@ func (s *state) drawField() { } } -func (s *state) drawMessageWithRestart() { +func (s *state) congrats() { w, h := defaultWinWidth, defaultWinHeight var lineHeight int32 = 50 rl.SetWindowSize(w, h) - if s.gameOver { - rl.DrawText("GAME OVER :(", padding, lineHeight, size, rl.White) - } if s.gameWon { rl.DrawText("WELL DONE !", padding, lineHeight, size, rl.White) } - clicked := gui.Button(rl.NewRectangle(padding, float32(2*lineHeight), float32(w-2*padding), size), "ONE MORE") + clicked := gui.Button(rl.NewRectangle(padding, float32(2*lineHeight), float32(w-2*padding), size), "PLAY AGAIN") if clicked { s.reset() } } func main() { - game := &state{} + game := &state{ + rows: 9, + cols: 9, + mines: 10, + } game.reset() rl.InitWindow(defaultWinWidth, defaultWinHeight, "minesweeper") @@ -247,8 +288,8 @@ func main() { rl.BeginDrawing() rl.ClearBackground(rl.DarkGray) - if game.gameOver || game.gameWon { - game.drawMessageWithRestart() + if game.gameWon { + game.congrats() } else if game.menu { game.drawMenu() } else {