代写CS 1027 Computer Science Fundamentals II Assignment 1代写留学生Java程序

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: GridPositionSnake, 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 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 gridFile is as follows. The first 6 lines contain each one number. For the rest of the file, the following 3 lines contain a number,a number, and a string; then the next 3 lines contain a number, a number, and a string, and soon until the end of the file.

o The numbers in the first 2 lines of the file 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 gridFile andre-run the program; if the board is too large then decrease these numbers andre-run the program.

o The third number is the number columns of the grid, which you must store in board_cols.

o The fourth number is the number of rows of the grid, which you must store in board_rows.

o The fifth number is the row and the sixth number is the column where the snake is initially

positioned on the grid. Initially the snake has length 1. A new object of the class Snake must be created and stored in instance variable 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_rows][board_cols]. All entries of the array areinitialized 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 group of 3 lines your code will read two numbers (number1 and    number2) and a strings and then you must stores ingridMatrix[number1][number2].

An example gridFile is shown below, where the grid squares are of size 100 by 100 pixels, the grid 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. 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 also 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 gridMatrix instance variable will be a 2-dimensional array of size [8][15].

Another note (not so important). If you want to write your own board files, make it sure the number of columns of the board is at least 15 and the number of rows is at least 6, so it displays correctly on the screen.

The other public methods that you must implement in this class are the following:

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

•    void setObject(int row, int col, String newObject): stores newObject ingridMatrix [row][col].

•    Snake getSnake(): returns theSnake.

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

•    intgetNumCols(): returns board_cols.

•    intgetNumRows(): returns board_rows.

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

•    void invertGrid(): modifies gridMatrix so the first column is switched with the last column, the second

column is switched with the second to last column and soon, so at the end gridMatrix is “inverted” (a mirror image of itself). For example, if gridMatrix contains the information in the following matrix on the left, then after invoking this method gridMatrix will contain the information shown on the matrix on the  right.

apple

 

 

 

scissors

 

 

 

 

rock

 

 

 

 

 

rock

 

 

 

 

 

 

scissors

 

 

 

apple

 

 

 

 

rock

 

 

 

 

 

 

 

rock

 

In this method you CANNOT use another 2-dimensional array. You can use an array if you want, but it cannot be a 2-dmensional array.

After inverting gridMatrix this method must invoke method theSnake.invertSnake() to invert the position of the snake; this method is described below.

3.3 Class Snake

This class stores the information about the snake as it moves around the board. This class will have two private instance variables:

•    int snakeLength: this is the number of grid squares occupied by the snake. For example, the snake shown in the above figure in page 1 has a length of 4.

•    GridPosition[] snakeBody: the grid squares occupied by the snake will be stored in this array. The grid

square with the head of the snake will be stored in index 0 of the array; the grid square where the tail of  the snake is will be stored in index snakeLength-1 of the array. For example, for the snake in the figure in page 1, the array snakeBody will store the following information:


Position of head

(1, 2)

(1, 1)

(1, 0)

(2, 0)

 

0                   1                      2                      3                      4

object of class GridPosition

In this class you need to implement the following public methods.

•    Snake(int row, int col): this is the constructor for the class; the parameters are the coordinates of the

head of the snake. Initially the snake has length 1, so in this method the value of the instance variable

snakeLength will be set to 1. Instance variable snakeBody is initialized to an array of length 5 of

GridPosition objects. An object of class GridPosition will be created storing the values of row and col and this GridPosition object will then be stored in the first entry of array snakeBody.

•    int getLength(): returns the value of instance variable snakeLength.

•    GridPosition[] getSnakeBody(): returns instance variable snakeBody.

•    GridPosition getGridPosition(int index): returns the GridPosition object stored in snakeBody[index]. It returns null if index < 0 or index >= snakeLength.

•    void shrink(): decreases the value of snakeLength by 1.

•    boolean snakeGridPosition(GridPosition pos): returns true if pos is in array snakeBody, and it returns false otherwise. Notice that you must use method equals from class GridPosition to compare two objects of

the class GridPosition.

•    GridPosition newHeadGridPosition(String direction): returns the new position of the head of the snake   when the snake moves in the direction specified by the parameter. The values that parameter direction can take are “right”, “left”, “up” and “down” . If, for example, the head of the snake is at (2,3) and

direction is “right” then the new position would be (2,4); if direction is “down” then the new position would be (3,3). If the head is at (0,0) and direction is “up” the new position would be (-1,0).

•   void moveSnake(String direction): moves the snake in the specified direction; this means that array

snakeBody must be updated so it contains the positions of the grid squares that the snake will occupy

