aaa---wikiwords.nlogo
来自「NETLOGO」· NLOGO 代码 · 共 748 行 · 第 1/2 页
NLOGO
748 行
globals[ text ;; text to be written left-edge ;; pxcor of leftmost column where letters can go right-edge ;; ditto for rightmost all-letters line-spacing left-margin right-margin]breeds[ letters students stamps]letters-own[ word-length ;; the length of the word the turtle is in num-word ;; the number the turtle's word is in the text leader ;; the turtle with the character before this turtle's character new-line? ;; whether we should start a new line or not happy]students-own[ user-id letter real-color]stamps-own[ stamper-id ]to startup hubnet-set-client-interface "COMPUTER" ["wikiwords client.nlogo"] hubnet-resetendto listen-clients while [ hubnet-message-waiting? ] [ hubnet-fetch-message ifelse hubnet-enter-message? [ create-new-student ] [ ifelse hubnet-exit-message? [ remove-student ] [ execute-command hubnet-message-tag ask students with [user-id = hubnet-message-source] [send-info-to-clients] ] ] ]endto create-new-student create-custom-students 1 [ setxy random 22 - 3 random 22 - 14 set letter random-one-of all-letters set shape "blank" set label letter set size 1 set user-id hubnet-message-source set real-color (random 5) + (random 14) * 10 + 5 set label-color real-color send-info-to-clients ]endto send-info-to-clients hubnet-send user-id "Your letter:" (letter) hubnet-send user-id "your-x" xcor hubnet-send user-id "your-y" ycor hubnet-send user-id "your-color" colorendto remove-student ask students with [user-id = hubnet-message-source] [die]endto stamp-letter ask students with [user-id = hubnet-message-source] [ hatch-stamps 1 [ set label-color white set label label-of myself set shape "blank" set stamper-id user-id-of myself ] set letter random-one-of all-letters hubnet-send user-id "Your letter:" (letter) set label letter ]endto delete-letter ask students with [user-id = hubnet-message-source] [ ask other-stamps-here [ if user-id-of myself != stamper-id [die] ] ]endto execute-command [command] if command = "up" [ask students with [user-id = hubnet-message-source] [set heading 0 fd 1 ]] if command = "right" [ask students with [user-id = hubnet-message-source] [set heading 90 fd 1 ]] if command = "down" [ask students with [user-id = hubnet-message-source] [set heading 180 fd 1 ]] if command = "left" [ask students with [user-id = hubnet-message-source] [set heading 270 fd 1 ]] if command = "flash" [ask students with [user-id = hubnet-message-source] [set label-color label-color + 1]] if command = "stamp" [stamp-letter] if command = "delete" [delete-letter]end;; setup the variables and the Viewto setup ca set text "Why did the farmer say to his wife when he got home?" set line-spacing 2 set left-margin 2 set right-margin 2 update-margins draw-margins ;; make the turtles "disappear" using a shape that has nothing ;; in it. we can't use hide-turtle since we want to be able to see the ;; turtle's label set-default-shape letters "blank" ;; create enough turtles to have one for every character in text create-custom-letters (length text) [ set word-length 0 set leader nobody set new-line? false scatter ] setup-letters finish-letters set all-letters ["A" "B" "C" "D" "E" "F" "G" "H" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"]endto scatter ;; turtle procedure setxy random-float screen-size-x random-float screen-size-yendto draw-margins ask patches [ ifelse (pxcor = left-margin - screen-edge-x) or (pxcor = screen-edge-x - right-margin) [ set pcolor red ] [ set pcolor black ] ]end;; assign the letters to the turtles, determine who leads them,;; and set how long each word isto setup-letters let remaining-text text let word-count 1 let prev-letter nobody ask letters [ without-interruption [ set happy false set label first remaining-text set leader prev-letter set num-word word-count if label = " " [ set word-count word-count + 2 set num-word num-word + 1 ] set remaining-text but-first remaining-text set prev-letter self ] ] let index 1 repeat word-count [ let letters-in-word letters with [num-word = index] ask letters-in-word [ set word-length count letters-in-word ] set index index + 1 ]endto finish-letters while [any? letters with [happy = false]] [ ask letters [ set new-line? false pick-heading ;; choose the heading to the appropriate patch ;; if the turtle is not satisfied with where it is in ;; relation to its leader, go forward a bit ifelse happy? [ setxy pxcor pycor set happy true] ;; move to center of patch [ fd 0.5 set happy false] ] ]end;; the main procedure called by the GO forever buttonto go listen-clients ask students [ if label-color != real-color [ set label-color (label-color + 1) mod 140 ] ]end;; updates the left-edge and right-edge variables to match the margin;; sliders, and redraws the margins if necessaryto update-margins if left-edge != left-margin - screen-edge-x + 1 [ set left-edge left-margin - screen-edge-x + 1 draw-margins ] if right-edge != screen-edge-x - right-margin - 1 [ set right-edge screen-edge-x - right-margin - 1 draw-margins ]end;; set the heading of the turtle to the appropriate patchto pick-heading ifelse leader = nobody ;; first letter goes to upper-leftmost patch [ face-nowrap patch left-edge screen-edge-y ] ;; other patches head toward patch to the right of leader's patch [ face-nowrap value-from leader [patch-at 1 0] ifelse right-edge - left-edge < word-length [ ;; if our word is too long for the width of text, ;; check to see if the patch to the right of the leader is on or over ;; the right margin if pxcor-of leader >= right-edge [ ;; if it is change the heading to the beginning of the next line set new-line? true face-nowrap patch left-edge new-line-pycor ] ] [ ;; if our word is short enough to fit on a single line but there is not ;; enough room on this particular line, try to word wrap. that is to say, ;; if the turtle is at the beginning of a word and the word is too long to ;; be completed on a single line given the starting point of the word, ;; have the leader of the word, move to the next line. if (num-word != num-word-of leader) and (pxcor-of leader + word-length >= right-edge) [ ;; if it is change the heading to the beginning of the next line set new-line? true face-nowrap patch left-edge new-line-pycor ] ] ]end;; reports pycor of the new line the turtle is supposed to go to;; based on the location of its leader and the width of the textto-report new-line-pycor ;; turtle procedure ifelse abs (pycor-of leader - line-spacing) > screen-edge-y [ report pycor-of leader ] [ report pycor-of leader - line-spacing ]end;; reports true if turtle is satisifed with its current positionto-report happy? ;; turtle procedure if leader = nobody ;; if the turtle is the first letter... ;; ...is it on the upper-left-most patch that it can be? [ report (pxcor = left-edge) and (pycor = screen-edge-y) ] ifelse new-line? ;; do we want to start a new-line? ;; is the turtle at the beginning of the next line? [ report (pxcor = left-edge) and (pycor = new-line-pycor) ] ;; is the turtle on the patch to the right of its leader? [ report patch-at -1 0 = value-from leader [patch-here] ]end; *** NetLogo Model Copyright Notice ***;; This model was created as part of the project: CONNECTED MATHEMATICS:; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL; MODELS (OBPML). The project gratefully acknowledges the support of the; National Science Foundation (Applications of Advanced Technologies; Program) -- grant numbers RED #9552950 and REC #9632612.;; Copyright 1998 by Uri Wilensky. All rights reserved.;; Permission to use, modify or redistribute this model is hereby granted,; provided that both of the following requirements are followed:; a) this copyright notice is included.; b) this model will not be redistributed for profit without permission; from Uri Wilensky.; Contact Uri Wilensky for appropriate licenses for redistribution for; profit.;; This model was converted to NetLogo as part of the project:; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN; CLASSROOMS. The project gratefully acknowledges the support of the; National Science Foundation (REPP program) -- grant number REC #9814682.; Converted from StarLogoT to NetLogo, 2001. Updated 2003.;; To refer to this model in academic publications, please use:; Wilensky, U. (1998). NetLogo Wandering Letters model.; http://ccl.northwestern.edu/netlogo/models/WanderingLetters.; Center for Connected Learning and Computer-Based Modeling,; Northwestern University, Evanston, IL.;; In other publications, please use:; Copyright 1998 by Uri Wilensky. All rights reserved. See; http://ccl.northwestern.edu/netlogo/models/WanderingLetters; for terms of use.;; *** End of NetLogo Model Copyright Notice ***@#$#@#$#@GRAPHICS-WINDOW19310580418141413.01101110001CC-WINDOW5432589527Command Center
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?