Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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. For this, you are creating an immutable data type to represent hexagon coordinates. 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 rst 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 rst 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 dimensionconstant: 0, 0, 0 to 1, 0, 1 to 2, 0, 2 to 3, 0, 3 . Total travel: 2 + 3 = 5.ab Travel rst 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

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 multiple 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.

CODE:- 1) HexCordinate.java

package edu.uwm.cs351;
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 << 8);
//return some combination of a, b and c that distinguishes similar coordinates
}
@Override
public String toString() {
// TODO: return a string of the form <3,2,1>
}
/**
* 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)
}
}

2) Terrain.java

ackage edu.uwm.cs351;
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
}
}

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

Students also viewed these Databases questions