Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.66 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4. * BoardInterface represents the board state in the game of tabula (not including dice and players).
  5. *
  6. * Requires a constructor with no parameters which creates and initialises all of the locations for the startPosition of the game.
  7. *
  8. **/
  9. public class Board implements BoardInterface {
  10. private String name;
  11. private ArrayList<LocationInterface> board;
  12. private LocationInterface knockArr;
  13. private LocationInterface startArr;
  14. private LocationInterface endArr;
  15.  
  16.  
  17. private static final int PIECES_PER_PLAYER = 15;
  18. private static final int NUMBER_OF_LOCATIONS = 24;
  19.  
  20.  
  21. // Initialises board array
  22. public Board() {
  23.  
  24. ArrayList<LocationInterface> board = new ArrayList<LocationInterface>();
  25. this.board=board;
  26.  
  27. //Initialises Knocked Array
  28.  
  29. //Add knocked as the first position in the board
  30.  
  31.  
  32. // Initilises startPosition array
  33. LocationInterface startPosition = new Location("startPosition");
  34. startPosition.setMixed(true);
  35. // Add startPosition to the board as the second position on the board
  36. board.add(startPosition);
  37.  
  38.  
  39. // Adds the rest of the positins on the board
  40. for (int i = 1; i < NUMBER_OF_LOCATIONS+1 ; i++) {
  41. LocationInterface InitLocation = new Location(Integer.toString(i));
  42. board.add(InitLocation);
  43. }
  44. //Adds a final position on the board
  45. LocationInterface end = new Location("Ended");
  46. end.setMixed(true);
  47. board.add(end);
  48.  
  49.  
  50. LocationInterface knocked = new Location("Knocked");
  51. knocked.setMixed(true);
  52. board.add(knocked);
  53. //Creates variables that point to key arrays for easy access
  54. knockArr=board.get(NUMBER_OF_LOCATIONS+2); //0
  55. startArr=board.get(0); //1
  56. endArr=board.get(NUMBER_OF_LOCATIONS+1); //25
  57.  
  58. //Adds the starting amount of pieces to the starting position
  59. for (int i = 1; i <= PIECES_PER_PLAYER; i++) {
  60.  
  61. try{
  62. startArr.addPieceGetKnocked(Colour.GREEN);
  63.  
  64. }
  65.  
  66. catch (Exception ex){
  67.  
  68. }
  69. try{
  70. startArr.addPieceGetKnocked(Colour.BLUE);
  71.  
  72. }
  73.  
  74. catch (Exception ex){
  75.  
  76. }
  77. }
  78.  
  79.  
  80. }
  81.  
  82. public void setName(String name) {
  83. this.name = name;
  84. }
  85.  
  86. /**
  87. * @return the Location off the board where all pieces startPosition the game. This will be a mixed location.
  88. **/
  89. public LocationInterface getStartLocation() {
  90. return startArr;
  91. }
  92.  
  93. /**
  94. * @return the Location off the board where pieces get to when they have gone all the way round the board. This will be a mixed location.
  95. **/
  96. public LocationInterface getEndLocation() {
  97. return endArr;
  98. }
  99.  
  100. /**
  101. * @return the Location where pieces go to when they are knocked off the board by an opposing piece. This will be a mixed location.
  102. **/
  103. public LocationInterface getKnockedLocation() {
  104. return knockArr;
  105. }
  106.  
  107. /**
  108. * @return the Location corresponding to a numbered position on the board. This will not be a mixed location.
  109. *
  110. * @param locationNumber the number of the location going from 1-24
  111. *
  112. * @throws NoSuchLocationException when position is not in the range 1-24
  113. **/
  114. public LocationInterface getBoardLocation(int locationNumber) throws NoSuchLocationException
  115. { //Returns the position on the board associated with the location Number
  116. if ( !((0 <= locationNumber) && (locationNumber <= NUMBER_OF_LOCATIONS)) ) {
  117. throw new NoSuchLocationException("Invalid Location");
  118. }
  119. return board.get(locationNumber +1);
  120. }
  121.  
  122. /**
  123. * @param colour the colour to move
  124. *
  125. * @param move the move to make
  126. *
  127. * @return true if and only if, from the current board state it would be legal for the given colour to make the given move.
  128. **/
  129.  
  130. //Returns whether a move is allowed
  131. public boolean canMakeMove(Colour colour, MoveInterface move) {
  132.  
  133. //What's the square the piece is trying to move to?
  134. int destinationLocation = move.getDiceValue() + move.getSourceLocation();
  135.  
  136. //If this is greater than the 24 squares on the board, sanitise the input so stops the on the end array
  137. if(destinationLocation>NUMBER_OF_LOCATIONS+1)
  138. {
  139. destinationLocation=NUMBER_OF_LOCATIONS+1;
  140. }
  141.  
  142. //flag for a valid move, starts off as true and is tested against conditions that will make it false
  143. boolean canMove=true;
  144.  
  145. // Does the knocked list have pieces in it? If so, is the move from the knocked list?
  146. if ( knockArr.numberOfPieces(colour)>0) { // knocked off section has piecces in it
  147. if ( move.getSourceLocation() !=0 ) {
  148. return canMove=false;
  149.  
  150. }
  151.  
  152. }
  153. else {
  154. //Does the source location have pieces in it of that colour? If not, the move cannot be made
  155. try {
  156. if (board.get(move.getSourceLocation()).numberOfPieces(colour)==0) {
  157. return canMove=false;
  158. }
  159.  
  160. }
  161. catch (Exception e) {}
  162. }
  163.  
  164. // Can you add that piece to the destination location?
  165.  
  166. if (!board.get(destinationLocation).canAddPiece(colour))
  167. {
  168. return canMove=false;
  169. }
  170.  
  171. return canMove;
  172. }
  173.  
  174. /**
  175. * Update the Board state by making the given move for the given colour, including any knocking off.
  176. *
  177. * @param colour the colour to move
  178. *
  179. * @param move the move to make
  180. *
  181. * @throws IllegalMoveException if and only if the move is not legal.
  182. **/
  183. public void makeMove(Colour colour, MoveInterface move) throws IllegalMoveException {
  184.  
  185. // Calculates destination
  186. int destination = move.getDiceValue() + move.getSourceLocation(); // destination is the position in the array list
  187.  
  188. //Check if you can make a move
  189. if (!canMakeMove(colour, move) ) {
  190. throw new IllegalMoveException("This move is illegal.");
  191. }
  192. //Sanitises the destination input, so if you complete the board, it stays in the end array
  193. if(destination>NUMBER_OF_LOCATIONS)
  194. {
  195. destination=NUMBER_OF_LOCATIONS;
  196. }
  197.  
  198. //Sets the start location
  199. LocationInterface source=board.get(move.getSourceLocation());
  200. //Sets the destination location
  201. LocationInterface dest=board.get(destination);
  202. //Adds a piece to the destination. If a piece is knocked out, it feeds it into the knockArr input
  203. knockArr.addPieceGetKnocked(dest.addPieceGetKnocked(colour));
  204. //Takes a piece from the source
  205. source.removePiece(colour);
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. }
  213.  
  214. /**
  215. * Update the Board state by making the all of the moves in the given turn in order, including any knocking off, based on the given diceValues.
  216. *
  217. * @param colour the colour to move
  218. *
  219. * @param turn the turn to take
  220. *
  221. * @param diceValues the values of the dice available in no particular order. There will be repeated values in the list if a double is thrown
  222. *
  223. * @throws IllegalTurnException if and only if the turns in the move are not legal for the diceValues give. Each of the moves has to be legal, and the diceValues in the moves of the turn must match the diceValues parameter. The number of moves in the turn must be no less than the maximum possible number of legal moves: all available dice must be used. If IllegalTurnException is thrown then the board state remains unchanged.
  224. **/
  225. public void takeTurn(Colour colour, TurnInterface turn, List<Integer> diceValues) throws IllegalTurnException {
  226. List<MoveInterface> listOfMoves = new ArrayList<MoveInterface>();
  227. listOfMoves = turn.getMoves();
  228.  
  229. for (MoveInterface currentMove : listOfMoves) {
  230. if ( !diceValues.contains(currentMove.getDiceValue()) || listOfMoves.size() > 4 ) {
  231. throw new IllegalTurnException("This move cannot be made");
  232. }
  233. canMakeMove(colour, currentMove); // to check
  234. }
  235.  
  236. //implement the moves
  237. for (MoveInterface currentMove : listOfMoves) {
  238. try {
  239. makeMove(colour, currentMove);
  240. }
  241. catch (IllegalMoveException e) {}
  242. }
  243. }
  244.  
  245. /**
  246. * @param colour the colour to check
  247. *
  248. * @return true if and only if the given colour has won
  249. **/
  250. public boolean isWinner(Colour colour) {
  251. if ( endArr.numberOfPieces(colour) >= PIECES_PER_PLAYER ) {
  252. return true;
  253. }
  254. return false;
  255. }
  256.  
  257. /**
  258. * @return the colour of the winner if there is one, otherwise null
  259. **/
  260. public Colour winner() {
  261. if ( isWinner(Colour.GREEN) ) {
  262. return Colour.GREEN;
  263. }
  264. else if ( isWinner(Colour.BLUE) ) {
  265. return Colour.BLUE;
  266. }
  267. return null;
  268. }
  269.  
  270. /**
  271. * @return true if and only if the Board is in a valid state (do not need to check whether or not it could be reached by a valid sequence of moves)
  272. **/
  273. public boolean isValid() {
  274.  
  275. //Set flag for validity
  276. boolean valid=true;
  277.  
  278. //Checks each position on the board
  279. for(LocationInterface Loc : board)
  280. {
  281. valid=Loc.isValid();
  282.  
  283. //If it finds a single invalid position, it returns that the board is in an invalid state
  284. if(valid==false)
  285. {
  286. return false;
  287. }
  288.  
  289. }
  290. return valid;
  291. }
  292. /**
  293. * @param colour the colour to move next
  294. *
  295. * @param diceValues the dice values available to use
  296. *
  297. * @return a list of moves that the given colour can make from the current board state with (any one of) the given diceValues
  298. **/
  299. public Set<MoveInterface> possibleMoves(Colour colour, List<Integer> diceValues) {
  300. Set<MoveInterface> possibleMoves = new HashSet<MoveInterface>();
  301. List<Integer> diceAvalaible = diceValues;
  302.  
  303. //startPosition
  304. if ( startArr.numberOfPieces(colour) != 0 ) {
  305. for (Integer currentDice : diceAvalaible) {
  306. MoveInterface move = new Move();
  307. try {
  308. move.setSourceLocation(0);
  309. move.setDiceValue(currentDice);
  310. }
  311. catch ( NoSuchLocationException e1 ) {}
  312. catch ( IllegalMoveException e2 ) {
  313. //System.out.println("The dice value is invalid " + currentDice);
  314. }
  315. if ( canMakeMove(colour, move) && !moveAlreadyPresent(possibleMoves, move) ) {
  316. possibleMoves.add(move);
  317. }
  318. }
  319. }
  320.  
  321. if ( isknockedEmpty(colour) ) {
  322. //board loop
  323. for (LocationInterface currentSpace : board) {
  324. if ( currentSpace.numberOfPieces(colour) != 0 ) {
  325. for (Integer currentDice : diceAvalaible) {
  326. MoveInterface move = new Move();
  327. try {
  328. move.setSourceLocation(Integer.parseInt(currentSpace.getName()));
  329. move.setDiceValue(currentDice);
  330. if ( canMakeMove(colour, move) && !possibleMoves.contains(move) ) {
  331. possibleMoves.add(move);
  332. }
  333. }
  334. catch ( NoSuchLocationException e1 ) {}
  335. catch( IllegalMoveException e2 ) {}
  336. }
  337. }
  338. }
  339. }
  340.  
  341. return possibleMoves;
  342. }
  343.  
  344. /**
  345. * @return a copy of the board that can be passed to players to work with
  346. */
  347. public BoardInterface clone() {
  348. BoardInterface boardClone = new Board();
  349. //startPosition
  350. while (boardClone.getStartLocation().numberOfPieces(Colour.values()[0]) > getStartLocation().numberOfPieces(Colour.values()[0])) {
  351. try {
  352. boardClone.getStartLocation().removePiece(Colour.values()[0]);
  353. }
  354. catch ( Exception e) {}
  355. }
  356. while (boardClone.getStartLocation().numberOfPieces(Colour.values()[1]) > getStartLocation().numberOfPieces(Colour.values()[1])) {
  357. try {
  358. boardClone.getStartLocation().removePiece(Colour.values()[1]);
  359. }
  360. catch ( Exception e) {}
  361. }
  362.  
  363. //knocked
  364. while (boardClone.getKnockedLocation().numberOfPieces(Colour.values()[0]) < getKnockedLocation().numberOfPieces(Colour.values()[0])) {
  365. try {
  366. boardClone.getKnockedLocation().addPieceGetKnocked(Colour.values()[0]);
  367. }
  368. catch ( Exception e) {}
  369. }
  370. while (boardClone.getKnockedLocation().numberOfPieces(Colour.values()[1]) < getKnockedLocation().numberOfPieces(Colour.values()[1])) {
  371. try {
  372. boardClone.getKnockedLocation().addPieceGetKnocked(Colour.values()[1]);
  373. }
  374. catch ( Exception e) {}
  375. }
  376.  
  377. //end
  378. while (boardClone.getEndLocation().numberOfPieces(Colour.values()[0]) < getEndLocation().numberOfPieces(Colour.values()[0])) {
  379. try {
  380. boardClone.getEndLocation().addPieceGetKnocked(Colour.values()[0]);
  381. }
  382. catch ( Exception e) {}
  383. }
  384. while (boardClone.getEndLocation().numberOfPieces(Colour.values()[1]) < getEndLocation().numberOfPieces(Colour.values()[1])) {
  385. try {
  386. boardClone.getEndLocation().addPieceGetKnocked(Colour.values()[1]);
  387. }
  388. catch ( Exception e) {}
  389. }
  390.  
  391. //board
  392. try {
  393. int i = 1;
  394. for (LocationInterface currentSpace : board) {
  395. while (boardClone.getBoardLocation(i).numberOfPieces(Colour.values()[0]) < getBoardLocation(i).numberOfPieces(Colour.values()[0])) {
  396. try {
  397. boardClone.getBoardLocation(i).addPieceGetKnocked(Colour.values()[0]);
  398. }
  399. catch ( Exception e) {}
  400. }
  401.  
  402. while (boardClone.getBoardLocation(i).numberOfPieces(Colour.values()[1]) < getBoardLocation(i).numberOfPieces(Colour.values()[1])) {
  403. try {
  404. boardClone.getBoardLocation(i).addPieceGetKnocked(Colour.values()[1]);
  405. }
  406. catch ( Exception e) {}
  407. }
  408. i++;
  409. }
  410. }
  411. catch (Exception e) {}
  412.  
  413. return boardClone;
  414. }
  415.  
  416. /**
  417. * Overrides toString() from Object with a suitable String representation of the board state for displaying via the console to a human
  418. **/
  419. public String toString() {
  420. String boardCondition = "";
  421. int dimensions = 20;
  422.  
  423. //startPosition
  424. boardCondition += "+---------------+--------------------+--------------------+" + '\n';
  425. boardCondition += "| Location |" + makeCertainLength(Colour.values()[0].toString(), dimensions) + "|" + makeCertainLength(Colour.values()[1].toString(), dimensions) + "|" + '\n';
  426. boardCondition += "+---------------+--------------------+--------------------+" + '\n';
  427. boardCondition += "| startPosition |" + makeCertainLength( String.valueOf(startArr.numberOfPieces(Colour.values()[0])), dimensions) + "|" + makeCertainLength( String.valueOf(startArr.numberOfPieces(Colour.values()[1])), dimensions) + "|" + '\n';
  428. boardCondition += "+---------------+--------------------+--------------------+" + '\n';
  429. boardCondition += "| Knocked Off |" + makeCertainLength( String.valueOf(knockArr.numberOfPieces(Colour.values()[0])) , dimensions) + "|" + makeCertainLength( String.valueOf(knockArr.numberOfPieces(Colour.values()[1])), dimensions) + "|" + '\n';
  430. boardCondition += "+---------------+--------------------+--------------------+" + '\n';
  431.  
  432. for (int i = 1; i <= NUMBER_OF_LOCATIONS ; i++) {
  433. LocationInterface currentLocation = null;
  434. try {
  435. currentLocation = getBoardLocation(i); // to avoid having to call a location of 0
  436. }
  437. catch (Exception e) {}
  438. boardCondition += "|" + makeCertainLength( String.valueOf(i) , 15) + "|" + makeCertainLength( String.valueOf(currentLocation.numberOfPieces(Colour.values()[0])), dimensions) + "|" + makeCertainLength(String.valueOf(currentLocation.numberOfPieces(Colour.values()[1])), dimensions) + "|" + '\n';
  439. boardCondition += "+---------------+--------------------+--------------------+" + '\n';
  440. }
  441.  
  442. boardCondition += "| ended |" + makeCertainLength(String.valueOf(endArr.numberOfPieces(Colour.values()[0])), dimensions) + "|" + makeCertainLength(String.valueOf(endArr.numberOfPieces(Colour.values()[1])), dimensions) + "|" + '\n';
  443. boardCondition += "+---------------+--------------------+--------------------+" + '\n';
  444.  
  445. return boardCondition;
  446. }
  447.  
  448. // extra methods
  449. public boolean pieceHasended(int destination) {
  450. if ( destination > NUMBER_OF_LOCATIONS) {
  451. return true;
  452. }
  453. else {
  454. return false;
  455. }
  456. }
  457.  
  458. /**
  459. * checks whethert the knocked section contains any pieces of the given colour
  460. */
  461. public boolean isknockedEmpty(Colour colour) {
  462. if (knockArr.numberOfPieces(colour) == 0) {
  463. return true;
  464. }
  465. else {
  466. return false;
  467. }
  468. }
  469.  
  470. /**
  471. * colour is the colour that is being moved
  472. */
  473. public void sendRemovedToknocked(Colour colour, int destination) {
  474. if ( destination <= NUMBER_OF_LOCATIONS ) {
  475. int numberToMove = 0;
  476. try {
  477. numberToMove = getBoardLocation(destination).numberOfPieces(colour);
  478. }
  479. catch (Exception e) {}
  480. for (int i = 1; i <= numberToMove ; i++) {
  481. try {
  482. knockArr.addPieceGetKnocked(colour);
  483. }
  484. catch (Exception e) {}
  485. }
  486. }
  487. }
  488.  
  489. public String makeCertainLength(String text, int length) {
  490. String textAltered = "";
  491. int boardOnEachSide = (length - text.length())/2;
  492.  
  493. for (int i = 0; i < boardOnEachSide ; i++) {
  494. textAltered += " ";
  495. }
  496. textAltered += text;
  497. for (int i = 0; i < boardOnEachSide ; i++) {
  498. textAltered += " ";
  499. }
  500.  
  501. if ( textAltered.length() != length ) {
  502. textAltered += " ";
  503. }
  504.  
  505. return textAltered;
  506. }
  507.  
  508. public boolean moveAlreadyPresent(Set<MoveInterface> possibleMoves, MoveInterface move) {
  509. int diceValue = move.getDiceValue();
  510. int sourceLocation = move.getSourceLocation();
  511.  
  512. for (MoveInterface currentMove : possibleMoves ) {
  513. if ( currentMove.getDiceValue() == diceValue && currentMove.getSourceLocation() == sourceLocation ) {
  514. return true;
  515. }
  516. }
  517. return false;
  518. }
  519. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement