mirror of
https://github.com/13hannes11/archive.git
synced 2024-09-03 21:50:58 +02:00
added all past projects
This commit is contained in:
11
Java/Wettbewerb_ForestFire/Wettbewerb_ForestFire.iml
Normal file
11
Java/Wettbewerb_ForestFire/Wettbewerb_ForestFire.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
3
Java/Wettbewerb_ForestFire/manifest.mf
Normal file
3
Java/Wettbewerb_ForestFire/manifest.mf
Normal file
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
X-COMMENT: Main-Class will be added automatically by build
|
||||
|
||||
64
Java/Wettbewerb_ForestFire/src/Main.java
Normal file
64
Java/Wettbewerb_ForestFire/src/Main.java
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
import java.util.HashMap;
|
||||
import processing.core.PApplet;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Main extends PApplet {
|
||||
|
||||
Map map = new Map();
|
||||
int recSize;
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
PApplet.main(new String[]{"Main"});
|
||||
}
|
||||
|
||||
public void setup() {
|
||||
recSize = 50;
|
||||
map.load("C:\\Users\\Hannes\\Desktop\\map.txt");//scanner.next());
|
||||
size(map.getWidth() * recSize, map.getHeight() * recSize);
|
||||
map.burnsIn(false);
|
||||
}
|
||||
// TODO: setup application
|
||||
|
||||
|
||||
public void draw() {
|
||||
//Drawing
|
||||
Tile[][] tmpMap = map.getTileMap();
|
||||
for (int x = 0; x < tmpMap.length; x++) {
|
||||
for (int y = 0; y < tmpMap[x].length; y++) {
|
||||
switch (tmpMap[x][y].getType()) {
|
||||
case 0: //Normaler Wald
|
||||
fill(0, 255, 0);
|
||||
break;
|
||||
case 1: //Blockierter Wald
|
||||
fill(120);
|
||||
break;
|
||||
case 2: //Feuer
|
||||
fill(255, 0, 0);
|
||||
break;
|
||||
}
|
||||
rect(x * recSize, y * recSize, recSize, recSize);
|
||||
if (tmpMap[x][y].isExt()) {
|
||||
fill(0, 0, 255);
|
||||
line(x * recSize, y * recSize, (x + 1) * recSize, (y + 1) * recSize);
|
||||
line((x + 1) * recSize, y * recSize, x * recSize, (y + 1) * recSize);
|
||||
}
|
||||
fill(0);
|
||||
text(tmpMap[x][y].getScore(), recSize * x + 1, recSize * y + 11);
|
||||
text((int) tmpMap[x][y].getRound(), recSize * x + 1, recSize * (y + 1) - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased() {
|
||||
System.out.println("Key Released: " + keyCode);
|
||||
if (keyCode == 39) {
|
||||
map.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
339
Java/Wettbewerb_ForestFire/src/Map.java
Normal file
339
Java/Wettbewerb_ForestFire/src/Map.java
Normal file
@@ -0,0 +1,339 @@
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author Hannes
|
||||
*/
|
||||
public class Map {
|
||||
|
||||
private boolean burning = false;
|
||||
private ArrayList<Tile> closedList;
|
||||
private int width;
|
||||
private int height;
|
||||
// private byte currentRound;
|
||||
private Tile[][] tileMap;
|
||||
|
||||
protected Map(int w, int h, ArrayList<Tile> closed, Tile[][] map) {
|
||||
closedList = closed;
|
||||
tileMap = map;
|
||||
|
||||
height = h;
|
||||
width = w;
|
||||
}
|
||||
|
||||
public Map() {
|
||||
closedList = new ArrayList<Tile>();
|
||||
|
||||
width = 0;
|
||||
height = 0;
|
||||
tileMap = null;
|
||||
}
|
||||
|
||||
public void extinguish(int x, int y) {
|
||||
tileMap[x][y].extinguish();
|
||||
closedList.remove(tileMap[x][y]);
|
||||
// burnsIn(false);
|
||||
}
|
||||
|
||||
public void ext() {
|
||||
int bestScore = Integer.MIN_VALUE;
|
||||
Map _map = this.clone();
|
||||
_map.burnsIn(true);
|
||||
int[] mainScore = _map.returnScoreArray();
|
||||
|
||||
int bestX = 0, bestY = 0;
|
||||
for (int x = 0; x < tileMap.length; x++) {
|
||||
for (int y = 0; y < tileMap[x].length; y++) {
|
||||
if (!tileMap[x][y].isExt() && tileMap[x][y].getType() != 1) {
|
||||
Map _tmpMap = this.clone();
|
||||
_tmpMap.extinguish(x, y);
|
||||
_tmpMap.burnsIn(true);
|
||||
|
||||
|
||||
int _score = _tmpMap.calcScore(mainScore, mainScore);
|
||||
if (_score >= bestScore && (!burning || tileMap[x][y].getType() == 2)) {
|
||||
bestScore = _score;
|
||||
bestX = x;
|
||||
bestY = y;
|
||||
}
|
||||
|
||||
tileMap[x][y].setScore(_score);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
System.out.println(
|
||||
"[" + bestX + "|" + bestY + "|"+ bestScore + "]");
|
||||
|
||||
extinguish(bestX, bestY);
|
||||
}
|
||||
|
||||
public void next() {
|
||||
nextRound();
|
||||
ext();
|
||||
}
|
||||
|
||||
public void clearRoundList() {
|
||||
for (int x = 0; x < tileMap.length; x++) {
|
||||
for (int y = 0; y < tileMap[x].length; y++) {
|
||||
if (closedList.contains(tileMap[x][y])) {
|
||||
tileMap[x][y].setRound(0);
|
||||
} else {
|
||||
tileMap[x][y].setRound(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void burnsIn(boolean ignite) { //Fehlder liegt vermutlich in dieser Methode
|
||||
clearRoundList();
|
||||
ArrayList<Tile> setThisRound = new ArrayList<Tile>();
|
||||
ArrayList<Tile> open = new ArrayList<Tile>();
|
||||
for (Tile tile : closedList) {
|
||||
if (tile.getType() == 2) {
|
||||
open.add(tile);
|
||||
}
|
||||
}
|
||||
int i = 0;
|
||||
while (open.size() != 0) {
|
||||
ArrayList<Tile> _open = (ArrayList<Tile>) open.clone();
|
||||
open.clear();
|
||||
for (Tile tile : _open) {
|
||||
if (!contains(tile, setThisRound)) {
|
||||
setThisRound.add(tile);
|
||||
ArrayList<Tile> _n = new ArrayList<Tile>();
|
||||
_n.add(getNeighbour(tile, 1, 0));
|
||||
_n.add(getNeighbour(tile, 0, 1));
|
||||
_n.add(getNeighbour(tile, -1, 0));
|
||||
_n.add(getNeighbour(tile, 0, -1));
|
||||
for (Tile _t : _n) {
|
||||
if (_t != null && !contains(_t, setThisRound)) {
|
||||
tileMap[_t.getX()][_t.getY()].setRound(i);
|
||||
open.add(tileMap[_t.getX()][_t.getY()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (ignite) {
|
||||
for (int x = 0; x < tileMap.length; x++) {
|
||||
for (int y = 0; y < tileMap[x].length; y++) {
|
||||
if (tileMap[x][y].getRound() == 0) {
|
||||
tileMap[x][y].ignite();
|
||||
if (!contains(tileMap[x][y], closedList)) {
|
||||
closedList.add(tileMap[x][y]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
//System.out.println(open.size());
|
||||
|
||||
}
|
||||
|
||||
public Tile getNeighbour(Tile t, int offsetX, int offsetY) {
|
||||
if (t.isExt()) {
|
||||
return null;
|
||||
}
|
||||
int _x = t.getX() + offsetX;
|
||||
int _y = t.getY() + offsetY;
|
||||
if (_x < 0 || _x >= tileMap.length || _y < 0 || _y >= tileMap[_x].length) {
|
||||
return null;
|
||||
}
|
||||
if (tileMap[_x][_y].isExt() || tileMap[_x][_y].getType() == 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return tileMap[_x][_y];
|
||||
}
|
||||
|
||||
private boolean contains(Tile t, ArrayList<Tile> list) {
|
||||
for (Tile tile : list) {
|
||||
if (t.getX() == tile.getX() && t.getY() == tile.getY()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void nextRound() {
|
||||
burnsIn(true);
|
||||
//extinguish(5, 5);
|
||||
//extinguish(4, 4);
|
||||
//System.out.println("Score: " + calcScore(new int[]{5, 4, 3, 2, 1}, returnScoreArray()));
|
||||
}
|
||||
|
||||
public int[] returnScoreArray() {
|
||||
// ArrayList<Integer> list = new ArrayList<Integer>();
|
||||
// for (int x = 0; x < tileMap.length; x++) {
|
||||
// for (int y = 0; y < tileMap[x].length; y++) {
|
||||
// int r = tileMap[x][y].getRound();
|
||||
// while (r >= list.size()) {
|
||||
// list.add(0);
|
||||
// }
|
||||
// if (r >= 0) {
|
||||
// list.set(r, list.get(r) + 1);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//int[] i = new int[list.size()];
|
||||
// for (int l = 0; l < i.length; l++) {
|
||||
// i[l] = list.get(l);
|
||||
// }
|
||||
int[] i = new int[10];
|
||||
for (int z = 0; z < i.length; z++) {
|
||||
if (z == 0) {
|
||||
//System.out.println("");
|
||||
}
|
||||
for (int x = 0; x < tileMap.length; x++) {
|
||||
if (z == 0) {
|
||||
//System.out.println("");
|
||||
}
|
||||
for (int y = 0; y < tileMap.length; y++) {
|
||||
|
||||
if (z == 0) {
|
||||
//System.out.print(tileMap[x][y].getRound() + ";");
|
||||
}
|
||||
|
||||
if (tileMap[x][y].getRound() == z) {
|
||||
i[z]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
public int calcScore(int[] factor, int[] mainScore) {
|
||||
//burnsIn(false);
|
||||
int[] arr = returnScoreArray();
|
||||
int score = 0;
|
||||
int length = (arr.length < factor.length) ? arr.length : factor.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
int mainS = (i < mainScore.length) ? mainScore[i] : 0;
|
||||
score += (mainS - arr[i]) * factor[i];
|
||||
}
|
||||
return score;
|
||||
}
|
||||
// <editor-fold defaultstate="collapsed" desc="File Management">
|
||||
|
||||
public void load(String path) {
|
||||
Tile[][] tmp = null;
|
||||
BufferedReader br = null;
|
||||
try {
|
||||
StringBuilder stringB = new StringBuilder();
|
||||
br = new BufferedReader(new FileReader(path));
|
||||
width = readInt(br);
|
||||
height = readInt(br);
|
||||
tmp = new Tile[width][height];
|
||||
|
||||
for (int y = 0; y < width; y++) {
|
||||
for (int x = 0; x < height; x++) {
|
||||
switch ((char) br.read()) {
|
||||
case '0': //Normaler Wald
|
||||
tmp[x][y] = new Tile(x, y, (byte) 0);
|
||||
break;
|
||||
case '1': //Blockierter Wald
|
||||
tmp[x][y] = new Tile(x, y, (byte) 1);
|
||||
break;
|
||||
case '2': //Feuer
|
||||
tmp[x][y] = new Tile(x, y, (byte) 2);
|
||||
closedList.add(tmp[x][y]);
|
||||
break;
|
||||
default:
|
||||
x--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
setTileMap(tmp);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private int readInt(BufferedReader br) throws IOException {
|
||||
char c;
|
||||
int i;
|
||||
|
||||
c = readNonwhiteChar(br);
|
||||
i = 0;
|
||||
do {
|
||||
i = i * 10 + c - '0';
|
||||
c = (char) br.read();
|
||||
} while (c >= '0' && c <= '9');
|
||||
return i;
|
||||
}
|
||||
|
||||
private static char readNonwhiteChar(BufferedReader bf) throws IOException {
|
||||
char c;
|
||||
|
||||
do {
|
||||
c = (char) bf.read();
|
||||
} while (c == ' ' || c == '\t' || c == '\n' || c == '\r');
|
||||
|
||||
return c;
|
||||
}
|
||||
// </editor-fold>
|
||||
|
||||
@Override
|
||||
public Map clone() {
|
||||
ArrayList<Tile> _tmpClosed = new ArrayList<Tile>();
|
||||
for (Tile tile : closedList) {
|
||||
_tmpClosed.add(tile.clone());
|
||||
}
|
||||
Tile[][] _tmpMap = new Tile[tileMap.length][tileMap[0].length];
|
||||
for (int x = 0; x < tileMap.length; x++) {
|
||||
for (int y = 0; y < tileMap[x].length; y++) {
|
||||
_tmpMap[x][y] = tileMap[x][y].clone();
|
||||
}
|
||||
}
|
||||
return new Map(width, height, _tmpClosed, _tmpMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String str = "";
|
||||
for (int y = 0; y < tileMap[0].length; y++) {
|
||||
for (int x = 0; x < tileMap.length; x++) {
|
||||
str = str + tileMap[x][y].getRound() + ";";
|
||||
}
|
||||
str = str + "\n";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
// <editor-fold defaultstate="collapsed" desc="Getter and Setter">
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public Tile[][] getTileMap() {
|
||||
return tileMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param tileMap the tileMap to set
|
||||
*/
|
||||
public void setTileMap(Tile[][] tileMap) {
|
||||
this.tileMap = tileMap;
|
||||
}
|
||||
// </editor-fold>
|
||||
}
|
||||
100
Java/Wettbewerb_ForestFire/src/Tile.java
Normal file
100
Java/Wettbewerb_ForestFire/src/Tile.java
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Hannes
|
||||
*/
|
||||
public class Tile {
|
||||
|
||||
private int x, y;
|
||||
private byte type;
|
||||
private int round;
|
||||
private int score;
|
||||
private boolean ext;
|
||||
|
||||
public Tile(int myX, int myY, byte TileType) {
|
||||
score = 0;
|
||||
x = myX;
|
||||
y = myY;
|
||||
type = TileType;
|
||||
if (type == 2) {
|
||||
ignite();
|
||||
} else {
|
||||
round = -1;
|
||||
}
|
||||
ext = false;
|
||||
}
|
||||
|
||||
protected Tile(int myX, int myY, byte TileType, int r, int s, boolean extinguished) {
|
||||
score = s;
|
||||
x = myX;
|
||||
y = myY;
|
||||
type = TileType;
|
||||
round = r;
|
||||
ext = extinguished;
|
||||
}
|
||||
|
||||
public void extinguish() {
|
||||
ext = true;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void ignite() {
|
||||
if (type == 0 && !isExt()) {
|
||||
type = 2;
|
||||
round = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void setRound(int r) {
|
||||
if (type != 2) {
|
||||
round = r;
|
||||
}
|
||||
}
|
||||
|
||||
public int getRound() {
|
||||
return round;
|
||||
}
|
||||
|
||||
public boolean isExt() {
|
||||
return ext;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tile clone() {
|
||||
return new Tile(x, y, type, round, getScore(),ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the score
|
||||
*/
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param score the score to set
|
||||
*/
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[" + x + "|" + y + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user