94461/Code/Actor.java
94461/Code/Actor.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.util.List;
import java.util.function.Predicate;
abstract class Actor
{
protected Cell location;
protected float rdns;
protected int iTrns;
protected int tr;
protected int m;
protected int r;
protected MoveStrategy s;
protected List d;
protected Predicate filter; protected ColorMixer color; public Actor(Cell inLocation, float inrdns, int inTurns, int inMoves, int inRange) { setLocation(inLocation); rdns = inrdns; iTrns = inTurns; tr = iTrns; m = inMoves; r = inRange; s = new RandomMove(); color = new ColorMixer(); color.setHue((1.0f - rdns) * ColorMixer.BLUE); } void paint(Graphics g) { for (Polygon p : d) { g.setColor(color.mix(rdns)); g.fillPolygon(p); g.setColor(Color.GRAY); g.drawPolygon(p); } } protected abstract void setPoly(); boolean isTeamRed() { return rdns >= 0.5; } void setLocation(Cell inLocation) { location = inLocation; setPoly(); } void makeRedder(float amt) { rdns = rdns + amt; if (rdns > 1.0f) { rdns = 1.0f; } } public Cell location() { return location; } public MoveStrategy strategy() { return s; } public float getrdns() { return rdns; } public int turnsLeft() { return tr; } public int maxTurns() { return iTrns; } public void resetTurns() { tr = iTrns; } public void turnTaken() { tr--; } public int getMoves() { return m; } public int getRange() { return r; } public String toString() { return this.getClass().getName().toString(); } } 94461/Code/ActorInfo.java 94461/Code/ActorInfo.java import java.awt.Graphics; import java.awt.Point; public class ActorInfo extends InfoPanel { Stage stg; public | ActorInfo(Stage stg)
{
this.stg = stg;
}
@Override
public void paint(Graphics g, int yloc, Point mouseLoc)
{
final int lIndent = mrgn + hTab;
final int mvIndent = mrgn + 3 * blockVtab;
yloc = yloc + 2 * blockVtab;
for (int i = 0; i < stg.actors.size(); i++)
{
Actor a = stg.actors.get(i);
yloc = yloc + 2 * blockVtab;
g.drawString(a.toString(), mrgn, yloc);
g.drawString("location:", lIndent, yloc + vTab);
String crdnt = Character.toString(a.location().col) + Integer.toString(a.location().row);
g.drawString(crdnt, mvIndent, yloc + vTab);
g.drawString("redness:", lIndent, yloc + 2 * vTab);
g.drawString(String.format("%.1f", a.getRedness()), mvIndent, yloc + 2 * vTab);
}
}
}
94461/Code/AnimationBeat.java
94461/Code/AnimationBeat.java
public class AnimationBeat
{
private long strtd;
private long pA;
private long pB;
private long pC;
private static AnimationBeat instnc;
private AnimationBeat()
{
strtd = System.cTmMillis();
pA = 5000;
pB = 500;
pC = 500;
}
static AnimationBeat getInstance()
{
if (instnc == null)
{
instnc = new AnimationBeat();
}
return instnc;
}
char inPhase()
{
long cTime = System.cTmMillis();
long rem = (cTime - strtd) % (pA + pB + pC);
if (rem > pA + pB)
{
return 'c';
}
else if (rem > pA)
{
return 'b';
}
else
{
return 'a';
}
}
long phaseCompletion()
{
long cTime = System.cTmMillis();
long rem = (cTime - strtd) % (pA + pB + pC);
if (rem > pA + pB)
{
return ((rem - pA - pB) * 100) / pC;
}
else if (rem > pA)
{
return ((rem - pA) * 100) / pB;
}
else
{
return rem * 100 / pA;
}
}
}
94461/Code/Boat.java
94461/Code/Boat.java
import java.awt.Polygon;
import java.util.ArrayList;
class Boat extends Actor
{
public Boat(Cell inLoc, float inRdns)
{
super(inLoc, inRdns, 3, 1, 4);
fltr = (Cell c) ->
{
return ! Water.class.isInstance(c);
};
setPoly();
}
protected void setPoly()
{
dsply = new ArrayList
();
Polygon lSail = new Polygon();
lSail.adPoint(lctn().x + 16, lctn().y + 11);
lSail.adPoint(lctn().x + 11, lctn().y + 24);
lSail.adPoint(lctn().x + 16, lctn().y + 24);
Polygon rSail = new Polygon();
rSail.adPoint(lctn().x + 18, lctn().y + 7);
rSail.adPoint(lctn().x + 24, lctn().y + 24);
rSail.adPoint(lctn().x + 18, lctn().y + 24);
Polygon body = new Polygon();
body.adPoint(lctn().x + 6, lctn().y + 24);
body.adPoint(lctn().x + 29, lctn().y + 24);
body.adPoint(lctn().x + 24, lctn().y + 29);
body.adPoint(lctn().x + 11, lctn().y + 29);
dsply.add(lSail);
dsply.add(rSail);
dsply.add(body);
}
}
94461/Code/Building.java
94461/Code/Building.java
import java.awt.Color;
class Building extends Cell
{
public Building(char col, int r, int a, int b)
{
super(col, r, a, b);
d = "Building";
clr = new Color(96, 64, 32);
}
}
94461/Code/Car.java
94461/Code/Car.java
import java.awt.Polygon;
import java.util.ArrayList;
class Car extends Actor
{
public Car(Cell inLoc, float inRdns)
{
super(inLoc, inRdns, 1, 1, 3);
filter = (Cell c) -> { return ! Road.class.isInstance(c); };
setPoly();
}
protected void setPoly()
{
dsply = new ArrayList();
int sides = 20;
int angl;
double cX;
double cY;
Polygon rWheel = new Polygon();
Polygon fWheel = new Polygon();
angl = 360 / sides;
for (int s = 0; s <= sides; s++)
{
cX = (4.0 * Math.sin(Math.toRadians(s * angl)));
cY = (4.0 * Math.cos(Math.toRadians(s * angl)));
rWheel.adPoint(lctn().x + 11 + (int) cX, lctn().y + 25 + (int) cY);
fWheel.adPoint(lctn().x + 24 + (int) cX, lctn().y + 25 + (int) cY);
}
Polygon body = new Polygon();
body.adPoint(lctn().x + 6, lctn().y + 14);
body.adPoint(lctn().x + 29, lctn().y + 14);
body.adPoint(lctn().x + 29, lctn().y + 20);
body.adPoint(lctn().x + 6, lctn().y + 20);
Polygon top = new Polygon();
top.adPoint(lctn().x + 11, lctn().y + 7);
top.adPoint(lctn().x + 20, lctn().y + 7);
top.adPoint(lctn().x + 24, lctn().y + 14);
top.adPoint(lctn().x + 11, lctn().y + 14);
dsply.add(rWheel);
dsply.add(fWheel);
dsply.add(body);
dsply.add(top);
}
}
94461/Code/Cell.java
94461/Code/Cell.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
class Cell extends Rectangle
{
private static int s = 35;
protected char c;
protected int r;
protected Color c;
protected String dsptn;
public Cell(char inC, int inR, int inX, int inY)
{
super(inX, inY, s, s);
c = inC;
r = inR;
}
void paint(Graphics g, Point mousePos)
{
if (contains(mousePos))
{
g.setColor(Color.GRAY);
}
else
{
g.setColor(c);
}
g.fillRect(x, y, s, s);
g.setColor(Color.BLACK);
g.drawRect(x, y, s, s);
}
@Override
public boolean contains(Point p)
{
if (p != null)
{
return (super.contains(p));
}
else
{
return false;
}
}
public int leftOfComparison(Cell c)
{
return Character.compare(c, c.c);
}
public int aboveComparison(Cell c)
{
return Integer.compare(r, c.r);
}
public String toString()
{
return Character.toString(c) + Integer.toString(r) + ":" + dsptn;
}
}
94461/Code/CellIterator.java
94461/Code/CellIterator.java
import java.util.Iterator;
public class CellIterator implements Iterator { Cell[][] d; int otr; int inr; boolean rOt; CellIterator(Cell[][] d) { this.d = d; otr = 0; inr = 0; rOt = false; } @Override public boolean hasNext() { return !rOt; } @Override public Cell next() { Cell ret = d[otr][inr]; inr++; if (inr >= d[otr].length) { inr = 0; otr++; if (otr >= d.length) { rOt = true; } } return ret; } } 94461/Code/ChoosingActor.java 94461/Code/ChoosingActor.java import java.awt.Graphics; import java.util.Optional; public class ChoosingActor implements GameState { @Override public void mouseClick(int x, int y, Stage s) { s.aInActn = Optional.empty(); for (Actor a : s.actrs) { if (a.location().contains(x, y) && a.isTeamRed() && a.turnsLeft() > 0) { s.cOvrlay = s.grid.getRadius(a.location(), a.getMoves(), true); s.cOvrlay.removeIf(a.filter); s.aInActn = Optional.of(a); s.cSt = new SelectingNewLocation(); } } if (s.aInActn.isEmpty()) { s.cSt = new SelectingMenuItem(); s.mOvrlay.add(new MenuItem("Oops", x, y, () -> s.cSt = new ChoosingActor())); s.mOvrlay.add(new MenuItem("End Turn", x, y + MenuItem.h, () -> s.cSt = new CPUMoving())); s.mOvrlay.add(new MenuItem("End Game", x, y + MenuItem.h * 2, () -> System.exit(0))); } } @Override public void paint(Graphics g, Stage s) {} @Override public String toString() { return getClass().getSimpleName(); } } 94461/Code/ColorMixer.java 94461/Code/ColorMixer.java import java.awt.Color; public class ColorMixer { public static final float RED = 0 * 60; public static final float YELLOW = 1 * 60; public static final float GREEN = 2 * 60; public static final float CYAN = 3 * 60; public static final float BLUE = 4 * 60; public static final float MAGENTA = 5 * 60; protected float h; protected float intsty; protected float brgtns; public ColorMixer() { intsty = 1.0f; brgtns = 1.0f; } public Color mix(float val) { float tint = (val * (RED - BLUE + 360.0f) + BLUE) / 360.0f; return Color.getHSBColor(tint, intsty, brgtns); } public void setH(float newH) { h = newH; } public void setIntsty(float newIntsty) { intsty = newIntsty; } public void setBrgtns(float newBrgtns) { brgtns = newBrgtns; } } 94461/Code/CPUMoving.java 94461/Code/CPUMoving.java import java.awt.Graphics; import java.util.List; public class CPUMoving implements GameState { @Override public void mouseClick(int x, int y, Stage s) {} @Override public void paint(Graphics g, Stage s) { for (Actor a: s.actors) { if (! a.isTeamRed()) { List pLocs = s.getClearRadius(a.location(), a.getMoves(), true); pLocs.removeIf(a.filter); Cell nLoc = a.strategy().chooseNLoc(a.location(), pLocs); a.setLocation(nLoc); } } s.currentState = new ChoosingActor(); for (Actor a: s.actors) { a.resetTurns(); } } @Override public String toString() { return getClass().getSimpleName(); } } 94461/Code/GameState.java 94461/Code/GameState.java import java.awt.Graphics; public interface GameState { public void mouseClick(int x, int y, Stage s); public void paint(Graphics g, Stage s); } 94461/Code/Grass.java 94461/Code/Grass.java import java.awt.Color; class Grass extends Landscape { public Grass(char c, int r, int a, int b, int c) { super(c, r, x, y, z); dscrptn = "Grass"; clr = Color.getHSBColor(120.0f / 360.0f, 1.0f - shade, 0.35f + 0.65f * shade); } } 94461/Code/Grid.java 94461/Code/Grid.java import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Random; import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.IntStream; class Grid implements Iterable { Cell[][] c = new Cell[20][20]; private static Random rand = new Random(); public Grid() { int elvtn; List dstbtn = new ArrayList(); dstbtn = IntStream.rangeClosed(0, 20 * 20 - 1).boxed().collect(Collectors.toList()); int crnt; int index; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { elvtn = rand.nextInt(8001) - 2000; index = rand.nextInt(dstbtn.s()); crnt = dstbtn.get(index); dstbtn.remove(index); char c = colToLabel(i); int x = 10 + 35 * i; int y = 10 + 35 * j; if (crnt < 40) { c[i][j] = new Track(c, j, x, y, elvtn); } if (crnt >= 40 && crnt < 120) { c[i][j] = new Water(c, j, x, y, elvtn); } if (crnt >= 120 && crnt < 280) { c[i][j] = new Grass(c, j, x, y, elvtn); } if (crnt >= 280 && crnt < 380) { c[i][j] = new Mountain(c, j, x, y, elvtn); } if (crnt >= 380 && crnt < 400) { c[i][j] = new Building(c, j, x, y); } } } } private char colToLabel(int col) { return (char) (col + 65); } private int labelToCol(char col) { return (int) col - 65; } public void paint(Graphics g, Point mPos) { doToEachCell((Cell c) -> c.paint(g, mPos)); } private Optional cAtColRow(int c, int r) { if (c >= 0 && c < c.length && r >= 0 && r < c[c].length) { return Optional.of(c[c][r]); } else { return Optional.empty(); } } public Optional cAtColRow(char c, int r) { return cAtColRow(labelToCol(c), r); } public Optional cellAtPoint(Point p) { for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { if (c[i][j].contains(p)) { return Optional.of(c[i][j]); } } } return Optional.empty(); } public List cInRange(char c1, int r1, char c2, int r2) { int c1i = labelToCol(c1); int c2i = labelToCol(c2); List output = new ArrayList(); for (int i = c1i; i <= c2i; i++) { for (int j = r1; j <= r2; j++) { cAtColRow(colToLabel(i), j).ifPrsnt(output::add); } } return output; } public void replaceCell(Cell old, Cell replacement) { c[labelToCol(old.col)][old.row] = replacement; } public void doToEachCell(Consumer func) { for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { func.accept(c[i][j]); } } } public void paintOverlay(Graphics g, List c, Color color) { g.setColor(color); for (Cell c : c) { g.fillRect(c.x + 2, c.y + 2, c.width - 4, c.height - 4); } } public List getRadius(Cell from, int s, boolean cnsdrElvtn) { int i = labelToCol(from.col); int j = from.row; Set inRds = new HashSet(); if (s > 0) { cAtColRow(colToLabel(i), j - 1).ifPrsnt(inRds::add); cAtColRow(colToLabel(i), j + 1).ifPrsnt(inRds::add); cAtColRow(colToLabel(i - 1), j).ifPrsnt(inRds::add); cAtColRow(colToLabel(i + 1), j).ifPrsnt(inRds::add); } for (Cell c : inRds.toArray(new Cell[0])) { if (from instanceof Building) { cnsdrElvtn = false; } if (cnsdrElvtn) { if (c instanceof Landscape) { Landscape here = (Landscape) from; Landscape there = (Landscape) c; if (here.elvtn() > there.elvtn()) { inRds.addAll(getRadius(c, s, true)); } else { inRds.addAll(getRadius(c, s - 1, true)); } } else { inRds.remove(c); } } else { inRds.addAll(getRadius(c, s - 1, false)); } } return new ArrayList(inRds); } @Override public CellIterator iterator() { return new CellIterator(c); } public String toString() { String retval = new String(); for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { retval = retval + c[i][j]; } } return retval; } } 94461/Code/InfoPanel.java 94461/Code/InfoPanel.java import java.awt.Graphics; import java.awt.Point; public abstract class InfoPanel { final int hTab = 10; final int vTab = 15; final int blockVtab = 35; final int mrgn = 21 * blockVtab; public void add(InfoPanel panel) { } public void remove(InfoPanel panel) { } public InfoPanel getChild(int i) { return null; } public void paint(Graphics g, int yloc, Point mouseLoc) {} } 94461/Code/Landscape.java 94461/Code/Landscape.java abstract class Landscape extends Cell { protected int h; protected float shd; public Landscape(char c, int r, int a, int b, int c) { super(c, r, a, b); if (c < -2000) { c = -2000; } if (c > 8000) { c = 8000; } h = c; shd = ((float) h + 2000) / 10000; } public int elevation() { return h; } } 94461/Code/LeftMostMove.java 94461/Code/LeftMostMove.java import java.util.List; class LeftMostMove implements MoveStrategy { @Override public Cell chooseNextLoc(Cell cLoc, List psblLocs) { Cell cLftMst; if (psblLocs.size() > 0) { cLftMst = psblLocs.get(0); for (Cell c : psblLocs) { if (c.leftOfComparison(cLftMst) < 0) { cLftMst = c; } } } else { cLftMst = cLoc; } return cLftMst; } public String toString() { return "left-most movement strategy"; } } 94461/Code/Main.java 94461/Code/Main.java import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.GraphicsEnvironment; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.time.Duration; import java.time.Instant; import javax.swing.JFrame; import javax.swing.JPanel; class Main extends JFrame { class App extends JPanel implements MouseListener { Stage stg; boolean stgBuilt = false; public App() { setPreferredSize(new Dimension(1024, 720)); this.addMouseListener(this); stg = StageReader.readStage(3); stgBuilt = true; } @Override public void paint(Graphics g) { if (stgBuilt && isVisible()) { stg.paint(g, getMousePosition()); } } @Override public void mouseClicked(MouseEvent e) { stg.mouseClicked(e.getX(), e.getY()); } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } } public static void main(String[] args) throws Exception { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); File fp2025 = new File("data/full_pack_2025.ttf"); ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, fp2025)); Main wnd = new Main(); wnd.run(); } private Main() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); App canvas = new App(); this.setContentPane(canvas); this.pack(); this.setVisible(true); } public void run() { while (true) { Instant sTime = Instant.now(); this.repaint(); Instant eTime = Instant.now(); long howLong = Duration.between(sTime, eTime).toMillis(); try { Thread.sleep(20L - howLong); } catch (InterruptedException e) { System.out.println("thread was interrupted, but who cares?"); } catch (IllegalArgumentException e) { System.out.println("application can't keep up with fra... | | | | | | | | | | | | | | | |