简单的JS鸿蒙小游戏—经典24点纸牌

系统 OpenHarmony
今天给大家带来的就是这个经典的纸牌游戏,大家学会以后在空闲时可以玩一玩,锻炼一下速算能力。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

前言

相信大家都玩过24点纸牌游戏,今天给大家带来的就是这个经典的纸牌游戏,大家学会以后在空闲时可以玩一玩,锻炼一下速算能力。

项目结构

#打卡不停更# 简单的JS鸿蒙小游戏——经典24点纸牌-开源基础软件社区

页面构建

页面设计得比较简单,上半是数字区,放着四张纸牌;下半是运算符号区,分别是加、减、乘、除;右侧则是得分记录和两个操作按钮。

#打卡不停更# 简单的JS鸿蒙小游戏——经典24点纸牌-开源基础软件社区

<div class="container">
<div class="gamezone">
<div class="poker">
<div for="(index, item) in current" class="card {{ item.css }}" show="{{ item.show }}" disabled="{{ item.disabled }}">
<image src="{{ item.shape }}" onclick="optnum(index)"></image>
<text class="number">{{ item.text }}</text>
</div>
</div>
<div class="sign">
<image for="(index, item) in operator" src="{{ item.src }}" class="operator {{ item.css }}"
disabled="{{ operdis }}" onclick="setsign(index)"></image>
</div>
</div>
<div class="side">
<text class="score">得分\n{{ score }}</text>
<button class="btn" onclick="replace">换一批</button>
<button class="btn" onclick="revert">重置</button>
</div>
</div>

对于4种花型各13张纸牌需要分别定制样式,因此建立了Poker.js纸牌字典,格式如下:

export let Poker =  [
{
num: 1,
text: 'A',
shape: "common/images/spade.png"
},
{
num: 2,
text: '2',
shape: "common/images/spade.png"
},
…………
{
num: 13,
text: 'K',
shape: "common/images/diamond.png"
},
]

export default Poker;

游戏逻辑

  • 随机抽牌:这里分别写了两种随机抽牌逻辑。

一是将4×13共52张牌打乱顺序后抽取前四张;

disorder() {
let many, ran, temp = 0;
for(many=0; many<26; many++) {
ran = Math.floor(Math.random()*52);
temp = this.pokers[many];
this.pokers[many] = this.pokers[ran];
this.pokers[ran] = temp;
}
this.origin = [0, 1, 2, 3];
},

二是52张牌的排序不变,随机生成4个不重复的数,以这四个数作为数组下标抽牌。

getrandom() {
this.origin = [];
let compare = false;
let temp = 0;
while(4 > this.origin.length) {
compare = false;
temp = Math.floor(Math.random()*52);
for(let i=0; i<this.origin.length; i++) {
if(temp == this.origin[i]) {
// 抽到两张相同的牌,不放入初始牌组
compare = true;
break;
}
}
if(false == compare) {
this.origin.push(temp);
}
}
},
  • 牌组替换与还原:因为抽牌是随机的,而并非所有的组合都是有解的,所以需要有操作按钮,可以让玩家主动替换或还原当前牌组。
// 换一组牌,并赋值给当前牌组
replace() {
console.log("—————————更换牌组—————————");
// this.disorder(); // 换牌方法一
this.getrandom(); // 换牌方法二
this.initcards();
console.log("当前牌组为" + JSON.stringify(this.current));
},

// 还原牌组
revert() {
console.log("—————————还原牌组—————————");
this.initcards();
console.log("还原当前牌组为" + JSON.stringify(this.current));
},
  • 选中样式:选中的数字或运算符需要有特别的样式提示,以提高游戏体验,所以需要给纸牌和运算符设置样式属性。
// 选择数字
optnum(num_index) {
// 运算符为空,处于选第一个数的环节
if(null == this.operindex) {
this.operdis = false;
first = num_index;
for(let i=0; i<4; i++) {
this.current[i].css = "";
}
this.current[num_index].css = "select";
}
// 运算符不为空,选择了第二个数
else {
this.operdis = true;
second = num_index;
this.calculate();
}
},
// 选择运算符
setsign(op_index) {
// 若点击的运算符为未选中状态,
if("" == this.operator[op_index].css) {
for(let i=0; i<4; i++) {
this.operator[i].css = "";
}
this.operator[op_index].css = "select";
this.operindex = op_index;

// 关闭第一个数可选中,取消其同时作为第二个数的资格
this.current[first].disabled = true;
}
// 若点击的运算符为选中状态,取消选中,重启选第一个数环节
else {
this.operator[op_index].css = "";
this.current[first].disabled = false;
this.operindex = null;
}
},
  • 运算合法判定:在进行运算前,需要对运算的合法性进行判定。对于零不能作为除数,某些数不能被整除等情况,需要加以限制。当出现这些情况,撤销这次操作,恢复牌组状态,并弹出提示玩家。
// 判别运算是否合法,再进行运算
calculate() {
// 选好第二个数,除法运算是否合法
if((3 == this.operindex) && (0 == this.current[second].num)) {
prompt.showToast({
message: "0不能作为除数,请重新选择",
duration: 1500,
});
// 非法运算,重置选数环节
this.current[first].disabled = false;
this.current[first].css = "";
this.operator[this.operindex].css = "";
this.operindex = null;
this.operdis = true;
first = second = 0;
}
else if((3 == this.operindex) && (0 != (this.current[first].num % this.current[second].num))) {
prompt.showToast({
message: "无法被整除,请重新选择",
duration: 1500,
});
// 无法被整除,重置选数环节
this.current[first].disabled = false;
this.current[first].css = "";
this.operator[this.operindex].css = "";
this.operindex = null;
this.operdis = true;
first = second = 0;
}
// 正常触发运算
else {
switch (this.operindex) {
case 0:
this.current[first].num += this.current[second].num;
break;
case 1:
this.current[first].num -= this.current[second].num;
break;
case 2:
this.current[first].num *= this.current[second].num;
break;
case 3:
this.current[first].num /= this.current[second].num;
break;
default:
console.log("运算意外出错");
break;
}
// 运算结束,将结果赋值给一,文本显示数字,花型为某种,恢复一的可点击和未选中样式,隐藏二的显示,运算符为null
this.current[first].text = this.current[first].num;
this.current[first].shape = "";
this.current[first].disabled = false;
this.current[first].css = "";
this.current[second].show = false;
this.operator[this.operindex].css = "";
this.operindex = null;
this.checkover();
}
},
  • 得分判定:根据场上最后一张牌的数值进行得分判定,等于24则加分替换新牌组,否则将牌组重置为原状。
// 得分判定,先判断是否只剩一张牌,若是则进行结果比较,答对加分换牌,答错还原
checkover() {
let temp = 4;
for(let i=0; i<4; i++) {
if(false == this.current[i].show) {
temp --;
}
}
if(1 == temp) {
// 结果判断是否等于24
if(24 == this.current[first].num) {
prompt.showToast({
message: "答对啦,加分,换牌组",
duration: 1500,
});
this.score += 10;
this.replace();
}
else {
prompt.showToast({
message: "答错啦……还原牌组",
duration: 1500,
});
this.revert();
}
}
},

最终效果

#打卡不停更# 简单的JS鸿蒙小游戏——经典24点纸牌-开源基础软件社区

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: 51CTO开源基础软件社区
相关推荐

2022-11-01 15:17:48

JS鸿蒙小游戏

2022-02-11 14:39:11

游戏JS鸿蒙

2022-02-11 14:02:09

游戏JS鸿蒙

2022-02-17 20:18:27

JS鸿蒙操作系统

2022-10-31 15:22:37

JS鸿蒙小游戏

2021-01-12 12:16:55

鸿蒙HarmonyOS游戏

2012-01-10 12:48:52

Java

2022-07-08 14:53:46

扫雷小游戏鸿蒙

2023-08-07 15:18:29

游戏开发鸿蒙Arkts

2012-09-11 09:19:35

JavaScriptJSjQ

2022-07-29 14:47:34

数独Sudoku鸿蒙

2011-03-15 13:19:11

jQuery

2023-10-17 10:20:53

VueReact

2022-08-25 21:41:43

ArkUI鸿蒙

2021-01-15 12:15:36

鸿蒙HarmonyOS游戏

2022-08-04 13:55:08

拼数字小游戏鸿蒙

2022-08-22 17:28:34

ArkUI鸿蒙

2020-12-09 11:42:18

WiFi IoT鸿蒙开发

2021-08-25 09:54:51

鸿蒙HarmonyOS应用

2020-11-12 09:44:43

鸿蒙
点赞
收藏

51CTO技术栈公众号