Skip to content

Instantly share code, notes, and snippets.

View magomar's full-sized avatar
💭
Forever Learning

Mario Gómez Martínez magomar

💭
Forever Learning
View GitHub Profile
@magomar
magomar / TerrainType_0.0.2
Last active November 14, 2018 10:32
HexagonalMaps: TerrainType
public enum TerrainType implements MovementEffects, ImageProviderFactory {
OPEN(0),
SAND(1),
HILLS(2),
MOUNTAINS(3),
ALPINE(IMPASSABLE),
MARSH(3),
WATER(IMPASSABLE),
CROPLANDS(1),
URBAN(0),
@magomar
magomar / testEnumMap
Last active November 14, 2018 10:31
Test EnumMap
@Test
public void testEnumMap() throws Exception {
System.out.println("\n*** Testing enum maps manipulation ***\n");
System.out.println("Creating enum maps...");
Map<TerrainType, String> enumMap = new EnumMap<TerrainType, String>(TerrainType.class);
Set<TerrainType> woods = EnumSet.of(TerrainType.FOREST, TerrainType.LIGHT_WOODS);
for (TerrainType terrainType : woods) {
enumMap.put(terrainType, terrainType.name().toLowerCase() + ".png");
}
System.out.println(enumMap);
@magomar
magomar / testEnumSet
Last active November 14, 2018 10:05
Test EnumSet
@Test
public void testEnumSet() throws Exception {
System.out.println("\n*** Testing enum sets manipulation ***\n");
System.out.println("Creating enum sets...");
Set<TerrainType> anyTerrain = EnumSet.allOf(TerrainType.class);
Set<TerrainType> emptyTerrain = EnumSet.noneOf(TerrainType.class);
Set<TerrainType> stream = EnumSet.of(TerrainType.RIVER, TerrainType.SUPER_RIVER);
Set<TerrainType> woods = EnumSet.of(TerrainType.FOREST, TerrainType.LIGHT_WOODS);
Set<TerrainType> water = EnumSet.of(TerrainType.WATER);
Set<TerrainType> land = EnumSet.complementOf((EnumSet<TerrainType>) water);
@magomar
magomar / testEnumManipulation
Last active December 28, 2015 20:59
Basic usage of enum types in Java
@Test
public void testEnumManipulation() throws Exception {
System.out.println("*** Testing enum manipulation ***\n");
System.out.println("Listing all enum values...");
TerrainType[] allTerrainTypes = TerrainType.values();
for (TerrainType tt : allTerrainTypes) {
System.out.println(tt.ordinal() + ": " + tt.name());
}
System.out.println("Basic usage of enums...");
TerrainType t1 = TerrainType.FOREST;
@magomar
magomar / TerrainType
Created November 20, 2013 10:56
Enum example in java
public enum TerrainType implements MovementEffects{
OPEN(0),
SAND(1),
HILLS(2),
MOUNTAINS(3),
ALPINE(IMPASSABLE),
MARSH(3),
WATER(IMPASSABLE),
CROPLANDS(1),
URBAN(0),
@magomar
magomar / UnitType
Last active November 14, 2018 10:02
HexagonalMaps: UnitType
public enum UnitType {
LIGHT_INFANTRY(4, 6),
INFANTRY(3, 6),
LIGHT_CAVALRY(5, 10),
HEAVY_CAVALRY(4, 8),
ARTILLERY(2, 4),
HORSE_ARTILLERY(2, 8);
private final int line;
private final int column;
@magomar
magomar / Util
Created September 5, 2013 13:20
Helper class with various utility methods
public class Util {
public static boolean isEven(int number) {
return (number & 1) == 0;
}
public static boolean isOdd(int number) {
return (number & 1) == 1;
}
}
@magomar
magomar / Direction
Last active December 22, 2015 09:09
Class used to describe the directions of the sides of an hexagon relative to its center.
public enum Direction {
N(0, -1, -1),
NE(1, 0, -1),
SE(1, 1, 0),
S(0, 1, 1),
SW(-1, 1, 0),
NW(-1, 0, -1),
C(0, 0, 0);
private final int incColumn;
private final int incRowEven;
@magomar
magomar / HexagonalMap-0.0.1
Last active December 22, 2015 06:59
JPanel implementation of an hexagonal map, with methods to convert between pixels and map coordinates (both directions, from map coordinates to pixels and viceversa)
import javax.swing.*;
import java.awt.*;
public class HexagonalMap extends JPanel {
private int width; // Number of columns
private int height; // Number of rows
private int hexSide; // Side of the hexagon
private int hexOffset; // Distance from left horizontal vertex to vertical axis
private int hexApotheme; // Apotheme of the hexagon = radius of inscribed circumference
private int hexRectWidth; // Width of the circumscribed rectangle
@magomar
magomar / HexagonalMapGUI-0.0.1
Last active December 22, 2015 06:59
JFrame used to show hexagonal maps and associated info. Includes a class implementing MouseMotionListener to interact with the map by mouse.
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class HexagonalMapGUI extends JFrame {
static final int HEX_SIDE = 25; // side of hexagonal tile in pixels
static final int MAP_WIDTH = 10; // number of columns
static final int MAP_HEIGHT = 10; // number of rows
private HexagonalMap map;