本文主要是介绍好运留下来,烦恼丢出去,龙年大吉各位,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// 主函数
const main = () => {console.log(`---魔术开始---`);// 生成初试牌组let cardList = [];let noRandom = false;cardList = createCardList(noRandom);console.log(`---您获取的初始牌组是${JSON.stringify(cardList)}---`);console.log(`————————————————————————————————————————————————————————————————————————————————————`);// 根据名字字数移动对应次数的牌组let nameNumber = 2;cardList = moveNameNum(cardList, nameNumber);console.log(`---您的名字是${nameNumber}个字,需要移动${nameNumber}次---`);console.log(`---您移动${nameNumber}次之后的牌组是${JSON.stringify(cardList)}---`);console.log(`————————————————————————————————————————————————————————————————————————————————————`);// 头三张重新插入牌组中间,获取隐藏牌,并藏在屁股下面const result_3 = topThreeInsert(cardList);cardList = result_3.cardList;console.log(`---您头三张牌重新插入之后的牌组是${JSON.stringify(cardList)},您的隐藏牌是${result_3.hiddenCard}---`);console.log(`————————————————————————————————————————————————————————————————————————————————————`);cardList.shift();console.log(`---您藏起隐藏牌之后的牌组是${JSON.stringify(cardList)},您的隐藏牌是${result_3.hiddenCard}---`);console.log(`————————————————————————————————————————————————————————————————————————————————————`);// 根据南北方人,再将对应头几张牌插入中间// 北方人-1,南方人-2,不清楚-3let personNumber = 2;const result_4 = placePersonInsert(cardList, personNumber);cardList = result_4.cardList;console.log(`---您${personNumber === 1 ? '是北方人' : personNumber === 2 ? '是南方人' : '不知道自己是哪里人'},所以你要将头${personNumber}张牌重新插入---`);console.log(`---您将头${personNumber}张牌重新插入之后的牌组是${JSON.stringify(cardList)},您的隐藏牌是${result_3.hiddenCard}---`);console.log(`————————————————————————————————————————————————————————————————————————————————————`);// 根据姓别丢掉对应个数的牌// 男士丢掉头1张,女士丢掉头2张let genderNumber = 1;cardList = moveCardBasedOnGender(cardList, genderNumber);console.log(`---您是${genderNumber === 2 ? '女' : '男'}性,所以你要丢掉头${genderNumber}张牌---`);console.log(`---您丢掉头${genderNumber}张牌之后的牌组是${JSON.stringify(cardList)},您的隐藏牌是${result_3.hiddenCard}---`);console.log(`————————————————————————————————————————————————————————————————————————————————————`);// 根据“见证奇迹的时刻”移动七次cardList = cardListMove(cardList);console.log(`---您根据“见证奇迹的时刻”移动七次之后的牌组是${JSON.stringify(cardList)},您的隐藏牌是${result_3.hiddenCard}---`);console.log(`————————————————————————————————————————————————————————————————————————————————————`);// 好运留下来,烦恼扔出去cardList = leaveGoodLuckDisCardTroubles(cardList);console.log(`---您最终的牌组是${JSON.stringify(cardList)}---`);console.log(`---您藏起来的牌是${result_3.hiddenCard}, 你手上的牌是${cardList[0]}---`);if (result_3.hiddenCard == cardList[0]) {console.log(`---好事成双,祝大家龙年大吉---`);} else {console.log(`---大侠请重新来过---`);}
}// 创建生成对应的牌组
const createCardList = (noRandom) => {let cardInitList = [];if (noRandom) { // 固定牌组cardInitList = [4, 2, 6, 1];} else { // 随机生成牌组const length = 4;for (let i = 0; i < length; i++) {const randomNum = Math.floor(Math.random() * 900);// 随机生成1~9的数字牌cardInitList.push(randomNum % (4 * 2) + 1);}}// 撕开合并cardList = cardInitList.concat(cardInitList);return cardList;
}// 移动名字字数对应的次数
const moveNameNum = (cardInitList, num) => {// 默认1num = num ? num : 1;// 获取需要移动的牌const frontList = cardInitList.slice(0, num);// 获取保留的牌const backList = cardInitList.slice(num, cardInitList.length);// 进行移动const cardList = backList.concat(frontList);return cardList;
}// 首三张插入牌组中间
const topThreeInsert = (cardInitList) => {// 获取头三张牌const topThreeList = cardInitList.slice(0, 3);// 保留下来的牌组const reserveList = cardInitList.slice(3, cardInitList.length);// 将头三张牌重新插到剩下的牌中间,那不包含首尾位置let cardList = reserveList;topThreeList.forEach(i => {// 随机生成插入位置,不包含首尾let insertIndex = (Math.floor(Math.random() * 900) % (reserveList.length - 2)) + 1;// 将牌插入牌组const handList = cardList.slice(0, insertIndex);handList.push(i);cardList = handList.concat(cardList.slice(insertIndex, cardList.length));})return {cardList,// 隐藏牌(藏屁股下面的)hiddenCard: cardList[0]};
}// 根据那方人在将对应牌插入牌组中间
const placePersonInsert = (cardInitList, num) => {// 默认1num = num ? num : 1;// 获取需要重新插入的头几张牌const needList = cardInitList.slice(0, num);// 保留下来的牌组const reserveList = cardInitList.slice(num, cardInitList.length);// 将需要重新插入的牌插到剩下的牌中间,那不包含首尾位置let cardList = reserveList;needList.forEach(i => {// 随机生成插入位置,不包含首尾let insertIndex = (Math.floor(Math.random() * 900) % (reserveList.length - 2)) + 1;// 将牌插入牌组const handList = cardList.slice(0, insertIndex);handList.push(i)cardList = handList.concat(cardList.slice(insertIndex, cardList.length));})return {cardList};
}// 根据姓别丢弃对应个数的牌
const moveCardBasedOnGender = (cardInitList, num) => {// 默认1num = num ? num : 1;let cardList = cardInitList.slice(num, cardInitList.length);return cardList;
}// 根据“见证奇迹的时刻”移动七次
const cardListMove = (cardInitList) => {const strValue = '见证奇迹的时刻';const strLength = strValue.length;let cardList = cardInitList.concat(cardInitList).concat(cardInitList);cardList = cardList.slice(strLength, strLength + cardInitList.length);return cardList;
}// 好运留下来,烦恼扔出去
const leaveGoodLuckDisCardTroubles = (cardInitList) => {let cardList = cardInitList;while (cardList.length > 1) {cardList.push(cardList.shift());cardList.shift();console.log(`---好运留下来,烦恼扔出去---`);console.log(`---此时,您手上的牌组是${JSON.stringify(cardList)}---`);}return cardList;
}// 好运留下来,烦恼扔出去 第二版
const leaveGoodLuckDisCardTroubles_1 = (cardInitList) => {let cardList = cardInitList;// 本次牌组长度是偶数还是奇数let currflag = 2;// 上一次牌组长度是偶数还是奇数let prevFlag = 2;// 是不是第一次let isInit = true;while (cardList.length > 1) {let list = [];// 获取本次牌组长度是偶数还是奇数currflag = cardList.length % 2 === 0 ? 2 : 1;if (isInit || (prevFlag % 2 === 0)) { // 第一次循环或者上一次是偶数是,丢到位置在偶数的牌,保留位置在奇数的牌isInit = false;cardList.forEach((item, index) => {if (index % 2 === 0) {list.push(item);}})cardList = list;} else { // 反之丢到位置在奇数的牌,保留位置在偶数的牌cardList.forEach((item, index) => {if (index % 2 !== 0) {list.push(item);}})cardList = list;}// 将本次牌组长度是偶数还是奇数赋值给上一次牌组长度是偶数还是奇数,供下一次循化使用prevFlag = currflag;console.log(`---好运留下来,烦恼扔出去---`);console.log(`---此时,您手上的牌组是${JSON.stringify(cardList)}---`);}return cardList;
}// 函数运行
main();
这篇关于好运留下来,烦恼丢出去,龙年大吉各位的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!