Assignment1 - Part A Instructions
Assignment1 - Part A Instructions
Assignment1 - Part A Instructions
IFB104
Building
IT
Systems
Semester
1,
2020
Motivation
One of the basic functions of any IT system is to process a given data set to produce some
form of human-readable output. This assignment requires you to produce a visual image by
following instructions stored in a list. It tests your abilities to:
• Process lists of data values;
• Produce maintainable, reusable code;
• Design a general solution to a non-trivial computational problem; and
• Display information in an attractive visual form.
In particular, you will need to think carefully about how to design reusable code segments,
via well-planned function definitions and the use of repetition, to make the resulting program
as concise as possible and easy to understand and maintain.
Goal
Connect Four (also known as Connect-4 and by many other names) is a popular children’s
game in which players take turns dropping coloured tokens into a vertical game board. The
first player to complete a vertical, horizontal or diagonal row of four tokens is the winner.
IFB104
Building
IT
Systems
Semester
1,
2020
In this assignment you will visually simulate a similar game, but with a number of differ-
ences. Our “Not Connect Four” game has:
four players instead of two;
random choices of which player gets to drop the next token;
illustrated square tokens instead of plain round ones; and
a different way of choosing the winner, which will be revealed only in Part B of the
assignment.
To draw the game you must follow a set of instructions, provided as a list of moves, to place
the tokens in various cells of the playing board. The tokens must be stacked in columns as in
the real game. Most importantly, the sequence of moves to be followed is generated ran-
domly, so your solution must be sufficiently general that it can work correctly for any pos-
sible sequence of moves.
Resources provided
A template Python 3 program, not_connect_4.py, is provided with these instructions.
When run it creates a drawing canvas and displays a simple image of the playing board on
which you will draw specific types of tokens in specific columns. You have a free choice in
the design of the four token types, but they must all be related by a common theme. The de-
fault image drawn by running the provided Python template appears as shown below. It con-
sists of a numbered grid representing the playing board, with spaces on either side for de-
scriptions of the tokens you have designed.
For convenience, the “home” coordinate (0, 0) is marked by a dot. The board is a 7 × 6 grid
of square cells. Each of the board’s cells measures 100 × 100 pixels. The spaces to the left
and right are where the descriptions of each of your token types will be drawn.
IFB104
Building
IT
Systems
Semester
1,
2020
Your task is to extend this template file so that it can draw games of “Not Connect Four” by
following a provided list of moves. To do so you must design four entirely distinct tokens,
each of which can be drawn in any square on the board. Your code will consist of a function
called play_game and any auxiliary functions you define to support it. This function takes
a single argument, which is a list of moves specifying in which column to place each type of
token. The moves are created by a provided function called random_game which randomly
generates the sequence of moves, so your code must work correctly for any possible game
that could be played!
Data format
The random_game function used to assess your solution returns a list of moves each repre-
senting the action of a player dropping one of their tokens into the board. Each of the moves
is expressed as a pair of values with the following general form.
[column, token_type]
The column values are letters ranging from ‘a’ to ‘g’ and the token types are integers ranging
from 1 to 4. For instance,
['c', 2]
IFB104
Building
IT
Systems
Semester
1,
2020
tells us to draw a token of type 2 in column ‘c’. Note, however, that we are not told in which
row to draw the token! As in the real Connect Four game, this depends on how many tokens
have already been dropped into this column, if any. Tokens cannot be drawn on top of one
another. For instance, if there have already been two tokens placed in column ‘c’ then this
new token must be drawn in row 3.
The random_game function generates a list of anywhere between zero and 42 (i.e., 7 × 6)
moves. When choosing which player makes the next move, no attempt is made by the func-
tion to be “fair”. The next player is chosen at random; we assume some other mechanism,
such as rolling a die, is used to choose who moves next. However, the game generated will
always be a valid one within the rules of “Not Connect Four”. In particular, the moves gen-
erated will never overfill a column.
In addition to the random_game function, the template file also contains a number of
“fixed” data sets. These are provided to help you develop your code, so that you can work
with a known sequence of moves, rather than a random one, while debugging your code.
However, these “fixed” patterns will not be used for assessing your solution. Your
play_game function must work correctly for any sequence of moves randomly generated
by function random_game.
Illustrative example
To illustrate the requirements we developed a solution whose four tokens follow the theme
“Apollo 11”, in recognition of last year’s 50th anniversary of the first moon landing. (Don’t
copy our example! Come up with your own idea!) We wrote Turtle graphics code that can
draw the following four tokens, each representing an aspect of the historic mission.
Each token is exactly 100 pixels wide and high, so they all fit perfectly into one of the game
board’s cells. All of these images were drawn using basic Turtle graphics drawing steps; no
separate image files are used to display the tokens. Your images do not need to be as compli-
cated as these examples, but they must still be recognisable and non-trivial.
The first requirement for the assignment is to clearly identify your four tokens and their
common theme. To do so you must put an appropriate title on the drawing window and a de-
scription of each token in the spaces indicated to the left and right of the game board. Our
sample solution is shown overleaf.
IFB104
Building
IT
Systems
Semester
1,
2020
The next requirement is for your implementation of the random_game function to draw
these tokens in appropriate places on the game board, as per the moves in the list provided as
its parameter. To do so you need to take into account the column and token type specified in
each move, as well as the number of tokens already placed in that column.
For instance, consider the following list of 13 moves returned by function random_game.
The first move requires us to drop a token of type 4 into column ‘d’, so our random_game
function draws the Lunar Module token in row 1 of this column. The next move is a token of
type 2 in column ‘e’, so we draw the Apollo 11 mission patch in row 1 of that column.
However, the third move requires us to put another token of the same type in the same col-
umn. Since there is already a token in column ‘e’ we draw the next Lunar Module in row 2.
The fourth move is token type 3 in column ‘f’. There are no other tokens in this column so
IFB104
Building
IT
Systems
Semester
1,
2020
we draw the Command Module in row 1. The fifth move is another token 2 in column ‘a’,
and again this is the first token to appear in that column. The sixth move is token 2 in col-
umn ‘f’ and because this is the second token to appear in that column we draw the mission
patch in row 2. This process continues for all the moves in the list, taking care to stack to-
kens placed in the same column on top of one another. The resulting image, showing all the
tokens drawn in this case, is as follows.
Each time we call function random_game it produces a different list of drawing instruc-
tions. As another example, consider the following longer list returned by the function.
In this case we begin with token 3 in column ‘f’, followed by tokens of type 1 and 4, both in
column ‘e’, and so on. The resulting image once all 29 moves have been drawn appears be-
low. Notice in the data set above that the only token ever placed in column ‘b’ is of type 4,
which is why we see only Lunar Module tokens in that column.
IFB104
Building
IT
Systems
Semester
1,
2020
The first move is a token of type 2 in column ‘f’, then two tokens of type 4 in column ‘c’,
and so on, ending with a type 4 token in column ‘g’. The complete game is shown below.
IFB104
Building
IT
Systems
Semester
1,
2020
Notice that to produce a nice looking image you can stop the Python template writing “Put
your token descriptions here” on the canvas by changing one of the arguments to function
create_drawing_canvas in the main program. You can also change the background
colour and the colour of the lines marking the board in the same way.
criterion you must provide a significant amount of code to assess; a few lines of well-
presented code will not be considered sufficient.
5. Extra feature (5%). Part B of this assignment will require you to make a last-minute
change to your solution. The instructions for Part B will not be released until shortly
before the final deadline for Assignment 1.
You must complete the assignment using basic Turtle graphics, random number and maths
functions only. You may not import any additional modules or files into your program other
than those already included in the given not_connect_4.py template. In particular, you
may not import any image files for use in creating your drawings.
Finally, you are not required to copy the example shown in this document. Instead you are
strongly encouraged to be creative in the design of your solution. Surprise us!
Development hints
• Before creating code to draw the individual tokens give careful thought to how you
can write the code so that they can be drawn in any place on the canvas. In particular,
you need to avoid “hardwiring” your drawing to fixed coordinates.
• The easiest part of this assignment is the description of the tokens and the theme, be-
cause these elements never change, so you may want to complete this part first.
• The columns are labelled using letters instead of numbers in this game, so it can be
awkward to convert them into coordinates on the Turtle graphics canvas. An elegant
solution can be achieved by noting that each letter of the alphabet is actually repre-
sented by an “ordinal” number in your computer. In Python you can convert from
numbers to letters using the built-in function chr, and from letters to numbers using
built-in function ord. For instance, chr(78) returns character 'N', whereas
ord('A') returns integer 65 and ord('Z') returns 90. Thus, although you don’t
need to use it to complete the assignment, the ord function can be helpful when con-
verting the column letters into y-axis coordinates expressed in pixels.
• If you are unable to complete the whole task, just submit whatever you can get work-
ing. You will receive partial marks for incomplete solutions.
• To help you debug your code we have provided some “fixed” patterns. Feel free to
use these when developing your program, and add additional ones if you like, but
keep in mind that these data sets will not be used for assessing your solution. Your
play_game function must work for any list that can be returned by function ran-
dom_game.
• Part B of this assignment will require you to change your solution in a short space of
time. You are therefore encouraged to keep code maintainability in mind while de-
veloping your solution to Part A. Make sure your code is neat and well-commented
so that you will find it easy to modify when the instructions for Part B are released.
ers will be created on Blackboard. (Sadly, additional marks will not be given to the winners.
The only reward is the envy of your peers.)
Portability
An important aspect of software development is to ensure that your solution will work cor-
rectly on all computing platforms (or at least as many as possible). For this reason you must
complete the assignment using standard Turtle graphics, random number and maths
functions only. You may not import any additional modules or files into your program other
than those already imported by the given template file. In particular, you may not import any
image files to help create your drawings or use non-standard image processing modules such
as Pillow.
Deliverable
You must develop your solution by completing and submitting the provided Python 3 file
not_connect_4.py as follows.
1. Complete the “statement” at the beginning of the Python file to confirm that this is
your own individual work by inserting your name and student number in the places
indicated. We will assume that submissions without a completed statement are not
your own work!
2. Complete your solution by developing Python code to replace the dummy
play_game function. You must complete your solution using only the standard Py-
thon 3 modules already imported by the provided template. In particular, you must
not use any Python modules that must be downloaded and installed separately because
the markers will not have access to these modules. Furthermore, you may not import
any image files into your solution; the entire game must be drawn using Turtle graph-
ics drawing primitives only, as we did in our sample solution.
3. Submit a single Python file containing your solution for marking. Do not submit
multiple files. Only a single file will be accepted, so you cannot accompany your so-
IFB104
Building
IT
Systems
Semester
1,
2020
lution with other files or pre-defined images. Do not submit any other files! Sub-
mit only a single Python 3 file!
Apart from working correctly your program code must be well-presented and easy to under-
stand, thanks to (sparse) commenting that explains the purpose of significant code segments
and helpful choices of variable, parameter and function names. Professional presentation of
your code will be taken into account when marking this assignment.
If you are unable to solve the whole problem, submit whatever parts you can get working.
You will receive partial marks for incomplete solutions.
To help you draw your images, below are some commonly-used Turtle graphics colours. The
named colours available can vary depending on the computing platform. Those below are
commonly cited on the Internet, but these names may not all be supported on all platforms.
IFB104
Building
IT
Systems
Semester
1,
2020
Source: www.discoveryplayground.com/computer-programming-for-kids/rgb-colors/