go语言实现扫雷

这篇具有很好参考价值的文章主要介绍了go语言实现扫雷。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

源码如下

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, k 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++ {
			d := m.data[i][j]
			if d == nil {
				d = new(grid)
				m.data[i][j] = d
			}
			d.state = 0

			if k < m.mineCnt {
				d.data = 10
				k++ // 布雷
			} else {
				d.data = 0
			}
		}
	}

	rand.Shuffle(m.h*m.w, func(i, j int) {
		mi := m.data[i/m.w][i%m.w]
		mj := m.data[j/m.w][j%m.w] // 洗牌算法打乱雷区
		mi.data, mj.data = mj.data, mi.data
	})

	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/N2QLCsIjqI2PBlJMgFl7NZWc4eBzRjoUXv03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AUA/IReFa9oBDeFMEIJr2gFQSwECFAAUAAIACAD4VmFYi4Hk444PAACJDwAABwAkAAAAAAAAACAAAAAlAwAAaWNvLnBuZwoAIAAAAAAAAQAYAIeMX/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
}

如下显示效果

go语言实现扫雷

长按鼠标左键划过格子会有提示,与windows扫雷效果一致。

左键既是单击,点数字时也是双击,右键标雷。

在界面输入,1 + 回车 = 初级,2 + 回车 = 中级,3 + 回车 = 高级

在界面输入, 11 22 33 + 回车 = 高度11,宽度22,总雷数33 。可以自定义数据

如果想在浏览器中运行,可到项目 LittleGame
执行

# 编译项目
.\build.bat minesweeper
# 运行http服务器
.\httpServer

然后浏览器访问: http://127.0.0.1:8080 ,效果显示如下

go语言实现扫雷文章来源地址https://www.toymoban.com/news/detail-839326.html

到了这里,关于go语言实现扫雷的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Go语言实现空中飘雪花的效果(附带源码)

    在 Go 语言中,你可以使用 github.com/fogleman/gg 包来实现空中飘雪花的效果。首先,确保你已经安装了该包: 然后,可以使用以下 Go 代码: 这个程序使用 github.com/fogleman/gg 包创建了一个绘图上下文,模拟了飘雪花的效果。运行程序后,它将每一帧保存为 PNG 图像文件,并以 fr

    2024年01月25日
    浏览(42)
  • C语言经典小游戏之扫雷(超详解释+源码)

    “少年气,是历尽千帆举重若轻的沉淀,也是乐观淡然笑对生活的豁达!” 今天我们学习一下扫雷游戏怎么用C语言来实现! 一个扫雷盘面由许多方格(cell)组成,方格中随机分布着一定数量的雷(mine),一个格子中至多只有1雷。胜利条件是打开所有安全格(非雷格,saf

    2024年02月12日
    浏览(61)
  • Go 语言实现归并排序算法的简单示例(附上源码)

    以下是使用 Go 语言实现归并排序算法的简单示例: 在这个例子中, mergeSort 函数接收一个整数切片,使用递归的方式进行归并排序。 merge 函数用于合并两个已排序的切片。在 main 函数中,我们定义了一个示例数组,调用 mergeSort 函数对其进行排序,并输出结果。 归并排序算

    2024年01月21日
    浏览(45)
  • 源码分享-go语言实现的祖冲之ZUC加密算法

    源码路径:free5gc/nas/security/zuc zuc.go zuc_test.go

    2024年02月16日
    浏览(35)
  • 【C语言】扫雷小游戏详细分析(模块化编程)——内附源码

    . 🚀write in front🚀 🔎大家好,我是gugugu。希望你看完之后,能对你有所帮助,不足请指正!共同学习交流🔎 🆔本文由 gugugu 原创 CSDN首发🐒 如需转载还请通知⚠ 📝个人主页:gugugu—CSDN博客 🎁欢迎各位→点赞👍 + 收藏⭐️ + 留言📝​ 📣系列专栏:gugugu的系列专栏——

    2024年02月14日
    浏览(243)
  • 源码分享-go语言实现的snow3g加密算法

    源码路径:free5gc/nas/security/snow3g snow3g.go snow3g_test.go

    2024年02月16日
    浏览(35)
  • 扫雷——C语言实现

    要用代码来实现扫雷这个小游戏,我们就需要先了解扫雷这个游戏的规则和有关操作。 我们先来看一段扫雷游戏的动画: 游戏目标:在N*N的区域中找到M个随机布置的地雷 若点击的区域不是雷,那么就会显示一个数字,这个数字代表着这块区域周围雷的个数 若点击的区域是

    2024年02月08日
    浏览(43)
  • C语言实现扫雷【经典】

    前言 本篇文章要实现的是 扫雷游戏 ,其代码实现与上一篇的三子棋游戏类同,都是在棋盘的基础上,与电脑进行对抗,不同的是,扫雷游戏一开始电脑就已经随机布置好了所有“雷”。 请戳 --- 三子棋 经典玩法: 扫雷就是要把所有非地雷的格子揭开即 胜利 ;踩到地雷格子

    2024年02月16日
    浏览(39)
  • 【C语言】实现扫雷游戏

    详细介绍扫雷游戏的思路和实现过程,并用随机数实现埋雷。 • 使用控制台实现经典的扫雷游戏 • 游戏可以通过菜单实现继续玩或者退出游戏 • 扫雷的棋盘是9*9的格⼦ • 默认随机布置10个雷 • 可以排查雷 ◦ 如果位置不是雷,就显示周围有几个雷 ◦ 如果位置是雷,就炸

    2024年02月04日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包