added all past projects

This commit is contained in:
Hannes
2017-11-10 00:13:57 +01:00
parent 5f63f0c599
commit 8c94608805
1391 changed files with 109456 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<fileset name="all" enabled="true" check-config-name="Programmieren_Assignment04_WS_2015" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Assignment4A_LangtonAnt</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
</component>
</module>

View File

@@ -0,0 +1,63 @@
package edu.kit.informatik;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* This class provides some simple methods for input/output from and to a terminal.
*
* Never modify this class, never upload it to Praktomat. This is only for your local use. If an assignment tells you to
* use this class for input and output never use System.out or System.in in the same assignment.
*
* @author ITI, VeriAlg Group
* @author IPD, SDQ Group
* @version 4
*/
public final class Terminal {
/**
* BufferedReader for reading from standard input line-by-line.
*/
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
/**
* Private constructor to avoid object generation.
*/
private Terminal() {
}
/**
* Print a String to the standard output.
*
* The String out must not be null.
*
* @param out
* The string to be printed.
*/
public static void printLine(String out) {
System.out.println(out);
}
/**
* Reads a line from standard input.
*
* Returns null at the end of the standard input.
*
* Use Ctrl+D to indicate the end of the standard input.
*
* @return The next line from the standard input or null.
*/
public static String readLine() {
try {
return in.readLine();
} catch (IOException e) {
/*
* rethrow unchecked (!) exception to prevent students from being forced to use Exceptions before they have
* been introduced in the lecture.
*/
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,20 @@
package edu.kit.informatik.exceptions;
/**
* The Class FileNotCompalitbleException.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class FileNotCompalitbleException extends Exception {
/**
* Instantiates a new file not compalitble exception.
*
* @param str
* the str
*/
public FileNotCompalitbleException(final String str) {
super(str);
}
}

View File

@@ -0,0 +1,20 @@
package edu.kit.informatik.exceptions;
/**
* The Class GameHasEndedException.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class GameHasEndedException extends Exception {
/**
* Instantiates a new game has ended exception.
*
* @param message
* the message
*/
public GameHasEndedException(final String message) {
super(message);
}
}

View File

@@ -0,0 +1,21 @@
package edu.kit.informatik.exceptions;
/**
* The Class InvalidParameterException. (Custom class because imports from
* java.security are not allowed)
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class InvalidParameterException extends Exception {
/**
* Instantiates a new invalid parameter exception.
*
* @param message
* the message
*/
public InvalidParameterException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,152 @@
package edu.kit.informatik.langton;
/**
* The Class Ant.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Ant {
/** The x position. */
private int xPos;
/** The y position. */
private int yPos;
/** The direction in degrees (clockwise) starting north (0<>) */
private int direction;
/**
* Instantiates a new ant.
*
* @param xPos
* the x-position
* @param yPos
* the y-position
* @param direction
* the direction
*/
public Ant(final int xPos, final int yPos, final int direction) {
if ((direction % 90) == 0)
this.direction = direction;
else
this.direction = 0;
this.xPos = xPos;
this.yPos = yPos;
}
/**
* Turns the ant by a multiple of 90<39>.
*
* @param degrees
* the degrees to turn
*/
public void turn(final int degrees) {
if (degrees % 90 != 0)
throw new IllegalArgumentException();
direction += degrees;
if (direction >= 360)
direction = direction - 360;
else if (direction <= -360)
direction = 0;
}
/**
* Movees the Ant in the direction it is facing by one unit.
*/
public void move() {
/*
* cos(0<>) is one therefore yPos will become smaller by one if the ant
* is facing north.
* cos(180<38>) is minus one therefore xPos will become smaller by one if
* the ant is facing south.
* cos(90<39>) and cos(270<37>) are zero therefore yPos will not increase if
* the ant is facing east or west
*/
yPos -= Math.round(Math.cos(Math.toRadians(direction)));
/*
* sin(90<39>) is one therefore xPos will become bigger by one if the ant
* is facing east.
* cos(270<37>) is minus one therefore xPos will become bigger by one if
* the ant is facing west.
* cos(0<>) and cos(180<38>) are zero therefore xPos will not increase if
* the ant is facing south or north.
*/
xPos += Math.round(Math.sin(Math.toRadians(direction)));
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return yPos + "," + xPos;
}
/**
* Gets the x pos.
*
* @return the x pos
*/
public int getXPos() {
return xPos;
}
/**
* Gets the y pos.
*
* @return the y pos
*/
public int getYPos() {
return yPos;
}
/**
* Sets the x pos.
*
* @param xPos
* the new x pos
*/
public void setXPos(final int xPos) {
this.xPos = xPos;
}
/**
* Sets the y pos.
*
* @param yPos
* the new y pos
*/
public void setYPos(final int yPos) {
this.yPos = yPos;
}
/**
* Gets the direction as string. Which converts 90 to O, 180 to S, 270 to W
* and everything else to N
*
* @return the direction as string
*/
public String getDirectionAsString() {
String ret = "N";
switch (direction) {
case 90:
ret = "O";
break;
case 180:
ret = "S";
break;
case 270:
ret = "W";
break;
default:
ret = "N";
}
return ret;
}
}

View File

@@ -0,0 +1,146 @@
package edu.kit.informatik.langton;
import edu.kit.informatik.exceptions.GameHasEndedException;
import edu.kit.informatik.exceptions.InvalidParameterException;
/**
* The Class Board.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Board {
/** The data. */
private Field[][] data;
/** The ant. */
private Ant ant;
/**
* Instantiates a new board.
*
* @param data
* the data of the board
* @param ant
* the ant
* @throws IllegalArgumentException
* if arguments that are used to instanziate Board have an error
*/
public Board(final Field[][] data, final Ant ant) throws IllegalArgumentException {
if (ant == null)
throw new IllegalArgumentException("ant needs to be initialized");
else if (data == null)
throw new IllegalArgumentException("data can't be null");
this.ant = ant;
this.data = data;
this.data[ant.getYPos()][ant.getXPos()] = Field.White;
}
/**
* Make turn.
*
* @throws GameHasEndedException
* throws exception if a new turn is not possible
*/
public void makeTurn() throws GameHasEndedException {
ant.move();
if (ant.getXPos() < 0 || ant.getXPos() >= data[0].length || ant.getYPos() < 0 || ant.getYPos() >= data.length)
leftBoard();
// Turning and coloring
if (data[ant.getYPos()][ant.getXPos()].equals(Field.Black)) {
ant.turn(270);
data[ant.getYPos()][ant.getXPos()] = Field.White;
} else if (Field.White.equals(data[ant.getYPos()][ant.getXPos()])) {
ant.turn(90);
data[ant.getYPos()][ant.getXPos()] = Field.Black;
}
}
/**
* Left board.
*
* @throws GameHasEndedException
* the exception is thrown if game needs to end
*/
protected void leftBoard() throws GameHasEndedException {
throw new GameHasEndedException("Game ended");
}
/**
* Gets the ant.
*
* @return the ant
*/
public Ant getAnt() {
return ant;
}
/**
* Gets the field at position x,y as string.
*
* @param x
* the x position
* @param y
* the y position
* @return the field as string
* @throws InvalidParameterException
* thrown if x or y outside of bounds of data array invalid
* parameter exception
*/
public String getFieldAsString(final int x, final int y) throws InvalidParameterException {
if (x < 0 || x >= data[0].length || y < 0 || y >= data.length)
throw new InvalidParameterException("x or y is to large");
String ret = "";
if (x == ant.getXPos() && y == ant.getYPos())
ret += ant.getDirectionAsString();
else
ret += data[y][x];
return ret;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String ret = "";
for (int y = 0; y < data.length; y++) {
for (int x = 0; x < data[y].length; x++) {
try {
ret += getFieldAsString(x, y);
} catch (InvalidParameterException e) {
e.printStackTrace();
}
}
ret += "\n";
}
return ret.trim();
}
/**
* @return the width of the board
*/
public int getWidth() {
if (data == null || data.length == 0)
return 0;
else
return data[0].length;
}
/**
* @return the height of the board
*/
public int getHeight() {
if (data == null)
return 0;
else
return data.length;
}
}

View File

@@ -0,0 +1,28 @@
package edu.kit.informatik.langton;
/**
* The Enum Field.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public enum Field {
/** field is white. */
White,
/** field is black. */
Black;
/*
* (non-Javadoc)
*
* @see java.lang.Enum#toString()
*/
@Override
public String toString() {
if (this.equals(Field.White))
return "" + 0;
else
return "" + 1;
}
}

