从列表中拉出元素时如何给出分数?

How to give a score when an element is pulled out from a list?

我想实现一部分代码,当代理从特定海龟生成的列表中选择一个元素时,他们可以获得分数。 我设

breed [playersA playerA]
breed [playersB playerB]
breed [balls ball]

playersA-own[
  my-list
  new_ball
  score
]

playersB-own[
  my-list
  new_ball
  score
]

to setup
  clear-all

  create-playersA 10
  ask playerA 0 [ create-links-with other playersA ]
  ask playerA 2 [ create-link-with playerA 1 ]

  create-playersB 10
  ask playerB 0 [ create-links-with other playersB ]
  ask playerB 2 [ create-link-with playerA 1 ]

  ask playersA[
  set my-list []
  set score 0
]
  ask playersB[
  set my-list []
  set score 0
]
end

to go
  let selected nobody
  let team-player nobody

  set selected one-of turtles with [breed=playersA or breed=playersB]
  ifelse [breed = playersA] of selected[
  ask selected [
          set size [count link-neighbors] of self
          show size
       ]
        create-balls 1[
          hide-turtle
       ]
    ]
    [ ask selected [
          set size [count link-neighbors] of self
          show size
       ]
        create-balls 1[
          hide-turtle
       ]
    ]

set team-player link-neighbors with [breed = playersA]
       ask team_player [
            set my-list lput my-ball my-list 
            ]

end

上面的代码应该 select 在随机海龟上并将一个新球添加到它的邻居列表中。我需要的可能是一个计数器,可以计算玩家之间共享了多少个球。

你能帮我弄清楚吗?

谢谢

您发布的代码存在很多问题,无法通过编辑器中的错误检查。其中一些会产生甚至没有意义的令人惊讶的错误消息,它们的发生是因为逻辑混合了上下文——也就是说,有些命令对 "observer" 级别有意义,有些则需要在 "turtle" 语境等

我认为您试图一次做太多事情,并试图向已经不起作用的代码添加一个计数器。首先你必须修复你的代码然后你可以看到在哪里添加一个计数器。

您绝对必须了解唯一代理 ID 号 "who" 的工作原理。每只海龟都有一个唯一的编号,从零开始。不管海龟的品种是 playerA 还是 playerB 还是球,它都会有一个唯一的编号。一旦你创建了 PlayerA 品种的前 10 只海龟,它们的编号将从 0 到 9。然后,当你创建接下来的 10 只 PlayerB 品种的海龟时,它们将被分配到 10 到 19 的编号。如果你然后创建一个球,比方说,它的编号为 20。

所以永远不会有 PlayerB 的 who 号码为 0 或 1 或 2。PlayerA 已经使用了这些号码。您的设置将因错误而崩溃:

playera 0 is not a PLAYERB error while observer running PLAYERB
called by Command Center

即使只有PlayerA,在设置代码中也不清楚你想要建立什么样的网络。为什么每个人都会 link 到玩家 0,然后还要在玩家 1 和玩家 2 之间添加一个 link?由于玩家只有 "see" 他们的 link 队友,所以只有零号玩家会看到其他人。其他玩家将只有一两个 link-neighbors,因此他们永远不会更新其他人的我的列表。

 create-playersA 10
  ask playerA 0 [ create-links-with other playersA ]
  ask playerA 2 [ create-link-with playerA 1 ]

无论如何,我建议您在尝试添加计数之前让这些工作正常进行。

我认为您不能仅通过查看代码来做到这一点。您需要尽可能摆脱复杂性,然后使用形状、颜色和大量打印语句来查看每个命令是否在执行您认为应该执行的操作。复杂的工作代码几乎总是从简单的工作代码演变而来。

所以完全摆脱 PlayersB(注释掉代码),只创建 5 个玩家 A,并在处理每个步骤时更改颜色和形状以确认它正在工作。编辑器允许您使用 ctrl-;注释掉整个代码块,或者一次取消注释它们,所以在你开始工作的时候尽可能地注释掉所有的东西,然后取消注释下一部分,让它工作,等等。

当您最终使一切正常时,您可以注释掉您在开发中使用的所有打印语句。

