1. Homepage
  2. Programming
  3. CSC108 Introduction to Computer Programming - Assignment 1 | The Slide Game

CSC108 Introduction to Computer Programming - Assignment 1 | The Slide Game

Engage in a Conversation
CanadaUniversity of TorontoCSC108Introduction to Computer ProgrammingThe Slide Game

Assignment 1 | The Slide Game

In Assignment 1, you write Python code that will be used by a game called "The Slide Game". You can complete the whole assignment with only the concepts from Weeks 1, 2, and 3 of the course. This handout explains the problem being solved, and the tasks to complete, for the assignment. Please read it carefully and in its entirety. CourseNana.COM

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: CourseNana.COM

CourseNana.COM

CourseNana.COM

    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: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.

CourseNana.COM

Slide Right

CourseNana.COM

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. CourseNana.COM

CourseNana.COM

Slide Left

CourseNana.COM

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. CourseNana.COM

CourseNana.COM

Winning the game

CourseNana.COM

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 downdward to the left. Assuming NUM_MATCH is 3, here are some examples of where squares should be placed in order to win the game: CourseNana.COM



CourseNana.COM

The Slide Game in Python

In this assignment, you will be writing Python code for the Slide Game. Instead of using colourful pictures of squares on a game board (as we saw in the last section), in our Python code we will use text to represent the squares (i.e., Python's str type). In the rest of this section, we provide some details to help you understand how to approach the code you need to write. CourseNana.COM

Starter Code

    For this assignment, we are giving you some files to help you get started. Please download the Assignment 1 Files and extract the zip archive. In total, we have provided you with three Python files: CourseNana.COM

    • slide_functions.py

      This is the file where you will write your solution. Your job is to complete the file by implementing all the required functions. See the What to do section for more details. CourseNana.COM

    • slide_game.py

      This is the main program that we have implemented for you. You will not modify this file. But you can run this file to play The Slide Game. Note that this won't be possible until you have correctly completed the functions in slide_functions.py. CourseNana.COM

    • a1_checker.py

      This is a checker program that you should use to check your code. You will not modify this file. See below for more information about a1_checker.py. CourseNana.COM

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 SQUARE_BLACK is assigned the value '-' at the beginning of the module and the value of SQUARE_BLACK should never change in your code. When writing your code, if you need to use a black square, you should use SQUARE_BLACK. The same applies for the other constant values. CourseNana.COM

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 SQUARE_BLACK 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 SQUARE_BLACK so it is clear to the reader what we mean. CourseNana.COM

Representing the game board

A game board can be thought of as a table with rows (see the constant NUM_ROW) and columns (NUM_COL). 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: CourseNana.COM

                           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. CourseNana.COM

We will use strings (i.e., the str type) to represent the squares on a game board. The black squares will be: '-' (SQUARE_BLACK). The red squares will be: 'R' (SQUARE_RED). The yellow squares will be: 'Y' (SQUARE_YELLOW). CourseNana.COM

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: CourseNana.COM

                           1   2   3   4   5
        1  R | - | - | - | -
        2  - | - | - | - | Y
        3  R | - | - | - | -
        4  Y | - | - | - | -
                

Would be represented as 'R--------YR----Y----'. CourseNana.COM

Accessing a square in the game board

CourseNana.COM

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! CourseNana.COM

Consider the following concrete example: CourseNana.COM

                           1   2   3   4   5
        1  A | B | C | D | E
        2  F | G | H | I | J
        3  K | L | M | N | O
        4  P | Q | R | S | T
                

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., NUM_ROW and/or NUM_COL). CourseNana.COM

locationindexcharacter
(1, 1)0A
(1, 2)1B
(1, 3)2C
(1, 4)3D
(1, 5)4E
(2, 1)5F
(2, 2)6G
(2, 3)7H
(2, 4)8I
(2, 5)9J
(3, 1)10K
(3, 2)11L
(3, 3)12M
(3, 4)13N
(3, 5)14O
(4, 1)15P
(4, 2)16Q
(4, 3)17R
(4, 4)18S
(4, 5)19T

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. CourseNana.COM

The squares in column 3 have indices 2, 7, 12 and 17. Moving one square down a column increases the index by 5, 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. CourseNana.COM

Let us now introduce variables that will allow us to express the formula explicitly. CourseNana.COM

Let a variable named str_index refer to the position of a square in the str representation of a game board with size NUM_ROW rows and NUM_COL 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 rowcol, and NUM_COL. That is, the following formula will compute str_index: CourseNana.COM

(row - 1) * NUM_COL + (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?). CourseNana.COM

What to do