View File

@@ -0,0 +1,70 @@
package edu.kit.informatik.langton;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import edu.kit.informatik.Terminal;
/**
* Helper class for reading text files.
*
* @author IPD Reussner, KIT
* @author ITI Sinz, KIT
* @version 1.1
*/
public final class FileInputHelper {
/**
* Private constructor to avoid instantiation.
*/
private FileInputHelper() {
// intentionally left blank
}
/**
* Reads the specified file and returns its content as a String array, where
* the first array field contains the
* file's first line, the second field contains the second line, and so on.
*
* @param file
* the file to be read
* @return the content of the file
*/
public static String[] read(final String file) {
final StringBuilder result = new StringBuilder();
FileReader in = null;
try {
in = new FileReader(file);
} catch (final FileNotFoundException e) {
Terminal.printLine("Error, " + e.getMessage());
System.exit(1);
}
final BufferedReader reader = new BufferedReader(in);
try {
String line = reader.readLine();
while (line != null) {
result.append(line);
line = reader.readLine();
if (line != null) {
result.append("\n");
}
}
} catch (final IOException e) {
Terminal.printLine("Error, " + e.getMessage());
System.exit(1);
} finally {
try {
reader.close();
} catch (final IOException e) {
// no need for handling this exception
}
}
return result.toString().split("\n");
}
}

View File

@@ -0,0 +1,99 @@
package edu.kit.informatik.langton;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.exceptions.FileNotCompalitbleException;
/**
* The Class LangtonGame.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class LangtonGame {
/**
* User input manager which manages input from the user and manipulates the
* game accordingly.
*/
private UserInputManager uInputMngr;
/**
* Instantiates a new langton game.
*
* @param path
* the path
* @param gameType
* the game type
*/
public LangtonGame(final String path, final String gameType) {
try {
uInputMngr = new UserInputManager(readFile(path, gameType));
} catch (IllegalArgumentException | FileNotCompalitbleException e) {
Terminal.printLine("Error: " + e.getMessage());
}
}
/**
* Runs the gameloop.
*
* @throws NullPointerException
* game has not been initilized correctly
*/
public void run() throws NullPointerException {
if (uInputMngr == null)
throw new NullPointerException("game cannot be run because UserInputManager has not bean set");
while (!uInputMngr.isReadyToQuit()) {
uInputMngr.doCommand(Terminal.readLine());
}
}
private Board readFile(final String path, final String gameType)
throws FileNotCompalitbleException, IllegalArgumentException {
final String[] file = FileInputHelper.read(path);
Field[][] fieldArr;
final int width = file[0].length();
final int height = file.length;
Ant ant = null;
fieldArr = new Field[height][width];
for (int i = 0; i < file.length; i++) {
final char[] c = file[i].toCharArray();
if (c.length != width)
throw new FileNotCompalitbleException("file is not formatted correctly");
for (int j = 0; j < c.length; j++) {
switch (c[j]) {
case 'N':
ant = new Ant(j, i, 0);
fieldArr[i][j] = Field.White;
break;
case 'E':
ant = new Ant(j, i, 90);
fieldArr[i][j] = Field.White;
break;
case 'S':
ant = new Ant(j, i, 180);
fieldArr[i][j] = Field.White;
break;
case 'W':
ant = new Ant(j, i, 270);
fieldArr[i][j] = Field.White;
break;
case '0':
fieldArr[i][j] = Field.White;
break;
case '1':
fieldArr[i][j] = Field.Black;
break;
default:
throw new FileNotCompalitbleException("file is not formatted correctly");
}
}
}
if (gameType.equals("torus"))
return new Torusboard(fieldArr, ant);
else if (gameType.equals("standard"))
return new Board(fieldArr, ant);
else
throw new IllegalArgumentException("gameType has to be either 'torus' or 'standard'");
}
}

View File

