~ 如何用C++自制一个日麻游戏 ~(一)大体框架构建 § 3 基本类型 Tile

导入

可能已经注意到了,前一篇的所有 Tile 类型都是指针
这是因为打麻将时一共只有136张牌,所以各种牌山、牌河、手牌、副露都可以用指针指向那原来的136张牌,也可以提高效率

正题

首先写出这些量的指向关系,其中:

  1. 只有牌常量才是 Tile 类型,其他都是指针
  2. 直线表示包含有相同指针,箭头表示指针指向
  3. 牌河、手牌、副露都来自于牌山,所以一定被真包含于(⊂)牌山;由于副露可能是吃、碰、杠他人打出(牌河)的牌,所以副露和牌河有交集

指针

由于其他都是指针,所以 Tile 类主要针对于牌常量(const_card)编写,可以写成以下的代码:
因为 const_card 作为唯一的非指针对象,并不需要改变他的值,所以 card 的值用 const 类型

class Tile
{
public:
	const _int_ card;		//存放牌信息
	Tile(_int_ _card) : card{ _card } {}		//构造
	operator _int_() const { return card; }		//直接返回牌的值
};

然后要决定不同的牌分别怎么表示,为了计算方便,我实行了如下设定:

charactercard
一萬 ~ 九萬0 ~ 8
一筒 ~ 九筒16 ~ 24
一索 ~ 九索32 ~ 40
48
56
西64
72
80
88
96

可以发现幺九牌都是 8 的倍数,这样做有如下好处:

  1. 判断幺九牌(%8)和数牌字牌(/8)很方便
  2. 判断牌与牌之间关系很方便(相减分辨两张牌是否相同或连续)
  3. 判断是否是五万、筒、索方便

然后便可以着手写 const_card,由于牌数是确定的,所以可以用 array 类型

array<Tile,136> const_card		//牌常量
{
	 0, 0, 0, 0,	 1, 1, 1, 1,	 2, 2, 2, 2,	 3, 3, 3, 3,	 4, 4, 4, 4,	 5, 5, 5, 5,	 6, 6, 6, 6,	 7, 7, 7, 7,	 8, 8, 8, 8,
	16,16,16,16,	17,17,17,17,	18,18,18,18,	19,19,19,19,	20,20,20,20,	21,21,21,21,	22,22,22,22,	23,23,23,23,	24,24,24,24,
	32,32,32,32,	33,33,33,33,	34,34,34,34,	35,35,35,35,	36,36,36,36,	37,37,37,37,	38,38,38,38,	39,39,39,39,	40,40,40,40,
	48,48,48,48,	56,56,56,56,	64,64,64,64,	72,72,72,72,	80,80,80,80,	88,88,88,88,	96,96,96,96
};

然后要考虑的是如何给牌山赋值(随机)
暂时先用简单的时间作为随机数种子:
首先将牌山整齐地对应牌常量,然后创建一个随机的数组 arr[136],在用冒泡法排序 arr[136] 同时,打乱牌山以达到随机牌山的目的

explicit Maj_system(_wind_ _wind) :wind{ _wind }		//(场风)开局
{
	srand((unsigned)time(nullptr));		//设置时间随机数种子
	for (_int_ i{ 0 }; i < 136; ++i)		//牌山初始化 
		shan[i] = &const_card[i];
	_int_ arr[136];		//随机数组
	for (_int_ i{ 0 }; i < 136; ++i)		//随机牌山 
		arr[i] = rand() % 32767 + 1;		//赋随机值 
	for (_ull_ i{ 0 }; i < 135; ++i)		//排序 
		for (_ull_ j{ 0 }; j < 135 - i; ++j)		//防止出现警告,这里用 unsigned long long 类型
			if (arr[j] > arr[j + 1])
			{
				swap(arr[j], arr[j + 1]);
				swap(shan[j], shan[j + 1]);
			}
	...
	...
	...
}

以上,关于 Tile 大体就介绍完毕了,这时(大概)就可以顺利运行该程序了233

规则参考

[1] 日麻百科
[2] 资深麻友
[3] 雀魂麻将
[4] 雀姬麻将

