Advertisement
Guest User

AsciiBot

a guest
Oct 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. AsciiBot:
  2. package robot.ascii;
  3. import control.Control;
  4. import control.RobotControl;
  5. import robot.Robot;
  6. import robot.ascii.impl.Arm;
  7. import robot.ascii.impl.Bar;
  8. import robot.ascii.impl.Block;
  9. import robot.ascii.impl.Drawable;
  10. import robot.impl.RobotImpl;
  11. import robot.impl.RobotInitException;
  12.  
  13. import java.io.IOException;
  14.  
  15. import javax.swing.JFrame;
  16.  
  17. import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
  18. import com.googlecode.lanterna.terminal.Terminal;
  19. import com.googlecode.lanterna.terminal.swing.SwingTerminal;
  20. import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame;
  21.  
  22. // designed by Caspar, additional code by Ross
  23.  
  24. public class ASCIIBot extends AbstractASCIIBot implements Robot {
  25. private Terminal terminal;
  26. private Bar bars[];
  27. private Block blocks[];
  28. private int columnHeights[];
  29. private Arm arm;
  30. private Block heldBlock;
  31. private int[] barHeights;
  32. private int[] blockHeights;
  33.  
  34. public static void main(String[] args) throws IOException {
  35. new RobotControl().control(new ASCIIBot(), null, null);
  36. }
  37.  
  38. // MUST CALL DEFAULT SUPERCLASS CONSTRUCTOR!
  39. public ASCIIBot() throws IOException {
  40. super();
  41. // create the terminal 20 rows, 15 columns
  42. // Terminal terminalFrame = new DefaultTerminalFactory().createTerminal();
  43. Terminal = terminalFrame.createSwingTerminal(15, Control.MAX_HEIGHT);
  44. //terminal = TerminalFacade.createSwingTerminal(22, 14);
  45.  
  46. // required by Lanterna framework to initialise
  47. terminalFrame.enterPrivateMode();
  48. terminalFrame.setCursorVisible(false);
  49.  
  50. terminalFrame.clearScreen();
  51.  
  52.  
  53. }
  54.  
  55.  
  56. @Override
  57. public void init(int[] barHeights, int[] blockHeights, int height, int width, int depth) {
  58. this.bars = new Bar[barHeights.length];
  59. this.blocks = new Block[blockHeights.length];
  60. this.columnHeights = new int [11];
  61. arm = new Arm (13, 1, 0);
  62. heldBlock = null;
  63.  
  64.  
  65.  
  66. // delay 100 milliseconds for next "frame"
  67. delayAnimation(1000);
  68.  
  69.  
  70. //drawing bars
  71. for(int i = 0; i < barHeights.length; i++) {
  72. bars[i] = new Bar(barHeights[i], i+3);
  73. columnHeights[i+3] = barHeights[i];
  74.  
  75.  
  76. }
  77.  
  78. //drawing blocks
  79. int blockThreePos = 3;
  80.  
  81. for (int i = 0; i < blockHeights.length; i++) {
  82.  
  83. if (blockHeights[i] == 1) {
  84. blocks[i] = new Block(blockHeights[i], 1, columnHeights[1]);
  85. columnHeights[1] += blockHeights[i];
  86. }
  87. if (blockHeights[i] == 2) {
  88. blocks[i] = new Block(blockHeights[i], 2, columnHeights[2]);
  89. columnHeights[2] += blockHeights[i];
  90. }
  91. if (blockHeights[i] == 3) {
  92. blocks[i] = new Block(blockHeights[i], blockThreePos, columnHeights[blockThreePos]);
  93. columnHeights[blockThreePos] += blockHeights[i];
  94. blockThreePos++;
  95.  
  96. }
  97.  
  98.  
  99. }
  100.  
  101.  
  102. drawAll();
  103.  
  104. }
  105.  
  106. //this is the draw all method
  107. private void drawAll () {
  108. terminalFrame.clearScreen();
  109. for (int i = 0; i < bars.length; i++) {
  110. bars[i].draw(terminalFrame);
  111. }
  112. for (int i = 0; i < blocks.length; i++) {
  113. blocks[i].draw(terminalFrame);
  114. }
  115. arm.draw(terminalFrame);
  116. delayAnimation(1000);
  117. }
  118.  
  119.  
  120. @Override
  121. public void pick()
  122. {
  123. for (int i = 0; i < blocks.length; i++) {
  124. if (blocks[i].getTop() == arm.getY() && blocks[i].getX() == arm.getX()) {
  125. heldBlock = blocks[i];
  126. }
  127. }
  128. }
  129.  
  130. @Override
  131. public void drop()
  132. {
  133. heldBlock = null;
  134.  
  135. }
  136.  
  137. @Override
  138. public void up()
  139. {
  140. arm.up();
  141. if (heldBlock != null) {
  142. heldBlock.up();
  143. }
  144. drawAll();
  145.  
  146.  
  147. }
  148.  
  149. @Override
  150. public void down()
  151. {
  152. arm.down();
  153. if (heldBlock != null) {
  154. heldBlock.down();
  155. }
  156.  
  157. drawAll();
  158.  
  159. }
  160.  
  161. @Override
  162. public void contract()
  163. {
  164.  
  165. arm.contract();
  166. if (heldBlock != null) {
  167. heldBlock.left();
  168.  
  169. }
  170.  
  171. drawAll();
  172. }
  173.  
  174. @Override
  175. public void extend()
  176. {
  177.  
  178. arm.extend();
  179. if (heldBlock != null) {
  180. heldBlock.right();
  181. }
  182. drawAll();
  183.  
  184. }
  185.  
  186. @Override
  187. public void lower()
  188. {
  189. arm.lower();
  190. if (heldBlock != null) {
  191. heldBlock.down();
  192. }
  193.  
  194. drawAll();
  195. }
  196.  
  197. @Override
  198. public void raise()
  199. {
  200. arm.raise();
  201. if (heldBlock != null) {
  202. heldBlock.up();
  203. }
  204. drawAll();
  205.  
  206. }
  207.  
  208. // delay in ms
  209. private void delayAnimation(int ms)
  210. {
  211. try
  212. {
  213. Thread.sleep(ms);
  214. } catch (InterruptedException e)
  215. {
  216. e.printStackTrace();
  217. }
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement