go語言實現掃雷

janbar發表於2024-03-02

原始碼如下

package main

import (
	"archive/zip"
	"bytes"
	"encoding/base64"
	"fmt"
	"image"
	"image/color"
	"image/png"
	"log"
	"math/rand"
	"strings"
	"time"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	"github.com/hajimehoshi/ebiten/v2/inpututil"
)

func main() {
	//goland:noinspection GoDeprecation
	rand.Seed(time.Now().Unix())

	m := &mine{h: 16, w: 30, mineCnt: 99}
	err := m.loadResources()
	if err != nil {
		log.Fatal(err)
	}
	m.initData() // 開局初始資料

	ebiten.SetWindowTitle("Mine Sweeping")
	if err = ebiten.RunGame(m); err != nil {
		log.Fatal(err)
	}
}

const (
	gridHW = 16 // 格子寬高
)

type (
	mine struct {
		// 雷區寬高
		h, w int
		// 0: 正常,1: 贏,2: 輸
		playing int
		// 雷區格子資料
		data [][]*grid
		// 總雷數
		mineCnt int
		// 開始時間
		timeStart time.Time
		// 計時器
		timeCnt int
		// 計時器最右側數字座標
		timeX float64
		// 介面寬高
		gridW, gridH int
		// 顯示哪個笑臉
		faceNum int
		// 笑臉座標
		faceX float64
		// 判斷在笑臉位置
		isFace func(h, w int) bool
		// 介面圖片,數字圖片,笑臉圖片
		img, num, face []*ebiten.Image
		// 背景圖片
		background *ebiten.Image
		// 顯示輸入資料
		text string
	}

	grid struct {
		data  int // 格子資料
		state int // 狀態
	}
)

var aroundPos = [][]int{
	{-1, 1, 0, 0, -1, -1, 1, 1},
	{0, 0, -1, 1, -1, 1, -1, 1},
}

func (m *mine) around(h, w int, f func(h, w int)) {
	var i, nh, nw int
	for ; i < 8; i++ {
		nh, nw = h+aroundPos[0][i], w+aroundPos[1][i]
		if nh >= 0 && nh < m.h && nw >= 0 && nw < m.w {
			f(nh, nw) // [h,w]周圍合法8個位置
		}
	}
}

func (m *mine) initData() {
	var i, j int

	if len(m.data) < m.h {
		m.data = make([][]*grid, m.h)
	}
	for i = 0; i < m.h; i++ {
		if len(m.data[i]) < m.w {
			m.data[i] = make([]*grid, m.w)
		}
	}

	for i = 0; i < m.h; i++ {
		for j = 0; j < m.w; j++ {
			if d := m.data[i][j]; d == nil {
				m.data[i][j] = new(grid)
			} else {
				d.data, d.state = 0, 0
			}
		}
	}

	cnt := 0
	for cnt < m.mineCnt {
		i, j = rand.Intn(m.h), rand.Intn(m.w)
		if d := m.data[i][j]; d.data != 10 {
			cnt++
			d.data = 10
		}
	}

	for i = 0; i < m.h; i++ {
		for j = 0; j < m.w; j++ {
			if d := m.data[i][j]; d.data != 10 {
				m.around(i, j, func(h, w int) {
					if m.data[h][w].data == 10 {
						d.data++
					}
				})
			}
		}
	}

	m.playing = 0
	m.timeStart = time.Time{}
	m.timeCnt = 0
	m.gridW, m.gridH = m.w*gridHW+6, (m.h+3)*gridHW+6

	faceX := m.gridW/2 - 18
	m.faceX = float64(faceX)
	m.isFace = func(h, w int) bool {
		return h >= 4 && h <= 28 && w >= faceX && w < faceX+24
	}
	m.faceNum = 0

	m.timeX = float64(m.gridW - 18)
	ebiten.SetWindowSize(m.gridW, m.gridH)

	m.background = ebiten.NewImage(m.gridW, m.gridH-gridHW)
	m.background.Fill(backgroundColor) // 建立背景圖片
	m.text = fmt.Sprintf("H:%d,W:%d,M:%d >", m.h, m.w, m.mineCnt)
}

func (m *mine) cursorPos() (h, w, state int) {
	w, h = ebiten.CursorPosition()
	if m.isFace(h, w) {
		state = 1
	} else {
		w, h = (w-3)/gridHW, h/gridHW-2
		if w >= 0 && w < m.w && h >= 0 && h < m.h {
			state = 2
		}
	}
	return
}

