Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created October 7, 2015 20:07
Show Gist options
  • Save AnnaBoro/00dbc71bd2af5ab91303 to your computer and use it in GitHub Desktop.
Save AnnaBoro/00dbc71bd2af5ab91303 to your computer and use it in GitHub Desktop.
package lesson3;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
/**
* @version 3.0
*/
public class Tanks 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 = 128;
int tankY = 512;
int bulletX = -100;
int bulletY = -100;
int speed = 10;
int bulletSpeed = 5;
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"},
{"B", " ", "B", "B", " ", " ", " ", " ", "B"}};
/**
* Write your code here.
*/
void runTheGame() throws Exception {
fire();
}
void initTanks() {
int[] randomQuadrant = getRandomQuadrant();
while (battleField[randomQuadrant[1]][randomQuadrant[0]].equals("B")) {
randomQuadrant = getRandomQuadrant();
}
tankX = randomQuadrant[0] * 64;
tankY = randomQuadrant[1] * 64;
}
int[] getRandomQuadrant() {
int[] randomNumbers = getRandomNumbers();
for (int i = 0; i < randomNumbers.length; i++) {
if (randomNumbers[i] > 8) {
randomNumbers[i] = randomNumbers[i] - 1;
}
}
return randomNumbers;
}
boolean processInterception() {
if (isOnTheField()) {
if (removeBrick(false)) {
bulletX = -100;
bulletY = -100;
}
return false;
}
return true;
}
boolean isOnTheField() {
if ((bulletX > 0 && bulletX < 575) && (bulletY > 0 && bulletY < 575)) {
return true;
}
return false;
}
boolean removeBrick(boolean removeType) {
String quadrant;
if (removeType) {
quadrant = getQuadrant(tankX, tankY);
}
else quadrant = getQuadrant(bulletX, bulletY);
int i = Integer.parseInt(quadrant.substring(0, quadrant.indexOf("_")));
int j = Integer.parseInt(quadrant.substring(quadrant.indexOf("_") + 1, quadrant.length()));
if (battleField[i][j] == "B") {
battleField[i][j] = " ";
repaint();
return true;
}
return false;
}
String getQuadrant(int v, int h) {
int x = v / 64;
int y = h / 64;
return y + "_" + x;
}
void fire() throws InterruptedException {
bulletX = tankX + 25;
bulletY = tankY + 25;
while (isOnTheField()) {
runBullet();
}
}
void runBullet() throws InterruptedException {
for (int i = 0; i < 64; ) {
if (tankDirection == 1) {
bulletY -= 1;
}
else if (tankDirection == 2) {
bulletY += 1;
}
else if (tankDirection == 3) {
bulletX -= 1;
}
else if (tankDirection == 4) {
bulletX += 1;
}
processInterception();
repaint();
Thread.sleep(bulletSpeed);
break;
}
}
String getQuadrantXY(int v, int h) {
return (v - 1) * 64 + "_" + (h - 1) * 64;
}
void move(int direction) throws Exception {
repaint();
Thread.sleep(speed);
turn(direction);
moveTank();
}
void moveTank() throws InterruptedException {
for (int i = 0; i < 64; i++) {
if (tankDirection == 1) {
if (tankY !=0) {
tankY -= 1;
}
else System.out.println("Wrong direction");
}
else if (tankDirection == 2) {
if (tankY != 512) {
tankY += 1;
}
else System.out.println("Wrong direction");
}
else if (tankDirection == 3) {
if (tankX != 0) {
tankX -= 1;
}
else System.out.println("Wrong direction");
}
else if (tankDirection == 4) {
if (tankX != 512) {
tankX += 1;
}
else System.out.println("Wrong direction");
}
repaint();
Thread.sleep(speed);
}
removeBrick(true);
System.out.println(tankY + "_" + tankX);
}
void turn(int direction) {
tankDirection = direction;
}
void moveRandom() throws InterruptedException {
while (true) {
int direction = getRandomDirection();
repaint();
Thread.sleep(speed);
if (direction == 1) {
if (tankY !=0) {
tankY -= 64;
repaint();
System.out.println("Up: " + tankX + "_" + tankY);
Thread.sleep(speed * 2);
}
else System.out.println("Wrong direction");
}
else if (direction == 2) {
if (tankY != 512) {
tankY += 64;
repaint();
System.out.println("Down: " + tankX + "_" + tankY);
Thread.sleep(speed * 2);
}
else System.out.println("Wrong direction");
}
else if (direction == 3) {
if (tankX != 0) {
tankX -= 64;
repaint();
System.out.println("Left: " + tankX + "_" + tankY);
Thread.sleep(speed * 2);
}
else System.out.println("Wrong direction");
}
else if (direction == 4) {
if (tankX != 512) {
tankX += 64;
repaint();
System.out.println("Right: " + tankX + "_" + tankY);
Thread.sleep(speed * 2);
}
else System.out.println("Wrong direction");
}
}
}
int getRandomDirection(){
int direction = 0;
int[] randomNumbers = getRandomNumbers();
int randNumInt1 = randomNumbers[0];
int randNumInt2 = randomNumbers[1];
if (randNumInt1 > randNumInt2) {
if (randNumInt1 % 2 == 0) {
direction = 1;
}
else {
direction = 2;
}
}
else {
if (randNumInt2 % 2 == 0) {
direction = 3;
}
else {
direction = 4;
}
}
return direction;
}
int[] getRandomNumbers() {
String randNum = String.valueOf(System.currentTimeMillis());
String randNum1 = randNum.substring(randNum.length()-1);
String randNum2 = randNum.substring(randNum.length()-2, randNum.length()-1);
int randNumInt1 = Integer.parseInt(randNum1);
int randNumInt2 = Integer.parseInt(randNum2);
int[] randomNumbers = {randNumInt1, randNumInt2};
return randomNumbers;
}
void moveToQuadrant(int v, int h) throws Exception {
repaint();
Thread.sleep(speed);
String quadrant = getQuadrant(v, h);
int lineIndex = quadrant.indexOf("_");
int tankXNew = 64 * Integer.parseInt(quadrant.substring(0, lineIndex));
int tankYNew = 64 * Integer.parseInt(quadrant.substring(lineIndex+1));
if ((tankXNew - tankX) > 0) {
int steps = (tankXNew - tankX) / 64;
for (int step = 0; step < steps; step++) {
move(4);
}
}
else if ((tankXNew - tankX) < 0) {
int steps = Math.abs((tankXNew - tankX) / 64);
for (int step = 0; step < steps; step ++) {
move(3);
}
}
if ((tankYNew - tankY) > 0) {
int steps = (tankYNew - tankY) / 64;
for (int step = 0; step < steps; step++) {
move(2);
}
}
else if ((tankYNew - tankY) < 0) {
int steps = Math.abs((tankY - tankYNew) / 64);
for (int step = 0; step < steps; step ++) {
move(1);
}
}
}
void clean() throws Exception {
turn(3);
while (isEmptyX()) {
fire();
}
moveToQuadrant(tankY, 0);
turn(1);
while (isEmptyY()) {
fire();
}
moveToQuadrant(0, 0);
turn(4);
while (isEmptyX()) {
fire();
}
for (int i = 0; i < battleField.length; i++) {
moveToQuadrant(0, i * 64);
turn(2);
while (isEmptyY()) {
fire();
}
}
}
boolean isEmptyY() {
int index = tankX / 64;
int firstPoint = 0;
int endPoint = tankY / 64;
if (tankDirection == 2) {
firstPoint = tankY / 64;
endPoint = battleField[index].length;
}
for (int i = firstPoint; i < endPoint; i++) {
if (battleField[i][index] == "B") {
return true;
}
}
return false;
}
boolean isEmptyX() {
int index = tankY / 64;
int firstPoint = 0;
int endPoint = tankX / 64;
if (tankDirection == 4) {
firstPoint = tankX / 64;
endPoint = battleField[index].length;
}
for (int i = firstPoint; i < endPoint; i++) {
if (battleField[index][i] == "B") {
return true;
}
}
return false;
}
// Magic bellow. 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 {
Tanks bf = new Tanks();
bf.initTanks();
bf.clean();
}
public Tanks() throws Exception {
JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
frame.setLocation(750, 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment