📄 parity.nlogo
字号:
; Parity Simulation Model ; written by Teresa Carrigan, 2004 globals [ from-number from-string start-x myDigits praise digits1 save-base base step ] breeds [ digit arrow ones ] arrow-own [ state ]patches-own [ name ]; runs the program when it is first loadedto startup setupendto random-setup set parity random-one-of [ "even" "odd" ] set number-of-digits 1 + random 8 set placement random-one-of [ "far left" "far right" ] setupend; initializes variablesto setup locals [here-x current n] ca set praise ["Awesome!" "You got it!" "Right!" "Correct!" "Perfect!" "Bravo!" "Splendid!"] set digits1 [ "0" "1" ] set base 2 set save-base base set myDigits [] set n 0 set start-x 9 set here-x 7 set current 1 set n 0 repeat 2 [ set myDigits lput ( item n digits1 ) myDigits set n (n + 1) ] set n 1 ; initialize binary number set from-number "" repeat number-of-digits [ set from-number (word from-number (random-one-of myDigits)) ] ; create explanation bar at bottom ask patches with [ pycor < -3 ] [set pcolor blue] ask patch-at 6 -4 [ set plabel (word "How should we send " add-space from-number) set plabel-color white set name 1 ] ask patch-at 6 -5 [ set plabel-color white set plabel "using " + parity + " parity?" set name 2 ] set step 1 setup-bits from-number setup-arrowendto step1 explain 1 "Ignore the 0 bits." explain 2 "" ask digit with [ label = "1 " ] [ hatch 1 [ set breed ones set shape "circle" set color yellow set size 2 set label-color black ] ] ask ones [ set heading 180 repeat 4 [ fd 0.5 wait slow-motion ] ] set step step + 1endto step2 ifelse count ones with [ color = yellow ] = 0 [ set step step + 1] [ ask arrow [ showturtle ifelse any? other-ones-here [ ask other-ones-here [ set color red ] if count ones with [color = red] = 2 [ explain 2 "Dropping a pair of ones." ask ones with [color = red] [ die ] wait slow-motion ] fd 1 ] [ explain 2 "" fd 1 ] ] ]end ; read the digits, storing it as a stringto-report get-number locals [ target n num] set target "" ask max-one-of digit [ xcor ] [set n xcor ] repeat count digit [ set num label-of random-one-of digit with [ xcor = n ] set target (word num target) set n (n - 2) ] set target remove " " target report targetend; adds a space in the number so it can be read easierto-report add-space [ number ] locals [ save k ] set save "" set k 0 while [ (length number) > 0 ] [ set save (word last number save ) set number butlast number set k (k + 1) if (k = 4) and (length number > 0) [ set save (word " " save ) set k 0 ] ] set number save report numberend; this is for the bottom explanationto explain [ which what ] ask patches with [ name = which ] [ set plabel what ]end; setup the red arrowto setup-arrow locals [here-y ] ask random-one-of digit [ set here-y ycor ] ; create red arrow cct-arrow 1 [ hideturtle setxy start-x ( here-y - 2) set heading -90 set color red set shape "arrow" set size 2 set state 0 ]end; setup the bitsto setup-bits [ unsigned ] locals [ here-x here-y] set here-x ( start-x - 2) set here-y 2 repeat number-of-digits [ cct-digit 1 [ setxy here-x here-y set shape "circle" set color white set size 2 set label-color black set label (word (last unsigned) " ") ] ; end cct digits set unsigned but-last unsigned set here-x (here-x - 2) wait slow-motion ] endto step3 locals [ here-parity ] ask arrow [ hideturtle ] ifelse count ones = 0 [ set here-parity "even" explain 1 "No ones left, so parity is currently even." ] [ set here-parity "odd" explain 1 "A one left, so parity is currently odd." ] explain 2 "" wait slow-motion set step step + 1endto make-parity-bit [ x ] locals [ here-x ] ask random-one-of digit [ set here-x xcor ] cct-digit 1 [ setxy here-x 0 set shape "circle" set color yellow set size 2 set label x + " " set label-color black ]endto step4 locals [ here-parity ] ifelse count ones = 0 [ set here-parity "even" ] [ set here-parity "odd" ask ones [ die ] ] ifelse here-parity = parity [ explain 2 "We want " + parity + " so we just add a 0." make-parity-bit 0 ] [ explain 2 "We want " + parity + " so we must add a 1." make-parity-bit 1 ] wait slow-motion set step step + 1endto step5 locals [ to-x to-y ] ifelse placement = "far left" [ ask min-one-of digit [ xcor ] [ set to-x xcor set to-y ycor ] set to-x (to-x - 2) ] [ ; far right ask max-one-of digit [ xcor ] [ set to-x xcor set to-y ycor ] set to-x (to-x + 2) ] ask digit with [ color = yellow ] [ showturtle ifelse to-x < xcor [ set heading -90 ] [ set heading 90 ] while [ xcor != to-x ] [ fd 1 wait slow-motion / 2 ] set heading 0 repeat 4 [ fd 0.5 wait slow-motion / 2 ] ] set step (step + 1)endto step6 explain 1 "Original bits = " + add-space from-number + "." explain 2 parity + " parity, placed at " + placement + " = " + add-space get-number set step 10end; cleans up the user inputto-report clean-input [guess] locals [ pos k upper lowerList] set guess remove " " guess set guess remove "," guess report guess end; creates a random quiz quesiton and praise if it is right and says its wrong if it isto ask-question locals [ guess save-slow target line] set line (word "How should we send " from-number " using " parity " parity,\n") set line (word line "with the parity bit at the " placement "?") set guess user-input line set guess (word " " guess " " ) set guess clean-input guess set save-slow slow-motion set slow-motion 0 while [ step < 10 ] [ go ] set slow-motion save-slow without-interruption [ set target get-number ] ifelse guess = target [ user-message random-one-of praise ] [ user-message "I'm sorry, but the correct answer is " + add-space target + "."]end; calls the quiz when the button is pressedto quiz locals [ save-slow ] set save-slow slow-motion set slow-motion 0 without-interruption [random-setup ] wait .5 ask-question set slow-motion save-slowend; flips the digits (called by the invert procedure)to flip if any? other-digit-here [ ask other-digit-here [ set color yellow ifelse first label = "0" [ set label "1 " ] [ set label "0 " ] ] ]end; do whatever step comes next, then wait until user wants to continueto one-step locals [ which ] if step < 10 [ set which (word "step" step) run which ]end; do all remaining stepsto go ifelse step < 10 [ one-step wait slow-motion ] [ stop ]endto show-again locals [here-x current n] set start-x 9 set here-x 7 set current 1 set n 1 ask turtles [ die ] ask patches [ set plabel "" ] explain 1 (word "How should we send " add-space from-number) explain 2 "using " + parity + " parity?" set step 1 setup-bits from-number setup-arrowend; *** NetLogo Model Copyright Notice ***;; Copyright 2004 by Teresa W. Carrigan. 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 Teresa W. Carrigan.; Contact Teresa W. Carrigan for appropriate licenses for redistribution ; for profit.;; To refer to this model in academic publications, please use:; Carrigan, T. (2004). Parity Simulation model.; Blackburn College, Carlinville, IL.;; In other publications, please use:; Copyright 2004 by Teresa W. Carrigan. All rights reserved. ;; *** End of NetLogo Model Copyright Notice ***@#$#@#$#@GRAPHICS-WINDOW131054831610525.0118111CC-WINDOW27459564629Command CenterBUTTON1331876351NILsetupNIL1TOBSERVERTSLIDER216315372348slow-motionslow-motion010.50.11NILBUTTON1335176384stepone-stepNIL1TOBSERVERTBUTTON77352140385NILgoT1TOBSERVERTSLIDER376317532350number-of-digitsnumber-of-digits18811bitsCHOICE216350373395parityparity"even" "odd"1BUTTON77317140350randomrandom-setupNIL1TOBSERVERTCHOICE377351531396placementplacement"far right" "far left"1BUTTON141352214385NILquizNIL1TOBSERVERTBUTTON141317214350NILshow-againNIL1TOBSERVERT@#$#@#$#@WHAT IS IT?-----------This model demonstrates storing of bit patterns using either even or odd parity. HOW IT WORKS------------A random bit pattern is generated, with the number of bits specified by the number-of-digits slider. Any bit that is a 1 will hatch a yellow copy of itself. The red arrow then discards pairs of yellow 1 bits. If there are no yellow bits left over, the current parity is even; if there is a bit left over, the current parity is odd.Finally, one extra bit is added to the original bit pattern. If the current parity is the same as the specified parity, then we add a 0 bit. If the current parity is incorrect, we add a 1 bit.HOW TO USE IT-------------The setup button generates a random bit pattern using the number of bits specified by the number-of-digits slider.The random button generates random settings for the number-of-digits slider, parity choice box, and placement choice box. Then a random bit pattern is generated.The step button demonstrates the next step, and then stops so you can take notes. This is useful when you are first learning the method.The go button does every remaining step, at a speed determined by the slow-motion slider. This is useful when you do not need to take notes between each step.The show-again button starts the exact problem from the beginning. You may then click either the step button or the go button to see the same demonstration.The slow-motion slider is an easy way to adjust the speed of the display. Set it to zero if you want to show the final result as quickly as possible. 0.5 is a good setting for most purposes.The number-of-digits slider specifies the number of bits for the random bit pattern generated when the setup button is pushed.The quiz button will generate a random bit pattern and ask the user how it would be sent using a specified parity.The parity choice box specifies whether even or odd parity will be demonstrated.The placement choice box specifies whether the parity bit added at the end will be placed at the far right or the far left of the other bits.THINGS TO NOTICE----------------Any bits that are zero are always ignored. The number of bits does not change the parity either.The receiver must know not only whether the message was sent using even or odd parity, but also whether the parity bit is the first or last bit.THINGS TO TRY-------------Set slow-motion to 0.5, click random, and then click go.Set the parity and placement to a type you wish to drill, and click setup. Work each step by hand, and then click the step button to check your answer.EXTENDING THE MODEL-------------------Allow the user to input the starting bit pattern.NETLOGO FEATURES----------------one-step uses the NetLogo run command combined with a global integer variable step to run the next step, without needing nested ifelse blocks. without-interruption runs all the commands in the block without allowing other agents to "interrupt". That is, other agents are put "on hold" and do not execute any commands until the commands in the block are finished. RELATED MODELS--------------Parity Error Detection SimulationCREDITS AND REFERENCES----------------------This model was written by Teresa W. Carrigan, 2004. 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 Teresa W. Carrigan.Contact Teresa W. Carrigan for appropriate licenses for redistribution for profit.To refer to this model in academic publications, please use: Carrigan, T. (2004). Parity Simulation model. Blackburn College, Carlinville, IL.In other publications, please use: Copyright 2004 by Teresa W. Carrigan. All rights reserved. FOR MORE INFORMATION--------------------For more information on parity, see:Null, L. and Lobur, J. "Essentials of Computer Organization and Architecture", First Edition. Jones and Bartlett. page 65.@#$#@#$#@defaulttrue0Polygon -7566196 true true 150 5 40 250 150 205 260 250arrowtrue0Polygon -7566196 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150boxtrue0Polygon -7566196 true true 45 255 255 255 255 45 45 45circlefalse0Circle -7566196 true true 35 35 230@#$#@#$#@NetLogo 2.0.1@#$#@#$#@@#$#@#$#@@#$#@#$#@
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -