Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. ·...

16
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004

Transcript of Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. ·...

Page 1: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

Lawrence  Snyder  University  of  Washington,  Seattle  

© Lawrence Snyder 2004

Page 2: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

   

2/20/15 © 2010-2015 Larry Snyder 2

The app works like the children’s toy. To move a piece, click on the one to be moved

Page 3: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

   

2/20/15 © 2010-2015 Larry Snyder 3

Page 4: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

   

2/20/15 © 2010-2015 Larry Snyder 4

Page 5: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

   

2/20/15 © 2010-2015 Larry Snyder 5

Page 6: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

   

2/20/15 © 2010-2015 Larry Snyder 6

Page 7: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

¡  Use  array  to  “hold  the  tiles”;  index  is  grid  position  

¡  How  does  it  work?  Exchange  values  on  a  move  

2/20/15 © 2010-2015 Larry Snyder 7

Initial Click 4 Click 5

Page 8: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

¡  The  tiles[  ]  array  is  initialized  with  digits,  but  it  could  have  been  assigned  in  a  loop  

¡  The  shade[  ]  array  is  just  a  series  of  cell  colors  …  it  is  initialized  in  a  loop  in  setup(  )  

   

2/20/15 © 2010-2015 Larry Snyder 8

Page 9: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

¡   We  can  now  draw  numbers  from  tiles[  ]  with  colors  from  shade[  ]  

2/20/15 © 2010-2015 Larry Snyder 9

Page 10: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

¡  convert  from  r/c  ref.s  to  array  index  ref.s  

2/20/15 © 2010-2015 Larry Snyder 10

Page 11: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

   

2/20/15 © 2010-2015 Larry Snyder 11

Page 12: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

¡  Like  the  birthday  array,  when  we  click  on  a  tile,  we  compute  the  row  and  column  as  row  =  (mouseY  -­‐  100)/50;  col      =  (mouseX  -­‐  100)/50;  

¡  If  the  present  open  position  is  at  ex,wy  then  what  are  legal    moves?  

§  The  row  or  col  can  change  by  1  but  not  both  §  Say  that  as:  abs(row-­‐ex)  +  abs(col-­‐wy)  ==  1  

2/20/15 © 2010-2015 Larry Snyder 12

Page 13: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

¡  And  if  (abs(row-­‐ex)  +  abs(col-­‐wy)  ==  1)    then  what?  §  Exchange  tiles[  ]  values  §  Exchange  shade[  ]  values  …  for  ex,wy  with  row,col  

¡  An  example  exchange  …  §  To  exchange  values  of  a  and  b  

   int  temp  =  a;      a  =  b;      b  =  temp;  

2/20/15 © 2010-2015 Larry Snyder 13

Standard Technique

Page 14: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

   

2/20/15 © 2010-2015 Larry Snyder 14

Page 15: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

   

2/20/15 © 2010-2015 Larry Snyder 15

Page 16: Lawrence(Snyder University(of(Washington,(Seattle · 2015. 2. 20. · Like!the!birthday!array,!when!we!click!on!a! tile,!we!compute!the!row!and!column!as! row!=!(mouseY!O100)/50;!

¡  Sketched  the  idea  on  paper  ¡  Started  with  simplest  stuff  ¡  Built  on  previous  work  by  adding  one  function  or  one  idea  at  a  time  

¡  Checked  after  every  improvement  

2/20/15 © 2010-2015 Larry Snyder 16