Skip to content

Instantly share code, notes, and snippets.

@e-tverdokhleb
Last active November 24, 2015 21:44
Show Gist options
  • Save e-tverdokhleb/41355c459700059840c2 to your computer and use it in GitHub Desktop.
Save e-tverdokhleb/41355c459700059840c2 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2014 kademika.com
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.text.AbstractDocument.LeafElement;
public class BattleFieldTemplate extends JPanel {
boolean COLORDED_MODE = true;
int desk_step = 64;
int tank_move_step = 7;
long tank_speed = 500;
int tankX = 0;
int tankY = 0;
void runTheGame() throws Exception {
tankX = desk_step * 5;
tankY = desk_step * 5;
moveToQuadrant("i", "9");
}
void moveToQuadrant(String v, String h) throws InterruptedException {
String str_v = "abcdefghi";
String currnetTankPos = getQuadrant(tankY, tankX);
String movetoPos = v + "_" + h;
int vCurrent = str_v.indexOf(currnetTankPos.substring(0, currnetTankPos.indexOf("_"))) + 1;
int hCurrent = Integer.parseInt(currnetTankPos.substring(currnetTankPos.indexOf("_") + 1, currnetTankPos.length()));
int vPurp = str_v.indexOf(v) + 1;
int hPurp = Integer.parseInt(h);
// moving UP-DOWN
if (vCurrent != vPurp) {
while (vCurrent != vPurp) {
if (vCurrent < vPurp) {
move(2);
} else {
move(1);
}
currnetTankPos = getQuadrant(tankY, tankX);
vCurrent = str_v.indexOf(currnetTankPos.substring(0,
currnetTankPos.indexOf("_"))) + 1;
vPurp = str_v.indexOf(v) + 1;
System.out.println(vCurrent + " " + vPurp);
}
}
// moving LEFT-RIGHT
System.out.println(hCurrent);
if (hCurrent != hPurp) {
while (hCurrent != hPurp) {
if (hCurrent < hPurp) {
move(4);
} else {
move(3);
}
currnetTankPos = getQuadrant(tankY, tankX);
hCurrent = Integer.parseInt(currnetTankPos.substring(
currnetTankPos.indexOf("_") + 1,
currnetTankPos.length()));
hPurp = Integer.parseInt(h);
System.out.println(hCurrent + " " + hPurp);
}
}
}
void moveRandom() throws InterruptedException {
while (true) {
int i = getRandom();
System.out.println(i);
move(i);
repaint();
Thread.sleep(tank_speed);
}
}
int getRandom() {
String str = String.valueOf(System.currentTimeMillis());
int y = 0;
for (int i = 0; i < str.length() - 1; i++) {
if ((i % 2) == 0) {
y += Integer.parseInt(str.valueOf(str.charAt(i)));
System.out.print(i + " ");
} else {
y -= Integer.parseInt(str.valueOf(str.charAt(i)));
System.out.print(i + " ");
}
}
y = (Math.abs(y) % 4 + 1);
System.out.println("y: " + y);
return y;
}
// 1 - UP, 2 - DOWN, 3 - LEFT, 4 - RIGHT;
void move(int direction) throws InterruptedException {
System.out.println("ready to move...");
if (direction == 1) {
if ((tankY - desk_step) < 0) {
System.out.println("there is no way to move!");
} else {
tankY -= desk_step;
System.out.println("move 1 step UP");
}
repaint();
}
if (direction == 2) {
if ((tankY + desk_step) >= BF_HEIGHT) {
tankY = BF_HEIGHT - desk_step;
System.out.println("there is no way to move!");
} else {
tankY += desk_step;
System.out.println("move 1 step DOWN");
}
repaint();
}
if (direction == 3) {
if ((tankX - desk_step) < 0) {
System.out.println("there is no way to move!");
} else {
tankX -= desk_step;
System.out.println("move 1 step LEFT");
}
repaint();
}
if (direction == 4) {
if ((tankX + desk_step) >= BF_WIDTH) {
tankX = BF_WIDTH - desk_step;
System.out.println("there is no way to move!");
} else {
tankX += desk_step;
System.out.println("move 1 step RIGHT");
}
repaint();
}
Thread.sleep(tank_speed);
}
static String getQuadrant(String v, String h) {
String str_v = "abcdefghi";
return (str_v.indexOf(v) + 1) * 64 + "_" + (Integer.valueOf(h) * 64);
}
String getQuadrant(int x, int y) {
String str_v = "abcdefghi";
return str_v.charAt(x / desk_step) + "_" + ((y / 64) + 1);
}
// Magic bellow. Do not worry about this now, you will understand everything
// in this course.
// Please concentrate on your tasks only.
final int BF_WIDTH = 576;
final int BF_HEIGHT = 576;
public static void main(String[] args) throws Exception {
BattleFieldTemplate bf = new BattleFieldTemplate();
bf.runTheGame();
}
public BattleFieldTemplate() throws Exception {
JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
frame.setLocation(500, 150);
frame.setMinimumSize(new Dimension(BF_WIDTH + 14, BF_HEIGHT + 36));
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(135, 206, 255);
} 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);
}
}
g.setColor(new Color(218, 112, 214));
g.fillRect(tankX, tankY, 64, 64);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment