Assignment 3:: Due 8am On Mon, Oct 28, 2019
Assignment 3:: Due 8am On Mon, Oct 28, 2019
Summary of Instructions
Note Read the instructions carefully and follow them exactly
Assignment Weight 6% of your course grade
Due Date and time 8am on Monday, Oct 28, 2019
As outlined in the syllabus, late submissions will not be accepted.
Important Any files with syntax errors will automatically be excluded from grading. Be sure to test
your code before you submit it
For all functions, both in Part 1 and 2, make sure you’ve written good docstrings that
include type contract, function description and the preconditions if any.
This is an individual assignment. Please review the Plagiarism and Academic Integrity policy presented in the first
class, i.e. read in detail pages 15 18 of the course outline (i.e. slides of Lecture 1). You can find that file on Brightspace
under Lecture 1. While at it, also review Course Policies on pages 13 and 14.
In addition to the assignment files specified later, if you used any code that you did not create/write yourself, your
submission will need to contain declaration-YOUR-FULL-NAME.txt file. Specifically:
The goal of this assignment is to learn and practice the concepts covered thus far: function design, lists and loops.
You can make multiple submissions, but only the last submission before the deadline will be graded. For this assign-
ment, you do not need to submit a3_xxxxxx.txt as proof that you tested your functions. By now we trust that you learnt
and understand the need for and importance of testing your functions and code in general.
The assignment has two parts. Each part explains what needs to be submitted. Put all those required documents into
a folder called a3_xxxxxx where you changed xxxxxx to your student number, zip that folder and submit it as explained
in Lab 1. In particular, the folder should have the following files:
In part 1 you will implement 4 shorter programs. In part 2, you will implement a card game. Put all the below five
required documents (and your declaration file) into a folder, zip it the folder, and submit the resulting a3_xxxxxx.zip
as explained in lab 1. (In particular, the folder (and thus your submission) should have the following files:
1
+ declaration-YOUR-FULL-NAME.txt (if you used code you did not create/write, as detailed above.)
All programs must run without syntax errors. In particular, when grading your assignment, TAs will first open your
file, e.g. a3_GAME_xxxxxx.py with IDLE and press Run Module. If pressing Run Module causes any syntax error, the
grade for Part 2 becomes zero. The same applies to Part 1.
Furthermore, for each of the functions (in Part 1 and Part 2), I have provided one or more tests to test your functions
with. To obtain a partial mark your function may not necessarily give the correct answer on these tests. But if your
function gives any kind of python error when run on the given tests, that question will be marked with zero points.
Some text cases are given inside docstrings like: remove_pairs in the provided starter file a3_game_xxxxxx.py; and,
clean_up and is_rigorous in a3_Q4_xxxxxx.py.
To determine your grade, your functions will be tested both with examples provided for Part 1 and Part 2 and with
some other examples. Thus you too should test your functions with more example than what I provided.
Keywords: break and continue are not allowed.
Global variables in bodies of functions are not allowed. If you do not know what that means, for now, interpret this
to mean that inside of your functions you can only use variables that are created in that function. For example, this is not
allowed, since variable x is not a parameter of function a_times(a) nor is it a variable created in function a_times(a).
It is a global variable created outside of all functions.
def a_times(a):
result=x*a
return result
2
Please input a list of numbers separated by space: 1 4 3 3 4
True
RUN 1:
Enter the name of the file: file1.txt
Before clean-up:
['D','F','B','G','$','$','$','A','A','C','G','D','A','$','C','*','P','E','D','*','D','D','E','B','$','#','D','D]
After clean-up:
['$', '$', '$', '$', 'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'D', 'D', 'D', 'D', 'E', 'E', 'G', 'G']
RUN 2:
3
Enter the name of the file: file2.txt
Before clean-up:
['A', '*', '$', 'C', '*', '*', 'P', 'E', 'D', 'D', '#', 'D', 'E', 'B', '$', '#']
After clean-up:
['#', '#', '$', '$', 'D', 'D', 'E', 'E']
RUN 3:
Enter the name of the file: file3.txt
Before clean-up:
['A', 'B', '*', 'C', '*', 'D', '*', '*', '*', 'E']
After clean-up:
[]
RUN 4:
Enter the name of the file: file4.txt
Before clean-up:
['A', 'A', 'A', 'A', 'A', 'A', 'A']
After clean-up:
['A', 'A', 'A', 'A', 'A', 'A']
RUN 5:
Enter the name of the file: file5.txt
Before clean-up:
[]
After clean-up:
[]
2 Part 2: Single Player Rummy Game with Dice and strange deck
(80 points)
For this part, you will program a card game described here:
http://www.classicgamesandpuzzles.com/Old-Maid.html
You will implement the two player version. One player will be the computer (i.e. your program) and the other a user
of your program. In what follows, let’s refer to the computer player as Robot and user player as Human. You may assume
that Robot will always deal the cards.
As part of this assignment I provided the file called a3_GAME_xxxxxx.py. Replace xxxxxx in the file name with
your student number. You should open that file and run it to see what it does already. All of your code has to go
inside of that file. The file already has some functions that are fully coded for you and other functions for which only
docstrings and partial or no code are provided. Designing your program by decomposing it into smaller subproblems
(to be implemented as functions) makes programming easer, less prone to errors and makes your code more readable.
No part of the given code can be changed. Your code must go into clearly indicated places. No code can be added to
the main. You can design some extra functions of your own if you like.
Functions make_deck, wait_for_player and shuffle_deck are already fully coded for you.
You need to develop the remaining functions: deal_cards, remove_pairs, print_deck, get_valid_input, and
play_game. The functions must meet the requirements as specified in their docstrings (and as implied by the exam-
ple program runs below and as implied by the video instructions).
The main bulk of your code (the game playing part) will go into the function called play_game. That function
should use/call the other functions that you are required to develop (i.e. deal_cards, remove_pairs, print_deck, and
get_valid_input).
When developing function get_valid_input you may assume that Human will enter integer when asked for an
integer, but you may not assume that it will be in the correct range.
4
The function get_valid_input gets the input from Human about which face-down card of Robot it wants. When it
is Robot’s turn to play you must implement it such that Robot takes a random card from Human. Also recall that what
Human calls 3rd card, for example, is in position/index 2 in Robot’s deck (as it is represented by a list).
Study the example of the program run below carefully to understand how your program should behave. The be-
haviour of the program that you see in the run is required –s all aspects of it. Also watch the video I made to get even
better idea of how your program must behave. The video can be found here: https://youtu.be/Dmh5Rsdwn18
Some suggestions:
• Study the provided code and understand what it does and how it should be used.
• Spend some time thinking about various parts of the game that need to be implemented. For example, it needs to
be able to display Human’s deck to Human, it needs to be able to ask Human for what card she wants, it needs to
be able to remove pairs from either Human or Robot’s deck ... etc. The provided functions do quite of bit of that
job for you.
• When you are coding individual functions recall that you can test each function in the shell without finishing
the remaining functions. For example, when implementing function remove_pairs you can test it in the shell by
typing something like:
Thus you can code and test your functions one by one (without completing the other parts)
• The game alternates between Robot and Human. Think about how you can represent whose turn it is to play, in
your program. One way is to have a variable that you set to zero when it is Robot’s turn and to one when it is
Human’s turn. You also need to figure out what to test to see if the game is over.
5
EXAMPLE RUNS OF YOUR GAME:
============================================================================
RUN 1
============================================================================
***********************************************************
Your turn.
7♢ 4♠ 8♠ 10♢ A♣ K♣
7♢ 4♠ 8♠ 10♢ A♣ K♣ 10♣
K♣ 7♢ A♣ 8♠ 4♠
***********************************************************
My turn.
***********************************************************
Your turn.
K♣ 7♢ A♣ 4♠
K♣ 7♢ A♣ 4♠ 7♣
A♣ K♣ 4♠
***********************************************************
My turn.
***********************************************************
Your turn.
A♣ 4♠
A♣ 4♠ Q♢
A♣ Q♢ 4♠
***********************************************************
My turn.
***********************************************************
Your turn.
A♣ 4♠
A♣ 4♠ A♠
4♠
***********************************************************
My turn.
***********************************************************
Ups. You do not have any more cards
Congratulations! You, Human, win
============================================================================
RUN 2
============================================================================
***********************************************************
Your turn.
5♣ Q♡ 4♣ 3♢
5♣ Q♡ 4♣ 3♢ 4♠
***********************************************************
My turn.
***********************************************************
Your turn.
5♣ 3♢
5♣ 3♢ Q♡
5♣ Q♡ 3♢
***********************************************************
My turn.
***********************************************************
Your turn.
Q♡ 3♢
Q♡ 3♢ 3♣