ZBLOG

golang 实现2048小游戏

抱歉,作为 AI 语言模型,我无法提供图形界面的游戏实现。不过,以下是一个基于命令行的简单 Golang 2048 游戏:

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"time"
)

const (
	size = 4 // 游戏棋盘大小
)

var (
	board [size][size]int // 游戏棋盘
	score int             // 得分
)

func init() {
	rand.Seed(time.Now().UnixNano()) // 初始化随机数种子
	addRandomTile()
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	for {
		printBoard()
		fmt.Println("WASD to move, Q to quit")
		input, _ := reader.ReadString('\n')
		switch string(input[0]) {
		case "w":
			moveUp()
		case "a":
			moveLeft()
		case "s":
			moveDown()
		case "d":
			moveRight()
		case "q":
			os.Exit(0)
		}
		if isGameOver() {
			fmt.Println("Game Over!")
			os.Exit(0)
		}
	}
}

// 打印游戏棋盘
func printBoard() {
	fmt.Println("Score:", score)
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			tile := board[i][j]
			if tile == 0 {
				fmt.Printf("%5s", ".")
			} else {
				fmt.Printf("%5d", tile)
			}
		}
		fmt.Println()
	}
}

// 检查游戏是否结束
func isGameOver() bool {
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if board[i][j] == 0 {
				return false // 还有空格,游戏继续
			}
			if i != 0 && board[i-1][j] == board[i][j] {
				return false // 上方有相等的格子,游戏继续
			}
			if j != 0 && board[i][j-1] == board[i][j] {
				return false // 左方有相等的格子,游戏继续
			}
		}
	}
	return true // 无法移动了,游戏结束
}

// 添加一个随机数字格子
func addRandomTile() {
	var emptyTiles [size * size][2]int // 空格子坐标数组
	emptyTilesCount := 0              // 空格子数量
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if board[i][j] == 0 {
				emptyTiles[emptyTilesCount][0] = i
				emptyTiles[emptyTilesCount][1] = j
				emptyTilesCount++
			}
		}
	}
	if emptyTilesCount == 0 {
		return // 没有空格子了,不添加数字格子
	}
	randIndex := rand.Intn(emptyTilesCount)
	tileValue := 2
	if rand.Float32() < 0.1 { // 10% 的概率出现 4
		tileValue = 4
	}
	board[emptyTiles[randIndex][0]][emptyTiles[randIndex][1]] = tileValue
}

// 移动棋盘
func moveBoard(dx, dy int) {
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if board[i][j] != 0 {
				x, y := i+dx, j+dy // 目标位置
				if x >= 0 && x < size && y >= 0 && y < size && board[x][y] == 0 {
					// 目标位置为空格子,移动当前格子
					board[x][y] = board[i][j]
					board[i][j] = 0
					i, j = x-dx, y-dy // 继续检查当前格子
				} else if x >= 0 && x < size && y >= 0 && y < size && board[x][y] == board[i][j] {
					// 目标位置有相等的格子,合并当前格子
					board[x][y] *= 2
					score += board[x][y]
					board[i][j] = 0
				}
			}
		}
	}
	addRandomTile()
}

// 上移棋盘
func moveUp() {
	moveBoard(-1, 0)
}

// 左移棋盘
func moveLeft() {
	moveBoard(0, -1)
}

// 下移棋盘
func moveDown() {
	moveBoard(1, 0)
}

// 右移棋盘
func moveRight() {
	moveBoard(0, 1)
}

这个实现中,使用二维数组 board 存储棋盘上每个格子的数值,使用整数变量 score 记录当前得分。在初始化时,通过随机数生成函数 rand.Intn()rand.Float32() 随机添加一个初始数字格子。然后,使用命令行输入读取函数 bufio.NewReader() 获取玩家的移动操作,再调用相应的移动函数来移动棋盘。最后,使用检查游戏结束函数 isGameOver() 判断游戏是否结束。

需要注意的是,这个实现只是一个基本版的 2048 游戏,没有图形界面,并且可能存在一些不太完善的地方。如果想要实现更好的游戏体验和更高的代码质量,建议使用专业的游戏引擎或游戏框架,例如 Unity、Unreal Engine、Cocos2d-x 等。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=2077

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?