-
-
Save anonymous/31e7a2193c8083cc9de3 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.Random; | |
import java.util.ArrayList; | |
import java.awt.event.KeyEvent; | |
import java.lang.Math; | |
int GRIDSIZE = 20; | |
int TILESIZE = 32; | |
double lumber = 0; | |
double maws = 0; | |
int month = 0; | |
int time = 0; | |
ArrayList<Tile> tiles = new ArrayList<Tile>(); | |
ArrayList<Tile> add = new ArrayList<Tile>(); | |
ArrayList<Tile> remove = new ArrayList<Tile>(); | |
PrintWriter writer; | |
PrintWriter writer2; | |
PrintWriter writer3; | |
void setup() { | |
writer = createWriter("data.txt"); | |
writer2 = createWriter("data2.txt"); | |
writer3 = createWriter("data3.txt"); | |
Random r = new Random(); | |
for (int i=0; i<(GRIDSIZE*GRIDSIZE)*0.5; i++) { | |
createRandomTree(r); | |
} | |
for (int i=0; i<(GRIDSIZE*GRIDSIZE)*0.1; i++) { | |
createRandomJack(r, true); | |
} | |
for (int i=0; i<(GRIDSIZE*GRIDSIZE)*0.02; i++) { | |
createRandomBear(r, false); | |
} | |
size(GRIDSIZE*TILESIZE, GRIDSIZE*TILESIZE); | |
} | |
void createRandomTree(Random r) { | |
boolean placed = false; | |
do { | |
int x = r.nextInt(GRIDSIZE); | |
int y = r.nextInt(GRIDSIZE); | |
if (isSpotFree(x, y)) { | |
tiles.add(new TileTree(x, y)); | |
placed = true; | |
} | |
} while(!placed); | |
} | |
void createRandomJack(Random r, boolean needsFree) { | |
boolean placed = false; | |
do { | |
int x = r.nextInt(GRIDSIZE); | |
int y = r.nextInt(GRIDSIZE); | |
if (!needsFree || isSpotFree(x, y)) { | |
tiles.add(new TileLumberjack(x, y)); | |
placed = true; | |
} | |
} while(!placed); | |
} | |
void createRandomBear(Random r, boolean needsFree) { | |
boolean placed = false; | |
do { | |
int x = r.nextInt(GRIDSIZE); | |
int y = r.nextInt(GRIDSIZE); | |
if (!needsFree || isSpotFree(x, y)) { | |
tiles.add(new TileBear(x, y)); | |
placed = true; | |
} | |
} while(!placed); | |
} | |
boolean isSpotFree(int x, int y) { | |
for (Tile t : tiles) { | |
if (t.x/TILESIZE == x && t.y/TILESIZE == y) return false; | |
} | |
return true; | |
} | |
int getJacks() { | |
int jackCount = 0; | |
for (Tile t : tiles) { | |
if (t instanceof TileLumberjack) { | |
jackCount++; | |
} | |
} | |
return jackCount; | |
} | |
int getTrees() { | |
int treeCount = 0; | |
for (Tile t : tiles) { | |
if (t instanceof TileTree) { | |
treeCount++; | |
} | |
} | |
return treeCount; | |
} | |
int getBears() { | |
int bearCount = 0; | |
for (Tile t : tiles) { | |
if (t instanceof TileBear) { | |
bearCount++; | |
} | |
} | |
return bearCount; | |
} | |
void removeRandomJack() { | |
Tile firedJack = null; | |
for (Tile t : tiles) { | |
if (t instanceof TileLumberjack) { | |
firedJack = t; | |
} | |
} | |
tiles.remove(firedJack); | |
} | |
void removeRandomBear() { | |
Tile zooBear = null; | |
for (Tile t : tiles) { | |
if (t instanceof TileBear) { | |
zooBear = t; | |
} | |
} | |
tiles.remove(zooBear); | |
} | |
void stop() { | |
writer.flush(); | |
writer.close(); | |
writer2.flush(); | |
writer2.close(); | |
writer3.flush(); | |
writer3.close(); | |
} | |
void draw() { | |
if (month >= 4800) { | |
exit(); | |
} | |
background(20, 150, 50); | |
if (time % 1 == 0) { | |
writer.println(getJacks()); | |
writer2.println(getTrees()); | |
writer3.println(getBears()); | |
println(month); | |
if (month % 12 == 0) { | |
if (maws == 0) { | |
createRandomBear(new Random(), false); | |
} else { | |
removeRandomBear(); | |
} | |
maws = 0; | |
double jacks = getJacks(); | |
if (lumber-jacks >= 0) { | |
double newJacks = Math.floor((lumber-jacks) / 10)+1; | |
for (int i=0; i<newJacks; i++) { | |
createRandomJack(new Random(), false); | |
} | |
} else { | |
if (jacks > 1) { | |
double removeJacks = Math.floor((jacks-lumber) / 10)+6; | |
for (int i=0; i<removeJacks; i++) { | |
if (getJacks() == 1) break; | |
removeRandomJack(); | |
} | |
} | |
} | |
lumber = 0; | |
} | |
for (Tile t : tiles) { | |
t.update(); | |
} | |
month++; | |
} | |
for (Tile t : tiles) { | |
t.draw(); | |
} | |
for (Tile t : add) { | |
tiles.add(t); | |
} | |
add.clear(); | |
for (Tile t : remove) { | |
tiles.remove(t); | |
} | |
remove.clear(); | |
fill(255, 255, 255); | |
textSize(40); | |
textAlign(LEFT, TOP); | |
text("Year: "+String.valueOf(Math.floor(month/12)), 0, 0); | |
time++; | |
} | |
void keyPressed() { | |
if (keyCode == KeyEvent.VK_SPACE) { | |
for (Tile t : tiles) { | |
t.update(); | |
} | |
} | |
} | |
This file contains 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
class Tile { | |
public int x, y; | |
public Tile(int x, int y) { | |
this.x = x*TILESIZE; | |
this.y = y*TILESIZE; | |
} | |
public void update() {} | |
public void draw() {} | |
} |
This file contains 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
class TileBear extends Tile { | |
public TileBear(int x, int y) { | |
super(x, y); | |
} | |
public void update() { | |
for (int i=0; i<5; i++) { | |
step(); | |
TileLumberjack jack = findJack(); | |
if (jack != null) { | |
remove.add(jack); | |
maws++; | |
return; | |
} | |
} | |
} | |
public TileLumberjack findJack() { | |
for (Tile t : tiles) { | |
if (t.x == this.x && t.y == this.y && t instanceof TileLumberjack) { | |
return (TileLumberjack) t; | |
} | |
} | |
return null; | |
} | |
public void step() { | |
Random r = new Random(); | |
boolean moved = false; | |
do { | |
int x = r.nextInt(3)-1; | |
int y = r.nextInt(3)-1; | |
if (x != 0 || y != 0) { | |
int oldX = this.x; | |
int oldY = this.y; | |
this.x = this.x+x*TILESIZE; | |
this.y = this.y+y*TILESIZE; | |
if (this.x < 0 || this.y < 0 || this.x > TILESIZE*GRIDSIZE || this.y > TILESIZE*GRIDSIZE) { | |
this.x = oldX; | |
this.y = oldY; | |
} else { | |
moved = true; | |
} | |
} | |
} while(!moved); | |
} | |
public void draw() { | |
fill(188, 123, 3); | |
rect(this.x+TILESIZE/8, this.y+TILESIZE/8, TILESIZE*0.75, TILESIZE*0.75); | |
fill(0, 0, 0); | |
rect(this.x+TILESIZE/4, this.y+TILESIZE/4, 4, 4); | |
rect(this.x+TILESIZE-(TILESIZE/4)-4, this.y+TILESIZE/4, 4, 4); | |
rect(this.x+TILESIZE/4, this.y+TILESIZE-TILESIZE/4-4, TILESIZE/2, 4); | |
} | |
} |
This file contains 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
class TileLumberjack extends Tile { | |
public TileLumberjack(int x, int y) { | |
super(x, y); | |
} | |
public void update() { | |
for (int i=0; i<3; i++) { | |
step(); | |
TileTree tree = findTree(); | |
if (tree != null) { | |
remove.add(tree); | |
if (tree.state == 1) { | |
lumber++; | |
} else if (tree.state == 2) { | |
lumber += 2; | |
} | |
return; | |
} | |
} | |
} | |
public TileTree findTree() { | |
for (Tile t : tiles) { | |
if (t.x == this.x && t.y == this.y && t instanceof TileTree && ((TileTree)t).state > 0) { | |
return (TileTree) t; | |
} | |
} | |
return null; | |
} | |
public void step() { | |
Random r = new Random(); | |
boolean moved = false; | |
do { | |
int x = r.nextInt(3)-1; | |
int y = r.nextInt(3)-1; | |
if (x != 0 || y != 0) { | |
int oldX = this.x; | |
int oldY = this.y; | |
this.x = this.x+x*TILESIZE; | |
this.y = this.y+y*TILESIZE; | |
if (this.x < 0 || this.y < 0 || this.x > TILESIZE*GRIDSIZE || this.y > TILESIZE*GRIDSIZE) { | |
this.x = oldX; | |
this.y = oldY; | |
} else { | |
moved = true; | |
} | |
} | |
} while(!moved); | |
} | |
public void draw() { | |
fill(255, 255, 180); | |
rect(this.x+TILESIZE/8, this.y+TILESIZE/8, TILESIZE*0.75, TILESIZE*0.75); | |
fill(0, 0, 0); | |
rect(this.x+TILESIZE/4, this.y+TILESIZE/4, 4, 4); | |
rect(this.x+TILESIZE-(TILESIZE/4)-4, this.y+TILESIZE/4, 4, 4); | |
rect(this.x+TILESIZE/4, this.y+TILESIZE-TILESIZE/4-4, TILESIZE/2, 4); | |
} | |
} |
This file contains 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
class TileTree extends Tile { | |
private int state; | |
private int growthTimer; | |
public TileTree(int x, int y) { | |
super(x, y); | |
this.state = 0; | |
this.growthTimer = 12; | |
} | |
public void update() { | |
if (this.growthTimer > 0) { | |
this.growthTimer--; | |
} | |
if (this.growthTimer == 0) { | |
if (this.state == 0) { | |
this.state = 1; | |
this.growthTimer = 120; | |
} else if (this.state == 1) { | |
this.state = 2; | |
} | |
} | |
if (state == 1) { | |
Random r = new Random(); | |
if (r.nextInt(10) == 0) { | |
boolean placed = false; | |
int[][] tried = new int[8][2]; | |
do { | |
int x = (r.nextInt(3)-1); | |
int y = (r.nextInt(3)-1); | |
if (x != 0 || y != 0) { | |
if (this.x/TILESIZE+x > 0 && this.x/TILESIZE+x < GRIDSIZE && | |
this.y/TILESIZE+y > 0 && this.y/TILESIZE+y < GRIDSIZE && | |
isSpotFree(this.x/TILESIZE+x, this.y/TILESIZE+y)) { | |
add.add(new TileTree(this.x/TILESIZE+x, this.y/TILESIZE+y)); | |
placed = true; | |
} else { | |
if (tried.length == 8) { | |
break; | |
} | |
boolean found = false; | |
for (int[] t : tried) { | |
if (t[0] == x && t[1] == y) { | |
found = true; | |
break; | |
} | |
} | |
if (!found) { | |
tried[tried.length] = new int[] {x, y}; | |
} | |
} | |
} | |
} while(!placed); | |
} | |
} | |
if (state == 2) { | |
Random r = new Random(); | |
if (r.nextInt(5) == 0) { | |
boolean placed = false; | |
int[][] tried = new int[8][2]; | |
do { | |
int x = (r.nextInt(3)-1); | |
int y = (r.nextInt(3)-1); | |
if (x != 0 || y != 0) { | |
if (this.x/TILESIZE+x > 0 && this.x/TILESIZE+x < GRIDSIZE && | |
this.y/TILESIZE+y > 0 && this.y/TILESIZE+y < GRIDSIZE && | |
isSpotFree(this.x/TILESIZE+x, this.y/TILESIZE+y)) { | |
add.add(new TileTree(this.x/TILESIZE+x, this.y/TILESIZE+y)); | |
placed = true; | |
} else { | |
if (tried.length == 8) { | |
break; | |
} | |
boolean found = false; | |
for (int[] t : tried) { | |
if (t[0] == x && t[1] == y) { | |
found = true; | |
break; | |
} | |
} | |
if (!found) { | |
tried[tried.length] = new int[] {x, y}; | |
} | |
} | |
} | |
} while(!placed); | |
} | |
} | |
} | |
public void draw() { | |
fill(190, 104, 3); | |
rect(this.x+TILESIZE/3, this.y+TILESIZE/4, TILESIZE/3, TILESIZE*0.3+(0.21*TILESIZE)*this.state); | |
fill(39, 174, 96); | |
if (state == 0) { | |
rect(this.x+TILESIZE/3.5, this.y, TILESIZE/3.5*1.5, TILESIZE/3); | |
} else { | |
rect(this.x+TILESIZE/4.4, this.y, TILESIZE/2, TILESIZE/2); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment