Advertisement
Guest User

Untitled

a guest
Jan 13th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. //----------------------------------------
  2. //Java:
  3.  
  4. Location l = location;
  5. int x = l.x;
  6. int y = l.y;
  7.  
  8. switch( direction % 8 ) {
  9. case 0 :
  10. // North
  11. y = l.y > 0 ? l.y - 1 : l.y;
  12. break;
  13. case 1 :
  14. // South
  15. y = l.y < ( maze.getHeight( ) - 1 ) ? l.y + 1 : l.y;
  16. break;
  17. case 2 :
  18. // West
  19. x = l.x > 0 ? l.x - 1 : l.x;
  20. break;
  21. case 3 :
  22. // East
  23. x = l.x < ( maze.getWidth( ) - 1 ) ? l.x + 1 : l.x;
  24. break;
  25. case 4 :
  26. // North-east
  27. x = l.x < ( maze.getWidth( ) - 1 ) ? l.x + 1 : l.x;
  28. y = l.y < ( maze.getHeight( ) - 1 ) ? l.y + 1 : l.y;
  29. break;
  30. case 5 :
  31. // North-west
  32. x = l.x > 0 ? l.x - 1 : 0;
  33. y = l.y < ( maze.getHeight( ) - 1 ) ? l.y + 1 : l.y;
  34. break;
  35. case 6 :
  36. // South-west
  37. x = l.x > 0 ? l.x - 1 : l.x;
  38. y = l.y > 0 ? l.y - 1 : l.y;
  39. break;
  40. case 7 :
  41. // South-east
  42. x = l.x < ( maze.getWidth( ) - 1 ) ? l.x + 1 : l.x;
  43. y = l.y > 0 ? l.y - 1 : l.y;
  44. break;
  45. default :
  46. throw new RuntimeException( "This is a bug" );
  47. }
  48. Location newLocation = new Location( x,y );
  49.  
  50.  
  51.  
  52. //----------------------------------------
  53. //F#:
  54.  
  55. let l = location
  56. let newLocation =
  57. match direction % 8 with
  58. | 0 -> (fst l), (snd l)-1 //North
  59. | 1 -> (fst l), (snd l)+1 //South
  60. | 2 -> (fst l)-1, (snd l) //West
  61. | 3 -> (fst l)+1, (snd l) //East
  62. | 4 -> (fst l)+1, (snd l)-1 //North-East
  63. | 5 -> (fst l)-1, (snd l)-1 //North-West
  64. | 6 -> (fst l)+1, (snd l)+1 //South-East
  65. | 7 -> (fst l)-1, (snd l)+1 //South-West
  66. | _ -> failwith("This is a bug")
  67.  
  68.  
  69. //----------------------------------------
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement