Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Last active November 10, 2015 12:08
Show Gist options
  • Save AnnaBoro/808ae4a06b3ef49f1883 to your computer and use it in GitHub Desktop.
Save AnnaBoro/808ae4a06b3ef49f1883 to your computer and use it in GitHub Desktop.
Tiger, BT7 extends
package lesson4.game;
public class BT7 extends Tank1 {
private int speed;
public BT7() {
}
public BT7(ActionField actionField, BattleField battleField, int x, int y, Direction direction) {
super(actionField, battleField, x, y, direction);
speed = super.getSpeed() / 2;
}
}
package lesson4.game;
import lesson4.game.ActionField;
import lesson4.game.BattleField;
import lesson4.game.Bullet;
public class Tank1 {
private Direction direction;
private int x = 128;
private int y = 512;
protected int speed = 10;
private ActionField actionField;
private BattleField battleField;
public Tank1() {
}
public Tank1(ActionField actionField, BattleField battleField) {
this(actionField, battleField, 128, 512, Direction.UP);
}
public Tank1(ActionField actionField, BattleField battleField, int x, int y, Direction direction) {
this.actionField = actionField;
this.battleField = battleField;
this.x = x;
this.y = y;
this.direction = direction;
}
public void destroy() {
x = -100;
y = -100;
actionField.repaint();
}
public void turn(Direction direction) {
this.direction = direction;
actionField.processTurn(this);
}
public void move() throws InterruptedException {
actionField.processMove(this);
}
public void fire() throws InterruptedException {
Bullet bullet = new Bullet(x + 25, y + 25, direction);
actionField.processFire(bullet);
}
public void clean() throws InterruptedException {
turn(Direction.LEFT);
while (isEmptyX()) {
fire();
}
moveToQuadrant(getY(), 0);
turn(Direction.UP);
while (isEmptyY()) {
fire();
}
moveToQuadrant(0, 0);
turn(Direction.RIGHT);
while (isEmptyX()) {
fire();
}
for (int i = 0; i < battleField.getBattleField().length; i++) {
moveToQuadrant(0, i * 64);
turn(Direction.DOWN);
while (isEmptyY()) {
fire();
}
}
}
public boolean isEmptyY() {
int index = getX() / 64;
int firstPoint = 0;
int endPoint = getY() / 64;
if (getDirection() == Direction.DOWN) {
firstPoint = getY() / 64;
endPoint = battleField.getBattleField()[index].length;
}
for (int i = firstPoint; i < endPoint; i++) {
if (battleField.getBattleField()[i][index] == "B") {
return true;
}
}
return false;
}
public boolean isEmptyX() {
int index = getY() / 64;
int firstPoint = 0;
int endPoint = getX() / 64;
if (getDirection() == Direction.RIGHT) {
firstPoint = getX() / 64;
endPoint = battleField.getBattleField()[index].length;
}
for (int i = firstPoint; i < endPoint; i++) {
if (battleField.getBattleField()[index][i] == "B") {
return true;
}
}
return false;
}
public int[] getRandomQuadrant() {
int[] randomNumbers = getRandomNumbers();
for (int i = 0; i < randomNumbers.length; i++) {
if (randomNumbers[i] > 8) {
randomNumbers[i] = randomNumbers[i] - 1;
}
}
return randomNumbers;
}
public 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;
}
public Direction getRandomDirection(){
// Direction direction;
int[] randomNumbers = getRandomNumbers();
int randNumInt1 = randomNumbers[0];
int randNumInt2 = randomNumbers[1];
if (randNumInt1 > randNumInt2) {
if (randNumInt1 % 2 == 0) {
direction = Direction.DOWN;
}
else {
direction = Direction.UP;
}
}
else {
if (randNumInt2 % 2 == 0) {
direction = Direction.LEFT;
}
else {
direction = Direction.RIGHT;
}
}
return direction;
}
public void moveRandom() throws InterruptedException {
while (true) {
turn(getRandomDirection());
move();
}
}
public void moveToQuadrant(int v, int h) throws InterruptedException {
String quadrant = actionField.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 - getX()) > 0) {
int steps = (tankXNew - getX()) / 64;
for (int step = 0; step < steps; step++) {
turn(Direction.RIGHT);
if (battleField.getBattleField()[getY()/64][getX()/64 + 1].equals("B")) {
fire();
}
move();
}
} else if ((tankXNew - getX()) < 0) {
int steps = Math.abs((tankXNew - getX()) / 64);
for (int step = 0; step < steps; step++) {
turn(Direction.LEFT);
if (battleField.getBattleField()[getY()/64][getX()/64 - 1].equals("B")) {
fire();
}
move();
}
}
if ((tankYNew - getY()) > 0) {
int steps = (tankYNew - getY()) / 64;
for (int step = 0; step < steps; step++) {
turn(Direction.DOWN);
if (battleField.getBattleField()[getY()/64 + 1][getX()/64].equals("B")) {
fire();
}
move();
}
} else if ((tankYNew - getY()) < 0) {
int steps = Math.abs((getY() - tankYNew) / 64);
for (int step = 0; step < steps; step++) {
turn(Direction.UP);
if (battleField.getBattleField()[getY()/64 - 1][getX()/64].equals("B")) {
fire();
}
move();
}
}
}
public Direction getDirection() {
return direction;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void updateX(int i) {
x += i;
}
public void updateY(int i) {
y += i;
}
public int getSpeed() {
return speed;
}
}
package lesson4.game;
public class Tiger extends Tank1{
private int armor;
public Tiger() {
}
public Tiger(ActionField actionField, BattleField battleField, int x, int y, Direction direction) {
super(actionField, battleField, x, y, direction);
armor = 1;
}
public void setArmor(int armor) {
this.armor = armor;
}
public int getArmor() {
return armor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment