9.1.7 Checkerboard V2 Codehs =link=
Mastering Checkerboard V2 in CodeHS: A Step-by-Step Guide
If you're working through the CodeHS Java course (or similar), you've likely encountered the 9.1.7 Checkerboard V2 exercise. It builds on the basic checkerboard concept but adds constraints that force you to think carefully about loops, conditionals, and drawing order.
In this post, I’ll break down exactly what the problem asks, how to plan your solution, and provide the complete code with explanations.
The Problem Goal
You need to draw an 8x8 grid. The squares must alternate colors:
- If the sum of the row index and column index is even, the square is one color (e.g., Red).
- If the sum is odd, the square is another color (e.g., Black).
4. Graphical Version – Gaps Between Squares
Using setFilled(true) alone might leave a tiny border. Some CodeHS exercises expect setFilled(true) without setColor for the outline. If gaps appear, set the outline color to match the fill color: 9.1.7 Checkerboard V2 Codehs
square.setColor(square.getFillColor());
Complexity
- Time: O(N^2) to create N^2 squares.
- Space: O(N^2) graphic objects (can be reduced by drawing directly onto a canvas if supported).
If you want, I can:
- Provide CodeHS-specific JavaScript or Python code for the environment you’re using.
It looks like you're asking for the solution or an explanation for the Checkerboard V2 problem (Exercise 9.1.7) on CodeHS.
Since I don't have access to your specific instance of the problem (which usually involves a specific width, height, or starting color), I will provide the standard logic used to solve this problem in Python. This logic works for any version of the problem. Mastering Checkerboard V2 in CodeHS: A Step-by-Step Guide
Advanced Tips for the "V2" Autograder
The "V2" autograder on CodeHS is stricter. It may check for:
- No global variables (unless constants).
- Use of
forloops (notwhileor hardcoded positions). - Correct color order (usually top-left must be a specific color).
- No flickering or overlapping squares.
Pro Tip: Run your code with a board size of 4x4 first to verify the pattern before scaling to 8x8.
Expected 4x4 pattern:
R B R B
B R B R
R B R B
B R B R
The Code Solution
Here’s a clean, V2-compliant solution in Java (using CodeHS’s GraphicsProgram and GRect): If the sum of the row index and
import acm.graphics.*; import acm.program.*; import java.awt.*;public class CheckerboardV2 extends GraphicsProgram private static final int SIZE = 50; // square side length private static final int ROWS = 8; private static final int COLS = 8;
public void run() boolean isBlack = true; // start with black for row 0 for (int row = 0; row < ROWS; row++) for (int col = 0; col < COLS; col++) int x = col * SIZE; int y = row * SIZE; GRect square = new GRect(x, y, SIZE, SIZE); if (isBlack) square.setFilled(true); square.setFillColor(Color.BLACK); else square.setFilled(true); square.setFillColor(Color.RED); // or Color.WHITE add(square); // Toggle color for next square in the row isBlack = !isBlack; // After finishing a row, toggle the starting color for the next row // Since we toggled an odd number of times (8 toggles), // the boolean is already opposite of row start. // But careful: after even number of columns, toggling 8 times // brings us back to original state. So we must toggle once more // to alternate row starting color. if (COLS % 2 == 0) isBlack = !isBlack;
3. Alternating Logic (The Pattern)
A checkerboard alternates on both axes. The formula (row + col) % 2 is the golden rule.
- If
(row + col) % 2 == 0, draw one color (e.g., black). - If
(row + col) % 2 == 1, draw the other color (e.g., red).