package pgdp.oop;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.io.File;
public abstract class Animal
protected int x, y;
static String filename;
protected File f;
protected Image image;
protected boolean alive;
protected static Animal antarktis;
public Animalint x, int y
this.x x;
this.y y;
public void move
TODO
if alive
if antarktisx 1 41 41y ! null antarktisxy 1 41 41 ! null
antarktisx 1 41y ! null antarktisxy 1 41 ! null
left
ifcanEatantarktisx 1 41 41y !antarktisx 1 41 41y 1 41.canEatthis
!antarktisx 2 41 41y.canEatthis
!antarktisx 1 41 41y 1 41 41.canEatthis
antarktisx 1 41 41y.alive false;
x x 1 41 41;
up
else if canEatantarktisxy 1 41 41 !antarktisxy 2 41 41.canEatthis
!antarktisx 1 41 41y 1 41 41.canEatthis
!antarktisx 1 41 41y 1 41.canEatthis
antarktisxy 1.alive false;
y y 1 41 41;
right
else if canEatantarktisx 1 41y !antarktisx 2 41y.canEatthis
!antarktisx 1 41y 1 41.canEatthis
!antarktisx 1 41y 1 41 41.canEatthis
antarktisx 1 41y.alive false;
x x 1 41;
down
else if canEatantarktisxy 1 41 !antarktisxy 2 41.canEatthis
!antarktisx 1 41 41y 1 41.canEatthis
!antarktisx 1 41y 1 41.canEatthis
antarktisx 1 41y.alive false;
y y 1 41;
else if antarktisx 1 41 41y null antarktisxy 1 41 41 null
antarktisx 1 41y null antarktisxy 1 41 null
left
if antarktisx 1 41 41y null
!antarktisx 1 41 41y 1 41.canEatthis
!antarktisx 2 41 41y.canEatthis
!antarktisx 1 41 41y 1 41 41.canEatthis
x x 1;
up
else if antarktisxy 1 41 41 null !antarktisxy 2 41 41.canEatthis
!antarktisx 1 41 41y 1 41 41.canEatthis
!antarktisx 1 41 41y 1 41.canEatthis
y y 1 41 41;
right
else if antarktisx 1 41y null !antarktisx 2 41y.canEatthis
!antarktisx 1 41y 1 41.canEatthis
!antarktisx 1 41y 1 41 41.canEatthis
x x 1 41;
down
else if antarktisxy 1 41 null !antarktisxy 2 41.canEatthis
!antarktisx 1 41 41y 1 41.canEatthis
!antarktisx 1 41y 1 41.canEatthis
y y 1 41;
else
x x;
y y;
public abstract boolean canEatAnimal animal;
protected abstract boolean eatenByPenguin penguin;
protected abstract boolean eatenByPlayerPenguin playerPenguin;
protected abstract boolean eatenByWhale whale;
protected abstract boolean eatenByLeopardSeal leopardSeal;
protected abstract boolean eatenByFish fish;
public static void setAntarktisAnimal antarktis
Animal.antarktis antarktis;
Graphics Stuff You dont have to do anything here
private void paintSymbolGraphics g, Color c, int height, int width
GradientPaint gradient new GradientPaint15, 0, c, width, 0, Color.LIGHTGRAY;
Graphics2D g.setPaintgradient;
Graphics2D g.setRenderingHintRenderingHints.KEYANTIALIASING, RenderingHints.VALUEANTIALIASON;
g.fillOvalint width 0.3, int height 0.3, int width 0.5,
int height 0.5;
public void drawGraphics g, int height, int width
if image null
paintSymbolg, Color.YELLOW, height, width;
return;
Graphics2D g.drawImageimage, 0, 0, width, height, 0, 0, image.getWidthnull,
image.getHeightnull, null;
package pgdp.oop;
import java.awt.event.WindowEvent;
public class Antarktis extends Maze
private static int width, height;
private static Penguin lostPenguin;
private static Whale whales new Whale5;
private static LeopardSeal leopardSeals new LeopardSeal20;
private static Fish fishes new Fish500;
private static PlayerPenguin playerPenguin;
public static void mainString args
width 41;
height 41;
antarktis generateMazewidth, height;
setupMaze;
draw;
gameLoop;
Close the opnend frame
closeFrame;
private static void gameLoop
boolean end false;
while !end
TODO maybe
draw;
TODO maybe
if currentEvent UP
playerPenguin.y playerPenguin.y 1;
end playerPenguin.moveplayerPenguin.x, playerPenguin.y 1;
else if currentEvent DOWN
playerPenguin.y playerPenguin.y 1;
end playerPenguin.moveplayerPenguin.x, playerPenguin.y 1;
else if currentEvent LEFT
playerPenguin.x playerPenguin.x 1;
end playerPenguin.moveplayerPenguin.x 1, playerPenguin.y;
else if currentEvent RIGHT
playerPenguin.x playerPenguin.x 1;
end playerPenguin.moveplayerPenguin.x 1, playerPenguin.y;
if end return;
else moveAll;
currentEvent NOTHING;
TODO maybe
private static void moveAll
TODO
playerPenguin.move
for int i 0; i whales.length; i
whalesi.move;
for int i 0; i leopardSeals.length; i
leopardSealsi.move;
lostPenguin.move;
for int i 0; i fishes.length; i
fishesi.move;
Example Setup for easier Testing during development
private static void setupMaze
int pos;
playerPenguin new PlayerPenguin3, 3;
antarktis33 playerPenguin;
for int i 0; i whales.length; i
pos getRandomEmptyField;
whalesi new Whalepos0, pos1;
antarktispos0pos1 whalesi;
for int i 0; i leopardSeals.length; i
pos getRandomEmptyField;
leopardSealsi new LeopardSealpos0, pos1;
antarktispos0pos1 leopardSealsi;
for int i 0; i fishes.length; i
pos getRandomEmptyField;
fishesi new Fishpos0, pos1;
antarktispos0pos1 fishesi;
antarktis2020 new Penguin20, 20;
lostPenguin Penguin antarktis2020;
package pgdp.oop;
import java.awt.Toolkit;
import java.io.File;
public class Fish extends Animal
static String filename fish.png;
public Fishint x, int y
superx, y;
f new Filefilename;
image Toolkit.getDefaultToolkit.getImagef.getAbsolutePath;
public void move
if alive
if antarktisx 1 41 41y null antarktisxy 1 41 41 null
antarktisx 1 41y null antarktisxy 1 41 null
up
if antarktisxy 1 41 41 null !antarktisxy 2 41 41.canEatthis
!antarktisx 1 41 41y 1 41 41.canEatthis
!antarktisx 1 41 41y 1 41.canEatthis
y y 1 41 41;
right
else if antarktisx 1 41y null !antarktisx 2y.canEatthis
!antarktisx 1 41y 1 41.canEatthis
!antarktisx 1 41y 1 41 41.canEatthis
x x 1 41;
down
else if antarktisxy 1 41 null !antarktisxy 2 41.canEatthis
!antarktisx 1 41 41y 1 41.canEatthis
!antarktisx 1 41y 1 41.canEatthis
y y 1 41;
left
else if antarktisx 1 41 41y null
!antarktisx 1 41 41y 1 41.canEatthis
!antarktisx 2 41 41y.canEatthis
!antarktisx 1 41 41y 1 41 41.canEatthis
x x 1 41 41;
else
x x;
y y;
public boolean canEatAnimal animal
return animal.eatenBythis;
protected boolean eatenByPenguin penguin
this.alive false;
antarktisxy null;
return true;
protected boolean eatenByPlayerPenguin playerPenguin
this.alive false;
antarktisxy null;
return true;
protected boolean eatenByWhale whale
return false;
protected boolean eatenByLeopardSeal leopardSeal
this.alive false;
antarktisxy null;
return true;
protected boolean eatenByFish fish
return false;
package pgdp.oop;
import java.awt.Toolkit;
import java.io.File;
public class LeopardSeal extends Animal
static String filename leopard.png;
public LeopardSealint x, int y
superx, y;
f new Filefilename;
image Toolkit.getDefaultToolkit.getImagef.getAbsolutePath;
public boolean canEatAnimal animal
return animal.eatenBythis;
protected boolean eatenByPenguin penguin
return false;
protected boolean eatenByPlayerPenguin playerPenguin
return false;
protected boolean eatenByWhale whale
this.alive false;
antarktisxy null;
return true;
protected boolean eatenByLeopardSeal leopardSeal
return false;
protected boolean eatenByFish fish
return false;
package pgdp.oop;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Maze extends MiniJava
public static boolean draw true;
protected static final int NOTHING 1;
protected static final int LEFT 1;
protected static final int UP 2;
protected static final int RIGHT 3;
protected static final int DOWN 4;
private static JFrame frame null;
static Animal antarktis;
private static int wannaSleep 100;
private static boolean pause false;
private static Random r new Random;
protected static int currentEvent NOTHING;
SuppressWarningsserial
private static class Field extends JPanel
Point p;
int x, y;
public Fieldint x, int y
this.x x;
this.y y;
p getLocation;
public void paintGraphics g
super.paintg;
GradientPaint gradient new GradientPaint0, 50, Color.WHITE, getWidth, 0, Color.GRAY;
Graphics2D g.setPaintgradient;
g.fillRectp.getLocation.x, p.getLocation.y, getWidth 2, getHeight;
if antarktisxy ! null
antarktisxy.drawg, getWidth, getHeight;
private void paintSymbolGraphics g, Color c
GradientPaint gradient new GradientPaint15, 0, c, getWidth, 0, Color.LIGHTGRAY;
Graphics2D g.setPaintgradient;
Graphics2D g.setRenderingHintRenderingHints.KEYANTIALIASING, RenderingHints.VALUEANTIALIASON;
g.fillOvalint getWidth 0.3, int getHeight 0.3, int getWidth 0.5,
int getHeight 0.5;
private static void setupint height, int width
JPanel content new JPanelnew GridLayoutwidth, height;
for int y 0; y antarktis0.length; y
for int x 0; x antarktis.length; x
content.addnew Fieldx, y;
frame new JFrameAMazeIng;
frame.getContentPane.addcontent;
frame.setSize400, 400 antarktis0.length antarktis.length;
frame.setDefaultCloseOperationJFrame.EXITONCLOSE;
frame.addComponentListenernew ComponentAdapter
Override
public void componentResizedComponentEvent e
frame.repaint;
;
frame.addKeyListenernew KeyAdapter
Override
public void keyPressedKeyEvent ke
switch ke.getKeyCode
case KeyEvent.VKLEFT:
currentEvent LEFT;
break;
case KeyEvent.VKRIGHT:
currentEvent RIGHT;
break;
case KeyEvent.VKUP:
currentEvent UP;
break;
case KeyEvent.VKDOWN:
currentEvent DOWN;
break;
default:
break;
;
frame.setVisibletrue;
public static void draw
if !draw antarktis.length 0
return;
if frame null
setup41, 41;
try
while pause
Thread.sleep50;
update;
Thread.sleepwannaSleep;
catch InterruptedException ie
Intentionally left blank
private static void update
frame.repaint;
public static Animal generateMazeint width, int height
Animal maze new Animalwidthheight;
for int i 0; i maze.length; i
for int j 0; j mazei.length; j
mazeij null;
return maze;
public static int getRandomEmptyField
int x, y;
do
x r.nextInt41;
y r.nextInt41;
while antarktisxy ! null;
return new intx, y;
public static void closeFrame
frame.dispatchEventnew WindowEventframe, WindowEvent.WINDOWCLOSING;
package pgdp.oop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.Random;
This class provides convenience methods to make the barrier of entry into
programming easier. In addition to that it realizes an educational
sublanguage to Java called Mini Java with simpler grammar.
p
iDiese Klasse stellt simple Methoden zur Verfgung um den Einstieg in die
Programmierung zu vereinfachen. Zustzlich dazu wird damit eine fr die Lehre
zusammengestellte SubProgrammiersprache von Java umgesetzt, die sich Mini
Java nennt und eine einfachere Grammatik besitzti
public class MiniJava
private static Random rand;
private static InputStream is System.in;
private static BufferedReader bufferedReader new BufferedReadernew InputStreamReaderis;
Reads a link String from the console, and prompts the user by printing the
given codetextcode with a line break to the console.
p
iLiest einen link String von der Konsole ein, und fordert den oder die
Benutzerin durch die Ausgabe des bergebenen codetextcodees zur Eingabe
auf mit Zeilenumbruch.i
param text the text to display on the console before reading an input.
return the input string or codenullcode, if no input is available
should normally not happen
public static String readStringString text
Exchange the reader in case System.in has changed.
This is necessary for testing, as for every test input, System.in is changed.
TODO: Consider whether a better solution is possible with mockinga different
InputStream implementation
on the test side.
if System.in ! is
is System.in;
bufferedReader new BufferedReadernew InputStreamReaderis;
try
System.out.printlntext;
return bufferedReader.readLine;
catch IOException e
We hide the exception in the method signature by rethrowing an unchecked
exception
throw new UncheckedIOExceptionKonnte Eingabe nicht lesen., e;
Reads a link String from the console, and prompts the user by printing the
line
pre
Eingabe:
pre
iLiest einen link String von der Konsole ein, und fordert den oder die
Benutzerin dazu auf durch die Ausgabe von
pre
Eingabe:
pre
i
public static String readString
return readStringEingabe:;
Tries to read an codeintcode from the console, and retires if the input
was not a valid integer. It prompts the user by printing the given
codetextcode with a line break to the console.
p
iVersucht einen codeintcodeWert von der Konsole einzulesen, und
wiederholt die Anfrage solange es nicht nicht um eine ganze Zahl handelt. Der
oder die Benutzerin wird durch die Ausgabe des bergebenen
codetextcodees zur Eingabe aufgefordert mit Zeilenumbruch.i
see IntegerparseIntString
public static int readIntString text
Integer x null;
do
String s readStringtext;
if s null
throw new IllegalStateExceptionEs ist keine Eingabe mehr verfgbar.;
try
x Integer.parseInts.trim;
catch SuppressWarningsunused NumberFormatException e
try again
while x null;
return x;
Works like link readIntString, but uses the following preset text to
prompt the user:
pre
Geben Sie eine ganze Zahl ein:
pre
iFunktioniert wie link readIntString, nutzt jedoch folgenden
vorgegebenen Text:
pre
Geben Sie eine ganze Zahl ein:
pre
i
public static int readInt
return readIntGeben Sie eine ganze Zahl ein:;
Identical to link readIntString.
p
iIdentisch zu link readIntStringi
public static int readString text
return readInttext;
Identical to link readInt.
p
iIdentisch zu link readInti
public static int read
return readInt;
Tries to read an codedoublecode from the console, and retires if the
input was not a valid double value. It prompts the user by printing the given
codetextcode with a line break to the console.
p
iVersucht einen codedoublecodeWert von der Konsole einzulesen, und
wiederholt die Anfrage solange es nicht nicht um eine gltige
codedoublecodeFliekommazahl handelt. Der oder die Benutzerin wird
durch die Ausgabe des bergebenen codetextcodees zur Eingabe
aufgefordert mit Zeilenumbruch.i
see DoubleparseDoubleString
public static double readDoubleString text
Double x null;
do
String s readStringtext;
if s null
throw new IllegalStateExceptionEs ist keine Eingabe mehr verfgbar.;
try
x Double.parseDoubles.trim;
catch SuppressWarningsunused NumberFormatException e
try again
while x null;
return x;
Works like link readDoubleString, but uses the following preset text to
prompt the user:
pre
Geben Sie eine Zahl ein:
pre
iFunktioniert wie link readDoubleString, nutzt jedoch folgenden
vorgegebenen Text:
pre
Geben Sie eine Zahl ein:
pre
i
public static double readDouble
return readDoubleGeben Sie eine Zahl ein:;
Prints the link String to the console and breaks the line.
p
iGibt den link String auf der Konsole aus und beginnt eine neue
Zeile.i
public static void writeString output
System.out.printlnoutput;
Prints the codeintcode to the console and breaks the line.
p
iGibt den codeintcodeWert auf der Konsole aus und beginnt eine neue
Zeile.i
public static void writeint output
write output;
Prints the codedoublecode to the console and breaks the line.
p
iGibt den codedoublecodeWert auf der Konsole aus und beginnt eine
neue Zeile.i
public static void writedouble output
write output;
Identical to link writeString.
p
iIdentisch zu link writeStringi
public static void writeLineConsoleString output
System.out.printlnoutput;
Identical to link writeint.
p
iIdentisch zu link writeinti
public static void writeLineConsoleint output
writeLineConsole output;
Identical to link writedouble.
p
iIdentisch zu link writedoublei
public static void writeLineConsoledouble output
writeLineConsole output;
Prints a line separator in the console.
p
iGibt einen Zeilenumbruch auf der Konsole aus.i
public static void writeLineConsole
writeLineConsole;
Prints the link String to the console. Does not end with a line break.
p
iGibt den link String auf der Konsole aus. Endet nicht mit einem
Zeilenumbruch.i
public static void writeConsoleString output
System.out.printoutput;
Prints the codeintcode to the console. Does not end with a line break.
p
iGibt den codeintcodeWert auf der Konsole aus. Endet nicht mit einem
Zeilenumbruch.i
public static void writeConsoleint output
writeConsole output;
Prints the codedoublecode to the console. Does not end with a line
break.
p
iGibt den codedoublecodeWert auf der Konsole aus. Endet nicht mit
einem Zeilenumbruch.i
public static void writeConsoledouble output
writeConsole output;
public static void resetRandom
rand null;
public static void setRandomWithSeedint seed throws IllegalAccessException
if rand ! null
throw new IllegalAccessExceptionDont touch this;
rand new Randomseed;
public static void setRandom throws IllegalAccessException
setRandomWithSeed144;
public static int randomIntint minval, int maxval
return minval rand.nextIntmaxval minval 1;
Draws a random card, represented by its value. The value ranges from 2
inclusive to 11 inclusive.
p
iZieht eine zufllige Karte, reprsentiert durch ihren Wert. Der Wert liegt
im Bereich 2 inklusive bis 11 inklusive.i
public static int drawCard
return randomInt2, 11;
Returns a random integer between 1 inclusive to 6 inclusive.
p
iGibt eine zufllige Zahl zwischen 1 inklusive bis 6 inklusive
zurck.i
public static int dice throws IllegalAccessException
if rand null
setRandom;
return randomInt1, 6;
package pgdp.oop;
import java.awt.Toolkit;
import java.io.File;
public class Penguin extends Animal
static String filename tux.png;
public Penguinint x, int y
superx, y;
f new Filefilename;
image Toolkit.getDefaultToolkit.getImagef.getAbsolutePath;
public boolean canEatAnimal animal
return animal.eatenBythis;
protected boolean eatenByPenguin penguin
return false;
protected boolean eatenByPlayerPenguin playerPenguin
return false;
protected boolean eatenByWhale whale
this.alive false;
antarktisxy null;
return true;
protected boolean eatenByLeopardSeal leopardSeal
this.alive false;
antarktisxy null;
return true;
protected boolean eatenByFish fish
return false;
package pgdp.oop;
public class PlayerPenguin extends Penguin
public PlayerPenguinint x, int y
superx, y;
public boolean canEatAnimal animal
return animal.eatenBythis;
public boolean moveint newX, int newY
game continues
if antarktisnewXnewY null this.canEatantarktisnewXnewY
this.x newX;
this.y newY;
return false;
win
else if antarktisnewXnewY.getClass.isAssignableFromPenguin.class
this.x newX;
this.y newY;
return true;
lose
else
this.x newX;
this.y newY;
return true;
package pgdp.oop;
import java.awt.Toolkit;
import java.io.File;
public class Whale extends Animal
static String filename whale.png;
public Whaleint x, int y
superx, y;
f new Filefilename;
image Toolkit.getDefaultToolkit.getImagef.getAbsolutePath;
public boolean canEatAnimal animal
return animal.eatenBythis;
protected boolean eatenByPenguin penguin
return false;
protected boolean eatenByPlayerPenguin playerPenguin
return false;
protected boolean eatenByWhale whale
return false;
protected boolean eatenByLeopardSeal leopardSeal
return false;
protected boolean eatenByFish fish
return false;