在Java中:如何从容器中“拾取”一个元素并将其“放置”在另一个容器中?

huangapple 未分类评论61阅读模式
英文:

In Java: how to 'pickup' an element of a container and 'place' it in another container?

问题

  1. 我在探索如何创建一个基于文本的游戏并且我正在尝试处理在房间之间移动的部分我已经解决了这一部分并且创建了一个非常详细的地图你可以输入go north等命令唯一的问题是游戏非常枯燥我想加入物品的概念为了创建房间我做了以下的代码
  2. ```javascript
  3. var rooms = {
  4. "room0": {
  5. "description": "这里是描述",
  6. "directions": {
  7. "direction": "新房间"
  8. },
  9. "items": {
  10. "item1": "物品1",
  11. "item2": "物品2"
  12. }
  13. }
  14. }

因此,我想如果我创建一个名为“items”的单独元素,然后在房间中列出物品,就像我在“directions”中所做的一样。我编写了以下代码将物品添加到玩家的物品栏中:

  1. function grabItem(item) {
  2. if (inventory.length < 12 && rooms[currentRoom].items[item] !== undefined) {
  3. inventory.append(item);
  4. //在此处插入代码以从房间中移除物品
  5. }
  6. }

所以!重要的问题是,我可以使用什么方法来移除单个物品?

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m exploring how to create a text-based game, and I was tinkering around with moving between rooms. I have this part down and a very elaborate map created where you can type, &quot;go north&quot; etc. The only thing is that the game is very dry, and I wanted to incorporate items. To make the rooms I did this:
  4. ```javascript
  5. var rooms = {
  6. &quot;room0&quot;:{
  7. &quot;description&quot;: &quot;description here&quot;,
  8. &quot;directions&quot;:{
  9. &quot;direction&quot;: &quot;newRoom&quot;
  10. }
  11. }
  12. }

So, I was thinking if I made a separate element called "items" and then list the items in that room, the way I did with "directions". I wrote this code to add items to the player inventory:

  1. function grabItem(item){
  2. if(inventory.length &lt; 12 &amp;&amp; rooms[currentRoom].items[item] !== undefined) {
  3. inventory.append(item);
  4. //INSERT CODE HERE TO REMOVE THE ITEM FROM THE ROOM
  5. }
  6. }

SO! The big question is, what method can I use to remove an individual item?

答案1

得分: 0

Sure, here's the translated code:

  1. function grabItem(item){
  2. if(inventory.length < 12 && rooms[currentRoom].items[item] !== undefined) {
  3. inventory.append(rooms[currentRoom].items[item]);
  4. rooms[currentRoom].items[item] = undefined;
  5. }
  6. }
英文:
  1. function grabItem(item){
  2. if(inventory.length &lt; 12 &amp;&amp; rooms[currentRoom].items[item] !== undefined) {
  3. inventory.append(rooms[currentRoom].items[item]);
  4. rooms[currentRoom].items[item] = undefined;
  5. }
  6. }

NB java!==javascript

huangapple
  • 本文由 发表于 2020年4月7日 05:29:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/61069279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定