Rubiks Cube
Model was written in NetLogo 5.2.1
•
Viewed 216 times
•
Downloaded 13 times
•
Run 0 times
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
Info tab cannot be displayed because of an encoding error
Comments and Questions
Please start the discussion about this model!
(You'll first need to log in.)
Click to Run Model
breed [kernelsx kernelx] ; turtles in the center of the cube used to save orientation breed [kernelsy kernely] globals [mx ;saved mouse x-cor my ;saved mouse y-cor displacex ;difference between the saved mouse-xcor and current mouse-xcor displacey ;difference between the saved mouse-ycor and current mouse-ycor v;difference between ovtheta and vtheta t ;difference between otheta and theta scrambled ;checks if the cube has been scrambled reddone ;checks if red face is done bluedone ;checks if blue face is done whitedone ;checks if white face isdone yellowdone ;checks if yellow face is done greendone ;checks if green face is done orangedone ;checks if orange face is done ] turtles-own [ x-pos ;; x-pos, y-pos, and z-pos are the cartesian coordinates y-pos ;; don't confuse them with xcor and ycor, which are predefined z-pos ;; NetLogo variables for turtles p ;distance of turtle from origin rtheta ;angle of turtle projection in the z-y plane from x-axis (spherical) theta ; angle of the turtle’s projection on the x-y plane. phi ; turtles angle of incidence to the z axis. vtheta ;angle of the turtle’s projection on the x-z plane q ;distance from y-axis r ;distance from x-axis s ;distance from z-axis rsign ;side of the y-axis on which the turtles are located sign ; which side of x-axis (spherical) otheta; original theta value (changes upon rotation to reflect the rotation that took place) ovtheta ;original vtheta value (changes upon rotation) ortheta ;original rtheta value oq ;original q value orr ;original r value os ;original s value ] to mouse ;this procedure allows user to view the cube by dragging it using his mouse set mx mouse-xcor ;sets mx and my to current mouse coordinates set my mouse-ycor if mouse-down? [if mouse-xcor != mx ;if mouse-xcor changes [ifelse mouse-xcor < mx ;direction of change [set displacex mx - mouse-xcor ;amount of change ask turtles [set theta (theta - abs velocity * displacex) mod 360 ;rotate cube to reflect the change render-turtle cartesian x-pos y-pos z-pos scale] tick] [set displacex mouse-xcor - mx ask turtles [set theta (theta + abs velocity * displacex) mod 360 ;; same for all the others render-turtle cartesian x-pos y-pos z-pos scale] tick] ] if mouse-ycor != my [ifelse mouse-ycor < my [set displacex my - mouse-ycor ask turtles [set vtheta (vtheta + (abs velocity * displacey)) mod 360 vrender-turtle cartesian x-pos y-pos z-pos scale] tick] [set displacey mouse-ycor - my ask turtles [set vtheta (vtheta - (abs velocity * displacey)) mod 360 vrender-turtle cartesian x-pos y-pos z-pos scale] tick] ]] end to vgo ;vertical rotation of cube ask turtles [ set vtheta (velocity + vtheta) mod 360 ; increment vtheta to simulate vertical rotation vrender-turtle;renders turtle new position in cube cartesian x-pos y-pos z-pos];updates distances and angles tick end to go ; horizontal rotation of cube ask turtles [ set theta (theta + velocity) mod 360 ; increment theta to simulate rotation render-turtle cartesian x-pos y-pos z-pos ] tick end to setup ;Rubik’s cube spawns clear-all set-default-shape turtles "circle" create-turtles num-turtles [set size 1 ifelse y-pos > 0 [set sign 1] ;set signs [set sign -1] ifelse x-pos > 0 [set rsign 1] [set rsign -1] let temp-alpha 35 * (1 - 2 * (random 2)) ; +-35 ; random distribution bounded by +-35 let temp-beta 35 - 2 * (random-float 35) let temp-gamma (random 3) ; front/back or left/right? ifelse temp-gamma = 0 [ ifelse temp-alpha > 0 [set color orange];front is orange [set color red ] ;back is red ; generate front & back surrotate cartesian (temp-alpha) (temp-beta) (35 - (2 * (random-float 35))) ] [ ifelse temp-alpha > 0 [set color blue];right is blue [set color green ];left is green ifelse random 2 = 1 [cartesian (temp-beta) ; generating the side surrotate (temp-alpha) (35 - (2 * (random-float 35))) ] [ ifelse temp-alpha > 0 [set color yellow];above is yellow [set color white ] ;bottom is white ;generate top and bottom surrotate cartesian (35 - (2 * (random-float 35))) (temp-beta) (temp-alpha) ]] ] create-kernelsx 1 [set x-pos 10 ;orients one of the kernels to the x axis set y-pos 0.01 set z-pos 0.01 set hidden? true cartesian x-pos y-pos z-pos ] create-kernelsy 1 [set y-pos 10 ;the other is orineted to the y axis set x-pos 0.01 set z-pos 0.0 set hidden? true cartesian x-pos y-pos z-pos ] ask turtles [ render-turtle set ortheta rtheta ;;save all the original values set otheta theta set ovtheta vtheta set oq q set orr r set os s ] set scrambled 0 set reddone 0 set bluedone 0 set whitedone 0 set yellowdone 0 ;it hasn't been scrambled yet and none of the sides have been solved yet set greendone 0 set orangedone 0 reset-ticks end to cartesian [x y z] ;;updates angles and distances set r sqrt((y ^ 2) + (z ^ 2)) set p sqrt((x ^ 2) + (y ^ 2) + (z ^ 2)) set q sqrt((x ^ 2) + (z ^ 2)) set s sqrt((y ^ 2) + (x ^ 2)) set phi (atan sqrt((x ^ 2) + (y ^ 2)) z) set theta (atan y x) set vtheta (atan x z) set rtheta (atan y z) end to render-turtle ;wrapper function calculate-turtle-position scale set-turtle-position end to vrender-turtle ;similar wrapper function to be used in vertical rotation vcalculate-turtle-position scale set-turtle-position end to rrender-turtle ;to be used in rotation around the x-axis rcalculate-turtle-position scale set-turtle-position end ;; convert from spherical to cartesian coordinates after horizontal rotation to calculate-turtle-position set y-pos p * (sin phi) * (sin theta) set x-pos p * (sin phi) * (cos theta) set z-pos p * (cos phi) end ;; convert from spherical to cartesian coordinates after vertical rotation to vcalculate-turtle-position ifelse y-pos > 0 [set sign 1] [set sign -1] set y-pos (sqrt ((p ^ 2) - (q ^ 2))) * sign set x-pos q * (sin vtheta) set z-pos q * (cos vtheta) end ;; convert from spherical to cartesian coordinates rotation around the x-axis to rcalculate-turtle-position ifelse x-pos > 0 [set rsign 1] [set rsign -1] set x-pos sqrt (p ^ 2 - r ^ 2) * rsign set y-pos r * (sin rtheta) set z-pos r * (cos rtheta) end ;; set the turtle's position using netlogo coordinates to set-turtle-position setxy y-pos z-pos end to resetx ifelse ovtheta > vtheta [set v (ovtheta - vtheta)] [set v (vtheta - ovtheta)] ;to set v end to resety ifelse otheta > theta [set t (otheta - theta)] [set t (theta - otheta)] ;to set t end ;;;FACE ROTATION;;;; to rotate ask kernelsx [resetx] ;uses the kernels to set t and c ask kernelsy [resety] ask turtles [ rthis ;wrappers that return the cube to previous orientation this that ] tick if rotate-color = "blue" ;chooses which side to rotate based on the chooser [rotate-blue] ;and calls in the appropriate helper if rotate-color = "green" [rotate-green] if rotate-color = "white" [rotate-white] if rotate-color = "yellow" [rotate-yellow] if rotate-color = "red" [rotate-red] if rotate-color = "orange" [rotate-orange] if (count turtles with [shade-of? white color and z-pos < -34.9] > (8.3 / 54) * count turtles) [set whitedone 1] if (count turtles with [shade-of? yellow color and z-pos > 34.9] > (8.3 / 54) * count turtles) [set yellowdone 1] if (count turtles with [shade-of? orange color and x-pos > 33.9] > (8.3 / 54) * count turtles) [set orangedone 1] if (count turtles with [shade-of? red color and x-pos < -34.9] > (8.3 / 54) * count turtles) [set reddone 1] if (count turtles with [shade-of? green color and y-pos < -33.9] > (8.3 / 54) * count turtles) [set greendone 1] if (count turtles with [shade-of? blue color and y-pos > 34.9] > (8.3 / 54) * count turtles) [set bluedone 1] ;sets the “done” function to 1 when the side is completed if scrambled = 1 and whitedone = 1 and yellowdone = 1 and orangedone = 1 and reddone = 1 and greendone = 1 and bluedone = 1 [ask patches [set pcolor white]] ;sets the background to white if the cube is completed after being scrambled ask turtles with [breed != kernelsx and breed != kernelsy] ;resets the original values to reflect the rotation [set oq q set orr r set os s set ortheta rtheta set otheta theta set ovtheta vtheta] ask turtles [set theta (theta + t) mod 360; calculate-turtle-position cartesian x-pos y-pos z-pos] tick ;returns the cube to its previous orientation ask turtles [set vtheta (vtheta + v) mod 360 vrender-turtle cartesian x-pos y-pos z-pos scale] tick end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to rotate-blue ask turtles [ if y-pos > 11.83 [set vtheta vtheta + 90 ;rotates each side by determining the coordinates that ;are required for the turtle to be part of the chosen side ;and then rotating the required angle 90 degrees vcalculate-turtle-position cartesian x-pos y-pos z-pos]] tick end to rotate-green ask turtles [ if y-pos < -11.83 [set vtheta vtheta + 90 ] vcalculate-turtle-position cartesian x-pos y-pos z-pos] tick end to rotate-white ask turtles [ if z-pos < -11.83 [set theta theta + 90 ] calculate-turtle-position cartesian x-pos y-pos z-pos] tick end to rotate-yellow ask turtles [ if z-pos > 11.83 [set theta theta + 90 ] calculate-turtle-position cartesian x-pos y-pos z-pos] tick end to rotate-red ask turtles [ if x-pos < -11.83 [set rtheta rtheta + 90 ] rcalculate-turtle-position cartesian x-pos y-pos z-pos] tick end to rotate-orange ask turtles [ if x-pos > 11.83 [set rtheta rtheta + 90 ] rcalculate-turtle-position cartesian x-pos y-pos z-pos] tick end to that ;this, that, and rthis turns the cube to an easy to maneuver position ;sets center pieces to setup orientation and everything else relatively set q oq set vtheta ovtheta vcalculate-turtle-position cartesian x-pos y-pos z-pos end to this set theta otheta calculate-turtle-position cartesian x-pos y-pos z-pos end to rthis set r orr set rtheta ortheta rcalculate-turtle-position cartesian x-pos y-pos z-pos end to scale set color scale-color color x-pos -35 85 ;creates illusion of depth; the further back a turtle, end ;the darker it is to scramble [x] ;scrambles cube ask patches [set pcolor black] set reddone 0 ; sets all sides as not done set bluedone 0 set whitedone 0 set yellowdone 0 set greendone 0 set orangedone 0 set scrambled 1 repeat scramble_times [ ;rotates a random side scramble_times times set x random 6 if x = 0 [set rotate-color "red"] if x = 1 [set rotate-color "orange"] if x = 2 [set rotate-color "yellow"] if x = 3 [set rotate-color "blue"] if x = 4 [set rotate-color "white"] if x = 5 [set rotate-color "green"] rotate ] end
There is only one version of this model, created almost 7 years ago by Yiduo Ke.
This model does not have any ancestors.
This model does not have any descendants.