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. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章