func (m *mine) reactionChain(h, w int) {
	d := m.data[h][w]
	if d.state != 0 {
		return // 已開啟或插旗 或 遊戲完成
	}
	d.state = -1

	switch d.data {
	case 10:
		if m.playing == 0 {
			m.playing = 2
			d.data = 12 // 遊戲結束,標記第1個踩到的雷
		}
	case 0: // 遞迴點開所有空白區域
		m.around(h, w, func(h, w int) { m.reactionChain(h, w) })
	}
}

func (m *mine) Update() error {
	var state int
	if m.playing != 0 {
		if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
			_, _, state = m.cursorPos()
			if state == 1 {
				m.faceNum = 4
			} else {
				switch m.playing {
				case 1:
					m.faceNum = 3
				case 2:
					m.faceNum = 2
				}
			}
		} else if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
			_, _, state = m.cursorPos()
			if state == 1 {
				m.initData() // 左鍵小臉鬆開重新開始遊戲
			}
		}
		return nil
	}

	if m.timeCnt < 999 && !m.timeStart.IsZero() {
		m.timeCnt = int(time.Since(m.timeStart) / time.Second)
	}

	var d *grid
	var i, j int
	if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
		for i = 0; i < m.h; i++ {
			for j = 0; j < m.w; j++ {
				if d = m.data[i][j]; d.state == 1 {
					d.state = 0
				}
			}
		}

		i, j, state = m.cursorPos()
		switch state {
		case 1:
			m.faceNum = 4
		case 2:
			switch d = m.data[i][j]; d.state {
			case 0:
				d.state = 1
			case -1:
				m.around(i, j, func(ah, aw int) {
					if ad := m.data[ah][aw]; ad.state == 0 {
						ad.state = 1
					}
				})
			}
			m.faceNum = 1
		default:
			m.faceNum = 1
		}
	} else if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
		for i = 0; i < m.h; i++ {
			for j = 0; j < m.w; j++ {
				if d = m.data[i][j]; d.state == 1 {
					d.state = 0
				}
			}
		}
		m.faceNum = 0

		i, j, state = m.cursorPos()
		switch state {
		case 1:
			m.initData() // 笑臉位置鬆開左鍵,重新開局
			return nil
		case 2:
			if m.timeStart.IsZero() {
				m.timeStart = time.Now()
			}

			switch d = m.data[i][j]; d.state {
			case 0: // 判斷單擊
				m.reactionChain(i, j)
			case -1: // 判斷雙擊
				if d.data >= 1 && d.data <= 8 {
					state = 0
					m.around(i, j, func(ah, aw int) {
						if ad := m.data[ah][aw]; ad.state == 2 {
							state++
						}
					})
					if d.data == state {
						m.around(i, j, func(ah, aw int) {
							m.reactionChain(ah, aw)
						})
					}
				}
			}

			if m.playing == 2 {
				m.faceNum = 2 // 遊戲結束,輸了

				for i = 0; i < m.h; i++ {
					for j = 0; j < m.w; j++ {
						switch d = m.data[i][j]; d.state {
						case 0: // 將所有雷開啟
							if d.data == 10 {
								d.state = -1
							}
						case 2: // 插旗位置不是雷,設定標雷錯誤
							if d.data != 10 {
								d.state = -1
								d.data = 11
							}
						}
					}
				}
				return nil
			}

			state = 0
			for i = 0; i < m.h; i++ {
				for j = 0; j < m.w; j++ {
					if d = m.data[i][j]; d.state == -1 {
						state++
					}
				}
			}
			// 點開位置 + 總雷數 = 全部格子數, 此時贏
			if state+m.mineCnt == m.h*m.w {
				m.faceNum = 3 // 遊戲結束,贏了
				m.playing = 1

				for i = 0; i < m.h; i++ {
					for j = 0; j < m.w; j++ {
						if d = m.data[i][j]; d.state == 0 {
							d.state = 2 // 剩餘全插旗
						}
					}
				}
				return nil
			}
		}
	} else if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
		i, j, state = m.cursorPos()
		if state == 2 {
			switch d = m.data[i][j]; d.state {
			case 0:
				d.state = 2 // 插旗
			case 2:
				d.state = 0 // 取消
			}
		}
	}

	for k, v := range eKey {
		if inpututil.IsKeyJustReleased(k) {
			switch v {
			case "d":
				if i = len(m.text) - 1; m.text[i] != '>' {
					m.text = m.text[:i]
				}
			case "e":
				i = strings.IndexByte(m.text, '>') + 1

				var ok bool
				n, _ := fmt.Sscanf(m.text[i:], "%d %d %d", &i, &j, &state)
				switch n {
				case 3: // 讀取 h/w/mine 這3個資料
					if i >= 9 && i <= 45 && j >= 9 && j <= 45 &&
						state >= 10 && state <= (i-1)*(j-1) {
						m.h, m.w, m.mineCnt = i, j, state
						ok = true
					}
				case 1: // 輸入單個數字切換難度模式
					switch i {
					case 1:
						m.h, m.w, m.mineCnt = 9, 9, 10 // 初級
						ok = true
					case 2:
						m.h, m.w, m.mineCnt = 16, 16, 40 // 中級
						ok = true
					case 3:
						m.h, m.w, m.mineCnt = 16, 30, 99 // 高階
						ok = true
					case 4:
						m.h, m.w, m.mineCnt = 24, 30, 99 // 最大
						ok = true
					}
				}

				if ok {
					m.initData()
					return nil
				}
			default:
				m.text += v
			}
		}
	}
	return nil
}