CourseNana.COM

    At a high-level, your next steps are to:
    • Open the file slide_functions.py.
    • Make sure that the file is in the same directory as slide_game.pya1_checker.py, and pyta.
    • Complete the function definitions below. Make sure to follow the Function Design Recipe that you have been learning in this course.
    • Test your Python file by using the Python shell and a1_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 CSC108 Python Style Guidelines for the rules on how to write a docstring description. CourseNana.COM

    The functions listed below do not need to be completed in order -- if you get stuck on one, try another!
    1. Function: create_empty_board() -> str

      Specification: This function has no parameters. This function returns a string for storing information about a game board that has NUM_ROW rows and NUM_COL columns. Each character in the returned string is to have been set to the SQUARE_BLACK character. CourseNana.COM

      Notes: CourseNana.COM

      • We have completed this function for you.
      • You do not need to include docstring examples for this function.
    2. Function: is_board_full(str) -> bool

      Specification: This function has one parameter that refers to a valid game board. This function returns True if and only if all of the squares in the game board are not black. That is, True is returned if and only if there are no SQUARE_BLACK characters in the game board. CourseNana.COM

      Notes: CourseNana.COM

      • We have partially completed this function for you.
      • You must complete the function body yourself.
      • You do not need to add any more docstring examples for this function.
    3. Function: between(str, int, int) -> bool

      Specification: This function has three parameters. The first parameter refers to a str value, the second to the minimum value for a range of values, and the third to the maximum value for a range of values. Assume that the str value can be converted to an integer. Assume that the value of the second parameter is less than or equal to the value of the third parameter. This function returns True if and only if the converted integer value of the first parameter is not less than the second parameter and not more than the third parameter. In other words, True is returned if and only if the first parameter is between the second and third parameters, or equal to one or both of them. CourseNana.COM

      Notes: CourseNana.COM

      • We have partially completed this function for you.
      • You must complete the function body yourself.
      • You do not need to add any more docstring examples for this function.
    4. Function: calculate_str_index(int, int) -> int

      Specification: This function has two parameters. The first and second parameters refer to the row and column, respectively, of a location in a valid game board whose size is given by constants NUM_ROW and NUM_COL. Assume that each parameter refers to a valid value. This function returns the index in the string representation of the game board corresponding to the given row and column location. CourseNana.COM

      Notes: CourseNana.COM

    5. Function: calculate_increment(str) -> int

      Specification: This function has one parameter. The parameter refers to a string that describes one of the four directions: down, across, down-right, or down-left. Assume that the str value is one of: DIR_DOWNDIR_ACROSSDIR_DOWN_RIGHT, or DIR_DOWN_LEFT. The game board size is given by the constants NUM_ROW and NUM_COL. This function returns the difference between the str indices of two adjacent squares on a line that goes in the direction specified by the first parameter. CourseNana.COM

      Notes: CourseNana.COM

    6. Function: get_row(int, str) -> str

      Specification: This function takes two parameters. 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 NUM_ROW and NUM_COL. 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. CourseNana.COM

      Notes: CourseNana.COM

      • Follow the Function Design Recipe to complete this function.
      • Review the "str: indexing and slicing" material in the PCRS Week 3 Prepare module.
      • Re-read the Accessing a square in the game board if you are stuck.
      • Review the "Function Reuse" material in the PCRS Week 2 Prepare module. Consider using your calculate_increment and calculate_str_index functions to help.
    7. Function: get_column(int, str) -> str

      Specification: This function takes two parameters. The first parameter is a column number and the second parameter is the string representation of a game board. Assume that the column number and game board are valid. The game board size is given by the constants NUM_ROW and NUM_COL. This function returns only the characters in the game board found at the given column in the same top-to-bottom order as they appear in the column. CourseNana.COM

      Notes: CourseNana.COM

      • Follow the Function Design Recipe to complete this function.
      • Review the "str: indexing and slicing" material in the PCRS Week 3 Prepare module.
      • Re-read the Accessing a square in the game board section if you are stuck.
      • Review the "Function Reuse" material in the PCRS Week 2 Prepare module. Consider using your calculate_increment and calculate_str_index functions to help.
    8. Function: slide_right(str, int, str) -> str

      Specification: This function takes three parameters. 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: CourseNana.COM

      • 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.

      Notes: CourseNana.COM

      • Follow the Function Design Recipe to complete this function.
      • Review the "str: indexing and slicing" material in the PCRS Week 3 Prepare module.
      • Re-read the Slide right section if you are stuck.
      • Review the “Function Reuse” material in the PCRS Week 2 Prepare module. Consider using your calculate_str_index function to help.
    9. Function: slide_left(str, int, str) -> str

      Specification: This function takes three parameters. The first parameter is the square being added to the game board. The second parameter is a row 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: CourseNana.COM

      • the square from the first parameter has been slid into the “end” (i.e., last column) of the given row number;
      • the square at the “beginning” (i.e., first column) has slid off the game board;
      • the remaining squares have slid to the left.

      Notes: CourseNana.COM

      • Follow the Function Design Recipe to complete this function.
      • Review the "str: indexing and slicing" material in the PCRS Week 3 Prepare module.
      • Re-read the Slide left section if you are stuck.
      • Review the “Function Reuse” material in the PCRS Week 2 Prepare module. Consider using your calculate_str_index function to help.

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
Canada代写,University of Toronto代写,CSC108代写,Introduction to Computer Programming代写,The Slide Game代写,Canada代编,University of Toronto代编,CSC108代编,Introduction to Computer Programming代编,The Slide Game代编,Canada代考,University of Toronto代考,CSC108代考,Introduction to Computer Programming代考,The Slide Game代考,Canadahelp,University of Torontohelp,CSC108help,Introduction to Computer Programminghelp,The Slide Gamehelp,Canada作业代写,University of Toronto作业代写,CSC108作业代写,Introduction to Computer Programming作业代写,The Slide Game作业代写,Canada编程代写,University of Toronto编程代写,CSC108编程代写,Introduction to Computer Programming编程代写,The Slide Game编程代写,Canadaprogramming help,University of Torontoprogramming help,CSC108programming help,Introduction to Computer Programmingprogramming help,The Slide Gameprogramming help,Canadaassignment help,University of Torontoassignment help,CSC108assignment help,Introduction to Computer Programmingassignment help,The Slide Gameassignment help,Canadasolution,University of Torontosolution,CSC108solution,Introduction to Computer Programmingsolution,The Slide Gamesolution,