Assuming you are using the (which includes acm.graphics.* and java.awt.Color ), here is the most direct and effective solution.
To solve 9.1.7, you need to understand the relationship between the and the column index . Imagine your grid as a coordinate system: Square (0,0) is Row 0, Column 0. Square (0,1) is Row 0, Column 1. Square (1,0) is Row 1, Column 0. 9.1.7 checkerboard v2 answers
for row in range(5, 8): for col in range(8): if (row + col) % 2 != 0: board[row][col] = Checker('white') return board Assuming you are using the (which includes acm
This exercise is not just about drawing a pretty grid. It reinforces several critical programming concepts: Square (0,1) is Row 0, Column 1
// Draw the board for (int row = 0; row < board.size(); row++) for (int col = 0; col < board.get(row).size(); col++) int x = col * SQUARE_SIZE; int y = row * SQUARE_SIZE; GRect square = new GRect(x, y, SQUARE_SIZE, SQUARE_SIZE); square.setFilled(true); square.setFillColor(board.get(row).get(col)); add(square);
Instead of writing a nested loop to fill each individual cell, you can multiply a small list to fill the row. Multiplying [0, 1] by 4 creates a list of 8 elements: [0, 1, 0, 1, 0, 1, 0, 1] . This is a concise way to ensure each row has exactly 8 columns. 4. Print the final 2D structure
The solution for the exercise requires creating an grid of alternating
Мы используем файлы cookie и другие средства сохранения предпочтений и анализа действий посетителей сайта. Подробнее в Политика конфиденциальности. Нажмите «Принять», если даете согласие на это.