Question
slide_right THIS WAS ALL THE INFORMATION FOR MY FUNCTION THE EXAMPLE of one function def calculate_inx(ROWS: int, COLS: int) -> int: Returns the index
slide_right
THIS WAS ALL THE INFORMATION FOR MY FUNCTION
THE EXAMPLE of one function
def calculate_inx(ROWS: int, COLS: int) -> int: """ Returns the index in the string representation of the game board \ corresponding to the given row and column location
>>> calculate_inx(4,2) 16 """ Index = [(ROWS - 1) * N_COLUMNS + (COLS - 1)] return Index
THE CONSTANTS
N_COLUMNS = 5 # Number of columns in the game board N_ROWS = 4 # Number of rows in the game board
BLACK_SQUARE = '#' # The character that represents a black square RED_SQUARE = 'R' # The character that represents a red square YELLOW_SQUARE = 'Y' # The character that represents a blue square
ACROSS = 'across' # The horizontal direction DOWN = 'down' # The vertical direction DOWN_RIGHT = 'dright' # The diagonal direction: downward and rightward DOWN_LEFT = 'dleft' # The diagonal direction: downward and leftward
I NEED HELP TO WRTIE THIS FUNCTION PLEASEEE HELP I HAVE PROVIDED EVERY INFORMATION ABOVE
I NEED HELP FOR slide_right and slide_left
PLEASE SOMEONE HELP ASSSAAAPPPPPPPP.........
Goals of this assignment The main goal of this assignment is that students will use the Function Design Recipe to plan, document, implement, and test functions. This entails the following sub-goals: - Students will describe what a function does appropriately using docstrings. - Students will show how to use a function appropriately with docstring examples. - Students will write function bodies using variables, numeric types, strings, and conditional statements. Students will learn to use: Python 3, Wing 101, the provided starter code, a checker module, and other tools. The Slide Game The Slide Game is a two player game played on a two-dimensional game board. The game board has a number of rows and columns specified at the beginning of the game and starts off filled with black squares. The following image is an example of a game board with 4 rows and 5 columns: 1 2. 3 4 5 1 2 3 4 Each player has an infinite number of squares in one colour (e.g., one player has red squares; the other player has yellow Each player has an infinite number of squares in one colour (e.g., one player has red squares; the other player has yellow squares). There are two moves that a player can make to get their squares onto the game board: 1. Slide Right 2. Slide Left The first player to place NUM_MATCH of their squares in a straight line wins the game. Here, NUM_MATCH is a number like 3. The "straight line" can be a horizontal, vertical, or diagonal line of squares on the game board. Slide Right The "Slide Right" move allows a player to slide one of their squares onto the game board. The player must indicate which row they would like to slide their square onto. Because the size of the game board is fixed, the right-most square in the game board will "fall off" to make room for the player's own square. Slide Right Slide Left The "Slide Left" move allows a player to slide one of their squares onto the game board. The player must indicate which row they ould like to slide eir square onto. Because the size of the game board is fixed, the left-most square in the game board will "fall off" to make room for the player's own square. Slide Left The "Slide Left" move allows a player to slide one of their squares onto the game board. The player must indicate which row they would like to slide their square onto. Because the size of the game board is fixed, the left-most square in the game board will "fall off" to make room for the player's own square. Slide Left Winning the game The objective of the game is to be the first player to connect NUM_MATCH Squares in a row either horizontally (or simply "across"), vertically (or simply "down"), or diagonally. Diagonal lines come in two orientations: downward to the right and downward to the left. Assuming NUM_MATCH is 3, here are some examples of where squares should be placed in order to win the game: Constants Constants are special variables whose values should not change once assigned -- you saw constants in the Week 2 Prepare module. A different naming convention (uppercase pothole) is used for constants, so that programmers know to not change their values. For example, in the starter code, the constant BLACK_SQUARE is assigned the value '#' at the beginning of the module and the value of BLACK_SQUARE should never change in your code. When writing your code, if you need to use a black square, you should use BLACK_SQUARE . The same applies for the other constant values. Using constants simplifies code modifications and improves readability and changeability. For example, if we later decide to use a different character to represent a black square, we would only have to make a change in one place (the BLACK_SQUARE assignment statement), rather than throughout the program. This also makes our code more readable whether we use '#' or any other character to represent a black square, we write our code using the constant BLACK_SQUARE so it is clear to the reader what we mean. Representing the game board A game board can be thought of as a table with rows (see the constant N_ROWS ) and columns (N_COLUMNS ). The size of a game board does not change during the course of a game. For example, here is an empty game board with 4 rows and 5 columns: 1 2 3 4 5 1 # # # # # 2 # # # # # 3 # # # # # 4 # # # # # A location in the game board can be described by indicating the row and then the column. For example, using the empty game board above, (1, 1) indicates the location at the top-left of the game board. And (4,5) indicates the bottom-right of the game board. We will use strings (i.e., the str type) to represent the squares on a game board: - The black squares will be: '#' BLACK_SQUARE ). We will use strings (i.e., the str type) to represent the squares on a game board: - The black squares will be: '#' (BLACK_SQUARE ). - The red squares will be: 'R' RED_SQUARE ). - The yellow squares will be: 'Y' YELLOW_SQUARE ). We have not yet studied how to store 2-dimensional information in Python programs, so we will need a different way to represent our game board. The approach that we will take for this assignment is to store the rows one after another, starting with the first row. - Using our empty (all black squares) game board example, this is: '#################### - Using a slightly more interesting example: 1 2 3 4 5 1 R |# # # # 2 # # # # Y 3 R | # # # # 4 Y# # # # Would be represented as 'R########YR####Y#### Accessing a square in the game board We have used row and column indices to describe the position of each square in the grid representation of a game board. But each character in a Python str is in a position that is described by a single index. How is the Python program going to translate between row and column indices and str indices? To answer this question, we will have to determine a formula! Consider the following concrete example: row and column indices and str indices? to answer this question, we will have to determine a formula! Consider the following concrete example: 1 2 3 4 5 1 ABCDE 2 F|G|H|I|J 3 K|L|M|N|O 4 PIQRIST Which, as a string, is: 'ABCDEFGHIJKLMNOPQRST' . Let us use the table below to derive a formula that calculates the index based on: the location (i.e., row and column) and game board size (i.e., N_ROWS and/or N_COLUMNS ). Location Index Character (1, 1) o 1 B (1,2) |(1,3) 2 (1,4) 3 4 5 In mo 6 7 8 | (1,5) (2, 1) (2, 2) |(2,3) |(2, 4) (2,5) (3, 1) (3, 2) (3, 3) (3, 4) (3,5) 9 J 10 K 11 L 12 M 13 N 14 0 15 P (4,1) (4, 2) 16 Q (4,3) 17 IR 18 S (4,4) (4,5) 19 T From the table above, we see that the character in the square with position (2, 1) (i.e., the square at row 2 and column 1) has index 5. The other squares in that row have indices 6, 7, 8 and 9. We conclude that when moving one square across a row, the index increases by 1. The squares in column 3 have indices 2, 7, 12 and 17. Moving one square down a column increases the index by 5, which is the number of columns of the game board. We conclude that when moving one square down a column, the index increases by the game board's number of columns. Let us now introduce variables that will allow us to express the formula explicitly. Let a variable named str_index refer to the position of a square in the str representation of a game board with size N_ROWS rows and N_COLUMNS columns. Let variables row and col refer to the position of the same square in the grid representation of a game board. From what we have seen, we can conclude that str_index depends on row, col, and N_COLUMNS . That is, the following formula will compute str_index : (row - 1) * N_COLUMNS + (col - 1) The minus 1's are needed for the arithmetic to work out. This is because we chose to index our rows and columns starting at 1 (as a thought exercise, what would the formula be if we chose to index the rows and columns starting at 0?). What to do At a high-level, your next steps are to: 1. Open the file slide_functions.py . 2. Make sure that the file is in the same directory as slide_game.py, al_checker.py , and pyta. 3. Complete the function definitions below. Make sure to follow the Function Design Recipe that you have been learning in this (row 1) N_COLUMNS + (col - 1) The minus 1's are needed for the arithmetic to work out. This is because we chose to index our rows and columns starting at 1 (as a thought exercise, what would the formula be if we chose to index the rows and columns starting at 0?). What to do At a high-level, your next steps are to: 1. Open the file slide_functions.py . 2. Make sure that the file is in the same directory as slide_game.py, a1_checker.py, and pyta. 3. Complete the function definitions below. Make sure to follow the Function Design Recipe that you have been learning in this course. 4. Test your Python file by using the Python shell and al_checker.py (see below). We have included the type contracts and a specification of every function below; please read through them carefully. We will be evaluating your docstrings in addition to your code; include two examples in your docstrings. You will need to paraphrase the specifications of the functions to get an appropriate docstring description. Make sure you review the CSCA08 Python Style Guidelines for the rules on how to write a docstring description. The functions listed below do not need to be completed in order -- if you get stuck on one, try another! Functions to write for A1 Function name: (Parameter types) -> Return type Specifications Notes 822 slide_right (str, int, str) -> str The first parameter is the square being added to the game board. The second parameter is a row number - Follow the "Function Design and the third parameter is the string representation Recipe" to complete this functions. of a game board. - Review the "str: indexing and Assume that the square, row number, and game slicing" material in the PCRS Week board are valid. 3 Prepare module. This function returns a string that is like the original - Re-read the "Slide right" section game board, except that: if you are stuck. - the square from the first parameter has been slid - Review the "Function Reuse" into the "beginning" (i.e., first column) of the given material in the PCRS Week 2 row number; Prepare module. Consider using - the square at the "end" (i.e., last column) has slid your calculate_str_index functions off the game board; to help. - the remaining squares have slid to the right. - the remaining squares have slid to the right. The first parameter is the square being added to the game board. The second parameter is a row and the Follow the "Function Design third parameter is the string representation of a Recipe" to complete this functions. game board. - Review the "str: indexing and Assume that the square, row number, and game slicing" material in the PCRS Week board are valid. 3 Prepare module. slide_left This function returns a string that is like the original game board, except that: - Re-read the "Slide left" section if you are stuck. (str, int, str) -> str - the square from the first parameter has been slid into the "end" (i.e., last column) of the given row number; - Review the "Function Reuse" material in the PCRS Week 2 Prepare module. Consider using your calculate_str_index functions to help. - the square at the beginning" (i.e., first column) has slid off the game board; - the remaining squares have slid to the left. Goals of this assignment The main goal of this assignment is that students will use the Function Design Recipe to plan, document, implement, and test functions. This entails the following sub-goals: - Students will describe what a function does appropriately using docstrings. - Students will show how to use a function appropriately with docstring examples. - Students will write function bodies using variables, numeric types, strings, and conditional statements. Students will learn to use: Python 3, Wing 101, the provided starter code, a checker module, and other tools. The Slide Game The Slide Game is a two player game played on a two-dimensional game board. The game board has a number of rows and columns specified at the beginning of the game and starts off filled with black squares. The following image is an example of a game board with 4 rows and 5 columns: 1 2. 3 4 5 1 2 3 4 Each player has an infinite number of squares in one colour (e.g., one player has red squares; the other player has yellow Each player has an infinite number of squares in one colour (e.g., one player has red squares; the other player has yellow squares). There are two moves that a player can make to get their squares onto the game board: 1. Slide Right 2. Slide Left The first player to place NUM_MATCH of their squares in a straight line wins the game. Here, NUM_MATCH is a number like 3. The "straight line" can be a horizontal, vertical, or diagonal line of squares on the game board. Slide Right The "Slide Right" move allows a player to slide one of their squares onto the game board. The player must indicate which row they would like to slide their square onto. Because the size of the game board is fixed, the right-most square in the game board will "fall off" to make room for the player's own square. Slide Right Slide Left The "Slide Left" move allows a player to slide one of their squares onto the game board. The player must indicate which row they ould like to slide eir square onto. Because the size of the game board is fixed, the left-most square in the game board will "fall off" to make room for the player's own square. Slide Left The "Slide Left" move allows a player to slide one of their squares onto the game board. The player must indicate which row they would like to slide their square onto. Because the size of the game board is fixed, the left-most square in the game board will "fall off" to make room for the player's own square. Slide Left Winning the game The objective of the game is to be the first player to connect NUM_MATCH Squares in a row either horizontally (or simply "across"), vertically (or simply "down"), or diagonally. Diagonal lines come in two orientations: downward to the right and downward to the left. Assuming NUM_MATCH is 3, here are some examples of where squares should be placed in order to win the game: Constants Constants are special variables whose values should not change once assigned -- you saw constants in the Week 2 Prepare module. A different naming convention (uppercase pothole) is used for constants, so that programmers know to not change their values. For example, in the starter code, the constant BLACK_SQUARE is assigned the value '#' at the beginning of the module and the value of BLACK_SQUARE should never change in your code. When writing your code, if you need to use a black square, you should use BLACK_SQUARE . The same applies for the other constant values. Using constants simplifies code modifications and improves readability and changeability. For example, if we later decide to use a different character to represent a black square, we would only have to make a change in one place (the BLACK_SQUARE assignment statement), rather than throughout the program. This also makes our code more readable whether we use '#' or any other character to represent a black square, we write our code using the constant BLACK_SQUARE so it is clear to the reader what we mean. Representing the game board A game board can be thought of as a table with rows (see the constant N_ROWS ) and columns (N_COLUMNS ). The size of a game board does not change during the course of a game. For example, here is an empty game board with 4 rows and 5 columns: 1 2 3 4 5 1 # # # # # 2 # # # # # 3 # # # # # 4 # # # # # A location in the game board can be described by indicating the row and then the column. For example, using the empty game board above, (1, 1) indicates the location at the top-left of the game board. And (4,5) indicates the bottom-right of the game board. We will use strings (i.e., the str type) to represent the squares on a game board: - The black squares will be: '#' BLACK_SQUARE ). We will use strings (i.e., the str type) to represent the squares on a game board: - The black squares will be: '#' (BLACK_SQUARE ). - The red squares will be: 'R' RED_SQUARE ). - The yellow squares will be: 'Y' YELLOW_SQUARE ). We have not yet studied how to store 2-dimensional information in Python programs, so we will need a different way to represent our game board. The approach that we will take for this assignment is to store the rows one after another, starting with the first row. - Using our empty (all black squares) game board example, this is: '#################### - Using a slightly more interesting example: 1 2 3 4 5 1 R |# # # # 2 # # # # Y 3 R | # # # # 4 Y# # # # Would be represented as 'R########YR####Y#### Accessing a square in the game board We have used row and column indices to describe the position of each square in the grid representation of a game board. But each character in a Python str is in a position that is described by a single index. How is the Python program going to translate between row and column indices and str indices? To answer this question, we will have to determine a formula! Consider the following concrete example: row and column indices and str indices? to answer this question, we will have to determine a formula! Consider the following concrete example: 1 2 3 4 5 1 ABCDE 2 F|G|H|I|J 3 K|L|M|N|O 4 PIQRIST Which, as a string, is: 'ABCDEFGHIJKLMNOPQRST' . Let us use the table below to derive a formula that calculates the index based on: the location (i.e., row and column) and game board size (i.e., N_ROWS and/or N_COLUMNS ). Location Index Character (1, 1) o 1 B (1,2) |(1,3) 2 (1,4) 3 4 5 In mo 6 7 8 | (1,5) (2, 1) (2, 2) |(2,3) |(2, 4) (2,5) (3, 1) (3, 2) (3, 3) (3, 4) (3,5) 9 J 10 K 11 L 12 M 13 N 14 0 15 P (4,1) (4, 2) 16 Q (4,3) 17 IR 18 S (4,4) (4,5) 19 T From the table above, we see that the character in the square with position (2, 1) (i.e., the square at row 2 and column 1) has index 5. The other squares in that row have indices 6, 7, 8 and 9. We conclude that when moving one square across a row, the index increases by 1. The squares in column 3 have indices 2, 7, 12 and 17. Moving one square down a column increases the index by 5, which is the number of columns of the game board. We conclude that when moving one square down a column, the index increases by the game board's number of columns. Let us now introduce variables that will allow us to express the formula explicitly. Let a variable named str_index refer to the position of a square in the str representation of a game board with size N_ROWS rows and N_COLUMNS columns. Let variables row and col refer to the position of the same square in the grid representation of a game board. From what we have seen, we can conclude that str_index depends on row, col, and N_COLUMNS . That is, the following formula will compute str_index : (row - 1) * N_COLUMNS + (col - 1) The minus 1's are needed for the arithmetic to work out. This is because we chose to index our rows and columns starting at 1 (as a thought exercise, what would the formula be if we chose to index the rows and columns starting at 0?). What to do At a high-level, your next steps are to: 1. Open the file slide_functions.py . 2. Make sure that the file is in the same directory as slide_game.py, al_checker.py , and pyta. 3. Complete the function definitions below. Make sure to follow the Function Design Recipe that you have been learning in this (row 1) N_COLUMNS + (col - 1) The minus 1's are needed for the arithmetic to work out. This is because we chose to index our rows and columns starting at 1 (as a thought exercise, what would the formula be if we chose to index the rows and columns starting at 0?). What to do At a high-level, your next steps are to: 1. Open the file slide_functions.py . 2. Make sure that the file is in the same directory as slide_game.py, a1_checker.py, and pyta. 3. Complete the function definitions below. Make sure to follow the Function Design Recipe that you have been learning in this course. 4. Test your Python file by using the Python shell and al_checker.py (see below). We have included the type contracts and a specification of every function below; please read through them carefully. We will be evaluating your docstrings in addition to your code; include two examples in your docstrings. You will need to paraphrase the specifications of the functions to get an appropriate docstring description. Make sure you review the CSCA08 Python Style Guidelines for the rules on how to write a docstring description. The functions listed below do not need to be completed in order -- if you get stuck on one, try another! Functions to write for A1 Function name: (Parameter types) -> Return type Specifications Notes 822 slide_right (str, int, str) -> str The first parameter is the square being added to the game board. The second parameter is a row number - Follow the "Function Design and the third parameter is the string representation Recipe" to complete this functions. of a game board. - Review the "str: indexing and Assume that the square, row number, and game slicing" material in the PCRS Week board are valid. 3 Prepare module. This function returns a string that is like the original - Re-read the "Slide right" section game board, except that: if you are stuck. - the square from the first parameter has been slid - Review the "Function Reuse" into the "beginning" (i.e., first column) of the given material in the PCRS Week 2 row number; Prepare module. Consider using - the square at the "end" (i.e., last column) has slid your calculate_str_index functions off the game board; to help. - the remaining squares have slid to the right. - the remaining squares have slid to the right. The first parameter is the square being added to the game board. The second parameter is a row and the Follow the "Function Design third parameter is the string representation of a Recipe" to complete this functions. game board. - Review the "str: indexing and Assume that the square, row number, and game slicing" material in the PCRS Week board are valid. 3 Prepare module. slide_left This function returns a string that is like the original game board, except that: - Re-read the "Slide left" section if you are stuck. (str, int, str) -> str - the square from the first parameter has been slid into the "end" (i.e., last column) of the given row number; - Review the "Function Reuse" material in the PCRS Week 2 Prepare module. Consider using your calculate_str_index functions to help. - the square at the beginning" (i.e., first column) has slid off the game board; - the remaining squares have slid to the leftStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started