const print = (title, array) => { console.log(`${title}[${array.join("")}]`); };
const insertCards = (array, k) => { let result = [...array]; result = [result[k], ...array.slice(0, k), ...array.slice(k + 1)]; return result; };
const r = () => Math.ceil(Math.random() * 3 + 1);
let array = ["A", "B", "C", "D"].sort((a, b) => Math.random() - 0.5); print("随便四张牌", array); array = [...array, ...array]; print("对折完", array); let names = r(); array = [...array.slice(names), ...array.slice(0, names)]; print("自己的名字张数,放后面", array); array = insertCards(array, r()); print("顶部三张牌插入中间", array); let pop = array[0]; array.splice(0, 1); print(`第一张牌${pop}拿起来,放到屁股后面`, array); array = insertCards(array, r()); print("南方人/北方人,拿牌插中间", array); array.splice(0, 1); print("男生扔一张", array);
for (let i = 0; i < 7; i++) { array = [...array.slice(1), array[0]]; print("见证奇迹的时刻"[i], array); } while (array.length > 1) { array = [...array.slice(1), array[0]]; array.splice(0, 1); print("好运留下来,烦恼丢出去", array); } console.log("剩了一张了", array[0]);
|