- Joined
- Sep 8, 2019
- Posts
- 738
- Solutions
- 12
- Reaction
- 2,047
- Points
- 690
make standford karel code diagonal
function main() {
while (frontIsClear()) {
move();
turnLeft();
if (frontIsClear()) {
move();
turnRight();
}
}
}
function draw_face() {
// Draw the head
put_beeper();
move();
put_beeper();
move();
put_beeper();
turn_left();
move();
put_beeper();
move();
put_beeper();
move();
put_beeper();
turn_left();
move();
turn_left();
// Draw the eyes
put_beeper();
move();
put_beeper();
turn_left();
move();
move();
put_beeper();
turn_left();
move_to_mouth();
}
function move_to_mouth() {
// Move to the mouth position
turn_left();
move();
move();
put_beeper();
move();
put_beeper();
move();
put_beeper();
turn_left();
turn_left();
move();
move();
turn_left();
}
// Start at the top left corner of the face
move();
move();
// Draw the face
draw_face();
draw_face()
and move_to_mouth()
. draw_face()
is responsible for drawing the head, eyes and calling the move_to_mouth()
function to draw the mouth. move_to_mouth()
moves Karel to the position of the mouth and draws it using beepers. function main() {
while (frontIsClear()) {
makeRow();
changeRows();
}
}
function makeRow() {
while (frontIsClear()) {
putBeeper();
move();
if (frontIsClear()) {
move();
}
}
}
function changeRows() {
turnLeft();
move();
turnLeft();
while (frontIsClear()) {
move();
}
turnAround();
}
makeRow()
function places beepers on every other square, while changeRows()
moves Karel to the next row and changes the direction so that Karel moves along the squares in the opposite direction. the code doesnt seem a chessboardTo create a chessboard pattern in Stanford Karel code, you can use the following code:
Code:function main() { while (frontIsClear()) { makeRow(); changeRows(); } } function makeRow() { while (frontIsClear()) { putBeeper(); move(); if (frontIsClear()) { move(); } } } function changeRows() { turnLeft(); move(); turnLeft(); while (frontIsClear()) { move(); } turnAround(); }
This code will create a chessboard pattern, moving Karel along each row and alternate the placement of beepers. ThemakeRow()
function places beepers on every other square, whilechangeRows()
moves Karel to the next row and changes the direction so that Karel moves along the squares in the opposite direction.
Note that this code assumes that Karel starts at the bottom left corner of the first row facing east. If Karel starts at a different position or direction, you may need to adjust the code accordingly.
I apologize for the confusion. Here's an updated code that will create a chessboard pattern:the code doesnt seem a chessboard
function main() {
while (frontIsClear()) {
makeRow();
if (frontIsClear()) {
nextRow();
}
}
}
function makeRow() {
while (frontIsClear()) {
putBeeper();
move();
if (frontIsClear()) {
move();
}
}
}
function nextRow() {
turnLeft();
move();
turnLeft();
while (frontIsClear()) {
move();
}
turnRight();
move();
turnRight();
}
nextRow()
function turns Karel left, moves forward, turns left again, moves to the end of the row, turns right, moves forward, and turns right again to face the next row.