0% found this document useful (0 votes)
13 views

CS 112 Fall 2024 Programming Assignment 4: This Assignment Is An Individual Effort Assignment. The Honor Code Applies

latforms like Google Scholar, JSTOR, or Project Gutenberg (for older versions) might have parts of the book or related papers available for free. It might not be the full book, but useful sections or older editions may be available.

Uploaded by

Saqib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CS 112 Fall 2024 Programming Assignment 4: This Assignment Is An Individual Effort Assignment. The Honor Code Applies

latforms like Google Scholar, JSTOR, or Project Gutenberg (for older versions) might have parts of the book or related papers available for free. It might not be the full book, but useful sections or older editions may be available.

Uploaded by

Saqib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CS 112 – Fall 2024 – Programming Assignment 4

Multi-dimensional lists and Mutability


Due Date: Monday October 28th, (9:00 am)

This assignment is an Individual Effort assignment.


The Honor Code applies.

The purpose of this assignment is to gain experience using multi-dimensional lists and understand
mutability.
See the Assignment Basics file for more detailed information about getting assistance, running the test
file, grading, commenting, and many other extremely important things. Each assignment is governed by
the rules in that document. Assignment Basics can be found in the course Canvas (Modules section).
Needed Files: Download the attached files to apply the same tests that we will use when grading your
work.
• tester4.py (use this file to test your code for PA4.)
• Pa4_template.py (use this file to write your code for PA4 or you can create your own python file.
However, please ensure that your function definitions exactly match the signatures provided in
the template file.)

Learning Objectives:
• Using multi-dimensional lists
• Nesting lists
• Mutability
• Using list operations (a.k.a. list callables: functions and operators on lists)
• Using loop and nested loop statements, Mult-D lists, and list operations with functions
• Combining loops and lists with selection statements (if, elif, else)

A list, an object, can hold any type of object or another list as its element. A list can embed one or more
lists, resulting in a two-dimensional list (2D list) or multidimensional list (multi-D list), depending on
how many lists are embedded. Embedding lists inside other lists is called list nesting. Like 1D lists, multi-
D lists (a.k.a. nested lists) can be accessed using a single access operation. However, the elements of
the lists are accessed using two indexing operations.

Loop statements allow us to run the same block of code repeatedly, with the chance to use different
values for variables each time as the accumulated effects pile up. A single loop statement allows us to
iterate over a list or through the list’s elements. Typically, we use nested loops to iterate through the
elements of a multi-D List.

In this assignment, we will use single and nested loops, if statements and list operations to perform list
operations or adjustments on 2D lists. Also, we need nested or multiple loops for the particular tasks
below. Also, alternate implementations should be considered using the list operations covered in class.
Choose whichever solution helps you avoid errors and build it using the Python features and style
requirements covered in your section.

1
Assumptions

We have some assumptions to make the project easier. You may assume that:

• The types of the values that are sent to the functions are the proper ones. You do not have to
validate them.
• The input data is compliant to the stated assumptions in each individual task. Be sure to read these
assumptions!

Restrictions

Allowed Things:

• The +, +=, *, and all numeric operators


• in operator
• break and continue
• if, if-else, and elif
• Nested for loop
• str(), int(), float(), range(), len(), list(), append(), and index() functions
• List comprehension [] such as new_list = [expression for loop_variable_name in iterable]
• You can use input() and print() when testing your functions, but not as part of the function. Remove
all the input() and print() functions before submitting your file to Gradescope.

Disallowed Things:

• import anything.
• list slicing.
• string formatting or string indexing (string[index]).
• input() and print() functions.
• lambda expressions, dictionaries or any other Python features not covered in class

Tasks

Your non-profit organization is developing strategies to help college students effectively prioritize
competing demands to increase the number of tasks to finish in a tightly binding schedule and highly
dynamic learning environment. A business strategist recommended using the Tic-Tac-Toe strategy of
competing to win, defending against a loss, and planning a winning move.

In recognizing these three strategies of the Tic-Tac-Toe, students can seize opportunities, protect against
setbacks, and build on the success to offset the previous losses. To demonstrate how to map the Tic-
Tac-Toe game strategies to student success strategies, you were tasked to develop a custom Tic-Tac-
toe game as a training artifact by implementing the six functions described below. All functions worth 9
points except for two functions for initializing lists are 4.5 points each.

2
#-------------------------------------------------------------------------------------------#
# Task 1: Define Game Symbols (4.5 points) #
#-------------------------------------------------------------------------------------------#

def define_symbols(symbols_list):

Description: This function takes a pre-defined one-dimensional (1D) list of six different symbols, for
example, [‘@’, ‘#’, ‘X’, ‘O’, ‘1’, ‘0’ ] and builds a two-dimensional (2D) list consisting of three
nested lists, for example, [[‘@’, ‘#’], [‘X’, ‘O’], [‘1’, ‘0’]], each containing two symbols
from the ID list in the order they appear in the 1D (see the below examples on the resulting 2D list).

Parameters:
symbols_list (a 1D list of strings): This parameter reflects six pre-defined symbols.

Assumptions: None

Return value: The 2D list of game symbols (Type: list)

Examples:
The row no. cannot be greater than 2 and the column no. cannot be greater than 1.

Symbols_list = [‘@’, ‘#’, ‘X’, ‘O’, ‘1’, ‘0’]


define_symbols(symbols_list) -> [[‘@’, ‘#’], [‘X’, ‘O’], [‘1’, ‘0’]]

symbols_list = [‘+’, ‘=’, ‘?’, ‘:’, ‘$’, ‘!’]


define_symbols(symbols_list) -> [[‘+’, ‘=’], [‘?’, ‘:’], [‘$’, ‘!’]]

symbols_list = [[‘&’, ‘^’, ‘Q’, ‘U’, ‘0’, ‘9’]


define_symbols(symbols_list) -> [[‘&’, ‘^’], [‘Q’, ‘U’], [‘0’, ‘9’]]

#------------------------------------------------------------------------------------------#
# Task 2: First Player Game Symbol Selection (9 points) #
#------------------------------------------------------------------------------------------#

def select_player_symbol(symbols, symbol_row_no, symbol_col_no):

Description: This function takes a pre-defined 2D list containing three pairs of symbols, for example,
[[‘@’, ‘#’], [‘X’, ‘O’], [‘1’, ‘0’]] , and the index containing the player’s desired game
symbol using the row number and column number, for example, symbol X is at index [1][0] (see the
below figure). It iterates through the 2D list elements, locates the desired symbol by matching the row
no. (e.g., 1) and column no. (e.g., 0), and returns the symbol (e.g., X).

Parameters:
symbols (a multi-dimensional list of strings) reflects the pre-defined list of game symbols.
symbol_row_no (an integer ) represents the row no. containing the desired symbol.
symbol_col_no (an integer ) represents the col no. containing the desired symbol.

Assumptions: The row no. and column no. are integers. The 2D list of symbols cannot be empty.

Return value: The player’s desired symbol (Type: string).

3
0 1
0 [0][0] [0][1]
@ #
[1][0] [1][1]
1 X O
[2][0] [2][1]
2 1 0

Figure 1: Shows a diagram of a multi-dimensional list called symbol where the elements are accessed
using the following syntax: symbol [row no.][column no.]. For example, the element, @, in the first cell has a
row no. =0 and a column no. = 0.

Examples:
The row and col no values are (0-2). Values not within the range will return list index out of range
error.

Symbols = [[‘@’, ‘#’], [‘X’, ‘O’], [‘1’, ‘0’]]


select_player_symbol(symbols, 2, 1) -> 0
select_player_symbol(symbols, 1, 0) -> X
select_player_symbol(symbols, 0, 1) -> #
select_player_symbol(symbols, 1, 1) -> O

#-------------------------------------------------------------------------------------------#
# Task 3: Second Player Game Symbol Selection (9 points) #
#-------------------------------------------------------------------------------------------#

def select_other_player_symbol(symbols, player1_symbol):

Description: This function takes a pre-defined 2D list containing three pairs of symbols, for example, (@
and #), (X and O), and (1, 0), and a symbol selected by the first player (for example: X). It iterates through
the elements of the 2D list, locates the symbol that matches the first player’s symbol, assigns its pair
(i.e., O) to the second player, and returns the assigned symbol.

Parameters:
symbols (a multi-dimensional list of strings) reflects the pre-defined list of game symbols.
player1_symbol(a string ) representing the symbol selected by the first player (player 1).

Assumptions: At least a player has selected one symbol. The 2D list of symbols can have some of its
symbols empty, but it cannot be all empty.

Return value: The other or second player symbol (Type: string).

Examples:
symbols = [[‘@’, ‘#’], [‘X’, ‘O’], [‘1’, ‘0’]]
select_other_player_symbol(symbols, ‘#’) -> @
select_other_player_symbol(symbols, ‘O’) -> X
select_other_player_symbol(symbols, ‘1’) -> 0

4
#-------------------------------------------------------------------------------------------#
# Task 4: Update the Game Symbols After Symbol Selection (9 points) #
#-------------------------------------------------------------------------------------------#

def update_symbols(symbols, player1_symbol, player2_symbol):

Description: This function takes a pre-defined 2D list containing three pairs of symbols, for example, (@
and #), (X and O), and (1, 0), and the symbols selected by the first and second player (for example, X and
O). It iterates through the elements of the 2D list, locates the symbol that matches the first player (player
1) symbol, replaces it with “-“ (hyphen) to indicate the symbol is no longer available for selection,
locates the symbol that matches the other player (player 2) symbol, replaces it with “-“ to indicate the
symbol is no longer available for selection, and returns the updated 2D list of symbols.

Parameters:
symbols(a multi-dimensional list of strings) reflects the pre-defined list of game symbols.
player1_symbol(a string ) representing the symbol selected by the first player (player 1).
player2_symbol(a string ) representing the symbol selected by the other player (player 2).

Assumptions: At least each player has selected a symbol. The 2D list of symbols cannot be empty.

Return value: The updated 2D list of symbols (Type: list).

Examples:
symbols = [[‘@’, ‘#’], [‘X’, ‘O’], [‘1’, ‘0’]]
update_symbols(symbols, ‘#’ , ‘@’) -> [[‘-‘, ‘-‘], [‘X’, ‘O’], [‘1’, ‘0’]]
update_symbols(symbols, ‘X’ , ‘O’) -> [[‘@’, ‘#’], [‘-‘, ‘-‘], [‘1’, ‘0’]]
update_symbols(symbols, ‘0’ , ‘1’) -> [[‘@’, ‘#’], [‘X’, ‘O’], [‘-‘, ‘-‘]]

#-------------------------------------------------------------------------------------------#
# Task 5: Initialize Game Board (4.5 points) #
#-------------------------------------------------------------------------------------------#

def initialize_game_board(initial_symbol_list):

Description: This function takes a 1D list, initially containing three identical copies of the initial symbol *
(asterisk), for example, [‘*’, ‘*’, ‘*’], and expands it to nine identical copies of the initial symbol *,
for example, [‘*’, ‘*’, ‘*’, ‘*’, ‘*’, ‘*’, ‘*’, ‘*’, ‘*’ ]. It builds a multi-dimensional (multi-
D) list consisting of three nested lists, for example, [[‘*’,’*’,’*’],[‘*’,’*’, ‘*’],[‘*’,’*’, ‘*’]],
each containing three similar symbols from the ID list to initialize and return the game board it.

Parameters:
initial_symbol_list (a 1D list of strings) representing the initial list of three asterisks [‘*’, ‘*’, ‘*’]

Assumptions: The board is a multi-D list is a 3 (rows) x 3 (columns) list that is initially empty.

Return value: The initial board as a multi-D (3 X 3) list whose elements are asterisks * (Type: list).

Examples:
initial_symbol_list = [‘*’, ‘*’, ‘*’]
initialize_game_board(initial_symbol_list)->[[‘*’,’*’,’*’],[‘*’,’*’, ‘*’],[‘*’,’*’, ‘*’]]

5
#-------------------------------------------------------------------------------------------#
# Task 6: Players Marking the Board (9 pints) #
#-------------------------------------------------------------------------------------------#

def mark_board(board, move_row_no, move_col_no, player_symbol):

Description: This function takes an initialized game board [[‘*’,’*’,’*’],[‘*’,’*’, ‘*’],[‘*’,’*’,


‘*’]], the move row no. and move column no., and the player symbol. For example, if player 1 with
symbol X wants to place symbol X in the first cell, the row no is 0 and the column no is 0 (see the below
figure). It then iterate over the board elements while checking if the selected board cell using the row and
col number is empty. If empty, it replaces the initial symbol with the player symbol.

Parameters:
board (a multi-dimensional list of strings) reflects the game board in an empty state, i.e., all the elements
are * (asterisks).
move_row_no (an integer ) representing the no. of the row containing the board cell to mark with
the player symbol.
move_col_no (an integer ) representing the no. of the col containing the board cell to mark with
the player symbol.

Assumptions: The board is a multi-D list (3 X3). The function assumes that players will not enter the row
no and column no of a previously marked cell. Values not within the range will return list index out of
range error.

Return value: return the marked board (Type: list).

0 1 2
0
[0][0] [0][1] [0][2]
1
[1][0] [1][1] [1][2]
2
[2][0] [2][1] [2][2]

Figure 2: Shows a diagram of a multi-dimensional list called board where the elements are accessed using
the following syntax: board [row no.][column no.]. The element in the first cell has a row no. =0 and a
column no. = 0.

Examples:

board = [['*', '*', '*'], ['*', '*', '*'], ['*', '*', '*']]
mark_board(board, 1, 1, 'O') -> [['*', '*', '*'], ['*', 'O', '*'], ['*', '*', '*']]

board = [['*', '*', '*'], ['*', 'O', '*'], ['*', '*', '*']]
mark_board(board, 0, 0, 'X') -> [['X', '*', '*'], ['*', 'O', '*'], ['*', '*', '*']]

board = [['X', '*', '*'], ['*', 'O', '*'], ['*', '*', '*']]
mark_board(board, 2, 2, 'O') -> [['X', '*', '*'], ['*', 'O', '*'], ['*', '*', 'O']]

6
Testing Your Code
In this assignment, testing is likePA2 and PA3. You are given several tasks and for each of them you must
implement one Python function. A template with the function signatures is provided to you. The unit tester calls
your functions with certain arguments and examines the return values to decide the correctness of your code.
Your functions should not ask for user input and should not print anything, just return a value based on the
specification of the tasks.
To test your code with the provided tester, first make sure that you have downloaded/copied the file "tester4.py"
to the same folder as your assignment code. Then, from that directory, run the following command using your
command-line/terminal interface:
python3 tester4.py gmason_299_PA4.py
Running required definitions:
.............................................
----------------------------------------------------------------------
Ran 16 tests in 0.003s

OK

16/16 Required test cases passed

*Instead of python3, use py or python on Windows machines.


If you pass all the tests, you will see the above contents. If you do not pass a test, you will be informed of where
the mismatch in your output is, and you can figure out from there what needs to be fixed. Run the tester file using
the above command only AFTER you have resolved any syntax errors or run-time errors in your code by first
running the command:
python yourfilename.py
*Mac machines use python3

Submitting Your Code

Submit your .py file to Gradescope under Programming Assignment 4

• Link to Gradescope is in our Canvas page under the link “Gradescope”


• Do not submit the tester files, only submit your assignment code (.py) file.
• Do not forget to name your file netID_2xx_PA4.py, where netID is NOT your G-number but your GMU email
ID and 2xx is your lab section number.
• Do not forget to copy and paste the honor code statement from the Assignments Basics document to the
beginning of your submission .py file.
• Do not forget to comment your code!
• No hard coding!

Grading Rubric:
Correct Submission (file is named correctly): 1 pt # see Assignment Basics file for file
requirements!

Well-Documented: 4 pts
(comments + honor code statement) # see Assignment Basics file for how to
comment!

7
Tester Calculations Correct: 45 pts # see Assignment Basics file for how to test!
------------------------------------------------------------
TOTAL: 50 pts
Note: If your code does not run (immediately crashes due to errors), it will receive very few points. No
exceptions. As stated on our syllabus, turning in running code is essential.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy