微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Node将process.stdin作为Javascript传递?

如何解决如何使用Node将process.stdin作为Javascript传递?

我正在用 Javascript 编写一个扑克游戏,它应该采用标准输入,如下所示:

enter image description here

输入的第一行将包含一个代表玩家数量的整数。这个数字总是大于 0 小于 8。

以下 n 行,其中 n 是玩家人数,将包含一个代表玩家 ID 的整数,后跟三张空格分隔的卡片,代表玩家的手牌。

打印到标准输出输出将是获胜玩家的 ID。

enter image description here

但是我的函数是在 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:

enter image description here

解决方法

将传入的整数传递给您的 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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。