原创文章,转载请标明出处,如有谬误欢迎各位指正
欢迎交流麻将算法,QQ:2639914082
非常好的问题!制作一个自己的2048小游戏是很有趣的事情,而且可以很好地锻炼编程技能。下面是一个简单的HTML实现2048小游戏的步骤: 1. 编写HTML页面结构 首先,我们需要创建一个HTML页面来作为游戏的基础。在HTML页面中,我们需要有一个游戏板块的区域,以及一些用于控制游戏的按钮。 ```html <!DOCTYPE html> <html> <head> <title>2048 Game</title> <meta charset="UTF-8"> <link rel="stylesheet" href="style.css"> </head> <body> <h1>2048 Game</h1> <div id="game-container"> <!-- 游戏板块区域 --> </div> <div id="btn-container"> <!-- 游戏控制按钮区域 --> </div> <script src="game.js"></script> </body> </html> ``` 在上面的代码中,我们创建了一个 `game-container` 的 `div` 元素,用于显示游戏的面板。我们还创建了一个 `btn-container` 的 `div` 元素,用于放置游戏的控制按钮。注意,我们还引入了一个名为 `style.css` 的 CSS 文件和一个名为 `game.js` 的 JavaScript 文件。 2. 编写CSS样式 接下来,我们需要编写一些CSS样式来渲染游戏页面。我们将使用 `flexbox` 布局来创建游戏面板,并使用 `grid` 布局来创建游戏控制按钮。 ```css body { font-family: Arial, sans-serif; } h1 { text-align: center; margin-top: 20px; } #game-container { display: flex; flex-wrap: wrap; justify-content: space-between; width: 400px; height: 400px; margin: 0 auto; background-color: #BBADA0; padding: 10px; box-sizing: border-box; } .tile { display: flex; justify-content: center; align-items: center; font-size: 30px; font-weight: bold; color: #776E65; width: 90px; height: 90px; border-radius: 5px; background-color: #CDC1B4; margin-bottom: 10px; } #btn-container { display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 10px; margin-top: 20px; } .btn { display: flex; justify-content: center; align-items: center; font-size: 20px; font-weight: bold; color: #FFFFFF; width: 80px; height: 40px; border-radius: 5px; background-color: #8F7A66; cursor: pointer; } ``` 在上面的代码中,我们为 `game-container` 容器定义了 `flexbox` 布局,用于显示游戏面板。我们还使用 `grid` 布局来创建了 `btn-container` 容器,用于显示游戏控制按钮。 3. 编写JavaScript代码 现在,我们需要编写一些JavaScript代码来实现游戏逻辑。我们将使用一个二维数组来表示游戏面板,并使用随机函数来生成新的方块。 ```javascript const gameContainer = document.getElementById('game-container'); const btnContainer = document.getElementById('btn-container'); const scoreContainer = document.getElementById('score-container'); const ROW = 4; const COL = 4; let score = 0; let board = []; function init() { // 初始化游戏面板 for (let i = 0; i < ROW; i++) { board[i] = []; for (let j = 0; j < COL; j++) { board[i][j] = 0; const tile = document.createElement('div'); tile.className = 'tile'; tile.id = i + '-' + j; gameContainer.appendChild(tile); } } // 随机生成两个方块 generateNewTile(); generateNewTile(); } function generateNewTile() { // 随机生成一个新方块 const randomNumber = Math.random() < 0.5 ? 2 : 4; const emptyTiles = []; board.forEach((row, rowIndex) => { row.forEach((col, colIndex) => { if (col === 0) { emptyTiles.push({ rowIndex, colIndex }); } }); }); if (emptyTiles.length > 0) { const randomIndex = Math.floor(Math.random() * emptyTiles.length); const { rowIndex, colIndex } = emptyTiles[randomIndex]; board[rowIndex][colIndex] = randomNumber; const tile = document.getElementById(rowIndex + '-' + colIndex); tile.innerText = randomNumber; tile.style.backgroundColor = getTileColor(randomNumber); } } function getTileColor(number) { // 根据方块数字返回不同的颜色 switch (number) { case 2: return '#EEE4DA'; case 4: return '#EDE0C8'; case 8: return '#F2B179'; case 16: return '#F59563'; case 32: return '#F67C5F'; case 64: return '#F65E3B'; case 128: return '#EDCF72'; case 256: return '#EDCC61'; case 512: return '#EDC850'; case 1024: return '#EDC53F'; case 2048: return '#EDC22E'; default: return '#CDC1B4'; } } init(); ``` 在上面的代码中,我们定义了 `init` 函数,用于初始化游戏面板。我们还定义了 `generateNewTile` 函数,用于随机生成新的方块。在 `generateNewTile` 函数中,我们使用 `board` 数组来记录游戏面板上的数字,然后使用 `getTileColor` 函数来返回不同数字的方块颜色。 4. 添加游戏控制按钮 最后,我们需要添加一些游戏控制按钮来控制游戏的进行。我们将添加四个按钮,分别用于上下左右移动方块。 ```javascript const upBtn = document.createElement('div'); upBtn.className = 'btn'; upBtn.innerText = 'Up'; upBtn.addEventListener('click', () => { console.log('Up'); }); const downBtn = document.createElement('div'); downBtn.className = 'btn'; downBtn.innerText = 'Down'; downBtn.addEventListener('click', () => { console.log('Down'); }); const leftBtn = document.createElement('div'); leftBtn.className = 'btn'; leftBtn.innerText = 'Left'; leftBtn.addEventListener('click', () => { console.log('Left'); }); const rightBtn = document.createElement('div'); rightBtn.className = 'btn'; rightBtn.innerText = 'Right'; rightBtn.addEventListener('click', () => { console.log('Right'); }); btnContainer.appendChild(upBtn); btnContainer.appendChild(downBtn); btnContainer.appendChild(leftBtn); btnContainer.appendChild(rightBtn); ``` 在上面的代码中,我们使用 `createElement` 函数来创建游戏控制按钮,并使用 `addEventListener` 函数为每个按钮添加点击事件。注意,我们还需要将按钮添加到 `btn-container` 容器中。 这样就完成了一个简单的2048小游戏的制作!当然,这只是一个开始,你可以继续添加更多的功能,让游戏更加完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值