Skip to the content.

9.1.6 Checkerboard V1 Codehs Instant

Swapping row and col inside the setPosition function is the most common reason for a "sideways" or broken grid. Remember: X is Columns, Y is Rows.

The mathematical secret to this pattern is the . If you add the row index and the column index Even sums result in one color. Odd sums result in the other color. The Code Implementation 9.1.6 checkerboard v1 codehs

Below is a blog-style guide to solving this problem effectively. Mastering the CodeHS 9.1.6 Checkerboard Challenge Swapping row and col inside the setPosition function

The challenge is deciding when to use gray and when to use black. There is a simple mathematical trick: If you add the row index and the

: To get the "checkerboard" effect, you can't just alternate colors every other square, because each new row needs to start with a different color than the one above it to prevent vertical stripes. The Formula : A common trick is to add the current row index ( ) and column index ( (i + j) % 2 == 0 , use Color A. Otherwise, use Color B. Implementation Tips SQUARE_SIZE