Skip to content

Part 1: Board

Brief

In Pyturis, the board is stored as a 1D list in row-major order.

Row-major order stores values in the same row of a matrix consecutively and concatenates rows into a single list starting from the top.

Boardillu

The relationship between the coordinates of an item in the board — notated as (x, y) — and the index of the corresponding item in the row-major order list is represented by the following equation, and illustrated in the above image.

Attributes

The Board class is located in the board.py file. As with the Holder class, there is already a constructor method provided, which contains a few useful attributes.

  • [Int] self.num_rows represents the number of rows the board object has
  • [Int] self.num_cols represents the number of columns the board object has
  • [List] self.grid contains the values on the 2D board in row-major order

TODOs

You’ll need to fill out three methods in board.py:

  • Q1: get_board_item(board, x, y): Return the item at (x, y) on the board.
  • Q2: set_board_item(board, x, y, item): Return a new Board object where the input item is placed at (x, y) on the board. Hint: modify the new board created by copy, instead of directly modifying the original board!
  • Q3: valid_coordinate(board, coordinate): Take in a coordinate formatted as a tuple of two ints, (x, y), and return whether it represents an existing position on the board as a boolean.
    • x is valid if it is non-negative and less than the number of board columns
    • y is valid if it is non-negative and less than the number of board rows
    • coordinate is valid if x AND y are both valid.
  • Q4: get_row(board, y): returns a list containing all items in row y on the board.
  • Q5: check_row_full(board, y): Check if a row y on the board is full, such that it all of its grids contain a non-zero value. If so, return True. Else just return False. (In practice of the GUI, this works, because we gave an empty board a default value of 0, and then each types of pytromino a unique integer, so if some part of a pytromino is present at the first column of the row, then the row will look like [1, 0, 0, 0 ... 0]. Rendered by the GUI, this is a colored block at the first column, and then empty in all other columns! If the row is full, it should then contain no 0 in it.)

Hints

  • Keep in mind the equation above for calculating the index!

Local Tests

To check how your implementation is doing, you can run the local tests in the project directory with the following command. The code below tests all your work in board.py. To test each infividual question, replace the Board in the command below with Q1, Q2, or Q3.

If there is no output in the terminal, that means your tests are PASSING!

For Windows:

py grader.py Board

For Mac:

python3 grader.py Board