⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ---hammingerror.nlogo

📁 NETLOGO
💻 NLOGO
📖 第 1 页 / 共 2 页
字号:
; Hamming Error Detection Simulation ; written by Teresa Carrigan, 2004 globals [ from-number from-string start-x number-of-digits myDigits praise digits1 error-list         step parity bin-count-list error-found?] breeds [ digit arrow check ones] arrow-own [ state ]patches-own [ name column ]digit-own [ error? ]; runs the program when it is first loadedto startup  setupend; initializes variablesto setup  locals [ current n]  ca  set error-found? false  set praise ["Awesome!" "You got it!" "Right!" "Correct!" "Perfect!" "Bravo!" "Splendid!"]  set digits1 [ "0" "1" ]  set myDigits []    set n 0  set start-x 12  set parity "even"  set number-of-digits 12  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 add-space from-number " was sent using Hamming SEC (even).")      set plabel-color white      set name 1    ]  ask patch-at 6 -5    [      set plabel-color white      set plabel "Was there an error in transmission?"      set name 2    ]  set step 1  setup-bits from-number  setup-arrow  ask patches with [ plabel-color = cyan ]    [ set n plabel      set n remove " " n      set n remove "#" n      set n read-from-string n      set column n    ]endto step1  locals [ r p]  explain 1 "Determine the check bits."  set r 0  set p 1  while [ p <= number-of-digits ]    [  set r (r + 1)       explain 2 "C" + p + " goes in position " + p + "."       make-check-bit p       set p (2 * p)       wait slow-motion * 3    ]  explain 2 "C" + p + " would go in position " + p + " - no bit in that column."  wait slow-motion * 6  explain 2 "So, for " + number-of-digits + " total bits, there are " + r + " check bits."  set step (step + 1)  wait slow-motion * 4endto make-check-bit [ p ]  locals [ here-x here-y where phere-x n]  ask patches with [ column = p ]    [ set here-x pxcor      set here-y pycor    ]  set here-y here-y - 2  ask digit-at here-x here-y    [      set color yellow    ]endto step2  locals [ n k ]  explain 1 "Mark each position with the binary "  explain 2 "equivalent of the position number."  set bin-count-list ["0000" "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001" "1010" "1011" "1100" ]  set k 1  set n count turtles  while [ k <= n ]    [ wait slow-motion      ask patches with [ plabel = "#" + k + " " ]        [ ask patch-at 0 2            [  set plabel-color white               set plabel item k bin-count-list            ]         ]      set k (k + 1)    ]  set step (step + 1)endto step3  locals [ n check-set ]  explain 1 "C1 checks bits in positions marked ---1."  explain 2 ""  set check-set patches with [ column mod 2 = 1 ]  determine-parity check-set 1  ask ones [ die ]  set step step + 1endto step4  locals [ check-set ]      explain 1 "C2 checks bits in positions marked --1-."      set check-set patches with [ member? column [ 2 3 6 7 10 11] ]      determine-parity check-set 2      ask ones [ die ]      set step step + 1endto step5  locals [ check-set ]      explain 1 "C4 checks bits in positions marked -1--."      set check-set patches with [ member? column [ 4 5 6 7 12 13] ]      determine-parity check-set 4       ask ones [ die ]      set step step + 1endto step6  locals [ check-set ]      explain 1 "C8 checks bits in positions marked 1---."      set check-set patches with [ column >= 8 ]      determine-parity check-set 8       ask ones [ die ]      set step step + 1endto step7  finish-upendto determine-parity [ check-set ch ]  locals [ here-parity]  ask check-set    [  ask digit-at 0 -2          [hatch 1          [  set breed ones             set shape "circle"             set color yellow             set size 2             set label-color black          ]          ]    ]  wait slow-motion * 3  ask ones    [ set heading 180       repeat 4          [ fd 0.5            wait slow-motion          ]    ]  explain 2 "Ignore bits that are 0."  wait slow-motion * 3  ask ones with [ label = "0 " ]    [ die ]  ask arrow    [ set xcor start-x ]    while [ count ones with [ color = yellow ] > 0 ]    [   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                wait slow-motion             ]             [  explain 2 ""                fd 1                wait slow-motion             ]        ]          ]  wait slow-motion * 3  ask arrow    [ hideturtle ]  ifelse count ones = 0    [ set here-parity "even"       explain 1 "No ones left, so parity is  even."      explain 2 "No error in set of bits checked by C" + ch + "."    ]    [ set here-parity "odd"       explain 1 "A one left, so parity is currently odd."      explain 2 "Error in set of bits checked by C" + ch + "."      set-check-bit-error ch    ]  wait slow-motionendto set-check-bit-error [ ch ]  locals [ here-x ]  ask patches with [ column = ch ]    [ set here-x pxcor ]  ask digit with [ xcor = here-x ]    [  set error? true        set color red       set label-color white    ]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 n]  set here-x ( start-x - 2)  set here-y 1  set n 1  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)  " ")        set error? false      ] ; end cct digits      ask patch-at here-x (here-y + 2)        [  set plabel-color cyan           set plabel "#" + n + " "        ]      set unsigned but-last unsigned      set here-x (here-x - 2)         set n (n + 1)      wait slow-motion    ]  endto finish-up  set error-list [ ]  ask digit with [ error? = true ]    [ without-interruption [ask patch-at 0 2        [ set error-list lput column error-list ]        ]     ]  ifelse length error-list = 0    [  explain 1 "No errors detected."       explain 2 ""       set error-found? false       set step (step + 3)    ]    [  explain 1 "Errors detected with check bits " + error-list + "."       set error-found? true       set step (step + 1)    ]  wait slow-motionendto step8  locals [ col ]  set col sum error-list  explain 2 "Sum of error positions = " + col + "."  wait slow-motion * 3  ifelse col > number-of-digits    [  explain 2 col + " > " + number-of-digits + ", so multiple errors occurred.  Can't fix."       set step (step + 3)    ]    [  explain 2 "Single error in position " + col + "."       set step step + 1    ]endto step9  locals [ col ]  set col sum error-list  explain 1 "Fixing error in position " + col + "."  explain 2 ""  flip col  wait slow-motion  explain 1 "Error has been fixed."  wait slow-motion  set step step + 1endto step10  locals [ here-x ]  explain 1 "Strip the check bits, so we can see the data."  wait slow-motion  ask patches with [ member? column [ 1 2 4 8 16 ] ]    [  ask digit-at 0 -2          [ die ]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -