Have a question?
Message sent Close

ICS 32 Spring 2018 Thornton Notes And Examples

Sandra Watson
0 reviews
  • Description
  • Full Document

University of California Irvine

Into to Programming (ICS 32)

ICS 32 Spring 2018 , Notes and Examples: Two-Dimensional Lists

In a project earlier this quarter, you and a partner implemented a Connect Four game. The game logic
itself — the actual implementation of the rules of the game — was provided in a module called
connectfour.py, but even if you didn’t look through the module’s code, some of the details of its
implementation were made clear to you while you worked on the project. Connect Four is a game whose
playfield is a two-dimensional grid, with tiles being dropped into columns from the top or popped from
columns at the bottom; for that reason, connectfour.py organized the playfield as a two-dimensional list
(i.e., a list in which each element is a list), the most natural way to store a simple grid or matrix structure in
Python, with the list primarily containing each of the playfield’s columns in a sublist, and each element of
the sublist being one row of that column. So, for example, the playfield might be represented like this (with
some spacing added to make the structure clearer):

We used named constants, as well, so we could refer to the 0, 1, and 2 in our code as NONE, RED, and
YELLOW, which aided readability further; being able to call things what they are, instead of some
encoded version of what they are (e.g., RED instead of 1), is one of the hallmarks of well-written,
maintainable code.
While it’s important to note that this arrangement was the most convenient way to implement various parts
of the game logic — e.g., dropping and popping pieces, which had an effect on only one column (and
therefore only one sublist) — you faced the problem that this arrangement wasn’t the right one to show to
a human user, who would want to see the columns vertically and the rows horizontally. So you had to
write a function that could present that same playfield like this:



 ICS 32 Spring 2018 Thornton Notes And Examples

NOTE: Please check the details before purchasing the document.