Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2013 midgardabc.com
  3. */
  4. package com.all4rest;
  5.  
  6. import java.awt.Color;
  7. import java.awt.Dimension;
  8. import java.awt.Graphics;
  9. import java.util.Arrays;
  10. import java.util.Random;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.WindowConstants;
  14.  
  15. /**
  16. * @version 3.0
  17. */
  18. public class BattleFieldTemplate2 extends JPanel {
  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 = 64;
  29. int tankY = 384;
  30.  
  31. int bulletX = -100;
  32. int bulletY = -100;
  33.  
  34. int speed = 5;
  35. int bulletSpeed = 1;
  36.  
  37. int scan = 0;
  38.  
  39. String[][] battleField = {
  40. {" ", " ", " ", " ", " ", " ", "B", " ", "B"},
  41. {" ", "B", " ", " ", " ", " ", " ", " ", "B"},
  42. {"B", " ", "B", "B", "B", " ", " ", " ", "B"},
  43. {" ", "B", "B", " ", " ", " ", " ", " ", "B"},
  44. {" ", "B", " ", " ", "B", "B", "B", "B", "B"},
  45. {"B", "B", " ", "B", "B", "B", " ", " ", "B"},
  46. {" ", "B", " ", " ", " ", " ", " ", " ", "B"},
  47. {" ", "B", " ", "B", " ", "B", " ", " ", "B"},
  48. {" ", "B", " ", " ", " ", "B", " ", " ", "B"}
  49. };
  50.  
  51. /**
  52. * Write your code here.
  53. */
  54. void runTheGame() throws Exception {
  55.  
  56. if (tankX != 0 && tankX != 512 && tankY != 0 && tankY !=512){
  57.  
  58. movingToEdge();
  59. for (int i = 1; i <= 4; i++){
  60. int scan = scanning(i);
  61. for (int k = 1; k <= scan; k++){
  62. tankDirection = i;
  63. fire();
  64. Thread.sleep(200);
  65. }
  66. }
  67.  
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. while(true){
  75.  
  76. if (tankX == 0 && tankY == 0){
  77.  
  78. for (int i = 1; i < 10; i++){
  79. moveToQuadrant(1, i, 0);
  80. int scan = scanning(2);
  81. tankDirection = 2;
  82. for (int k = 1; k <= scan; k++){
  83. fire();
  84. Thread.sleep(50);
  85.  
  86. }
  87. }
  88. break;
  89. }
  90.  
  91.  
  92.  
  93. if (tankX == 512 && tankY == 0){
  94.  
  95. for (int i = 8; i >= 1; i--){
  96. moveToQuadrant(1, i, 0);
  97. int scan = scanning(2);
  98. tankDirection = 2;
  99. for (int k = 1; k <= scan; k++){
  100. fire();
  101. Thread.sleep(50);
  102.  
  103. }
  104. }
  105. break;
  106. }
  107.  
  108.  
  109. if (tankX == 512 && tankY == 512){
  110.  
  111.  
  112. for (int i = 9; i >= 1; i--){
  113. moveToQuadrant(9, i, 0);
  114. int scan = scanning(1);
  115. tankDirection = 1;
  116. for (int k = 1; k <= scan; k++){
  117. fire();
  118. Thread.sleep(50);
  119.  
  120. }
  121. }
  122. break;
  123. }
  124.  
  125. if (tankX == 0 && tankY == 512){
  126.  
  127.  
  128. for (int i = 1; i <= 9; i++){
  129. moveToQuadrant(9, i, 0);
  130. int scan = scanning(1);
  131. tankDirection = 1;
  132. for (int k = 1; k <= scan; k++){
  133. fire();
  134. Thread.sleep(50);
  135.  
  136. }
  137.  
  138. }
  139. break;
  140. }
  141.  
  142.  
  143. }
  144.  
  145. }
  146.  
  147. // moveToQuadrant(1, 2);
  148. // Thread.sleep(200);
  149. // moveToQuadrant(3, 3);
  150. // Thread.sleep(200);
  151. // Thread.sleep(200);
  152. // moveToQuadrant(9, 8);
  153. // moveToQuadrant(4, 5);
  154. // moveToQuadrant(1, 9);
  155.  
  156.  
  157.  
  158.  
  159. int scanning (int moveDirection) throws Exception {
  160.  
  161. int brickCount = 0;
  162.  
  163. if (moveDirection == 3){
  164. for (int i = (tankX/64 - 1); i >= 0; i--){
  165. if (battleField[tankY/64][i] != " "){
  166. brickCount++;
  167. }
  168. }
  169.  
  170. }
  171.  
  172. if (moveDirection == 4){
  173. for (int i = (tankX/64 + 1); i < 9; i++){
  174. if (battleField[tankY/64][i] != " "){
  175. System.out.println(i);
  176. brickCount++;
  177. }
  178.  
  179. }
  180. }
  181.  
  182.  
  183. if (moveDirection == 1){
  184. for (int i = (tankY/64 - 1); i >= 0; i--){
  185. if (battleField[i][tankX/64] != " "){
  186. brickCount++;
  187. }
  188. }
  189.  
  190. }
  191.  
  192. if (moveDirection == 2){
  193. for (int i = (tankY/64 + 1); i < 9; i++){
  194. if (battleField[i][tankX/64] != " "){
  195. brickCount++;
  196. }
  197. }
  198.  
  199. }
  200.  
  201.  
  202. return brickCount;
  203. }
  204.  
  205.  
  206.  
  207. void movingToEdge() throws Exception {
  208.  
  209. if (tankX >= 288 && tankY <= 288){
  210. moveToQuadrant(1, 9, 1);
  211.  
  212. } else if (tankX < 288 && tankY > 288){
  213. moveToQuadrant(9, 1, 1);
  214.  
  215. } else if (tankX <= 288 && tankY <= 288){
  216. moveToQuadrant(1, 1, 1);
  217.  
  218. } else if (tankX > 288 && tankY > 288){
  219. moveToQuadrant(9, 9, 1);
  220. }
  221.  
  222.  
  223. }
  224.  
  225. boolean processInterception() {
  226. getQuadrant(bulletX, bulletY);
  227. String coordinates = (getQuadrant(bulletX, bulletY));
  228. int y = Integer.parseInt(coordinates.split("_")[0]);
  229. int x = Integer.parseInt(coordinates.split("_")[1]);
  230. // System.out.println(getQuadrant(bulletX, bulletY));
  231. if (y >= 0 && y < 9 && x >= 0 && x < 9){
  232. if (!battleField[y][x].trim().isEmpty()){
  233. // System.out.println(battleField[y][x]);
  234. battleField[y][x] = " ";
  235. return true;
  236. }
  237.  
  238. }
  239.  
  240. return false;
  241. }
  242.  
  243. String getQuadrant(int x, int y) {
  244. String quadrant = Integer.toString(y/64) + "_" + Integer.toString(x/64);
  245. return quadrant;
  246. }
  247.  
  248. void fire() throws Exception {
  249.  
  250. bulletY = tankY + 25;
  251. bulletX = tankX + 25;
  252. int step = 1;
  253.  
  254. while((bulletY > -14 && bulletY < 576) && (bulletX > -14 && bulletX < 576)){
  255.  
  256. if (processInterception() == true){
  257. bulletY = -100;
  258. bulletX = -100;
  259. }
  260. if (tankDirection == 1){
  261. bulletY -= step;
  262. } else if (tankDirection == 2){
  263. bulletY += step;
  264. } else if (tankDirection == 3){
  265. bulletX -= step;
  266. } else if (tankDirection == 4){
  267. bulletX += step;
  268. }
  269. repaint();
  270. Thread.sleep(bulletSpeed);
  271. }
  272.  
  273. }
  274.  
  275.  
  276. String getQuadrantXY(int v, int h) {
  277. return (v - 1) * 64 + "_" + (h - 1) * 64;
  278. }
  279.  
  280.  
  281. void move(int direction) throws Exception {
  282. int step = 1;
  283. int covered = 0;
  284. // fire();
  285.  
  286. // check limits x: 0, 513; y: 0, 513
  287. if ((direction == 1 && tankY == 0) || (direction == 2 && tankY >= 512)
  288. || (direction == 3 && tankX == 0) || (direction == 4 && tankX >= 512)) {
  289. System.out.println("[illegal move] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  290. return;
  291. }
  292.  
  293. turn(direction);
  294.  
  295. while (covered < 64) {
  296. if (direction == 1) {
  297. tankY -= step;
  298. // System.out.println("[move up] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  299. } else if (direction == 2) {
  300. tankY += step;
  301. // System.out.println("[move down] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  302. } else if (direction == 3) {
  303. tankX -= step;
  304. // System.out.println("[move left] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  305. } else {
  306. tankX += step;
  307. // System.out.println("[move right] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
  308. }
  309. covered += step;
  310.  
  311. repaint();
  312. Thread.sleep(speed);
  313. }
  314. }
  315.  
  316. void turn(int direction) {
  317. if (tankDirection != direction) {
  318. tankDirection = direction;
  319. }
  320. }
  321.  
  322. void moveRandom() throws Exception {
  323. Random r = new Random();
  324. int i;
  325. while (true) {
  326. i = r.nextInt(5);
  327. if (i > 0) {
  328. move(i);
  329. }
  330. }
  331. }
  332.  
  333. void moveToQuadrant(int v, int h, int shoot) throws Exception {
  334. String coordinates = getQuadrantXY(v, h);
  335. int separator = coordinates.indexOf("_");
  336. int y = Integer.parseInt(coordinates.substring(0, separator));
  337. int x = Integer.parseInt(coordinates.substring(separator + 1));
  338.  
  339. if (tankX < x) {
  340. while (tankX != x) {
  341. move(4);
  342. if (shoot == 1){
  343. fire();
  344. }
  345. }
  346. } else {
  347. while (tankX != x) {
  348. move(3);
  349. if (shoot == 1){
  350. fire();
  351. }
  352. }
  353. }
  354.  
  355. if (tankY < y) {
  356. while (tankY != y) {
  357. move(2);
  358. if (shoot == 1){
  359. fire();
  360. }
  361. }
  362. } else {
  363. while (tankY != y) {
  364. move(1);
  365. if (shoot == 1){
  366. fire();
  367. }
  368. }
  369. }
  370. }
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379. // Magic below. Do not worry about this now, you will understand everything in this course.
  380. // Please concentrate on your tasks only.
  381.  
  382.  
  383. public static void main(String[] args) throws Exception {
  384. BattleFieldTemplate2 bf = new BattleFieldTemplate2();
  385. bf.runTheGame();
  386. }
  387.  
  388. public BattleFieldTemplate2() throws Exception {
  389. JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
  390. frame.setLocation(550, 150);
  391. frame.setMinimumSize(new Dimension(BF_WIDTH, BF_HEIGHT + 22));
  392. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  393. frame.getContentPane().add(this);
  394. frame.pack();
  395. frame.setVisible(true);
  396. }
  397.  
  398. @Override
  399. protected void paintComponent(Graphics g) {
  400. super.paintComponent(g);
  401.  
  402. int i = 0;
  403. Color cc;
  404. for (int v = 0; v < 9; v++) {
  405. for (int h = 0; h < 9; h++) {
  406. if (COLORDED_MODE) {
  407. if (i % 2 == 0) {
  408. cc = new Color(252, 241, 177);
  409. } else {
  410. cc = new Color(233, 243, 255);
  411. }
  412. } else {
  413. cc = new Color(180, 180, 180);
  414. }
  415. i++;
  416. g.setColor(cc);
  417. g.fillRect(h * 64, v * 64, 64, 64);
  418. }
  419. }
  420.  
  421. for (int j = 0; j < battleField.length; j++) {
  422. for (int k = 0; k < battleField.length; k++) {
  423. if (battleField[j][k].equals("B")) {
  424. String coordinates = getQuadrantXY(j + 1, k + 1);
  425. int separator = coordinates.indexOf("_");
  426. int y = Integer.parseInt(coordinates.substring(0, separator));
  427. int x = Integer.parseInt(coordinates.substring(separator + 1));
  428. g.setColor(new Color(0, 0, 255));
  429. g.fillRect(x, y, 64, 64);
  430. }
  431. }
  432. }
  433.  
  434. g.setColor(new Color(255, 0, 0));
  435. g.fillRect(tankX, tankY, 64, 64);
  436.  
  437. g.setColor(new Color(0, 255, 0));
  438. if (tankDirection == 1) {
  439. g.fillRect(tankX + 20, tankY, 24, 34);
  440. } else if (tankDirection == 2) {
  441. g.fillRect(tankX + 20, tankY + 30, 24, 34);
  442. } else if (tankDirection == 3) {
  443. g.fillRect(tankX, tankY + 20, 34, 24);
  444. } else {
  445. g.fillRect(tankX + 30, tankY + 20, 34, 24);
  446. }
  447.  
  448. g.setColor(new Color(255, 255, 0));
  449. g.fillRect(bulletX, bulletY, 14, 14);
  450. }
  451.  
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement