Advertisement
Guest User

Untitled

a guest
May 24th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.93 KB | None | 0 0
  1. import java.io.FileWriter;
  2. import java.io.IOException;
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5. import java.util.Scanner;
  6.  
  7. public class Coderun {
  8. static FileWriter fw;
  9.  
  10. /*
  11. * Sizes of the game field
  12. */
  13. private static final int FIELD_COLUMN_COUNT = 25;
  14. private static final int FIELD_ROW_COUNT = 16;
  15.  
  16. /*
  17. * Values of cells
  18. */
  19. private static final char REMOVED_BRICK = '-';
  20. private static final char LADDER = 'H';
  21. private static final char BRICK = '=';
  22. private static final char EMPTY = '.';
  23. private static final char GOLD = '*';
  24.  
  25. /*
  26. * Contains previous positions used by BFS
  27. */
  28. private static Position prevPosition[][] = new Position[FIELD_ROW_COUNT][FIELD_COLUMN_COUNT];
  29.  
  30. /*
  31. * Contains previous moves used by BFS
  32. */
  33. private static Move prevMove[][] = new Move[FIELD_ROW_COUNT][FIELD_COLUMN_COUNT];
  34.  
  35. /*
  36. * The game field
  37. */
  38. private static char[][] field = new char[FIELD_COLUMN_COUNT][FIELD_ROW_COUNT];
  39.  
  40. /*
  41. * Position queue used by BFS
  42. */
  43. private static Queue<Position> queue = new LinkedList<Position>();
  44.  
  45. /*
  46. * Programs of the enemies
  47. */
  48. private static String[] enemyPrograms;
  49.  
  50. /*
  51. * Positions of the enemies
  52. */
  53. private static Position[] enemies;
  54.  
  55. /*
  56. * Masters of the enemies
  57. */
  58. private static int[] masterOfEnemy;
  59.  
  60. /*
  61. * Positions of the runners, our runner is on the first place
  62. */
  63. private static Position[] runners;
  64.  
  65. /*
  66. * Some information about the runners
  67. */
  68. private static int[] scores;
  69. private static int[] delays;
  70.  
  71. /*
  72. * Scanner used for data reading
  73. */
  74. private static Scanner in;
  75.  
  76. /*
  77. * Enemy near runner position information
  78. */
  79. private static Position[] posi;
  80.  
  81. /*
  82. * Check runner on LADDER
  83. */
  84. private static boolean LADDER_FLAG;
  85.  
  86. // public Coderun() {
  87. // }
  88.  
  89. /*
  90. * Entry point of player
  91. */
  92. public static void main(String[] args) {
  93. try {
  94. in = new Scanner(System.in);
  95.  
  96. // Read initial state of the world
  97. int turns = in.nextInt();
  98. readFirstDescription();
  99.  
  100. // Pass through turns
  101. for (int turnIndex = 0; turnIndex < turns; turnIndex++) {
  102. int turn = in.nextInt();
  103. if (turn == -1) {
  104. break;
  105. }
  106.  
  107. // Read the current state of the world and make a move
  108. readStateDescription();
  109.  
  110. System.out.println(makeMove());
  111. System.out.flush();
  112. }
  113. } catch (Exception e) {
  114. System.err.println(e.getMessage());
  115. System.exit(-1);
  116. }
  117. }
  118.  
  119. /*
  120. * Calculates the next move for our runner
  121. */
  122. private static Move makeMove() {
  123. posi = new Position[13];
  124. for (int i = 0; i < 9; i++) {
  125. posi[i] = new Position(runners[0].getRow() - 1 + i / 3,
  126. runners[0].getColumn() - 1 + i % 3);
  127. }
  128.  
  129. posi[9] = new Position(runners[0].getRow() - 2, runners[0].getColumn());
  130. posi[10] = new Position(runners[0].getRow() + 2, runners[0].getColumn());
  131. posi[11] = new Position(runners[0].getRow(), runners[0].getColumn() - 2);
  132. posi[12] = new Position(runners[0].getRow(), runners[0].getColumn() + 2);
  133.  
  134. LADDER_FLAG = false;
  135. if (getCell(runners[0]) == LADDER)
  136. LADDER_FLAG = true;
  137.  
  138. // Is our runner died?
  139. if (!runners[0].correct()) {
  140. return Move.NONE;
  141. }
  142. Move togo = moveToGold();
  143. Move reverse = reverseMove(togo);
  144. Position togoposi = moveDirection(togo);
  145. Position revgoposi = moveDirection(reverse);
  146.  
  147. int toEnemy = isNearEnemy(togo);
  148. int reEnemy = isNearEnemy(reverse);
  149. if (LADDER_FLAG) {
  150. if (toEnemy != 0) {
  151. if (reEnemy != 0) {
  152. return Jump();
  153. } else {
  154. if (isLand()) {
  155. return Jump();
  156. }
  157. if (canGo(getCell(revgoposi)))
  158. return reverse;
  159. }
  160. }
  161. } else {
  162. if (toEnemy == 2) {
  163. if (togo.equals(Move.LEFT)) {
  164. return Move.DIG_LEFT;
  165. } else {
  166. return Move.DIG_RIGHT;
  167. }
  168. } else if (toEnemy == 1) {
  169. if (reEnemy == 0 && canGo(getCell(revgoposi))) {
  170. return reverse;
  171. } else {
  172. if (togo.equals(Move.LEFT)) {
  173. return Move.DIG_LEFT;
  174. } else {
  175. return Move.DIG_RIGHT;
  176. }
  177. }
  178. }
  179. }
  180.  
  181. // Clearing arrays for BFS
  182. for (int x = 0; x < FIELD_ROW_COUNT; x++) {
  183. for (int y = 0; y < FIELD_COLUMN_COUNT; y++) {
  184. prevPosition[x][y] = null;
  185. prevMove[x][y] = null;
  186. }
  187. }
  188.  
  189. // BFS
  190. queue.clear();
  191. Position target = null;
  192.  
  193. // Start BFS from our runner's position to all cells with gold
  194. update(runners[0], null, Move.NONE);
  195. while (!queue.isEmpty()) {
  196. Position position = queue.poll();
  197. if (getCell(position) == GOLD) {
  198. target = position;
  199. break;
  200. }
  201.  
  202. // Iterate through possible moves in order (LEFT, RIGHT, TOP,
  203. // BOTTOM)
  204. for (Move move : Move.values()) {
  205. Position newPosition = move(position, move);
  206. update(newPosition, position, move);
  207. }
  208. }
  209.  
  210. // If there is no available gold on the field, then do nothing
  211. if (target == null) {
  212. return Move.NONE;
  213. }
  214.  
  215. // Returning back through previous positions from the closest gold
  216. Position current = target, previous = target;
  217. while (!current.equals(runners[0])) {
  218. previous = current;
  219. current = prevPosition[current.getRow()][current.getColumn()];
  220. }
  221.  
  222. // Make a move to the closest gold
  223. return prevMove[previous.getRow()][previous.getColumn()];
  224. }
  225.  
  226. /*
  227. * Returns the next position if a one makes the given move from the given
  228. * position
  229. */
  230. private static Position move(Position currentPosition, Move move) {
  231. // Move ignoring trapped enemies
  232. Position newPosition = moveIgnoringEnemies(currentPosition, move, true);
  233.  
  234. // Checks for a enemy below in cell with removed brick
  235. if (!newPosition.equals(currentPosition)
  236. && getCell(newPosition) == REMOVED_BRICK) {
  237. for (Position enemy : enemies) {
  238. if (enemy.equals(newPosition)) {
  239. // Move without fall down
  240. Position position = moveIgnoringEnemies(currentPosition,
  241. move, false);
  242. if (position.equals(newPosition) && move == Move.BOTTOM) {
  243. return currentPosition; // Cannot move below to the cell
  244. // with enemy
  245. } else {
  246. return position;
  247. }
  248. }
  249. }
  250. }
  251.  
  252. return newPosition;
  253. }
  254.  
  255. /*
  256. * Returns the next position if a one makes the given move from the given
  257. * position, but ignores enemies in cells with removed bricks
  258. */
  259. private static Position moveIgnoringEnemies(Position position, Move move,
  260. boolean canFly) {
  261. char currentCell = getCell(position);
  262. Position shiftedTopPosition = position.shift(Move.TOP);
  263. Position shiftedRightPosition = position.shift(Move.RIGHT);
  264. Position shiftedBottomPosition = position.shift(Move.BOTTOM);
  265. Position shiftedLeftPosition = position.shift(Move.LEFT);
  266.  
  267. // Is flying?
  268. if (isEmptyLike(currentCell)
  269. && isEmptyLike(getCell(shiftedBottomPosition)) && canFly) {
  270. return shiftedBottomPosition;
  271. }
  272.  
  273. if (move == Move.NONE) {
  274. return position;
  275. }
  276.  
  277. switch (move) {
  278. case TOP:
  279. if (currentCell == LADDER && getCell(shiftedTopPosition) != BRICK) {
  280. return shiftedTopPosition;
  281. }
  282. break;
  283. case RIGHT:
  284. if (getCell(shiftedRightPosition) != BRICK) {
  285. return shiftedRightPosition;
  286. }
  287. break;
  288. case BOTTOM:
  289. if ((currentCell == LADDER || isEmptyLike(currentCell))
  290. && (getCell(shiftedBottomPosition) == LADDER || isEmptyLike(getCell(shiftedBottomPosition)))) {
  291. return shiftedBottomPosition;
  292. }
  293. break;
  294. case LEFT:
  295. if (getCell(shiftedLeftPosition) != BRICK) {
  296. return shiftedLeftPosition;
  297. }
  298. break;
  299. }
  300.  
  301. return position;
  302. }
  303.  
  304. /*
  305. * Returns true iff the cell looks like empty
  306. */
  307. private static boolean isEmptyLike(char cell) {
  308. return cell == EMPTY || cell == GOLD || cell == REMOVED_BRICK;
  309. }
  310.  
  311. /*
  312. * Returns cell iff the position is inside the game field and BRICK
  313. * otherwise.
  314. */
  315. private static char getCell(Position position) {
  316. if (0 <= position.getRow() && position.getRow() < FIELD_ROW_COUNT) {
  317. if (0 <= position.getColumn()
  318. && position.getColumn() < FIELD_COLUMN_COUNT) {
  319. return field[position.getRow()][position.getColumn()];
  320. }
  321. }
  322.  
  323. // The given position is out of the field
  324. return BRICK;
  325. }
  326.  
  327. /*
  328. * Updates values in BFS
  329. */
  330. private static void update(Position position, Position prev, Move move) {
  331. if (prevMove[position.getRow()][position.getColumn()] == null) {
  332. prevPosition[position.getRow()][position.getColumn()] = prev;
  333. prevMove[position.getRow()][position.getColumn()] = move;
  334. queue.add(position);
  335. }
  336. }
  337.  
  338. /*
  339. * Reads per-turn game state
  340. */
  341. private static void readStateDescription() {
  342. // Read the game field
  343. readGameField();
  344.  
  345. // Read descriptions of the runners
  346. for (int i = 0; i < 2; i++) {
  347. runners[i] = readPosition();
  348. scores[i] = in.nextInt();
  349. delays[i] = in.nextInt();
  350. }
  351.  
  352. // Read descriptions of the enemies
  353. for (int i = 0; i < enemies.length; i++) {
  354. enemies[i] = readPosition();
  355. masterOfEnemy[i] = in.nextInt();
  356. }
  357. }
  358.  
  359. /*
  360. * Reads one-time world description
  361. */
  362. private static void readFirstDescription() {
  363. // Read the game field
  364. readGameField();
  365. runners = new Position[2];
  366. scores = new int[2];
  367. delays = new int[2];
  368.  
  369. // Read descriptions of the runners
  370. readCharactersPositions(runners);
  371.  
  372. // Read descriptions of the enemies
  373. int enemyCount = in.nextInt();
  374. enemies = new Position[enemyCount];
  375. enemyPrograms = new String[enemyCount];
  376. masterOfEnemy = new int[enemyCount];
  377. for (int i = 0; i < enemies.length; i++) {
  378. enemies[i] = readPosition();
  379. enemyPrograms[i] = in.next();
  380. }
  381. }
  382.  
  383. /*
  384. * Reads the game field from the scanner
  385. */
  386. private static void readGameField() {
  387. for (int i = 0; i < FIELD_ROW_COUNT; i++) {
  388. String line = in.next();
  389. for (int j = 0; j < FIELD_COLUMN_COUNT; j++) {
  390. field[i] = line.toCharArray();
  391. }
  392. }
  393. }
  394.  
  395. /*
  396. * Reads array of positions from the scanner
  397. */
  398. private static void readCharactersPositions(Position[] positions) {
  399. for (int i = 0; i < positions.length; i++) {
  400. positions[i] = readPosition();
  401. }
  402. }
  403.  
  404. /*
  405. * Reads position from the scanner
  406. */
  407. private static Position readPosition() {
  408. int row = in.nextInt();
  409. int column = in.nextInt();
  410. return new Position(row, column);
  411. }
  412.  
  413. /*
  414. * Class for positions of runners and enemies on the game field
  415. */
  416. private static class Position {
  417. private final int row;
  418. private final int column;
  419.  
  420. private Position(int row, int column) {
  421. this.row = row;
  422. this.column = column;
  423. }
  424.  
  425. public int getRow() {
  426. return row;
  427. }
  428.  
  429. public int getColumn() {
  430. return column;
  431. }
  432.  
  433. // Correct position isn't equal to "-1 -1"
  434. public boolean correct() {
  435. return row >= 0 && column >= 0;
  436. }
  437.  
  438. // Shift position to specific direction
  439. public Position shift(Move move) {
  440. if (move == Move.LEFT) {
  441. return new Position(row, column - 1);
  442. }
  443.  
  444. if (move == Move.RIGHT) {
  445. return new Position(row, column + 1);
  446. }
  447.  
  448. if (move == Move.TOP) {
  449. return new Position(row - 1, column);
  450. }
  451.  
  452. if (move == Move.BOTTOM) {
  453. return new Position(row + 1, column);
  454. }
  455.  
  456. return new Position(row, column);
  457. }
  458.  
  459. @Override
  460. public String toString() {
  461. return "(" + row + ", " + column + ")";
  462. }
  463.  
  464. @Override
  465. public boolean equals(Object o) {
  466. if (this == o)
  467. return true;
  468. if (!(o instanceof Position))
  469. return false;
  470.  
  471. Position position = (Position) o;
  472. return row == position.row && column == position.column;
  473. }
  474.  
  475. @Override
  476. public int hashCode() {
  477. int result = row;
  478. result = 31 * result + column;
  479. return result;
  480. }
  481. }
  482.  
  483. /*
  484. * Name: getDistance Type: int Parameter: Position p1, Position p2
  485. * Operation: Return distance from p1 to p2
  486. */
  487. public static int getDistance(Position p1, Position p2) {
  488. return (p1.getRow() - p2.getRow() + p1.getColumn() - p2.getColumn());
  489. }
  490.  
  491. /*
  492. * Name: Landing Type: int Parameter: Position p Operation: Return distance
  493. * from the runner to land runner will meet after jumping
  494. */
  495.  
  496. public static int Landing(Position p) {
  497. for (int i = 0; i < 16; i++) {
  498. Position tmp = new Position(p.getRow() + i, p.getColumn());
  499. if (getCell(tmp) == BRICK) {
  500. return i;
  501. }
  502. }
  503. return 0;
  504. }
  505.  
  506. /*
  507. * Name: Jump() Type: Move Parameter: void Operation: Return jump when
  508. * enemies comes to you from both sides.
  509. */
  510.  
  511. public static Move Jump() {
  512. int left_landing;
  513. boolean left_flag;
  514. int left;
  515. int right_landing;
  516. boolean right_flag;
  517. int right;
  518. Move mtg = moveToGold();
  519. Move rtg = reverseMove(moveToGold());
  520. Position toGo = moveDirection(mtg);
  521. Position reversetoGo = moveDirection(rtg);
  522.  
  523. if (isEmptyLike(getCell(posi[3]))) { // left empty
  524. left_landing = Landing(posi[3]);
  525. left_flag = LandOkay(posi[3], left_landing);
  526. left = isNearEnemy(Move.LEFT);
  527. } else { // left brick
  528. left_landing = 0;
  529. left_flag = false;
  530. left = 0;
  531. }
  532. if (isEmptyLike(getCell(posi[5]))) { // right empty
  533. right_landing = Landing(posi[5]);
  534. right_flag = LandOkay(posi[5], right_landing);
  535. right = isNearEnemy(Move.RIGHT);
  536. } else { // right brick
  537. right_flag = false;
  538. right_landing = 0;
  539. right = 0;
  540. }
  541.  
  542. if (isLand()) { // bottom?
  543. if (isEmptyLike(getCell(posi[3])) && isEmptyLike(getCell(posi[6]))
  544. && left == 0) {
  545. return Move.LEFT;
  546. } else if (isEmptyLike(getCell(posi[5]))
  547. && isEmptyLike(getCell(posi[8])) && right == 0) {
  548. return Move.RIGHT;
  549. } else {
  550. if (right != 0 && left > right) {
  551. return Move.DIG_RIGHT;
  552. } else if (left != 0 && left <= right) {
  553. return Move.DIG_LEFT;
  554. }
  555. }
  556. }
  557.  
  558. if (left_landing == 0 && right_landing == 0) {
  559. if (isNearEnemy(mtg) > isNearEnemy(rtg)) {
  560. return mtg;
  561. } else if (canGo(getCell(reversetoGo))) {
  562. return rtg;
  563. }
  564. }
  565.  
  566. if (left_landing == 1 && right_landing == 1) {
  567. if (left == 0) {
  568. if (right == 0)
  569. return mtg;
  570. else if (canGo(getCell(posi[3])))
  571. return Move.LEFT;
  572. } else if (right == 0 && canGo(getCell(posi[5]))) {
  573. return Move.RIGHT;
  574. }
  575.  
  576. if (left == 1) {
  577. if (canDig(Move.RIGHT))
  578. return Move.DIG_RIGHT;
  579. return Move.RIGHT;
  580. } else {
  581. if (canDig(Move.LEFT))
  582. return Move.DIG_LEFT;
  583. if (isNearEnemy(mtg) > isNearEnemy(rtg)) {
  584. return mtg;
  585. } else if (canGo(getCell(reversetoGo))) {
  586. return rtg;
  587. }
  588. }
  589. } else if (left_landing == 1) {
  590. if (left == 0) {
  591. return Move.LEFT;
  592. } else if ((left == 1 || left == 2) && canDig(Move.LEFT)) {
  593. return Move.DIG_LEFT;
  594. } else {
  595. if (LandOkay(posi[5], Landing(posi[5]))
  596. && inthetrick(Move.RIGHT)) {
  597. return Move.RIGHT;
  598. }
  599. if (isNearEnemy(moveToGold()) == 2) {
  600. return mtg;
  601. }
  602. if (isNearEnemy(reverseMove(moveToGold())) == 2
  603. && canGo(getCell(reversetoGo))) {
  604. return rtg;
  605. }
  606. return Move.RIGHT;
  607. }
  608. } else if (right_landing == 1) {
  609. if (right == 0) {
  610. return Move.RIGHT;
  611. } else if ((right == 1 || right == 2) && canDig(Move.RIGHT)) {
  612. return Move.DIG_RIGHT;
  613. } else {
  614. if (LandOkay(posi[3], Landing(posi[3]))
  615. && inthetrick(Move.LEFT)) {
  616. return Move.LEFT;
  617. }
  618. if (isNearEnemy(moveToGold()) == 2) {
  619. return mtg;
  620. }
  621. if (isNearEnemy(reverseMove(moveToGold())) == 2
  622. && canGo(getCell(reversetoGo))) {
  623. return rtg;
  624. }
  625. return Move.LEFT;
  626. }
  627. } else {
  628. if (left_flag) {
  629. if (right_flag) {
  630. if (left_landing > right_landing) {
  631. return Move.RIGHT;
  632. } else {
  633. return Move.LEFT;
  634. }
  635. }
  636. return Move.LEFT;
  637. } else {
  638. if (right_flag) {
  639. return Move.RIGHT;
  640. }
  641. if (isNearEnemy(moveToGold()) == 2) {
  642. return mtg;
  643. }
  644. if (canGo(getCell(reversetoGo))) {
  645.  
  646. }
  647. return rtg;
  648. }
  649. }
  650. return Move.NONE;
  651. }
  652.  
  653. /*
  654. * Name: LandOkay Type: boolean Parameter: Position p, int index Operation:
  655. * Return true when the runner can live after jumping.
  656. */
  657.  
  658. public static boolean LandOkay(Position p, int index) {
  659. Position tmp = new Position(p.getRow() + index - 1, p.getColumn());
  660. Position isEL = new Position(tmp.getRow(), tmp.getColumn()
  661. - (index - 1) / 2);
  662. Position isELR = new Position(tmp.getRow(), tmp.getColumn()
  663. - (index - 1) / 2 + 1);
  664. Position isELL = new Position(tmp.getRow(), tmp.getColumn()
  665. - (index - 1) / 2 - 1);
  666. Position isER = new Position(tmp.getRow(), tmp.getColumn()
  667. + (index - 1) / 2);
  668. Position isERR = new Position(tmp.getRow(), tmp.getColumn()
  669. + (index - 1) / 2 + 1);
  670. Position isERL = new Position(tmp.getRow(), tmp.getColumn()
  671. + (index - 1) / 2 - 1);
  672.  
  673. for (Position e : enemies) {
  674. if (isEL.equals(e) || isELL.equals(e) || isELR.equals(e)
  675. || isER.equals(e) || isERL.equals(e) || isERR.equals(e)) {
  676. return false;
  677. }
  678. }
  679. return true;
  680. }
  681.  
  682. public static int isNearEnemy(Move m) {
  683.  
  684. int dir1 = 0, dir2 = 0, dir3 = 0, dir4 = 0;
  685. int dist = 0;
  686. int flag = 0;
  687.  
  688. if (m.equals(Move.LEFT)) {
  689. dir1 = 3;
  690. dir2 = 11;
  691. } else if (m.equals(Move.RIGHT)) {
  692. dir1 = 5;
  693. dir2 = 12;
  694. } else if (m.equals(Move.TOP)) {
  695. dir1 = 1;
  696. dir2 = 9;
  697. flag = 3;
  698. } else {
  699. dir1 = 7;
  700. dir2 = 10;
  701. flag = 4;
  702. }
  703. for (Position e : enemies) {
  704. if (posi[dir1].equals(e)) {
  705. dist = 1;
  706. break;
  707. }
  708. if (posi[dir2].equals(e)) {
  709. dist = 2;
  710. }
  711. }
  712. if (LADDER_FLAG && dist != 0) {
  713. switch (flag) {
  714. case 3:
  715. dir3 = 0;
  716. dir4 = 2;
  717. break;
  718. case 4:
  719. dir3 = 6;
  720. dir4 = 8;
  721. break;
  722. }
  723. if (flag != 0) {
  724. for (Position e : enemies) {
  725. if (posi[dir3].equals(e)) {
  726. dist = 2;
  727. break;
  728. }
  729. if (posi[dir4].equals(e)) {
  730. dist = 2;
  731. }
  732. }
  733. }
  734. }
  735.  
  736. return dist;
  737. }
  738.  
  739. private static Move moveToGold() {//
  740. // Clearing arrays for BFS
  741. for (int x = 0; x < FIELD_ROW_COUNT; x++) {
  742. for (int y = 0; y < FIELD_COLUMN_COUNT; y++) {
  743. prevPosition[x][y] = null;
  744. prevMove[x][y] = null;
  745. }
  746. }
  747. // BFS
  748. queue.clear();
  749. Position target = null;
  750. // Start BFS from our runner's position to all cells with gold
  751. update(runners[0], null, Move.NONE);
  752. while (!queue.isEmpty()) {
  753. Position position = queue.poll();
  754. if (getCell(position) == GOLD) {
  755. target = position;
  756. break;
  757. }
  758. // Iterate through possible moves in order (LEFT, RIGHT, TOP,
  759. // BOTTOM)
  760. for (Move move : Move.values()) {
  761. Position newPosition = move(position, move);
  762. update(newPosition, position, move);
  763. }
  764. }
  765. // If there is no available gold on the field, then do nothing
  766. if (target == null) {
  767. return Move.NONE;
  768. }
  769. // Returning back through previous positions from the closest gold
  770. Position current = target, previous = target;
  771. while (!current.equals(runners[0])) {
  772. previous = current;
  773. current = prevPosition[current.getRow()][current.getColumn()];
  774. }
  775. // Make a move to the closest gold
  776. return prevMove[previous.getRow()][previous.getColumn()];
  777. }
  778.  
  779. public static Move reverseMove(Move m) {
  780. if (m.equals(Move.LEFT)) {
  781. return Move.RIGHT;
  782. } else if (m.equals(Move.RIGHT)) {
  783. return Move.LEFT;
  784. } else if (m.equals(Move.TOP)) {
  785. return Move.BOTTOM;
  786. } else if (m.equals(Move.BOTTOM)) {
  787. return Move.TOP;
  788. } else {
  789. return Move.NONE;
  790. }
  791. }
  792.  
  793. public static boolean isLand() {
  794. if (getCell(posi[7]) == BRICK) {
  795. return true;
  796. }
  797. return false;
  798. }
  799.  
  800. public static boolean isEnemy(Position p) {
  801. for (Position e : enemies) {
  802. if (p.equals(e)) {
  803. return true;
  804. }
  805. }
  806. return false;
  807. }
  808.  
  809. private static boolean canDig(Move side) {
  810.  
  811. if (runners[0].correct()
  812.  
  813. && delays[0] == 0
  814.  
  815. && runners[0].getRow() != FIELD_COLUMN_COUNT - 1
  816.  
  817. && (getCell(runners[0]) == LADDER
  818.  
  819. || getCell(runners[0].shift(Move.BOTTOM)) == LADDER
  820.  
  821. || getCell(runners[0].shift(Move.BOTTOM)) == BRICK)
  822.  
  823. && getCell(runners[0].shift(Move.BOTTOM).shift(side)) == BRICK
  824.  
  825. && getCell(runners[0].shift(side)) != BRICK
  826.  
  827. && getCell(runners[0].shift(side)) != LADDER)
  828.  
  829. return true;
  830.  
  831. else
  832. return false;
  833.  
  834. }
  835.  
  836. private static boolean canGo(char cell) {
  837. return isEmptyLike(cell) || cell == LADDER;
  838. }
  839.  
  840. private static boolean inthetrick(Move move) {
  841. if (getCell(runners[0].shift(Move.RIGHT)) == REMOVED_BRICK
  842. && move == Move.RIGHT) {
  843. return true;
  844. }
  845.  
  846. else if (getCell(runners[0].shift(Move.LEFT)) == REMOVED_BRICK
  847. && move == Move.LEFT) {
  848. return true;
  849. }
  850. return false;
  851. }
  852.  
  853. private static Position moveDirection(Move m) {
  854. Position toGo;
  855. switch (m) {
  856. case BOTTOM:
  857. toGo = posi[7];
  858. break;
  859. case LEFT:
  860. toGo = posi[3];
  861. break;
  862. case RIGHT:
  863. toGo = posi[5];
  864. break;
  865. case TOP:
  866. toGo = posi[1];
  867. break;
  868. default:
  869. toGo = new Position(-1, -1);
  870. break;
  871. }
  872. return toGo;
  873. }
  874.  
  875. /*
  876. * Possible moves of runner
  877. */
  878. private static enum Move {
  879. NONE, LEFT, RIGHT, TOP, BOTTOM, DIG_LEFT, DIG_RIGHT
  880. }
  881.  
  882. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement