Advertisement
Guest User

ascii

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