|
- /**
- * @file Common.java
- *
- * @author Christos Choutouridis AEM:8997
- * @email cchoutou@ece.auth.gr
- */
- package net.hoo2.auth.labyrinth;
-
- import java.util.ArrayList;
- import java.util.Collections;
-
- /**
- * Class to hold constant values for entire application
- */
- class Const {
- static final int maxTileWalls = 2; /**< Number of maximum walls for each tile on the board */
- static final int noSupply =-1; /**< Number to indicate the absent of supply */
- static final int noTileId =-1; /**< Number to indicate wrong tileId */
- }
- /**
- * Application wide object to hold settings like values for the session.
- */
- class Session {
- static int boardSize = 15; /**< Default board's size (if no one set it via command line) */
- }
-
- /**
- * Helper C++-like enumerator class to hold direction
- */
- class Direction {
- static final int UP =1; /**< North direction */
- static final int RIGHT =3; /**< East direction */
- static final int DOWN =5; /**< South direction */
- static final int LEFT =7; /**< West direction */
- static final int Begin =1; /**< Iterator style begin of range direction (starting north) */
- static final int End =8; /**< Iterator style end of range direction (one place after the last) */
- static final int Step =2; /**< Step for iterator style direction */
-
- /**
- * Utility to get the opposite
- * @param direction Input direction
- * @return The opposite direction
- */
- static int opposite (int direction) { return (direction+4)%End; }
- }
-
- /**
- * @brief
- * An Application wide board position implementation holding just the id coordinate.
- *
- * Position is a helper class to enable us cope with the redundant position data (id and coordinates).
- * This class provide both static conversion functionalities between id and coordinates
- * and data representation in the coordinates system.
- * For clarity we adopt a row-column naming convention.
- */
- class Position {
-
- /**
- * Basic constructor from row-column coordinates
- * @param row The row coordinate
- * @param col The column coordinate
- */
- Position(int row, int col) {
- this.id = toID(row, col);
- }
-
- /**
- * Basic constructor from Id
- * @param tileId The id of tile
- */
- Position(int tileId) {
- this.id = tileId;
- }
-
- /**
- * Constructor from row-column coordinates and a direction.
- *
- * This constructor creates a position relative to coordinates.
- *
- * @param row The row coordinate
- * @param col The column coordinate
- * @param direction The direction
- */
- Position(int row, int col, int direction) {
- switch (direction) {
- case Direction.UP: this.id = toID(row+1, col); break;
- case Direction.DOWN: this.id = toID(row-1, col); break;
- case Direction.LEFT: this.id = toID(row, col-1); break;
- case Direction.RIGHT:this.id = toID(row, col+1); break;
- }
- }
-
- /** @name non-static API */
- /** @{ */
- int getRow() { return toRow(id); } /**< Read access to virtual row coordinate */
- int getCol() { return toCol(id); } /**< Read access to virtual column coordinate */
- int getId() { return id; } /**< Read access to id coordinate */
- /** @} */
-
- /** @name Static convention utilities */
- /** @{ */
- /**
- * Takes row and column coordinates and return the calculated Id coordinate
- * @param row The row coordinate
- * @param col The column coordinate
- * @return The converted value
- */
- static int toID(int row, int col) {
- return row * Session.boardSize + col;
- }
-
- /**
- * Takes Id coordinate and return the corresponding row coordinate
- * @param id The id coordinate
- * @return The row coordinate
- */
- static int toRow(int id){
- return id / Session.boardSize;
- }
- /**
- * Takes Id coordinate and return the corresponding column coordinate
- * @param id The id coordinate
- * @return The column coordinate
- */
- static int toCol(int id) {
- return id % Session.boardSize;
- }
- /** @} */
-
- /** @name private data types */
- /** @{ */
- private int id; /**< The id coordinate of the constructed Position object */
- /** @} */
- }
-
- /**
- * Class to create ranges of numbers
- */
- class Range {
- /**
- * Create the range [begin, end)
- * @param begin The first item on the range
- * @param end The item after the last on the range
- */
- Range (int begin, int end) {
- numbers = new ArrayList<Integer>();
- init (begin, end, 1);
- }
- /**
- * Create the range [begin, end) using step as interval between items.
- * @param begin The first item on the range
- * @param end The item after the last on the range
- * @param step The interval between items
- */
- Range(int begin, int end, int step) {
- numbers = new ArrayList<Integer>();
- init (begin, end, step);
- }
-
- /**
- * Common utility to create the range for all constructors
- */
- private void init (int begin, int end, int step) {
- numbers.clear();
- for (int i=begin ; i<end ; i+=step)
- numbers.add(i);
- }
- /**
- * Extract and return the first item from the range.
- * @return The first item of the range or Const.noTileId if there is none.
- */
- int get () {
- try {
- return numbers.remove(0);
- }
- catch (IndexOutOfBoundsException e) {
- return Const.noTileId;
- }
- }
-
- /** @name protected data types */
- /** @{ */
- protected ArrayList<Integer> numbers; /**< handle to range */
- /** @} */
- }
-
- /**
- * Class to create shuffled ranges of numbers
- */
- class ShuffledRange extends Range {
- /**
- * Create a shuffled version of range [begin, end)
- * @param begin The first item on the range
- * @param end The item after the last on the range
- */
- ShuffledRange(int begin, int end) {
- super(begin, end); // Delegate
- Collections.shuffle(numbers);
- }
- /**
- * Create a shuffled version of the range [begin, end)
- * using step as interval between items.
- *
- * @param begin The first item on the range
- * @param end The item after the last on the range
- * @param step The interval between items
- */
- ShuffledRange(int begin, int end, int step) {
- super(begin, end, step); // Delegate
- Collections.shuffle(numbers);
- }
- }
|