Back

9.1.6 Checkerboard V1 Codehs ^new^

The 9.1.6 Checkerboard V1 is designed to teach you to think ahead. By breaking the task into smaller, reusable functions— fillRow , reposition , turnRight —you make the problem much easier to manage.

function main() while (leftIsClear()) fillRow(); repositionToNextRow(); // Fill the very last row fillRow(); // Fills a row with the checkerboard pattern function fillRow() while (frontIsClear()) putBeeper(); if (frontIsClear()) move(); if (frontIsClear()) move(); putBeeper(); // Moves Karel up and aligns to the next row function repositionToNextRow() if (facingEast()) if (leftIsClear()) turnLeft(); move(); turnLeft(); else if (rightIsClear()) turnRight(); move(); turnRight(); // Helper to turn right function turnRight() turnLeft(); turnLeft(); turnLeft(); Use code with caution. 4. Detailed Explanation of the Code main() Function

Ensure your loops run while row < numRows , not <= , or you’ll hit an IndexOutOfBounds error.

The exercise asks you to create a grid representation of a checkerboard. In many programming challenges, a simple grid is represented as a list of lists. The goal is to fill an board where: Zeros ( ) represent empty spaces. Ones ( ) represent checker pieces. 9.1.6 checkerboard v1 codehs

) to create a grid pattern. In the 9.1.6 Checkerboard assignment, the goal is to alternate colors (usually black and red) across a grid of squares. Key Concepts Nested Loops : You use an outer loop for the and an inner loop for the

/* * This class represents a checkerboard. * It creates a grid of Rectangle objects. */ public class Checkerboard

If your squares are bleeding into each other or leaving white gaps, double-check your coordinate math. x2 must always be x1 + SQUARE_SIZE . The goal is to fill an board where:

Use the modulus operator (%) to create the "every other" effect. A reliable trick is checking if (i + j) % 2 == 0 .

If the remainder is 0 , the position is even, and it applies COLOR_ONE .

To create the offset pattern, look at the coordinates of the grid indices (Row, Column): Row 0, Col 0 →right arrow Color A (0 + 0 = 0, Even) Row 0, Col 1 →right arrow Color B (0 + 1 = 1, Odd) Row 1, Col 0 →right arrow Color B (1 + 0 = 1, Odd) Row 1, Col 1 →right arrow Color A (1 + 1 = 2, Even) the position is even

// Nested loops to iterate through the 2D array for(int row = 0; row < size; row++)

Once you've mastered "Checkerboard, v1," you can challenge yourself further:

Solving this exercise teaches you several fundamental programming concepts: