Problem 3
In this exercise, your job is to get Karel to create a checkerboard pattern of beepers inside
an empty rectangular world, as illustrated in the following before-and-after diagram:
This problem has a nice decomposition structure along with some interesting algorithmic
issues. As you think about how you will solve the problem, you should make sure that
your solution works with checkerboards that are different in size from the standard 8x8
checkerboard shown in the example. Odd-sized checkerboards are tricky, and you should
make sure that your program generates the following pattern in a 5x3 world:
Another special case you need to consider is that of a world which is only one column
wide or one row high. The starter folder contains several sample worlds that test these
special cases, and you should make sure that your program works for each of them.
/* * File: CheckerboardKarel.java * ---------------------------- * When you finish writing it, the CheckerboardKarel class should draw * a checkerboard using beepers, as described in Assignment 1. You * should make sure that your program works for all of the sample * worlds supplied in the starter folder. */ import stanford.karel.*; public class CheckerboardKarel extends SuperKarel { public void run() { if(frontIsClear())//more than one avenue { /*go through the 1st street*/ while(frontIsClear()) { putBeeper(); move(); if(frontIsClear()) { move(); } } /*check the LAST one whether need to putBeeper()*/ turnAround(); move(); if(noBeepersPresent()) { turnAround(); move(); putBeeper(); } else { turnAround(); move(); } /*go through every avenue*/ while(notFacingNorth()) { turnRight(); } while(frontIsClear()) { goThroughAvenue(); /*facing WEST*/ while(notFacingWest()) { turnRight(); } /*to NEXT avenue*/ move(); } goThroughAvenue(); }else //ONLY one avenue { goThroughAvenue(); } } /*through a single avenue*/ private void goThroughAvenue() { /*turn to the north*/ while(notFacingNorth()) { turnRight(); } /*go north & put beepers*/ while(frontIsClear()) { /*beeper-none-style : move ,then move and check the next street to put beeper*/ if(beepersPresent()) { move(); if(frontIsClear()) { move(); putBeeper(); } } else /*none-beeper-style : move and check the next street to put beeper , then move*/ { move(); putBeeper(); if(frontIsClear()) { move(); } } } /* facing SOUTH */ while(notFacingSouth()) { turnRight(); } /* BACK */ while(frontIsClear()) { move(); } } } 这个效果更好,高效率:http://blog.csdn.net/goodpress/archive/2011/01/29/6169122.aspx/* * File: CheckerboardKarel.java * ---------------------------- * When you finish writing it, the CheckerboardKarel class should draw * a checkerboard using beepers, as described in Assignment 1. You * should make sure that your program works for all of the sample * worlds supplied in the starter folder. */ import stanford.karel.*; public class CheckerboardKarel_ extends SuperKarel { public void run(){ putBeeper(); putOneLineBeeper(); backBasePoint(); turnRight(); while(frontIsClear()){ putAPairBeeper(); turnRight(); putOneLineBeeper(); turnLeft(); if(frontIsClear()){ putAPairBeeper(); turnLeft(); putOneLineBeeper(); turnRight(); } } } //返回到第一行的原点 private void backBasePoint(){ turnAround(); while (frontIsClear()){ move(); } } //成对放置一行棋子 private void putOneLineBeeper(){ while(frontIsClear()){ putAPairBeeper(); } } //一黑一白成对放置棋子的 private void putAPairBeeper(){ if (beepersPresent()){ move(); } else { move(); putBeeper(); } } }
下午自己改进了一下~~~
/* * File: CheckerboardKarel.java * ---------------------------- * When you finish writing it, the CheckerboardKarel class should draw * a checkerboard using beepers, as described in Assignment 1. You * should make sure that your program works for all of the sample * worlds supplied in the starter folder. */ import stanford.karel.*; public class CheckerboardKarel extends SuperKarel { public void run() { putBeeper();//put 1st beeper at the (s1,a1) throughStreet();//go through the 1st street while(notFacingNorth())//face north { turnRight(); } while(frontIsClear())//through other streets , the start position is the last avenue of the 1st street { upStreet(); throughStreet(); while(notFacingNorth())//redeay to check whether has any more street { turnRight(); } } } private void upStreet()//to get to an upper street { couple();//get up to a new street if(leftIsClear())//decide the direction karel should turn to { turnLeft(); } else { turnRight(); } } private void throughStreet()//go through the whole street where karel is { while(frontIsClear())//cotains 3 steps :check front & move ,where need to put beeper,the another move { couple(); } } private void couple() { if(beepersPresent())//beeper-none-style { move(); } else//none-beeper-style { move(); putBeeper(); } } }