Posted By

MediaTechWare on 04/24/13


Tagged

programming Stanford university Methodology CS106A Karel


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

MediaTechWare


CheckerboardKarel


 / Published in: Java
 

This code solves the CheckerboardKarel problem presented in Assignment 1 of the Stanford University CS106A Programming Methodology course.

  1. /*
  2.  * File: CheckerboardKarel.java
  3.  * ----------------------------
  4.  * The CheckerboardKarel class draws a checkerboard using beepers,
  5.  * as described in Assignment 1. The program works for all of the sample
  6.  * worlds supplied in the starter folder.
  7.  */
  8.  
  9. import stanford.karel.*;
  10.  
  11. public class CheckerboardKarel extends SuperKarel {
  12.  
  13. /* Karel starts on the corner of 1st St. and 1st Ave. at the beginning
  14. * of the program. Conceptually, the streets are separated as ones with odd
  15. * numbers and even numbers. Odd numbered streets start with a beeper on the first
  16. * corner, while even numbered streets start with a beeper on the second corner
  17. * to create the checkerboard effect. Karel lays the beepers on the first street through
  18. * the layOddStreet() command. At the end of the street, Karel is left facing East. If
  19. * at this point Karel detects that there is another street above, Karel moves all the
  20. * way to the beginning of the street, moves one street up and lays beepers, this time
  21. * for through the layEvenStreet() command. Karel alternates laying beepers on odd and
  22. * even numbered streets until it detects that there are no streets above.*/
  23. public void run() {
  24. layOddStreet();
  25. while(leftIsClear()) {
  26. getBackAndMoveOneStreetUp();
  27. layEvenStreet();
  28. if(leftIsClear()) {
  29. getBackAndMoveOneStreetUp();
  30. layOddStreet();
  31. }
  32. }
  33.  
  34. }
  35. /* The layOddStreet command assumes Karel is on one of the odd numbered streets.
  36. * Karel starts with laying down a beeper, and then carries on skipping one corner
  37. * and laying a beeper on the next corner until it detects a wall. At the end of
  38. * the execution of the command, Karel is left immediately against the wall at the
  39. * end of the street, facing East.
  40. */
  41. private void layOddStreet() {
  42. putBeeper();
  43. while(frontIsClear()) {
  44. move();
  45. if(frontIsClear()) {
  46. move();
  47. putBeeper();
  48. }
  49.  
  50. }
  51. }
  52. /* The layEvenStreet() command assumes Karel is on one of the even numbered streets.
  53. * Karel starts with moving one corner ahead if it detects that its front is clear.
  54. * From here, Karel treats the remainder of the street as an odd numbered street and
  55. * executes the layOddStreet() command.
  56. */
  57. private void layEvenStreet() {
  58. while(frontIsClear()) {
  59. move();
  60. layOddStreet();
  61. }
  62. }
  63.  
  64. /* This command assumes Karel is standing at the end of an odd or even numbered street
  65. * facing East. The command turns Karel around and moves him until he reaches the
  66. * western wall of the room. When he reaches the wall, Karel turns right, moves one corner
  67. * up and turns right once more to face East again.*/
  68. private void getBackAndMoveOneStreetUp() {
  69. turnAround();
  70. while(frontIsClear()) {
  71. move();
  72. }
  73. turnRight();
  74. move();
  75. turnRight();
  76. }
  77.  
  78. }

Report this snippet  

You need to login to post a comment.