Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help find get_row and slide_right already built calculate_increment:(direction:str) -> int, direction can be down downleft downright and across, and calculate_increment(down)=5, downleft=4 downright=6 across=1. The objective

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedhelp find get_row and slide_right

already built calculate_increment:(direction:str) -> int, direction can be down downleft downright and across, and calculate_increment(down)=5, downleft=4 downright=6 across=1.

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: Across Down Left Down Right Down 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: 1 2 3 4 5 1 ABCDE 2 F|GH|I|J 3 KLMNO 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 o A (1, 1) (1, 2) 1 B 2 C 3 D 4 E (1,3) (1,4) (1,5) (2, 1) (2, 2) (2,3) (2,4) 5 F 6 G 7 H 1 o o 10 IK 11 L (2,5) (3, 1) (3, 2) (3, 3) (3, 4) (3,5) 12 M 13 IN 14 O (4,1) 15 P (4,2) 16 Q (4,3) 17 R (4,4) 18 S (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) get_row (int, str) -> str The first parameter is a row number and the second parameter is the string representation of a game board. Assume that the row number and game board are valid. The game board size is given by the constants N_Rows and N_COLUMNS. This function returns only the characters in the game board found at the given row in the same left-to-right order as they appear in the row. The first parameter is the square being added to the game board. The second parameter is a row number and the third parameter is the string representation of a game board. Assume that the square, row number, and game board are valid. This function returns a string that is like the original game board, except that: slide_right (str, int, str) -> str - the square from the first parameter has been slid into the "beginning" (i.e., first column) of the given row number; - the square at the "end" (i.e., last column) has slid off the game board; - the remaining squares have slid to the right. 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: Across Down Left Down Right Down 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: 1 2 3 4 5 1 ABCDE 2 F|GH|I|J 3 KLMNO 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 o A (1, 1) (1, 2) 1 B 2 C 3 D 4 E (1,3) (1,4) (1,5) (2, 1) (2, 2) (2,3) (2,4) 5 F 6 G 7 H 1 o o 10 IK 11 L (2,5) (3, 1) (3, 2) (3, 3) (3, 4) (3,5) 12 M 13 IN 14 O (4,1) 15 P (4,2) 16 Q (4,3) 17 R (4,4) 18 S (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) get_row (int, str) -> str The first parameter is a row number and the second parameter is the string representation of a game board. Assume that the row number and game board are valid. The game board size is given by the constants N_Rows and N_COLUMNS. This function returns only the characters in the game board found at the given row in the same left-to-right order as they appear in the row. The first parameter is the square being added to the game board. The second parameter is a row number and the third parameter is the string representation of a game board. Assume that the square, row number, and game board are valid. This function returns a string that is like the original game board, except that: slide_right (str, int, str) -> str - the square from the first parameter has been slid into the "beginning" (i.e., first column) of the given row number; - the square at the "end" (i.e., last column) has slid off the game board; - the remaining squares have slid to the right

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Ehs 2.0 Revolutionizing The Future Of Safety With Digital Technology

Authors: Tony Mudd

1st Edition

B0CN69B3HW, 979-8867463663

More Books

Students also viewed these Databases questions