Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2013 midgardabc.com
- */
- package com.all4rest;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.util.Arrays;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- /**
- * @version 3.0
- */
- public class BattleFieldTemplate2 extends JPanel {
- final boolean COLORDED_MODE = false;
- final int BF_WIDTH = 576;
- final int BF_HEIGHT = 576;
- // 1 - top, 2 - bottom, 3 - left, 4 - right
- int tankDirection = 1;
- int tankX = 64;
- int tankY = 384;
- int bulletX = -100;
- int bulletY = -100;
- int speed = 5;
- int bulletSpeed = 1;
- int scan = 0;
- String[][] battleField = {
- {" ", " ", " ", " ", " ", " ", "B", " ", "B"},
- {" ", "B", " ", " ", " ", " ", " ", " ", "B"},
- {"B", " ", "B", "B", "B", " ", " ", " ", "B"},
- {" ", "B", "B", " ", " ", " ", " ", " ", "B"},
- {" ", "B", " ", " ", "B", "B", "B", "B", "B"},
- {"B", "B", " ", "B", "B", "B", " ", " ", "B"},
- {" ", "B", " ", " ", " ", " ", " ", " ", "B"},
- {" ", "B", " ", "B", " ", "B", " ", " ", "B"},
- {" ", "B", " ", " ", " ", "B", " ", " ", "B"}
- };
- /**
- * Write your code here.
- */
- void runTheGame() throws Exception {
- if (tankX != 0 && tankX != 512 && tankY != 0 && tankY !=512){
- movingToEdge();
- for (int i = 1; i <= 4; i++){
- int scan = scanning(i);
- for (int k = 1; k <= scan; k++){
- tankDirection = i;
- fire();
- Thread.sleep(200);
- }
- }
- }
- while(true){
- if (tankX == 0 && tankY == 0){
- for (int i = 1; i < 10; i++){
- moveToQuadrant(1, i, 0);
- int scan = scanning(2);
- tankDirection = 2;
- for (int k = 1; k <= scan; k++){
- fire();
- Thread.sleep(50);
- }
- }
- break;
- }
- if (tankX == 512 && tankY == 0){
- for (int i = 8; i >= 1; i--){
- moveToQuadrant(1, i, 0);
- int scan = scanning(2);
- tankDirection = 2;
- for (int k = 1; k <= scan; k++){
- fire();
- Thread.sleep(50);
- }
- }
- break;
- }
- if (tankX == 512 && tankY == 512){
- for (int i = 9; i >= 1; i--){
- moveToQuadrant(9, i, 0);
- int scan = scanning(1);
- tankDirection = 1;
- for (int k = 1; k <= scan; k++){
- fire();
- Thread.sleep(50);
- }
- }
- break;
- }
- if (tankX == 0 && tankY == 512){
- for (int i = 1; i <= 9; i++){
- moveToQuadrant(9, i, 0);
- int scan = scanning(1);
- tankDirection = 1;
- for (int k = 1; k <= scan; k++){
- fire();
- Thread.sleep(50);
- }
- }
- break;
- }
- }
- }
- // moveToQuadrant(1, 2);
- // Thread.sleep(200);
- // moveToQuadrant(3, 3);
- // Thread.sleep(200);
- // Thread.sleep(200);
- // moveToQuadrant(9, 8);
- // moveToQuadrant(4, 5);
- // moveToQuadrant(1, 9);
- int scanning (int moveDirection) throws Exception {
- int brickCount = 0;
- if (moveDirection == 3){
- for (int i = (tankX/64 - 1); i >= 0; i--){
- if (battleField[tankY/64][i] != " "){
- brickCount++;
- }
- }
- }
- if (moveDirection == 4){
- for (int i = (tankX/64 + 1); i < 9; i++){
- if (battleField[tankY/64][i] != " "){
- System.out.println(i);
- brickCount++;
- }
- }
- }
- if (moveDirection == 1){
- for (int i = (tankY/64 - 1); i >= 0; i--){
- if (battleField[i][tankX/64] != " "){
- brickCount++;
- }
- }
- }
- if (moveDirection == 2){
- for (int i = (tankY/64 + 1); i < 9; i++){
- if (battleField[i][tankX/64] != " "){
- brickCount++;
- }
- }
- }
- return brickCount;
- }
- void movingToEdge() throws Exception {
- if (tankX >= 288 && tankY <= 288){
- moveToQuadrant(1, 9, 1);
- } else if (tankX < 288 && tankY > 288){
- moveToQuadrant(9, 1, 1);
- } else if (tankX <= 288 && tankY <= 288){
- moveToQuadrant(1, 1, 1);
- } else if (tankX > 288 && tankY > 288){
- moveToQuadrant(9, 9, 1);
- }
- }
- boolean processInterception() {
- getQuadrant(bulletX, bulletY);
- String coordinates = (getQuadrant(bulletX, bulletY));
- int y = Integer.parseInt(coordinates.split("_")[0]);
- int x = Integer.parseInt(coordinates.split("_")[1]);
- // System.out.println(getQuadrant(bulletX, bulletY));
- if (y >= 0 && y < 9 && x >= 0 && x < 9){
- if (!battleField[y][x].trim().isEmpty()){
- // System.out.println(battleField[y][x]);
- battleField[y][x] = " ";
- return true;
- }
- }
- return false;
- }
- String getQuadrant(int x, int y) {
- String quadrant = Integer.toString(y/64) + "_" + Integer.toString(x/64);
- return quadrant;
- }
- void fire() throws Exception {
- bulletY = tankY + 25;
- bulletX = tankX + 25;
- int step = 1;
- while((bulletY > -14 && bulletY < 576) && (bulletX > -14 && bulletX < 576)){
- if (processInterception() == true){
- bulletY = -100;
- bulletX = -100;
- }
- if (tankDirection == 1){
- bulletY -= step;
- } else if (tankDirection == 2){
- bulletY += step;
- } else if (tankDirection == 3){
- bulletX -= step;
- } else if (tankDirection == 4){
- bulletX += step;
- }
- repaint();
- Thread.sleep(bulletSpeed);
- }
- }
- String getQuadrantXY(int v, int h) {
- return (v - 1) * 64 + "_" + (h - 1) * 64;
- }
- void move(int direction) throws Exception {
- int step = 1;
- int covered = 0;
- // fire();
- // check limits x: 0, 513; y: 0, 513
- if ((direction == 1 && tankY == 0) || (direction == 2 && tankY >= 512)
- || (direction == 3 && tankX == 0) || (direction == 4 && tankX >= 512)) {
- System.out.println("[illegal move] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- return;
- }
- turn(direction);
- while (covered < 64) {
- if (direction == 1) {
- tankY -= step;
- // System.out.println("[move up] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- } else if (direction == 2) {
- tankY += step;
- // System.out.println("[move down] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- } else if (direction == 3) {
- tankX -= step;
- // System.out.println("[move left] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- } else {
- tankX += step;
- // System.out.println("[move right] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- }
- covered += step;
- repaint();
- Thread.sleep(speed);
- }
- }
- void turn(int direction) {
- if (tankDirection != direction) {
- tankDirection = direction;
- }
- }
- void moveRandom() throws Exception {
- Random r = new Random();
- int i;
- while (true) {
- i = r.nextInt(5);
- if (i > 0) {
- move(i);
- }
- }
- }
- void moveToQuadrant(int v, int h, int shoot) throws Exception {
- String coordinates = getQuadrantXY(v, h);
- int separator = coordinates.indexOf("_");
- int y = Integer.parseInt(coordinates.substring(0, separator));
- int x = Integer.parseInt(coordinates.substring(separator + 1));
- if (tankX < x) {
- while (tankX != x) {
- move(4);
- if (shoot == 1){
- fire();
- }
- }
- } else {
- while (tankX != x) {
- move(3);
- if (shoot == 1){
- fire();
- }
- }
- }
- if (tankY < y) {
- while (tankY != y) {
- move(2);
- if (shoot == 1){
- fire();
- }
- }
- } else {
- while (tankY != y) {
- move(1);
- if (shoot == 1){
- fire();
- }
- }
- }
- }
- // Magic below. Do not worry about this now, you will understand everything in this course.
- // Please concentrate on your tasks only.
- public static void main(String[] args) throws Exception {
- BattleFieldTemplate2 bf = new BattleFieldTemplate2();
- bf.runTheGame();
- }
- public BattleFieldTemplate2() throws Exception {
- JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
- frame.setLocation(550, 150);
- frame.setMinimumSize(new Dimension(BF_WIDTH, BF_HEIGHT + 22));
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.getContentPane().add(this);
- frame.pack();
- frame.setVisible(true);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- int i = 0;
- Color cc;
- for (int v = 0; v < 9; v++) {
- for (int h = 0; h < 9; h++) {
- if (COLORDED_MODE) {
- if (i % 2 == 0) {
- cc = new Color(252, 241, 177);
- } else {
- cc = new Color(233, 243, 255);
- }
- } else {
- cc = new Color(180, 180, 180);
- }
- i++;
- g.setColor(cc);
- g.fillRect(h * 64, v * 64, 64, 64);
- }
- }
- for (int j = 0; j < battleField.length; j++) {
- for (int k = 0; k < battleField.length; k++) {
- if (battleField[j][k].equals("B")) {
- String coordinates = getQuadrantXY(j + 1, k + 1);
- int separator = coordinates.indexOf("_");
- int y = Integer.parseInt(coordinates.substring(0, separator));
- int x = Integer.parseInt(coordinates.substring(separator + 1));
- g.setColor(new Color(0, 0, 255));
- g.fillRect(x, y, 64, 64);
- }
- }
- }
- g.setColor(new Color(255, 0, 0));
- g.fillRect(tankX, tankY, 64, 64);
- g.setColor(new Color(0, 255, 0));
- if (tankDirection == 1) {
- g.fillRect(tankX + 20, tankY, 24, 34);
- } else if (tankDirection == 2) {
- g.fillRect(tankX + 20, tankY + 30, 24, 34);
- } else if (tankDirection == 3) {
- g.fillRect(tankX, tankY + 20, 34, 24);
- } else {
- g.fillRect(tankX + 30, tankY + 20, 34, 24);
- }
- g.setColor(new Color(255, 255, 0));
- g.fillRect(bulletX, bulletY, 14, 14);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement