Stanford编程方法学公开课作业 2 ---- Karel中生成棋盘的代码

解决如下图所示问题:(生成间隔棋盘状图案)

要求程序可以在 各类形状的地图中正常运行(如1x8 8x1 5x6 6x6 7x7 等等)
 
代码如下:
 
    
  1. /*
  2. * File: CheckerboardKarel.java
  3. * ----------------------------
  4. * The CheckerboardKarel class is able to draw
  5. * a checkerboard using beepers, as described in Assignment 1. You
  6. * should make sure that your program works for all of the sample
  7. * worlds supplied in the starter folder.
  8. */
  9. import stanford.karel.*;
  10. public class CheckerboardKarel extends SuperKarel {
  11. public void run() {
  12. goToOrigin();
  13. genReferenceRow(); //generate a reference row, choose 1st row as reference
  14. toFaceNorth(); //to face north
  15. while ( frontIsClear() ) {
  16. if ( beepersPresent() ) {
  17. moveToNextRow();
  18. if ( frontIsClear() ) {
  19. move();
  20. genReferenceRow();
  21. } //to deal with world with only one column
  22. } else {
  23. moveToNextRow();
  24. genReferenceRow();
  25. }
  26. toFaceNorth();
  27. } //end of while loop
  28. } //end of main method
  29. /*
  30. * Make Karel move to the next row without changing column number
  31. * Karel is always moving toward north part
  32. * Precondition: facing north
  33. * Postcondition: facing east/west depending on Karel is standing on
  34. * left end or right end
  35. */
  36. private void moveToNextRow() {
  37. if ( leftIsBlocked() ) { //at left end wall of the world
  38. move();
  39. turnRight(); //to face east
  40. } else { //at right end wall of the world
  41. move();
  42. turnLeft(); //to face west
  43. } // end of if-else
  44. } // end of moveToNextRow()
  45. /*
  46. * Is defined to generate one row of beepers, beepers are exactly 2 units apart
  47. * Precondition: any case
  48. * Postcondition: arrive at the end of one row
  49. */
  50. private void genReferenceRow() {
  51. if ( frontIsBlocked() ) {
  52. putBeeper(); //dealing with the world with only one column
  53. }
  54. while ( frontIsClear() ) {
  55. putBeeper();
  56. move(); //enter while means can move forward at least one step
  57. if ( frontIsClear() ) {
  58. //if width is even, second move could not be made
  59. move(); //second move
  60. //if width is odd, second move should make, one beeper should be added
  61. //in front of wall
  62. if ( frontIsBlocked() ) {
  63. putBeeper();
  64. }
  65. }
  66. }
  67. }
  68. /*Move Karel back to origin of map (1,1), make it facing east
  69. * Precondition: can be any case
  70. * Postcondition: standing on (1,1) facing east
  71. */
  72. private void goToOrigin() {
  73. while (notFacingSouth()) {
  74. turnLeft();
  75. }
  76. moveToWall();
  77. while (notFacingWest()) {
  78. turnLeft();
  79. }
  80. moveToWall();
  81. while (notFacingEast()) {
  82. turnLeft(); //Karel will face east
  83. }
  84. }
  85. private void moveToWall() { //move Karel to wall
  86. while (frontIsClear()) {
  87. move();
  88. }
  89. }
  90. private void toFaceNorth() {
  91. while ( notFacingNorth() ) {
  92. turnLeft();
  93. }
  94. }
  95. }

你可能感兴趣的:(编程方法学作业讨论,Stanford公开课)