Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3.2) (In JAVA)*** Class BoardGame This class represents the board game where the snake moves around eating apples. This class will have 4 private instance

3.2) (In JAVA)***

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:

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:

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 MyFileReader (provided below) which contains methods to open a text file, read a String or an integer and check whether the whole file has been read. 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.

o The third number is the length of the board, which you must store in board_length.

o 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(Don't worry about the class just make the object) must be created, and its address stored in theSnake: 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, string1 read your code must store string1 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 100 15 8 5 8 3 3 rock 7 10 apple 5 5 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].

String getObject(int row, int col): returns the string stored in matrix[row][col].

void setObject(int row, int col, String newObject): stores newObject in matrix[row][col].

Snake getSnake(): returns theSnake.

void setSnake(Snake newSnake): stores the value of newSnake in instance variable theSnake.

int getLength(): returns board_length.

int getWidth(): returns board_width.

String getType(int row, int col): returns matrix[row][col].

Here is the code for MyFileReader:

import java.io.*; /* This class contains methods to open a text file, read a line of the file, and read an integer value from the file. */ public class MyFileReader { private String buffer = null; private BufferedReader in = null; /* Open the file whose name was give as parameter */ public MyFileReader(String fileName) { try { in = new BufferedReader(new FileReader(fileName)); buffer = in.readLine(); // Store first line } catch (IOException e) { System.out.println("Cannot read file \'"+fileName+"\'"); System.exit(0); } } /* Returns true if the end of the file has been reached; it returns false otherwise. */ public boolean endOfFile() { if (buffer == null) return true; else return false; } /* Reads a line from the file */ public String readString() { String line = buffer; if (buffer == null) { System.out.println("Error. The end of file was reached, so another string cannot be read from it"); return null; } try { buffer = in.readLine(); } catch (IOException e) { System.out.println("Error. Cannot read from file"); System.exit(0); } return line; } /* Reads an integer from the file. Returns -1 if no integer could be read. */ public int readInt() { String line = readString(); if (line == null) return -1; else return Integer.parseInt(line); } }

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

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

Students also viewed these Databases questions

Question

What was the evidence that Reyes knew he was breaking the law?

Answered: 1 week ago