java assignment

profileHidk2
4.Assignment8-2020.pdf

COMP10062: Assignment 8 © Sam Sco tt , M o hawk Co llege, 2 0 2 0

The Assignment In this assignment you will implement some basic functionality for a grid-based game app. This is a summative activity that will require the use of encapsulation, inheritance, polymorphism, association, exception handling, and ArrayLists.

Basic Performance Many tabletop games are played on a grid (Chess, Checkers, Othello, Connect 4, Tic Tac Toe, and miniature-based combat games for example). For this app, you should start by presenting the user with a grid and some game pieces placed on that grid. There should be at least two different kinds of playing pieces. The pieces do not have to be fancy, but they should be distinguishable.

The user should be able to (at least) add, remove, and select pieces (selecting a piece should cause it to draw itself differently – perhaps with an outline or a change of background color). At least one of these operations should be implemented through mouse events. For example, you could allow the user to click on a piece to select it.

At least one operation must also be implemented using numeric parameters with TextField inputs, Buttons, etc. For example, you could allow the user to specify a row and column number in TextFields, and then press a Button to add a new piece to the grid.

The picture above shows an example with two types of pieces. One of the green pieces is selected. Your app should not look exactly like this

Each type of piece should be represented by its own class with a draw method, and you should use inheritance and abstract classes effectively when designing them. Each time a piece is created, it should be stored in a polymorphic ArrayList. When a piece is added, removed, or changed, you should clear the screen and redraw the grid along with every game piece in the ArrayList.

How to Snap to a Grid Let’s say you know your grid is 40 pixels wide. Then your canvas is split into 40-pixel-high rows and 40- pixel-wide columns. The top left corner of the grid square at row r and column c is at x = 40*c and y = 40*r. This simple calculation will help you draw the grid lines and place the pieces.

To find out where a user clicked on the grid, cast their x and y coordinates to integers, then x / 40 gets you the number of the column they clicked on and y / 40 gets you the row.

Error Handling It should be impossible for the user to trigger an unhandled exception. If a TextField or other element cannot be converted to a numeric value, you should catch the exception and display an error message.

Similarly, if a user tries to create a game piece on top of another piece, you should disallow it and show an error message.

You can display a message on screen in a Label control or use a dialog pop-up to do this as shown below:

The warning above was created with the code shown below:

new Alert(Alert.AlertType.WARNING, "Invalid Line Width").showAndWait();

Optional Extras There is so much more that you could do with this! All the ideas below are optional.

• Allow a larger variety of pieces

• Implement two “teams” of pieces in separate ArrayLists

• Allow the user to move pieces

• Make the board bigger than the canvas. Allow the user to zoom in and out and move the visible portion of the board around

• Add more types of pieces with different allowable moves

• Build on the basics to implement a playable game

• An ArrayList is not the most time-efficient structure for storing grid pieces. Research multi- dimensional arrays and then store the grid pieces in a two-dimensional array instead.

• etc…

Handing In See the due date and time on Canvas. Hand in by attaching a zipped version of your .java (not .class) files to the Canvas Assignment.

Make sure you follow the Documentation Standards for the course.

Evaluation Your assignment will be evaluated for performance (40%), structure (40%), and documentation (20%) using the rubric on Canvas.