Java Python CS 1027 Computer Science Fundamentals II
Assignment 1
Due Date: October 8, 11:55 pm
1. Learning Outcomes
To gain experience with
• Creating and using classes and objects.
• Using arrays and two-dimensional arrays.
• Reading input from a file.
• Algorithm design and modular design.
2. Introduction
The snake game is played on a rectangular grid. There are 4 classes of objects placed on the squares of the grid: rocks, apples, scissors, and a snake. The snake moves around the board trying to eat as many apples as it can, but avoiding the rocks, exiting the board, or overlapping with itself. Whenever the snake eats an apple, it grows and whenever it touches a pair of scissors it shrinks. The user controls the movement of the snake using the keyboard and the goal is for the snake to eat as many apples as possible. The game ends when the head of the snake touches a rock, it tries to move outside the board, or it touches any part of its body. The following figure shows a screenshot of the game.
Figure 1. A gameboard of 8 rows and 15 columns containing one apple, one rock, one pair of scissors, and a snake of length 4.
This assignment is an example of a collaborative project with modular design. Some classes of the project were designed by the CS1027 team and other classes need to be designed by you. The classes that you must design are specified below. It is very important that you follow exactly the specifications of the classes that you are to design, as otherwise your code will not work correctly with the code that is provided to you.
3. Classes to Implement
For this assignment you need to design and implement 3 Java classes: GridPosition, Snake, and GridBoard.
3.1 Class GridPosition
Each square in the grid can be specified by two numbers: its row and its column. An object of class GridPosition represents the position of a square of the grid. This class must have two private integer variables: posRow and posCol. In this class you must implement the following public methods:
• GridPosition(int row, int col): this is the constructor for the class. The value of the first parameter must be stored in instance variable posRow and the second in posCol.
• int getRow(): returns the value of posRow.
• int getCol(): returns the value of posCol.
• void setRow(int newRow): stores the value of newRow in posRow.
• void setCol(int newCol): stores the value of newCol in posCol.
• boolean equals(GridPosition otherPosition): returns true if this Position object and otherPosition have the same values stored in posRow and posCol; otherwise it returns false.
3.2 Class GridBoard
This class represents the boardgame where the snake moves around eating apples. This class will have 4 private instance variables:
• intboard_cols: the number of columns of the grid.
• intboard_rows: the number of rows of the grid.
• Snake theSnake: and object of the class Snake (described below) representing the playing snake.
• String[][] gridMatrix: a 2-dimensional array of Strings that will store the content of each one of the
squares of the grid. So, gridMatrix[r][c] is the value stored in row rand column c of the 2-dimensional array. Each entry of gridMatrix can contain the following possible values:
o “empty”: if the corresponding square of the grid is empty.
o “apple”: if the corresponding square of grid contains an apple.
o “scissors”: if the corresponding square of the grid contains a pair of scissors.
o “rock”: if the corresponding square of the grid contains a snake-killing rock.
In this class you need to implement the following public methods:
• GridBoard(String gridFile): this is the constructor for the class. The parameter is the name of a file
containing the dimensions of the game grid, the initial position of the snake, and the objects placed on the grid. You must open the file named by gridFile, read it and sore in the instance variables of this class the appropriate values. To help you with this task, you are provided with a java class called MyFileReader which contains methods to open a text file, read a String or an integer, and check whether&