Open courses at Stanford University--programming method Job 1-3__ programming

Source: Internet
Author: User

Problem 3
In this exercise, the your job is to the Karel to create a checkerboard pattern of beepers inside a empty the world, As illustrated in the following Before-and-after diagram:

This is a nice decomposition structure along with some interesting algorithmic issues problem has. As you are about how to solve the problem, should make sure this your solution works with Checkerboards Re different in size from the standard 8x8 checkerboard shown in the example. Odd-sized checkerboards are tricky, and you should make sure this your program generates the following pattern in a 5x3 wo Rld

Another special case your need to consider are that's a world which are only one column wide or one row. The Starter folder contains several sample worlds that test this special cases, and you should make sure this your progra M works for each of the them.

Program Source code:

* * * File:CheckerboardKarel.java *----------------------------* When you finish writing it, the Checkerboardkarel class Should draw * A checkerboard using beepers, as described in Assignment 1. You are * should make sure which your program works to all of the sample * worlds supplied in the Starter folder. * * Import stanford.karel.*; public class Checkerboardkarel extends Superkarel {public void run () {int. num=0; Putbeeper (); while (true) {num = Checkerr ow (num); TurnLeft (); num = Repositionforrow (num); if (0 = num) break; TurnLeft (); num = Checkerrow (num); TurnRight (); num = Repositionforrow (num); if (0 = num) break; TurnRight (); }/* Place beeper as required in one line and return the node numbers */private int checkerrow (int num) {while (Frontisclear ()) {move (); num++; if (num%2==0) {PU Tbeeper (); } return num; }/* To move forward processing, return the node numbers, 0 means all have been completed/private int repositionforrow (int num) {if (Frontisclear ()) {moving (); num++; if (num%2==0) { Putbeeper (); } else{num = 0;} return num; } }

result video address shown: http://v.youku.com/v_show/id_XMjI5NTUyODQ0.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.