Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help me write this class in JAVA: 3.2 Class BoardGame This class represents the board game where the snake moves around eating apples. This
Please help me write this class in JAVA:
3.2 Class BoardGame This class represents the board game where the snake moves around eating apples. This class will have 4 private instance variables: int board_length: the number of columns of the grid on the game board. int board_width: the number of rows of the grid. Snake theSnake: and object of the class Snake representing the playing snake. String[[] matrix: a 2-dimensional matrix that will store the content of each one of the squares of the grid. Each entry of matrix can contain the following possible values: "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: BoardGame(String boardFile): this is the constructor for the file. The parameter is the name of a file containing the dimensions of the game board, the initial position of the snake, and the objects placed on the game board. You must open the file named by boardFile, 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 MyFile Reader which contains methods to open a text file, read a String or an integer and check whether the whole file has been read. You are also given a Java class called TestMyFileReader that shows how some of these methods are used to read and print the content of a file. Study these classes carefully, so you know how to use MyFileReader for this assignment. The format of the boardFile is as follows. The first 6 lines contain each one number. For the rest of the file, the next 3 lines contain a number, a number and a string; then the next 3 lines contain a number, a number and a string, and so on until the end of the file. o The first 2 numbers are not going to be used by the code that you will write. So, your constructor will just read them and ignore them. The first number is the width of each grid square and the second number is the length of each grid square. The code given to you will use these two numbers to determine how the game board will be displayed in the screen. When running the program, if you see that the board is too small, then simply increase these two values in the boardFile and re-run the program; if the board is too large then decrease these numbers and re- run the program. o The third number is the length of the board, which you must store in board_length. The fourth number is the width of the board, which you must store in board_width. o The fifth number is the row and the sixth number is the column where the snake is initially positioned on the board. Initially the snake has length 1. A new object of the class Snake must be created, and its address stored in the Snake: theSnake = new Snake value of fifth number, value of sixth number); Once your code has read the first 6 lines of the file, it must create a 2-dimensional array of type and dimensions String[board_width][board_length). All entries of the array are initialized to contain the string "empty" (in lowercase; it is very important that all strings that you store in matrix are lowercase). Then, the rest of the file is read and for each triplet number1, number2, stringi read your code must store stringi in matrix(number1][number2]. An example boardFile is shown below, where the grid squares are of size 100 by 100 pixels, the board has 15 columns and 8 rows, the snake is initially positioned in row 5 and column 8, a rock is placed in row 3 and column 3, an apple is in row 7 and column 10, and a pair of scissors is placed in row 5 and column 5. 100 namm rock apple scissors Important note. Note that rows and columns are indexed starting at 0. So, the figure in page 1 shows the correct positioning for the above rock, apple, and scissors. Note that in that figure the snake has already eaten some apples and moved to a different location on the board than the one initially specified in the file. For the above example, the matrix instance variable will be a 2-dimensional array of size (8][15). 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. 3.2 Class BoardGame This class represents the board game where the snake moves around eating apples. This class will have 4 private instance variables: int board_length: the number of columns of the grid on the game board. int board_width: the number of rows of the grid. Snake theSnake: and object of the class Snake representing the playing snake. String[[] matrix: a 2-dimensional matrix that will store the content of each one of the squares of the grid. Each entry of matrix can contain the following possible values: "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: BoardGame(String boardFile): this is the constructor for the file. The parameter is the name of a file containing the dimensions of the game board, the initial position of the snake, and the objects placed on the game board. You must open the file named by boardFile, 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 MyFile Reader which contains methods to open a text file, read a String or an integer and check whether the whole file has been read. You are also given a Java class called TestMyFileReader that shows how some of these methods are used to read and print the content of a file. Study these classes carefully, so you know how to use MyFileReader for this assignment. The format of the boardFile is as follows. The first 6 lines contain each one number. For the rest of the file, the next 3 lines contain a number, a number and a string; then the next 3 lines contain a number, a number and a string, and so on until the end of the file. o The first 2 numbers are not going to be used by the code that you will write. So, your constructor will just read them and ignore them. The first number is the width of each grid square and the second number is the length of each grid square. The code given to you will use these two numbers to determine how the game board will be displayed in the screen. When running the program, if you see that the board is too small, then simply increase these two values in the boardFile and re-run the program; if the board is too large then decrease these numbers and re- run the program. o The third number is the length of the board, which you must store in board_length. The fourth number is the width of the board, which you must store in board_width. o The fifth number is the row and the sixth number is the column where the snake is initially positioned on the board. Initially the snake has length 1. A new object of the class Snake must be created, and its address stored in the Snake: theSnake = new Snake value of fifth number, value of sixth number); Once your code has read the first 6 lines of the file, it must create a 2-dimensional array of type and dimensions String[board_width][board_length). All entries of the array are initialized to contain the string "empty" (in lowercase; it is very important that all strings that you store in matrix are lowercase). Then, the rest of the file is read and for each triplet number1, number2, stringi read your code must store stringi in matrix(number1][number2]. An example boardFile is shown below, where the grid squares are of size 100 by 100 pixels, the board has 15 columns and 8 rows, the snake is initially positioned in row 5 and column 8, a rock is placed in row 3 and column 3, an apple is in row 7 and column 10, and a pair of scissors is placed in row 5 and column 5. 100 namm rock apple scissors Important note. Note that rows and columns are indexed starting at 0. So, the figure in page 1 shows the correct positioning for the above rock, apple, and scissors. Note that in that figure the snake has already eaten some apples and moved to a different location on the board than the one initially specified in the file. For the above example, the matrix instance variable will be a 2-dimensional array of size (8][15). Figure 1. A gameboard of 8 rows and 15 columns containing one apple, one rock, one pair of scissors, and a snake of length 4Step 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