反正我重构了你的代码,加了很多注释,加了很多打印语句,终于搞定了运行。如果您 运行 只是设置并查看视图,您就会明白我对网络的意思。 (我关闭了视图中的换行,以便网络看起来正确。)

这是我对您的代码的修改。它会在每一步后打印出每个玩家的 my-list 中的内容,因此您可以一目了然地看到它是否在按照您的意愿行事。 (不是。)

我在视图中为每个玩家添加了 who numbers 作为标签,这样你就明白我的意思了。

它会产生有用的输出,例如:

let's confirm that the lists have been updated. Here's the my-lists for playersA [[5 5 5 5 10] [0 0 0 0 0] [0 8 8] [0 0] [0 9]]

在尝试修复 go 部分之前,让 setup 步骤正常工作并生成您想要的网络。

breed [playersA playerA]
breed [playersB playerB]
breed [balls ball]

playersA-own[
  my-list
  new_ball
  score
]

playersB-own[
  my-list
  new_ball
  score
]

to setup
  clear-all

  ;; for debugging, only create 3 players and inspect the results to confirm it's working as you intended
  ;; use labels to see player numbers in the view

  create-playersA 5 [ set size 1 set color blue set shape "square" setxy random-xcor random-ycor set label who]
  ask playerA 0 [ create-links-with other playersA [set color blue]]
  ask playerA 2 [ create-link-with playerA 1   [set color red]]

  create-playersB  5 [ set size 2 set color yellow set shape "person"  setxy random-xcor random-ycor set label who]

 ; comment out this code until create-playersA is working properly
 ;  ask playerB 0 [ create-links-with other playersB ]
 ;  ask playerB 2 [ create-link-with playerA 1 ]          ;; copy-and-paste error? link with playerB intended?

  ask playersA[
  set my-list []
  set score 0
  ]

  ask playersB[
  set my-list []
  set score 0
  ]

  reset-ticks
end

to go
  let selected nobody
  let team-players nobody
  let hot-ball nobody

  set selected one-of turtles with [
     breed = playersA
     ;;  or breed = playersB   ;; always select one of playersA for debugging this code
     ]

  print ( word "At point 1, we selected turtle " [who] of selected " with breed " [breed] of selected)

  ;; we're still in the observer context here


  ifelse [breed = playersA] of selected [      ;; by mentioning breed, we shift into a turtle context silently

    print ( word " entering the TRUE part of the if-else statement  " )


          ask selected [
                  set size [count link-neighbors] of self
                  print ( word "at point 2 we set selected player's size to " size )
          ]


           create-balls 1 [
              set shape "circle" set size 3 set color blue set label who  
              set hot-ball who
              ; hide-turtle                        ;; for debugging show it so you can click on it and inspect it
              print ( word "at point 3 we set created a blue hot-ball with who= " hot-ball )
          ]

          ;; it seems you want to update the selected turtle's my-ball variable here with a reference to the ball just created??
             print " at point 4 we should set selected agent's my-ball to the ball we just made..."

          ask selected [
                   set new_ball hot-ball
          ]

         print (word " Confirming that selected player got the hot-ball " [new_ball] of selected )
        ;; ask ball hot-ball [ set hidden? true ]

          ;; this set of code seems to apply only when selected turtle is one of playersA, so it was moved INSIDE that ask-selected code
          ;; and put inside another  ask selected [ ] context

          ask selected [
               set team-players link-neighbors with [breed = playersA]   
               print (word "At point 5, For selected player " who ", here is the team-players agent set :"  )
               print (sort team-players)  ;; using "sort" here just to convert an agent set to a list for display
             ]

           print " ------------- about to ask team-players to update their my-lists and change to triangles ---"


          ask team-players [                             
                 set shape "triangle" set size 3                   ;; to see at a glance that everyone was processed
                 set my-list lput new_ball my-list       
                 print "... updated one my-list"
          ]

    print " let's confirm that the lists have been updated. Here's the my-lists for playersA "

    print   map [ i ->   [my-list]  of i ] sort playersA  ;; using "sort" to convert agent-set to a list

    print (word "At the end of the go step, we have this many balls: " count balls)

    ]
  ;; else we should have breed != playersA
  [

      error " we should only be looking at one of playersA here for testing"     ;; for debugging

   ]

 ;; tick

end