问题描述
我正在用 Javascript 编写一个扑克游戏,它应该采用标准输入,如下所示:
输入的第一行将包含一个代表玩家数量的整数。这个数字总是大于 0 小于 8。
以下 n 行,其中 n 是玩家人数,将包含一个代表玩家 ID 的整数,后跟三张空格分隔的卡片,代表玩家的手牌。
但是我的函数是在 Javascript 中,所以......我如何将这个 stdin 转换为一个整数和一个像这样的对象:
function playPoker(n,cardsDealt){
// functionality
}
const num = 3;
const hands = {
'0': ['4c,','5c','6c'],'1': ['Kd','5d,'6d'],'2': ['Jc','Qd,'Ks'],}
playPoker(num,hands);
我尽可能地使用 process.stdin,但不确定如何将输入解释为 Javascript:
解决方法
将传入的整数传递给您的 const playPoker = num => {
let i = 1;
let hands = [];
if (!isNan(parseInt(num))) {
while (i <= parseInt(num)) {
let hand = [];
// generate your hand
hands.push(hand);
i++;
}
return hands;
} else {
return 'Not a valid number';
}
}
process.stdin.on('data',data => {
const hands = playPoker(data);
const handsOut = hands.map(h => `${hands.indexOf(h)}: ${h[0]} ${h[1]} ${h[2]}`);
process.stdout.write(handsOut.toString().replace(',','\n'));
let winner;
// decide winner
process.stdout.write(winner);
});
函数,然后使用计数循环遍历您的手牌生成逻辑,将其保存到一个数组中,将数组传回,并使用它生成一个格式化的字符串并确定您的获胜者.
.page-wrapper {
display: grid;
grid-template-rows: auto 1fr;
}
.page-header a {
text-decoration: none;
}
.wrapper {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.menu {
font-size: 25px;
display: flex;
flex-direction: column;
background-color: #edc9b5;
border-right: 2px solid #808080;
padding: 1em 1.2em 1em 1.2em;
margin: 0;
height: 100vh;
}
.item {
text-decoration: none;
font-family: Comic Sans MS;
}
.video {
margin: 0 auto;
}
p {
text-align: center;
}