after it moves in the direction specified by the parameter. For example, for the snake in the figure in page

1 array snakeBody is as specified at the top of this page. If direction = “ up” then array snakeBody must change to this

(0, 2)

(1, 2)

(1, 1)

(1, 0)

 

0                   1                      2                      3                      4

If direction is “up” again then array snakeBody must change to this

(-1, 2)

(0, 2)

(1, 2)

(1, 1)

 

0                   1                      2                      3                      4

Notice that to determine the new array snakeBody what you must do is this:

o shift one position to the right all values in the array stored in indices 0 to snakeLength – 2. For    example, if the snake is as in the array on the left and direction = “right”, then shifting produces this array:

(1, 3)

(1, 3)

(1, 2)

(1, 1)

 

0                  1                      2                      3                      4                                0                   1                       2                      3                      4

o and then store in index 0 of the array the new position of the snake’s head (which you can

compute using method newHeadGridPostion(); in the above example we would store (1,4) in the first entry of the array.

•    void invertSnake(int gridCols): receives as parameter the number of columns in the grid and it changes the value stored in every entry of array snakeBody as follows:

o if snakeBody[i] stores a GridPosition object g, then change the value of instance variable g.posCol to gridCols – 1 – g.posCol.

The above operation moves the snake to a mirror position of its initial location.

•    void growSnake(String direction): increases the length of the snake by 1 and moves the snake’s head in the direction specified. This method is very similar to method moveSnake, but instead of shifting the

values stored in snakeBody from index 0 to snakeLength - 2, we need to shift all values from index 0 to index snakeLength – 1. For example, if the snake is as shown in the figure in page 1, and direction =

“right” then the new content of array snakeBody will be

(1, 3)

(1, 2)

(1, 1)            (1, 0)

(2, 0)

0                   1                      2                      3                      4

Notice that since the length of the snake increases you need to make sure that array snakeBody is large

enough to store the new information. If instance variable snakeLength has the same value as snakeBody.length(), the size of the array, then you must

i.   double the size of the array if snakeLength < 10,

ii.   otherwise you must increase the size of the array by 8.

To increase the size of the array you must implement the following private method called increaseArraySize():

•    void increaseArraySize(): increases the size of array snakeBody as specified above, preserving the information that was stored in it.

In all three above classes, GridPosition, Snake, and GridBoard you can implement more private methods, if you want to, but you cannot implement more public methods. You can also add more private instance variables, but only if they are required. The use of unnecessary instance variables will be penalized.

4. Classes Provided

You can download from the course’s webpage the following java classes: PlayGame.java, MyFileReader.java,  TestMyFileReader.java, and TestAsmt1.java. You can also download several imagefiles needed to display the game board and some grid input files.

Class PlayGame has the main method. Class TestAsmt1 contains several tests that you can use to check that your   code works correctly. Please note that the tests in this class are not exhaustive, so there might be errors in your     code that are not detected by this testing program. Hence, you are required to perform. additional tests on your     code to ensure that it works correctly. When theTA’s mark your assignment, they will perform. additional tests on your code.

5. How to Run the Program

If you are running the program from the terminal place all the files in the same directory and compile the

program by running javac PlayGame.java and then run it with java PlayGame (in this case the

program will ask for the name of the input grid file) or java PlayGame nameOfGridFile; sample grid    files board1.txt-board3.txt have been provided . If you run the program from Eclipse, read the instructions in the next section.

The program will display the grid with the objects specified in the input grid file. To start the game press any of the arrow keys. The arrow keys can then be used to change the direction in which the snake moves.

Additionally, you can type the following keys:

•    f: increases the speed of the snake

•    s: reduces the speed of the snake

•    p: pauses the game

•    i: inverts the grid. The grid will also invert at random times while you are playing the game to make it a bit more challenging

•    x: terminates the game

•    d: prints some debugging information that you might find useful when testing and debugging your program. You must first pause the game to print this information.

5.1 Running the Program from Eclipse

Place the java files in your Eclipse workspace under the Java project you have created for this assignment.

 

Place the imagefiles and the sample input grid files in the root directory for your project (not inside the “(default package)” folder).


 

Double-click PlayGame in the Package Explorer and select “run” .

If Eclipse cannot find the image and grid files, then you can place all of the image and grid files in the same folder as the java files and then click on Run -> Run Configurations


Select the correct Project and Main Class:

 

Then click on Arguments and in “Program arguments” you can enter the name of the grid input file. Under

“Working directory” select Other:” click on FileSystem” and select the folder where the image and txt files are stored. Finally click on the “Run” button.



热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图