|
- /**
- * @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 Const {
- static final int noSupply =-1;
- static final int noTileId =-1;
- }
- /**
- * 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;
- static final int End =8;
- static final int Step =2;
- }
-
- /**
- * @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 col-row coordinates
- * @param row The row coordinate
- * @param col The column coordinate
- */
- protected Position(int row, int col) {
- this.id = toID(row, col);
- }
-
- /**
- * Basic constructor from Id
- * @param tileId The id of tile
- */
- protected Position(int tileId) {
- this.id = tileId;
- }
-
- /** @name non-static API */
- /** @{ */
- protected int getRow() { return toRow(id); } /**< Read access to virtual row coordinate */
- protected int getCol() { return toCol(id); } /**< Read access to virtual column coordinate */
- protected 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
- */
- protected 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
- */
- protected 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
- */
- protected 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 ShuffledRange {
-
- ShuffledRange() {
- numbers = new ArrayList<Integer>();
- }
-
- ShuffledRange(int begin, int end) {
- numbers = new ArrayList<Integer>();
- init (begin, end);
- }
-
- ShuffledRange(int begin, int step, int end) {
- numbers = new ArrayList<Integer>();
- init (begin, step, end);
- }
-
- void init (int begin, int end) {
- numbers.clear();
- for (int i=begin ; i<end ; ++i)
- numbers.add(i);
- Collections.shuffle(numbers);
- }
-
- void init (int begin, int step, int end) {
- numbers.clear();
- for (int i=begin ; i<end ; i+=step)
- numbers.add(i);
- Collections.shuffle(numbers);
- }
-
- int get () {
- try {
- return numbers.remove(0);
- }
- catch (IndexOutOfBoundsException e) {
- return Const.noTileId;
- }
- }
-
- private ArrayList<Integer> numbers;
- }
|