var (
	eKey = map[ebiten.Key]string{
		ebiten.KeyDigit0: "0",
		ebiten.KeyDigit1: "1",
		ebiten.KeyDigit2: "2",
		ebiten.KeyDigit3: "3",
		ebiten.KeyDigit4: "4",
		ebiten.KeyDigit5: "5",
		ebiten.KeyDigit6: "6",
		ebiten.KeyDigit7: "7",
		ebiten.KeyDigit8: "8",
		ebiten.KeyDigit9: "9",
		ebiten.KeySpace:  " ",

		ebiten.KeyBackspace:   "d", // 刪除
		ebiten.KeyEnter:       "e", // 回車
		ebiten.KeyNumpadEnter: "e", // 回車
	}

	backgroundColor = color.RGBA{R: 0xc0, G: 0xc0, B: 0xc0, A: 0xff}
)

func (m *mine) Draw(screen *ebiten.Image) {
	screen.DrawImage(m.background, nil)

	ct := m.mineCnt
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(3, 2*gridHW)
	for i := 0; i < m.h; i++ {
		for j := 0; j < m.w; j++ {
			switch d := m.data[i][j]; d.state {
			case 0: // 預設狀態
				screen.DrawImage(m.img[15], op)
			case 1: // 按住左鍵不鬆開
				screen.DrawImage(m.img[0], op)
			case 2: // 標記旗子
				screen.DrawImage(m.img[14], op)
				ct--
			default: // 按照資料顯示
				if d.data == 11 {
					ct-- // 錯誤插旗也算標雷
				}
				screen.DrawImage(m.img[d.data], op)
			}
			op.GeoM.Translate(gridHW, 0)
		}
		op.GeoM.Translate(0, gridHW)
		op.GeoM.SetElement(0, 2, 3)
	}

	op.GeoM.Reset() // 顯示雷數
	op.GeoM.Translate(5, 5)
	var num []int
	if ct >= 0 {
		num = []int{(ct / 100) % 10, (ct / 10) % 10, ct % 10}
	} else {
		ct = -ct // 負數只顯示2位
		num = []int{11, (ct / 10) % 10, ct % 10}
	}
	for _, v := range num {
		screen.DrawImage(m.num[v], op)
		op.GeoM.Translate(13, 0)
	}

	op.GeoM.Reset() // 顯示時間
	op.GeoM.Translate(m.timeX, 5)
	for _, v := range []int{m.timeCnt % 10, (m.timeCnt / 10) % 10, (m.timeCnt / 100) % 10} {
		screen.DrawImage(m.num[v], op)
		op.GeoM.Translate(-13, 0)
	}

	op.GeoM.Reset() // 顯示笑臉
	op.GeoM.Translate(m.faceX, 4)
	screen.DrawImage(m.face[m.faceNum], op)

	// 列印輸入的[高 寬 雷]資料,以及輸入資料顯示
	ebitenutil.DebugPrintAt(screen, m.text, 10, m.gridH-gridHW)
}

func (m *mine) Layout(_, _ int) (int, int) {
	return m.gridW, m.gridH
}

