Skip to content

Instantly share code, notes, and snippets.

@edinak1
Last active October 26, 2015 20:54
Show Gist options
  • Select an option

  • Save edinak1/dd3a8970bcbed988f79d to your computer and use it in GitHub Desktop.

Select an option

Save edinak1/dd3a8970bcbed988f79d to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2014 kademika.com
*/
package tank;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class BattleFieldTemplate extends JPanel {
boolean COLORDED_MODE = true;
int tankX = 0;
int tankY = 0;
long speed = 10;
/**
* Write your code here.
*/
void runTheGame() throws Exception
{
String v,h;
int i=0;
while(true)
{
TimeUnit.NANOSECONDS.sleep(i++);
v=random1();
TimeUnit.NANOSECONDS.sleep(i++);
h=random2();
moveToQuadrant(v,h);
System.out.println(v+"_"+h);
TimeUnit.SECONDS.sleep(3);
}
}
static String getQuadrant(String v,String g)
{
String vertical="abcdefghi";
String gorizont="123456789";
String quadrant=64*gorizont.indexOf(g)+"_"+64*vertical.indexOf(v);
return quadrant;
}
void moveToQuadrant(String v,String h)throws Exception
{
String str=getQuadrant(v,h);
int x=Integer.parseInt(str.substring(0,str.indexOf("_")));
int y=Integer.parseInt(str.substring(str.indexOf("_")+1));
while(tankX!=x || tankY!=y)
{
if(tankX<x)
move(4);
else
if(tankX>x)
move(3);
else
if(tankY<y)
move(2);
else
if(tankY>y)
move(1);
}
}
String random1()throws Exception
{
String vertical="abcdefghi";
String coordinata;
int i=9;
while(i==9)
i=(int)(System.nanoTime()%10);
coordinata=String.valueOf(vertical.charAt(i));
return(coordinata);
}
String random2()throws Exception
{
String gorizont="123456789";
String coordinata;
int i=9;
while(i==9)
i=(int)(System.nanoTime()%10);
coordinata=String.valueOf(gorizont.charAt(i));
return(coordinata);
}
void move(int direction)throws Exception
{
int i=1;
while(i<=64)
{
if(direction==1 && tankY>0)
tankY--;
else
if(direction==2 && tankY<512)
tankY++;
else
if(direction==3 && tankX>0)
tankX--;
else
if(direction==4 && tankX<512)
tankX++;
repaint();
TimeUnit.MILLISECONDS.sleep(speed);
i++;
}
}
// 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, 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);
}
}
g.setColor(new Color(255, 0, 0));
g.fillRect(tankX, tankY, 64, 64);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment