{"id":948,"date":"2010-05-10T11:19:53","date_gmt":"2010-05-10T11:19:53","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=948"},"modified":"2011-01-14T14:01:11","modified_gmt":"2011-01-14T14:01:11","slug":"problem-3-2","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/10\/problem-3-2\/","title":{"rendered":"Problem 3"},"content":{"rendered":"<p><span id=\"zipfile\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/Assignment1_Code.zip\"><\/a><\/span>The problem 3 is &#8220;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:&#8221;<\/p>\n<figure id=\"attachment_949\" aria-describedby=\"caption-attachment-949\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/karel_checkboard.jpeg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/karel_checkboard.jpeg\" alt=\"Karels checkboard\" title=\"karel_checkboard\" width=\"450\" height=\"250\" class=\"size-full wp-image-949\" \/><\/a><figcaption id=\"caption-attachment-949\" class=\"wp-caption-text\">Karels checkboard<\/figcaption><\/figure>\n<p>The way that I viewed the problem is that the robot (Karel) had to move along the either in Up \/ Down or Left \/ Right and place a beeper every other step, so in basics if he moves up, he will move up 1 place see if he is able to move any more, if not turn right and if the front is not blocked move forward and right again to go back down the aisle next to the previous one we have just been up and whilst moving up and down the aisle to place a beeper every other step ( so need to keep check of the steps) or you could do the same process but going left and right instead, e.g. move right first to the end of that aisle and then go up if able to and then turn around and move left along the aisle above the previous aisle.  <\/p>\n<p>Here is my implementation of both ideas, again the source code for the assignment 1 is attached above in the zip file and also the PDF with the full assignment details.<\/p>\n<pre lang=\"java\">\r\n\/*\r\n * File: CheckerboardKarel.java\r\n * ----------------------------\r\n * When you finish writing it, the CheckerboardKarel class should draw\r\n * a checkerboard using beepers, as described in Assignment 1.  You\r\n * should make sure that your program works for all of the sample\r\n * worlds supplied in the starter folder.\r\n *\/\r\n\r\nimport stanford.karel.*;\r\n\r\npublic class CheckerboardKarel extends SuperKarel {\r\n\r\n\tprivate boolean putBeeper = true;\r\n\t\r\n\tprivate void placeBeeperAndMove()\r\n\t{\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\tif (putBeeper) {\r\n\t\t\t\tputBeeper();\r\n\t\t\t\tputBeeper = false;\r\n\t\t\t} else\r\n\t\t\t\tputBeeper = true;\r\n\t\t\t\r\n\t\t\tif (frontIsBlocked()) break;\r\n\t\t\tmove();\r\n\t\t}\r\n\t}\r\n\t\r\n\tprivate void goUpAndDown()\r\n\t{\r\n\t\tboolean turnLeft = true;\r\n\t\t\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\tif (turnLeft) \r\n\t\t\t\tturnLeft();\r\n\t\t\telse \r\n\t\t\t\tturnRight();\r\n\t\t\tplaceBeeperAndMove();\r\n\t\t\tturnLeft = (turnLeft == true ? false : true);\r\n\t\t\tif (turnLeft)\r\n\t\t\t\tturnLeft();\r\n\t\t\telse\r\n\t\t\t\tturnRight();\r\n\t\t\tif (frontIsBlocked()) break;\r\n\t\t\tmove();\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void goLeftAndRight()\r\n\t{\r\n\t\tboolean turnLeft = true;\r\n\t\t\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\tplaceBeeperAndMove();\r\n\t\t\tif (turnLeft) \r\n\t\t\t\tturnLeft();\r\n\t\t\telse \r\n\t\t\t\tturnRight();\r\n\t\t\tif (frontIsBlocked()) break;\r\n\t\t\tmove();\r\n\t\t\tif (turnLeft)\r\n\t\t\t\tturnLeft();\r\n\t\t\telse\r\n\t\t\t\tturnRight();\r\n\t\t\tturnLeft = (turnLeft == true ? false : true);\r\n\t\t}\r\n\t}\r\n\t\r\n\t\/\/ You fill in this part\r\n\tpublic void run()\r\n\t{\r\n\t\t\/\/ go up and down \r\n\t\t\/\/goUpAndDown();\r\n\t\t\/\/ go left and right\r\n\t\tgoLeftAndRight();\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The problem 3 is &#8220;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:&#8221; The way that I viewed the problem is that the robot (Karel) had to move along the either in Up \/ Down or &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/10\/problem-3-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Problem 3<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-948","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=948"}],"version-history":[{"count":7,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/948\/revisions"}],"predecessor-version":[{"id":1341,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/948\/revisions\/1341"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}