func (m *mine) loadResources() error {
	//goland:noinspection SpellCheckingInspection
	const sd = "UEsDBBQAAgAIAEdVYVjiKY4C/wIAAPoCAAAIAAAAZmFjZS5wbmcB+gIF/YlQTkcNChoKAAAADUlIRFIAAAAYAAAAeAgGAAAA6IMyogAAAAFzUkdCAK7OHOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACj0lEQVRoQ+2YAYoqQQxEPfoczZu5v4SSTLbSqay2f8FteDimk3raOiBejuO47eQuuF6v2/gTtIwEl8vlG6ovYgkYdrt9pxO1gio4U0mWAjecKEkpmIaTLLEFeO7WTjlKkIc4qMKq3keWK5jwgYJOqPofWUpw3zAlq/B7jivgcEb1nHIqAVABK3L4PWMlAK5Ehd/nOwHAcCXinpoDloAwLKL6IiPBT9gv+HeQ29ZWAcJxOg9BfFuvAOG/SzD9itoChk1vMktQBWeUpBW44SRLloJpOIkSW4DrjpEgD2RW9ShpBfExsqrZAjaD4zgHEdawn2utIAZN+f8CJamkqo8ZIwGHM6rHEgAVsCKGg1YAXEkOB5YAYLgScU/N2QLCsIjqI2PBlJMgFl7NZWc4eBzRjoUXv03Ak3nPh4ylmp5hLNh2HzBsy51cBWeUpBW44SRLloIYzussVHVcjwU5ZFW3BFX4pD4STPlgAeqZqm8pUJJVWH7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NvLnBuZwoAIAAAAAAAAQAYAIeMX/aDa9oBD4gzCIVr2gFKo5ixg2vaAVBLAQIUABQAAgAIAMJTYViPl6QvjQMAAIgDAAAIACQAAAAAAAAAIAAAANgSAABtaW5lLnBuZwoAIAAAAAAAAQAYADjB412Aa9oBdSQ0CIVr2gFnUll6gGvaAVBLAQIUABQAAgAIAClVYVhkYxGqDQQAAPwEAAAHACQAAAAAAAAAIAAAAIsWAABudW0ucG5nCgAgAAAAAAABABgAqput74Fr2gF1JDQIhWvaAZl0re+Ba9oBUEsFBgAAAAAEAAQAZgEAAL0aAAAAAA=="
	bd, err := base64.StdEncoding.DecodeString(sd)
	if err != nil {
		return err
	}

	data := bytes.NewReader(bd)
	zr, err := zip.NewReader(data, data.Size())
	if err != nil {
		return err
	}

	subImg := func(img image.Image, x, y, num int) []*ebiten.Image {
		var (
			ei = ebiten.NewImageFromImage(img)
			ri = make([]*ebiten.Image, num)
		)
		for i := 0; i < num; i++ {
			var rc image.Rectangle
			rc.Min.Y, rc.Max.X = i*y, x
			rc.Max.Y = rc.Min.Y + y
			ri[num-i-1] = ei.SubImage(rc).(*ebiten.Image)
		}
		return ri
	}

	for _, fv := range zr.File {
		fr, err := fv.Open()
		if err != nil {
			return err
		}

		img, err := png.Decode(fr)
		_ = fr.Close()
		if err != nil {
			return err
		}

		switch fv.Name {
		case "ico.png":
			ebiten.SetWindowIcon([]image.Image{img})
		case "mine.png":
			m.img = subImg(img, 16, 16, 16)
		case "num.png":
			m.num = subImg(img, 13, 23, 12)
		case "face.png":
			m.face = subImg(img, 24, 24, 5)
		}
	}
	return nil
}

如下顯示效果

image

長按滑鼠左鍵劃過格子會有提示,與windows掃雷效果一致。

左鍵既是單擊,點數字時也是雙擊,右鍵標雷。

在介面輸入,1 + 回車 = 初級,2 + 回車 = 中級,3 + 回車 = 高階

在介面輸入, 11 22 33 + 回車 = 高度11,寬度22,總雷數33 。可以自定義資料

如果想在瀏覽器中執行,可到專案 LittleGame
執行

# 編譯專案
.\build.bat minesweeper
# 執行http伺服器
.\httpServer

然後瀏覽器訪問: http://127.0.0.1:8080 ,效果顯示如下

image

相關文章