Created
February 4, 2016 14:19
-
-
Save Yur-ok/6cc46002b704eda41695 to your computer and use it in GitHub Desktop.
Final version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2013 midgardabc.com | |
*/ | |
package FinalVersionOfProgramms; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.util.Random; | |
/** | |
* @version 3.0 | |
*/ | |
public class Tanks extends JPanel { | |
static int amountOfBriks = 0; | |
final boolean COLORDED_MODE = false; | |
final int BF_WIDTH = 576; | |
final int BF_HEIGHT = 576; | |
int UP = 1; | |
int DOWN = 2; | |
int LEFT = 3; | |
int RIGHT = 4; | |
// 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 = 3; | |
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", " ", " ", "B"} | |
}; | |
/** | |
* Write your code here. | |
*/ | |
void runTheGame() throws Exception { | |
numbersOFBriks(); | |
clean(); | |
} | |
boolean processInterception(String name) throws Exception { | |
String coord = null; | |
if (name.equals("tank")) { | |
coord = getQuadrant(tankX, tankY); | |
} else { | |
coord = getQuadrant(bulletX, bulletY); | |
} | |
int x = Integer.parseInt(coord.substring(coord.indexOf("_") + 1)); | |
int y = Integer.parseInt(coord.substring(0, coord.indexOf("_"))); | |
if (name.equals("tank")) { | |
if (tankDirection == RIGHT && !battleField[y][x + 1].trim().isEmpty()) { | |
return true; | |
} else if (tankDirection == LEFT && !battleField[y][x - 1].trim().isEmpty()) { | |
return true; | |
} else if (tankDirection == DOWN && !battleField[y + 1][x].trim().isEmpty()) { | |
return true; | |
} else if (tankDirection == UP && !battleField[y - 1][x].trim().isEmpty()) { | |
return true; | |
} | |
} else { | |
if (x >= 0 && x <= 8 && y >= 0 && y <= 8) { | |
if (!battleField[y][x].trim().isEmpty()) { | |
battleField[y][x] = ""; | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
String getQuadrant(int x, int y) { | |
return y / 64 + "_" + x / 64; | |
} | |
void fire() throws Exception { | |
bulletX = tankX + 25; | |
bulletY = tankY + 25; | |
while ((bulletX > -14 && bulletX < 592) && (bulletY > -14 && bulletY < 592)) { | |
if (tankDirection == UP) { | |
bulletY -= 1; | |
} else if (tankDirection == DOWN) { | |
bulletY += 1; | |
} else if (tankDirection == LEFT) { | |
bulletX -= 1; | |
} else if (tankDirection == RIGHT) { | |
bulletX += 1; | |
} | |
repaint(); | |
Thread.sleep(bulletSpeed); | |
if (processInterception("bullet")) { | |
bulletX = -100; | |
bulletY = -100; | |
amountOfBriks--; | |
// System.out.println(amountOfBriks); | |
} | |
} | |
} | |
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; | |
// check limits x: 0, 512; y: 0, 512 | |
if ((direction == UP && tankY == 0) || (direction == DOWN && tankY >= 512) | |
|| (direction == LEFT && tankX == 0) || (direction == RIGHT && tankX >= 512)) { | |
System.out.println("[illegal move] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY); | |
return; | |
} | |
turn(direction); | |
while (covered < 64) { | |
if (direction == UP) { | |
tankY -= step; | |
System.out.println("[move up] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY); | |
} else if (direction == DOWN) { | |
tankY += step; | |
System.out.println("[move down] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY); | |
} else if (direction == LEFT) { | |
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) { | |
turn(i); | |
move(i); | |
} | |
} | |
} | |
void moveToQuadrant(int v, int h) 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)); | |
while (tankX < x) { | |
turn(RIGHT); | |
if (processInterception("tank")) { | |
fire(); | |
} else { | |
move(RIGHT); | |
} | |
} | |
while (tankX > x) { | |
turn(LEFT); | |
if (processInterception("tank")) { | |
fire(); | |
} else { | |
move(LEFT); | |
} | |
} | |
while (tankY < y) { | |
turn(DOWN); | |
if (processInterception("tank")) { | |
fire(); | |
} else { | |
move(DOWN); | |
} | |
} | |
while (tankY > y) { | |
turn(UP); | |
if (processInterception("tank")) { | |
fire(); | |
} else { | |
move(UP); | |
} | |
} | |
} | |
void clean() throws Exception { | |
long start = System.currentTimeMillis(); | |
moveToQuadrant(1, 1); | |
for (int i = 0, k = 2; i <= battleField.length - 1; i++, k++) { | |
for (int j = 1; j <= battleField[i].length - 1; j++) { | |
if (battleField[i][j].trim().equals("B")) { | |
turn(RIGHT); | |
fire(); | |
} | |
} | |
if (k <= 9) { | |
moveToQuadrant(k, 1); | |
System.out.println("Move to " + k + ", 1"); | |
} | |
} | |
if (amountOfBriks == 0) { | |
moveToQuadrant(5, 5); | |
System.out.println("Battle Field is clean!!!"); | |
long finish = System.currentTimeMillis() - start; | |
System.out.println(finish / 1000); | |
} | |
} | |
int numbersOFBriks() { | |
int count = 0; | |
for (int i = 0; i <= battleField.length - 1; i++) { | |
for (int j = 0; j <= battleField[i].length - 1; j++) { | |
if (battleField[i][j].equals("B")) { | |
count++; | |
} | |
} | |
} | |
System.out.println(count); | |
return amountOfBriks = count; | |
} | |
// 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.runTheGame(); | |
} | |
public Tanks() throws Exception { | |
JFrame frame = new JFrame("BATTLE FIELD, DAY 2"); | |
frame.setLocation(750, 150); | |
frame.setMinimumSize(new Dimension(BF_WIDTH + 16, BF_HEIGHT + 39)); | |
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); | |
} | |
} |
liuiv15
commented
Feb 7, 2016
- Все сложные проверки (в операторах if и while желательно вынести в отдельные методы.
- вопрос по условиям окончания в циклах (например, i <= battleField.length - 1), можно поставить строгое условие и не вычитать 1 ( например, i < battleField.length ).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment