Spring 2023 - Assignment 1 - Joker Twins

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Assignment 1: Joker Twins

Introduction to Computer Science (CS-UH 1001) - Spring 2023

1 Code of Conduct
All assignments are graded, meaning we expect you to adhere to the academic integrity
standards of NYU Abu Dhabi. To avoid any confusion regarding this, we will briefly outline what
is and isn't allowed when working on an assignment.
1. Any document and program code that you submit must be fully written by yourself.
2. You can discuss your work with fellow students, as long as these discussions are
restricted to general solution techniques. In other words, these discussions should not
be about concrete code you are writing, nor about specific results you wish to submit.
3. When discussing an assignment with others, this should never lead to you possessing
the complete or partial solution of others, regardless of whether the solution is in paper
or digital form, and independent of who made the solution.
4. You are not allowed to possess solutions by someone from a different year or section, by
someone from another university, code from the Internet, etc.
5. There is never a valid reason to share your code with fellow students.
6. There is no valid reason to publish your code online in any form.
7. Every student is responsible for the work they submit. If there is any doubt during the
grading about whether a student created the assignment themselves (e.g. if the solution
matches that of others), we reserve the option to let the student explain why this is the
case. In case doubts remain, or we decide to directly escalate the issue, the suspected
violations will be reported to the academic administration according to the policies of
NYU Abu Dhabi. More details can be found at:
https://students.nyuad.nyu.edu/academics/registration/academic-policies/academic-integ
rity/
Furthermore, every integrity violation will be penalized with (a) 10% or (b) the full
percentage of the gradable assessment, whichever is higher.

2 Introduction
Joker Twins is a modified version of the classical Memory game, a game that has long been a
favorite game for all generations. In fact it is so simple that very young children can play it with
ease. It requires observation, concentration and a good memory to win. The game is also
known as Concentration, Pelmanism, Shinkei-suijaku, Pexeso and Pairs. An example of the
classical memory game can be played here:
https://www.helpfulgames.com/subjects/brain-training/memory.html
The goal of this assignment is to implement the Joker Twins game in Python3, using the
material that has been covered in class. This will give you practice in working with lists, decision
structures, iterations and loops. Additionally, it is a good exercise in decomposing a larger
problem into smaller and more manageable parts.

The Joker Twins game in this assignment is a two-player game and is played on a
two-dimensional board where the memory cards are represented as letters. That is, the "twins"
are the upper and lower case of letters, for example "A" and "a". Each card and its twin are
randomly placed on the board and hidden from the players. The two players take turns to turn
over two cards in each turn. If the two cards have the same letter (upper and lower case), then
the player "keeps" the cards, the cards will be removed from the game, and the player continues
playing until two cards are turned over that are not twins. In that case, the cards will turn face
down again and the game continues with the other player's turn. The winner is the player with
the most cards (i.e. twins) when no cards are left in the game.

What sets the Joker Twins game apart from classical memory is that the game contains up to
two Joker cards that are added to the other cards. In the event that the board dimensions result
in an odd number of cells and there is one cell left empty, three Joker cards are placed on the
board. The Joker card, once turned over by a player, forms a twin with the next or previous
turned over card. As a result, both cards comprising the twin plus the Joker will be turned over
and all three cards are added to the player's score. Once a Joker card is utilized, it will also be
removed from the board.

Figure 1 shows example output of the Joker Twins game with two players.
Figure 1: Example output of the initial game (player 2 starts), after a few turns (player 1 just
entered coordinate G1 or B2 and the game pauses to show the two cards to both players), and
after the score of player 2 incremented and the program is awaiting input from player 1

3 Implementation
The implementation of this game consists mainly of two stages:

Initialization:
The game starts with a board where columns are identified by letters and rows by numbers (see
Figure 1). The board is represented as a two-dimensional list, with each element representing a
card. Initially, all cards are shown face down and are hidden from the player (denoted by a "#").
The cards are randomly placed on a second, hidden board that holds the actual cards (letters)
and the Jokers. This hidden board is used internally to save the locations of the cards, check for
matches, etc. The dimensions of both boards must match, can have arbitrary dimensions and
should be declared as global constant variables. In other words, do not "hardcode" the board
dimensions in your code and use global constant variables instead (one for the number of rows
and one for the number of columns, as shown below). This allows you to easily change the
board dimension by simply changing their values in the global constant variables. The game
should be able to support any dimensions resulting between 12 and 54 elements in total. Please
note that you do not need to ask the user to input the dimensions. However, you have to make
sure that the program fills all elements with letters twins and Jokers and that their locations are
randomly chosen.
The letters that represent a card should be dynamically generated by the program according to
the board dimensions. For example, if the board has dimensions of 5x6, the program should
generate letters from A-N and a-n, plus two Joker. The program must always generate twins of a
letter and they always start with an "A" and "a" and increment according to the English alphabet.
Again, do not "hardcode" a list of all possible letters, i.e. all 52 possible letter twins. These
dynamically generated letters, including two Jokers which are represented by a "*", will be
randomly placed on the board. If the board dimensions result in an odd number of elements,
three Jokers are randomly added to the game. Please note that your program must work with
any board dimensions, even those that may result in an uneven number of elements!

Since the game is a two-player game, the program chooses a random player for the first turn.
The following code can be used to define the board dimensions and to determine a random
player to start. Please note that the randint() function in line 10 should also be used to
randomly place letters on the board:

1 import random # imports the random module


2
3 NUM_ROWS = 5 # number of rows, e.g. 5
4 NUM_COLS = 7 # number of columns, e.g. 7
5 NUM_PLAYERS = 2 # constant variable for number of players
6
7 # the following command returns a random number between 1 and 2
8 turn = random.randint(1, NUM_PLAYERS)
9
10 # your code comes here
11
12 # alternating turns, i.e. turn alternates between 1 and 2
13 turn = (turn + 1) % NUM_PLAYER
14
15 # your code comes here

After creating the board and placing the twins and Jokers, the board should be printed on the
screen (as depicted in Figure 1) where all cards are showing face down and are represented as
a "# . In addition, both player scores should be printed above the board, and an input prompt for
the randomly chosen player below the board.
Game phase:
The game starts after the first player inputs the first location/coordinate of the card that should
be turned over. The coordinate the player enters consists of the column (a letter) followed by the
row (a digit). For example, valid coordinates are B4, C1, A5, etc. Notice that the column letter is
case-sensitive. If the player enters an invalid coordinate or an invalid input, the program informs
the player about this and asks the player for another input until the input is valid. Examples of an
invalid input are AB, A 6, 1A, 32, A;5, a6, A-1, 5C, 2a etc., coordinates out of the board
dimensions, coordinates that are currently shown as a turned over card or coordinates that do
not contain a card anymore (i.e. turned over in a previous turn).
After a valid user input, the program (temporarily) displays the card (i.e. displays the actual card
instead of the "#") and asks the player for the coordinate to turn over the second card. If both
turned over cards form a twin (i.e. an uppercase letter and its lowercase letter twin), both cards
are removed from the board (represented as " ") and the player continues its turn to find more
twins. Remember, the Joker can be used to form twins and will be removed from the board as
well, together with the other card of the twin that was not turned over yet. For example, if the
player turns over a Joker and then an "e" (or vice versa), the card with an "E" will automatically
be turned over and also removed from the board. Please note that Jokers twins are also
considered twins. In case the player found a twin, each card, including a Joker, counts as 1
point towards the player's score. Also, the player's score should be updated and displayed.

A player's turn ends once two cards are turned over that do not form a twin. In that case, the
program will pause for two seconds (use code below) so that both players have the chance to
memorize the turned over cards. Then, the board should be updated where both cards are
turned face down again (displayed as a "#") and the program should clear the screen (use code
below). The game continues with the other player following the same steps as described above.

The following functions can be used to pause the program for two seconds and to clear the
screen:

1 import os # imports the os module


2 import time # imports the time module
3
4 # your code comes here
5
6 time.sleep(2) # pause the program for 2 seconds
7 os.system("clear") # clears the screen
8
9 # your code comes here
After each turn, the program should check if further turns are possible and if not, the game ends
and the program terminates. The program should inform the user that the game is over, which
player won and what their scores are.

4 Grading

Description Score (/15)

Using global variables (constants) for board dimensions 0.5

Correctly creating the board based on board dimensions 0.5

Printing of the board, board labels, user scores and user input, as shown in Figure 1 2

Dynamically generating twins based on board dimensions 2

Randomly placing twins and Jokers on the board 2

Checking for invalid user input (letter followed by number) 2

Checking for invalid coordinates (repetitive input of same cell, out of board, etc)) 2

Correctly turning over cards (reveal and hide) 2

Detecting the end of the game and terminating the game after a win 1

Using comments and readability of code (style, variable naming, etc.) 1

5 Submission
Please note: Your submission must have set the board dimensions to 5x5.

Submission details/policy are mentioned below:

Submission Deadline: The deadline of this assignment is after 10 days of its release via
Brightspace. No extensions will be given.

Submission Format and System: You can directly submit your Python .py file on Brightspace.
Submissions via email are not accepted. Note that your solution must work using Python 3 and
Linux/Unix terminal, otherwise your submission will not be graded.

Late Submissions: Late submissions will be penalized by 20% per 24 hours.

You might also like