Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2013 midgardabc.com
  3. */
  4. package tanks;
  5.  
  6. import java.awt.Color;
  7. import java.awt.Dimension;
  8. import java.awt.Graphics;
  9. import java.util.Random;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12. import javax.swing.WindowConstants;
  13.  
  14. /**
  15. * @version 3.0
  16. */
  17. public class BattleFieldTemplate2 extends JPanel {
  18.  
  19.  
  20. final boolean COLORDED_MODE = false;
  21.  
  22. final int BF_WIDTH = 576;
  23. final int BF_HEIGHT = 576;
  24.  
  25. // 1 - top, 2 - bottom, 3 - left, 4 - right
  26. int tankDirection = 1;
  27.  
  28. int tankX = 128;
  29. int tankY = 512;
  30.  
  31. int bulletX = -100;
  32. int bulletY = -100;
  33.  
  34. int speed = 3;
  35. int bulletSpeed = 3;
  36.  
  37. String[][] battleField = {
  38. {" ", "B", "B", " ", " ", " ", "B", "B", "B"},
  39. {" ", " ", "B", " ", " ", " ", " ", " ", "B"},
  40. {"B", "B", "B", "B", "B", " ", "B", "B", "B"},
  41. {"B", "B", " ", " ", " ", " ", "B", "B", " "},
  42. {" ", " ", " ", " ", " ", " ", " ", " ", "B"},
  43. {"B", "B", "B", "B", "B", "B", " ", " ", "B"},
  44. {"B", " ", " ", " ", " ", " ", " ", " ", "B"},
  45. {"B", " ", " ", "B", "B", "B", " ", " ", "B"},
  46. {" ", " ", " ", "B", "B", "B", " ", " ", " "}
  47. };
  48.  
  49. /**
  50. * Write your code here.
  51. */
  52. void runTheGame() throws Exception {
  53.  
  54. clean();
  55. }
  56.  
  57. boolean processInterception() {
  58. String bulletCoord = getQuadrant(bulletX, bulletY);
  59. int x = Integer.parseInt(bulletCoord.substring(0, bulletCoord.indexOf("_")));
  60. int y = Integer.parseInt(bulletCoord.substring(bulletCoord.indexOf("_") + 1));
  61. if( x >= 0 && x <= 8 && y >= 0 && y <= 8){
  62. if(battleField[x][y].equals("B")){
  63. battleField[x][y]=" ";
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69.  
  70. String getQuadrant(int x, int y) {
  71. x = x / 64;
  72. y = y / 64;
  73. return y + "_" + x;
  74. }
  75.  
  76. void fire() throws Exception {
  77. bulletX = tankX + 25;
  78. bulletY = tankY + 25;
  79. while (bulletX >= -32 && bulletY >= -32 && bulletX < 601 && bulletY < 601) {
  80. if (tankDirection == 1) {
  81. bulletY --;
  82. if(processInterception() == true){
  83. bulletY = -100;
  84. bulletX = -100;
  85. }
  86. }
  87. if (tankDirection == 2) {
  88. bulletY ++;
  89. if(processInterception() == true){
  90. bulletY = -100;
  91. bulletX = -100;
  92. }
  93. }
  94. if (tankDirection == 4) {
  95. bulletX ++;
  96. if(processInterception() == true){
  97. bulletY = -100;
  98. bulletX = -100;
  99. }
  100. }
  101. if (tankDirection == 3) {
  102. bulletX --;
  103. if(processInterception() == true){
  104. bulletY = -100;
  105. bulletX = -100;
  106. }
  107. }
  108. processInterception();
  109. repaint();
  110. Thread.sleep(bulletSpeed);
  111. }
  112.  
  113. }
  114. void clearQuad(int tankDirection) throws Exception{
  115. String koord = getQuadrant(tankX, tankY);
  116. int separator = koord.indexOf("_");
  117. int y = Integer.parseInt(koord.substring(0, separator));
  118. int x = Integer.parseInt(koord.substring(separator + 1));
  119. if(tankDirection == 1 && battleField[y-1][x].equals("B")){
  120. fire();
  121. }
  122. if(tankDirection == 2 && battleField[y+1][x].equals("B")){
  123. fire();
  124. }
  125. if(tankDirection == 3 && battleField[y][x-1].equals("B")){
  126. fire();
  127. }
  128. if(tankDirection == 4 && battleField[y][x+1].equals("B")){
  129. fire();
  130. }
  131. }
  132.  
  133. void clean() throws Exception{
  134. int a = -1;
  135. for(int i = 1; i <= 9; i++){
  136. moveToQuadrant(9, i);
  137. a++;
  138. turn(1);
  139. for(int j = 1; j < battleField.length; j++)
  140. if(battleField[a][j] == "B"){
  141. fire();
  142. }
  143. }
  144.  
  145. }
  146.  
  147.  
  148. String getQuadrantXY(int v, int h) {
  149. return (v - 1) * 64 + "_" + (h - 1) * 64;
  150. }
  151.  
  152.  
  153.  
  154. void move(int direction) throws Exception {
  155. int step = 1;
  156. int covered = 0;
  157.  
  158. // check limits x: 0, 513; y: 0, 513
  159. if ((direction == 1 && tankY == 0) || (direction == 2 && tankY >= 512)
  160. || (direction == 3 && tankX == 0) || (direction == 4 && tankX >= 512)) {
  161. System.out.println("[illegal move] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  162.  
  163. return;
  164. }
  165.  
  166. turn(direction);
  167. clearQuad(direction);
  168. while (covered < 64) {
  169. if (direction == 1) {
  170. tankY -= step;
  171.  
  172. System.out.println("[move up] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  173. } else if (direction == 2) {
  174. tankY += step;
  175. System.out.println("[move down] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  176. } else if (direction == 3) {
  177. tankX -= step;
  178. System.out.println("[move left] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  179. } else {
  180. tankX += step;
  181. System.out.println("[move right] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  182. }
  183. covered += step;
  184.  
  185. repaint();
  186. Thread.sleep(speed);
  187. }
  188. }
  189.  
  190. void turn(int direction) {
  191. if (tankDirection != direction) {
  192. tankDirection = direction;
  193. }
  194. }
  195.  
  196. void moveRandom() throws Exception {
  197. Random r = new Random();
  198. int i;
  199. while (true) {
  200. i = r.nextInt(5);
  201. if (i > 0) {
  202. move(i);
  203. }
  204. }
  205. }
  206.  
  207. void moveToQuadrant(int v, int h) throws Exception {
  208. String coordinates = getQuadrantXY(v, h);
  209. int separator = coordinates.indexOf("_");
  210. int y = Integer.parseInt(coordinates.substring(0, separator));
  211. int x = Integer.parseInt(coordinates.substring(separator + 1));
  212.  
  213. if (tankX < x) {
  214. while (tankX != x) {
  215. move(4);
  216. }
  217. } else {
  218. while (tankX != x) {
  219. move(3);
  220. }
  221. }
  222.  
  223. if (tankY < y) {
  224. while (tankY != y) {
  225. move(2);
  226. }
  227. } else {
  228. while (tankY != y) {
  229. move(1);
  230. }
  231. }
  232. }
  233.  
  234. // Magic bellow. Do not worry about this now, you will understand everything in this course.
  235. // Please concentrate on your tasks only.
  236.  
  237.  
  238. public static void main(String[] args) throws Exception {
  239. BattleFieldTemplate2 bf = new BattleFieldTemplate2();
  240. bf.runTheGame();
  241. }
  242.  
  243. public BattleFieldTemplate2() throws Exception {
  244. JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
  245. frame.setLocation(750, 150);
  246. frame.setMinimumSize(new Dimension(BF_WIDTH, BF_HEIGHT + 22));
  247. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  248. frame.getContentPane().add(this);
  249. frame.pack();
  250. frame.setVisible(true);
  251. }
  252.  
  253. @Override
  254. protected void paintComponent(Graphics g) {
  255. super.paintComponent(g);
  256.  
  257. int i = 0;
  258. Color cc;
  259. for (int v = 0; v < 9; v++) {
  260. for (int h = 0; h < 9; h++) {
  261. if (COLORDED_MODE) {
  262. if (i % 2 == 0) {
  263. cc = new Color(252, 241, 177);
  264. } else {
  265. cc = new Color(233, 243, 255);
  266. }
  267. } else {
  268. cc = new Color(180, 180, 180);
  269. }
  270. i++;
  271. g.setColor(cc);
  272. g.fillRect(h * 64, v * 64, 64, 64);
  273. }
  274. }
  275.  
  276. for (int j = 0; j < battleField.length; j++) {
  277. for (int k = 0; k < battleField.length; k++) {
  278. if (battleField[j][k].equals("B")) {
  279. String coordinates = getQuadrantXY(j + 1, k + 1);
  280. int separator = coordinates.indexOf("_");
  281. int y = Integer.parseInt(coordinates.substring(0, separator));
  282. int x = Integer.parseInt(coordinates.substring(separator + 1));
  283. g.setColor(new Color(0, 0, 255));
  284. g.fillRect(x, y, 64, 64);
  285. }
  286. }
  287. }
  288.  
  289. g.setColor(new Color(255, 0, 0));
  290. g.fillRect(tankX, tankY, 64, 64);
  291.  
  292. g.setColor(new Color(0, 255, 0));
  293. if (tankDirection == 1) {
  294. g.fillRect(tankX + 20, tankY, 24, 34);
  295. } else if (tankDirection == 2) {
  296. g.fillRect(tankX + 20, tankY + 30, 24, 34);
  297. } else if (tankDirection == 3) {
  298. g.fillRect(tankX, tankY + 20, 34, 24);
  299. } else {
  300. g.fillRect(tankX + 30, tankY + 20, 34, 24);
  301. }
  302.  
  303. g.setColor(new Color(255, 255, 0));
  304. g.fillRect(bulletX, bulletY, 14, 14);
  305. }
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement