當盤面已出現了 2048 的方格,會判斷顯示遊戲已達成勝利條件。
這是 2048 遊戲的重要勝利條件,能讓玩家在達成勝利條件時,看到明確的「You Win」提示。
並且可以讓玩家選擇是否繼續往下操作盤面。
// is2048tileShow - 檢查是否已經達成 2048 tile 的完成條件
func (g *Game) is2048tileShow() bool {
for row := 0; row < sideSize; row++ {
for col := 0; col < sideSize; col++ {
if g.board[row][col] == 2048 {
return true
}
}
}
return false
}
// IsPlayerWin - 檢查玩家是否達成勝利條件
func (g *Game) IsPlayerWin() bool {
return g.is2048tileShow()
}
// drawYouWin 畫出 You Win
func (g *GameLayout) drawYouWin(screen *ebiten.Image) {
g.drawConverOnYouWin(screen)
// 設定顯示 Game Over 文字
textXPos := WinHeight / 2
textYPos := WinWidth / 2
textOpts := &text.DrawOptions{}
textOpts.ColorScale.ScaleWithColor(color.RGBA{220, 220, 0, 255})
textOpts.PrimaryAlign = text.AlignCenter
textOpts.SecondaryAlign = text.AlignCenter
textOpts.GeoM.Translate(float64(textXPos), float64(textYPos))
text.Draw(screen, "You Win", &text.GoTextFace{
Source: mplusFaceSource,
Size: 48.0,
}, textOpts)
g.drawContinueButton(screen)
}
// Update - 用來處理畫面偵測,與使用者互動,並且觸發狀態變更
func (g *GameLayout) Update() error {
// 判斷是否遊戲結束
if g.isGameOver {
// 處理 restart 邏輯
g.handleRestartGame()
return nil
}
// 判斷是否 Player Win
if g.isPlayerWin && !g.isContinue {
g.handleContinueGame()
return nil
}
// 根據輸入產生對應的更新
g.handleInput()
// 根據目前的盤面決定是否要顯示 You Win
if !g.isContinue && g.gameInstance.IsPlayerWin() {
g.isPlayerWin = true
return nil
}
// 根據目前的盤面跟更新是否能夠繼續執行
if g.gameInstance.IsGameOver() {
g.isGameOver = true
}
return nil
}
https://github.com/leetcode-golang-classroom/2048-game/actions/runs/17187588331
採地雷遊戲規則與玩法理解