Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can I get help with this java code, please? ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ import java.awt.Point; import java.awt.Polygon; /** * Coordinates on a hexagon-filled game board. * * a

Can I get help with this java code, please?

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.Point; import java.awt.Polygon;

/** * Coordinates on a hexagon-filled game board. *

*
a
left to right (0 = left edge, moving left half a hex each line down) *
b
top to bottom (0 = top edge) *
c
left to right (0 = top edge, moving right half a hex each line down) *
* The {@link #c()} coordinate is always the difference of the first two. */ public class HexCoordinate { private final int a, b, c; /** * Create a hexagonal coordinate by specifying the first two coordinates * and computing the third. * @param a first coordinate * @param b second coordinate */ public HexCoordinate(int a, int b) { // TODO: Very easy. See handout. } /** * Create a hexagonal coordinate by specifying all three coordinates, * which must be consistent. * @param a * @param b * @param c * @exception IllegalArgumentException if the coordinates are not consistent. */ public HexCoordinate(int a, int b, int c) throws IllegalArgumentException { // TODO: Check consistency and then assign fields. } /// three simple accessors /** Return the first coordinate (how far from left * plus more every line). * @return the first coordinate */ public int a() { return a; } /** * Return the second coordinate (how far from top). * @return the second coordinate */ public int b() { return b; }

/** * Return the third coordinate (how far from left * minus more very line). * @return the third coordinate */ public int c() { return c; } /// Overrides // no need to give a documentation comment if overridden documentation still is valid. // return true iff x is another HexCoordinate with the same value @Override public boolean equals(Object x) { // TODO } @Override public int hashCode() { return a ^ (b }

/** * Return the closest hex coordinate to this point. * If two are equally close, either may be returned. * @param p * @param width width of grid (must NOT be negative or zero) * @return closest hex coordinate */ public static HexCoordinate fromPoint(Point p, int width) { float height = width * HEIGHT_RATIO; float db = p.y/height; float da = (float)p.x/width + db/2.0f; float dc = da - db; int ac = (int)Math.floor((da+dc)); int ab = (int)Math.floor((da+db)); int bc = (int)Math.floor((db-dc)); int a = (int)Math.ceil((ab+ac)/3.0); int b = (int)Math.ceil((ab+bc)/3.0); return new HexCoordinate(a,b); }

/// Other accessors //define HEIGHT_RATIO is the ration of height to width public static final float HEIGHT_RATIO = (float) (Math.sqrt(3.0)/2.0); // height of a row, given width = 1.0 private static final float THIRD = 1.0f/3.0f; private static final float TWOTHIRD = 2.0f/3.0f; /** * Return center of hexagon as a point on the two-dimensional AWT plane. * @param width width of hexagon * @return Point in the center of the hexagon */ public Point toPoint(int width) { // TODO: call the other toPoint method with the correct coordinates } /** * A generalization of {@link #toPoint(int)} that takes two floats, to permit * fractions into the coordinate space. * @param width width of hexagon in grid * @param a first coordinate * @param b second coordinate * @return [x,y] point for this location. */ private static Point toPoint(int width, float a, float b) { float height; // TODO: compute the height using the width and the height ratio return new Point(Math.round(width*(a-b/2.0f)),Math.round(height*b)); } /** * Create a polygon (for rendering in AWT) for the hexagon around this * hex coordinate. The hexagons so creates tile the plane. * @param width width of hexagon in pixels * @return polygon for hexagon */ public Polygon toPolygon(int width) { Point[] ps = { toPoint(width,a-THIRD,b-TWOTHIRD), toPoint(width,a+THIRD,b-THIRD), toPoint(width,a+TWOTHIRD,b+THIRD), toPoint(width,a+THIRD,b+TWOTHIRD), toPoint(width,a-THIRD,b+THIRD), toPoint(width,a-TWOTHIRD,b-THIRD) }; Polygon result = new Polygon(); //TODO: add the points in ps to this Polygon // you may want to look up Oracle's page on Polygon return result; } /** * Return the number of steps to get from one hex to another. * We can use the smallest distance traveling along just two of the coordinates. * Thus we can add all the differences and remove the largest (not used). * Alternately, we can return the largest of the differences directly, * which (do the algebra!) is the same value. * @param other * @return number of steps from one hex to another */ public int distance(HexCoordinate other) { // TODO: return the distance between this hexagon and the other // see the handout for details // you can use Math.max(x, y) and Math.abs(x) }

} -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.Color;

/** A terrain class for a game board with hexagonal tiles.

* The game determines the meaning of the various terrains.

* We use light colors for rendering (except inaccessible) to help with contrast.

*/

public enum Terrain {

INACCESSIBLE(Color.BLACK),

WATER(Color.CYAN),

LAND(Color.WHITE),

FOREST(Color.GREEN),

MOUNTAIN(Color.LIGHT_GRAY),

CITY(Color.ORANGE),

DESERT(Color.YELLOW);

private final Color color;

//There is no public constructor. Why not?

private Terrain(Color c) {

// TODO: initialize the color field

}

/**

* Return the suggested color to use for this terrain.

* Color is light to permit dark foregrounds to be used on figures on tiles.

* @return color associated with this terrain.

*/

public Color getColor() {

// TODO: return the color for this terrain

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.Color; import java.awt.Graphics; import java.awt.Polygon;

/** * A hexagonal game tail with a particular coordinate and terrain. */ public class HexTile {

public static final int WIDTH = 50; // this value could be changed private final Terrain terrain; private final HexCoordinate location; /** * Create a hexagonal tile for the given terrain and location in hex coordinates. * @param t terrain, must not be null * @param loc location, must not be null * @exception IllegalArgumentException if either argument is null */ public HexTile(Terrain t, HexCoordinate loc) { //we will use this kind of exception a lot if (t == null || loc == null) throw new IllegalArgumentException("neither terrain nor location may be null"); terrain = t; location = loc; } @Override public String toString() { return terrain.toString() + location.toString(); } /** * Return terrain of this tile. * @return terrain of this tile */ public Terrain getTerrain() { return terrain; } /** * Return location of this tile. * @return location of this tile */ public HexCoordinate getLocation() { return location; } /** * Render the tile in a graphics context. * We fill the hexagon with the terrain suggested color and then * outline the tile in black. * @param g context to use. */ public void draw(Graphics g) { Polygon hexagon = location.toPolygon(WIDTH); g.setColor(terrain.getColor()); g.fillPolygon(hexagon); g.setColor(Color.BLACK); g.drawPolygon(hexagon); } }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Instructions in the images below.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

In this assignment, you will implement an ADT for a hexagonal coordinate system for creating games that operate on hexagon-tile boards. You will also implement a simple "enumerated class" for various kinds of terrains. 1 ADT: HexCoordinate For this homework, you are creating an immutable data type to represent hexagon coordi- nates. Each coordinate represents the center of a hexagon in a tiling of the plane: 0,0,0. 1,0,1 2.0,2 3,0,3 4,0,4 1,1,0 2,1,1 3,1,2 4,1,3 1,2,-1 2,2,0 3,2,1 4,2,2 5,2,3 2,3,-1 3,3,0 4,3,1 5,3,2 Each hexagon has three coordinates: the first increases as you go to the right (and down), the second as you go down, the third as you go right (and up). In the electronic version of this document, you can see that a band of hexagons with first coordinate equal to two have been highlighted in yellow. If you have a paper copy of this document, we recommend that you highlight in three contrasting colors bands of hexagons with the same coordinate equal to two. (There are a number of different hexagonal grids used in practice, but you should use thisone for this homework.) The third coordinate can always be computed from the other two? For example, if you know a and b, you can subtract one from the other to get c, as you can see in the diagram. We therefore often use just the first two numbers to create a coordinate. Two hexagonal coordinates are equal exactly when they have the same numbers in the same order. The "distance from one hex coordinate is the minimum number of moves to adjacent hexagons needed to get from one to another. Thus the distance from (3,0,3) to (5,2,3) is two, and the distance from (2,2,0) to (3, 0,3) is three. The distance from any hex coordinate to itself is zero. As it happens, the shortest path only requires travel along two of the three "dimensions", and thus you can compute the minimum distance by testing each possible pair of dimensions and choosing the shortest. For example, assuming we use "a", "b", and "c" for our three coordinates, then to compute the shortest path from (2,2,0) to (3,0,3), we can try all three pairs of dimensions to travel on: ac Travel first along the "a" dimension, keeping the "c" dimension constant: (2,2,0) to (3,3,0) and then along the "c" dimension while keeping the "a" dimension constant: (3,3,0) to (3, 2, 1) to (3, 1, 2) to (3,0,3). Total travel: 1+3 = 4. bc Travel first along the b dimension, keeping the c dimension constant: (2,2,0) to (1,1,0) to (0,0,0), and then along the c dimension while keeping the b dimension constant: (0,0,0) to (1,0, 1) to (2,0, 2) to (3,0,3). Total travel: 2+3 = 5. ab Travel first along the a dimension, keeping the b dimension constant: (2,2,0) to (3, 2, 1) and then along the b dimension while keeping the "a" dimension constant: (3, 2, 1) to (3, 1, 2) to (3, 0,3). Total travel: 1+2 = 3. The shortest distance is the sum of the two smaller distances in each of the three dimensions (a, b, c). An easy way to compute the shortest distance is to add the three distances and then subtract the maximum distance: 1+ 2+3 - 3 = 3; think about why this works. (Of course, you must use the absolute value of the difference: e.g.: 12-31 = 1 - 11 = 1). In order to render a hexagon in a Java graphics context, we have to decide how big the hexagons are going to be. This might change and so we use a "width" parameter. For those of you who don't recall your hexagonal geometry, here's a helpful diagram (not quite to scale) of a hexagon with "width = w: Thus if w = 30 and the hexagon is centered at (0,0), then (15,5/3) is one of the corners. Once you figure out how to create the Polygon for the hexagon at the hex coordinate (0,0,0), you need to see how much the (x,y) points should shift for other coordinate. The first coordinate is easy; it shifts the hexagon right increasing x) by w. The second is trickier since it shifts the hexagon down (increasing y) by w and back to the left (decreasing x) by 2 In all, you must implement the following methods: HexCoordinate (int, int) Main constructor. HexCoordinate(int, int, int) Alternate constructor. equals(Object) Return true if the argument is another hex coordinate with the same coordinate values. toString() Return a string of the form . fromPoint (Point,int) Return the coordinate of the hexagon that encloses the given (x, y) point using the given width. (We implement this method for you.) toPoint(int) Return the (1, y) center of the hexagon at this coordinate using the given width. toPolygon(int) Return a AWT Polygon object for the hexagon at this coordinate with specified width; the first point should be at the top of the hexagon, and the rest going clockwise from there. There should be six points (it is a hexagon). distance (HexCoordinate) Return the numbers of moves required to get from this hex coordinate to the parameter. Note: AWT Point and Polygon objects use integers, not floats. The supplied test files require that calculated values be clamped to the nearest integer so that the value 3.4 becomes 3, and the value -2.6 becomes -3. If many of your test cases initially fail with many values off by one, this may be the reason. 2 Enum: Terrain Java has "enumerated types." Please consult the Oracle documentation for information on how they work. We have provided a partially-implemented terrain enumerated type where each terrain is associated with a color: INACCESSIBLE Black WATER Cyan LAND White FOREST Green MOUNTAIN Light gray CITY Orange DESERT Yellow You will need to write the private constructor for the enum. Also write a get Color accessor to get the associated color for a terrain. 3 Required Coding Conventions In the code that you write for CS 351, you must follow some guidelines: 1. Classes and public methods must have appropriate Java-doc comments. 2. Fields should be private. 3. Methods should start with lowercase characters and use "camelCase to handle multi- ple words. 4. Methods that override those in a super class (notably toString() and clone ()) should be annotated @Override. 5. Code should be appropriately indented with tabs and/or spaces. In this assignment, you will implement an ADT for a hexagonal coordinate system for creating games that operate on hexagon-tile boards. You will also implement a simple "enumerated class" for various kinds of terrains. 1 ADT: HexCoordinate For this homework, you are creating an immutable data type to represent hexagon coordi- nates. Each coordinate represents the center of a hexagon in a tiling of the plane: 0,0,0. 1,0,1 2.0,2 3,0,3 4,0,4 1,1,0 2,1,1 3,1,2 4,1,3 1,2,-1 2,2,0 3,2,1 4,2,2 5,2,3 2,3,-1 3,3,0 4,3,1 5,3,2 Each hexagon has three coordinates: the first increases as you go to the right (and down), the second as you go down, the third as you go right (and up). In the electronic version of this document, you can see that a band of hexagons with first coordinate equal to two have been highlighted in yellow. If you have a paper copy of this document, we recommend that you highlight in three contrasting colors bands of hexagons with the same coordinate equal to two. (There are a number of different hexagonal grids used in practice, but you should use thisone for this homework.) The third coordinate can always be computed from the other two? For example, if you know a and b, you can subtract one from the other to get c, as you can see in the diagram. We therefore often use just the first two numbers to create a coordinate. Two hexagonal coordinates are equal exactly when they have the same numbers in the same order. The "distance from one hex coordinate is the minimum number of moves to adjacent hexagons needed to get from one to another. Thus the distance from (3,0,3) to (5,2,3) is two, and the distance from (2,2,0) to (3, 0,3) is three. The distance from any hex coordinate to itself is zero. As it happens, the shortest path only requires travel along two of the three "dimensions", and thus you can compute the minimum distance by testing each possible pair of dimensions and choosing the shortest. For example, assuming we use "a", "b", and "c" for our three coordinates, then to compute the shortest path from (2,2,0) to (3,0,3), we can try all three pairs of dimensions to travel on: ac Travel first along the "a" dimension, keeping the "c" dimension constant: (2,2,0) to (3,3,0) and then along the "c" dimension while keeping the "a" dimension constant: (3,3,0) to (3, 2, 1) to (3, 1, 2) to (3,0,3). Total travel: 1+3 = 4. bc Travel first along the b dimension, keeping the c dimension constant: (2,2,0) to (1,1,0) to (0,0,0), and then along the c dimension while keeping the b dimension constant: (0,0,0) to (1,0, 1) to (2,0, 2) to (3,0,3). Total travel: 2+3 = 5. ab Travel first along the a dimension, keeping the b dimension constant: (2,2,0) to (3, 2, 1) and then along the b dimension while keeping the "a" dimension constant: (3, 2, 1) to (3, 1, 2) to (3, 0,3). Total travel: 1+2 = 3. The shortest distance is the sum of the two smaller distances in each of the three dimensions (a, b, c). An easy way to compute the shortest distance is to add the three distances and then subtract the maximum distance: 1+ 2+3 - 3 = 3; think about why this works. (Of course, you must use the absolute value of the difference: e.g.: 12-31 = 1 - 11 = 1). In order to render a hexagon in a Java graphics context, we have to decide how big the hexagons are going to be. This might change and so we use a "width" parameter. For those of you who don't recall your hexagonal geometry, here's a helpful diagram (not quite to scale) of a hexagon with "width = w: Thus if w = 30 and the hexagon is centered at (0,0), then (15,5/3) is one of the corners. Once you figure out how to create the Polygon for the hexagon at the hex coordinate (0,0,0), you need to see how much the (x,y) points should shift for other coordinate. The first coordinate is easy; it shifts the hexagon right increasing x) by w. The second is trickier since it shifts the hexagon down (increasing y) by w and back to the left (decreasing x) by 2 In all, you must implement the following methods: HexCoordinate (int, int) Main constructor. HexCoordinate(int, int, int) Alternate constructor. equals(Object) Return true if the argument is another hex coordinate with the same coordinate values. toString() Return a string of the form . fromPoint (Point,int) Return the coordinate of the hexagon that encloses the given (x, y) point using the given width. (We implement this method for you.) toPoint(int) Return the (1, y) center of the hexagon at this coordinate using the given width. toPolygon(int) Return a AWT Polygon object for the hexagon at this coordinate with specified width; the first point should be at the top of the hexagon, and the rest going clockwise from there. There should be six points (it is a hexagon). distance (HexCoordinate) Return the numbers of moves required to get from this hex coordinate to the parameter. Note: AWT Point and Polygon objects use integers, not floats. The supplied test files require that calculated values be clamped to the nearest integer so that the value 3.4 becomes 3, and the value -2.6 becomes -3. If many of your test cases initially fail with many values off by one, this may be the reason. 2 Enum: Terrain Java has "enumerated types." Please consult the Oracle documentation for information on how they work. We have provided a partially-implemented terrain enumerated type where each terrain is associated with a color: INACCESSIBLE Black WATER Cyan LAND White FOREST Green MOUNTAIN Light gray CITY Orange DESERT Yellow You will need to write the private constructor for the enum. Also write a get Color accessor to get the associated color for a terrain. 3 Required Coding Conventions In the code that you write for CS 351, you must follow some guidelines: 1. Classes and public methods must have appropriate Java-doc comments. 2. Fields should be private. 3. Methods should start with lowercase characters and use "camelCase to handle multi- ple words. 4. Methods that override those in a super class (notably toString() and clone ()) should be annotated @Override. 5. Code should be appropriately indented with tabs and/or spaces

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_2

Step: 3

blur-text-image_3

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

How To Make A Database In Historical Studies

Authors: Tiago Luis Gil

1st Edition

3030782409, 978-3030782405

More Books

Students also viewed these Databases questions

Question

Can I get help with this java code, please?...

Answered: 1 week ago