@@ -0,0 +1,35 @@
package edu.kit.informatik.langton;
import edu.kit.informatik.Terminal;
/**
* The Class Main.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public final class Main {
private Main() {
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(final String[] args) {
LangtonGame lg;
try {
lg = new LangtonGame(args[0], args[1]);
lg.run();
} catch (ArrayIndexOutOfBoundsException e) {
Terminal.printLine("Error: please use 2 arguments to start the program ('path', 'torus' | 'standard')");
} catch (NullPointerException e) {
Terminal.printLine("Error: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,46 @@
package edu.kit.informatik.langton;
import edu.kit.informatik.exceptions.GameHasEndedException;
/**
* The Class Torusboard extends Board.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class Torusboard extends Board {
/**
* Instantiates a new torusboard.
*
* @param data
* the data of the board
* @param ant
* the ant
* @throws IllegalArgumentException
* if arguments that are used to instanziate Board have an error
*/
public Torusboard(final Field[][] data, final Ant ant) throws IllegalArgumentException {
super(data, ant);
}
/*
* (non-Javadoc)
*
* @see edu.kit.informatik.langton.Board#leftBoard()
*/
@Override
protected void leftBoard() throws GameHasEndedException, IllegalArgumentException {
if (this.getAnt().getXPos() >= this.getWidth()) {
this.getAnt().setXPos(this.getAnt().getXPos() - this.getWidth());
} else if (this.getAnt().getYPos() >= this.getHeight()) {
this.getAnt().setYPos(this.getAnt().getYPos() - this.getHeight());
} else if (this.getAnt().getXPos() < 0) {
this.getAnt().setXPos(getWidth() + this.getAnt().getXPos());
} else if (this.getAnt().getYPos() < 0) {
this.getAnt().setYPos(getHeight() + this.getAnt().getYPos());
} else {
throw new IllegalArgumentException("leftBoard() should not have been called");
}
}
}

View File

@@ -0,0 +1,115 @@
package edu.kit.informatik.langton;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.exceptions.GameHasEndedException;
import edu.kit.informatik.exceptions.InvalidParameterException;
/**
* The Class UserInputManager.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class UserInputManager {
/** The board. */
private final Board board;
/** The boolean ready to quit used to store if game should end. */
private boolean readyToQuit;
/**
* Instantiates a new user input manager.
*
* @param board
* the board
*/
public UserInputManager(final Board board) {
this.board = board;
readyToQuit = false;
}
/**
* Converts commands to actions that manipulates the Board.
*
* @param command
* the command as string
*/
public void doCommand(final String command) {
final String[] arr = command.split("[\\s,\\,]+");
if (arr != null && arr[0] != null) {
try {
switch (arr[0]) {
case "move":
if (arr.length != 2) {
throw new InvalidParameterException("move expects 1 parameter");
}
final int turnNumber = Integer.parseInt(arr[1]);
for (int i = 0; i < turnNumber; i++) {
board.makeTurn();
}
break;
case "print":
if (arr.length != 1) {
throw new InvalidParameterException("print expects 0 parameter");
}
Terminal.printLine(board.toString());
break;
case "position":
if (arr.length != 1) {
throw new InvalidParameterException("position expects 0 parameters");
}
Terminal.printLine(board.getAnt().toString());
break;
case "field":
if (arr.length != 3) {
throw new InvalidParameterException("field expects 2 parameters");
}
final int column = Integer.parseInt(arr[1]);
final int row = Integer.parseInt(arr[2]);
try {
Terminal.printLine(board.getFieldAsString(row, column));
} catch (final edu.kit.informatik.exceptions.InvalidParameterException e) {
throw new InvalidParameterException(
"coordinates are outside the board they have to be from 0 to "
+ (board.getHeight() - 1) + " for the first parameter and from 0 to "
+ (board.getWidth() - 1) + " for the second parameter");
}
break;
case "direction":
if (arr.length != 1) {
throw new InvalidParameterException("print expects 0 parameters");
}
Terminal.printLine(board.getAnt().getDirectionAsString());
break;
case "quit":
if (arr.length != 1) {
throw new InvalidParameterException("quit expects 0 parameter");
}
readyToQuit = true;
break;
default:
throw new InvalidParameterException(
"Invalid command. Commands are: 'quit', 'direction', 'field <i> <j>', "
+ "'position', 'print', 'move <numberOfTurns>'");
}
} catch (final InvalidParameterException e) {
Terminal.printLine("Error: " + e.getMessage());
} catch (final GameHasEndedException e) {
readyToQuit = true;
} catch (final NumberFormatException e) {
Terminal.printLine("Error: Expected Number");
}
}
}
/**
* Checks if is program ready to quit.
*
* @return true, if is ready to quit
*/
public boolean isReadyToQuit() {
return readyToQuit;
}
}