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_Assignment05_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>Assignment5A_ConnectFour</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,274 @@
package edu.kit.informatik;
import edu.kit.informatik.exceptions.IllegalMethodCallException;
/**
* The Class ConnectFourBoard.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class ConnectFourBoard {
private static final char FIRST_PLAYER = '0';
private static final char SECOND_PLAYER = '1';
private static final char EMPTY_BOARD = '-';
private static final int HEIGHT = 7;
private static final int WIDTH = 7;
/**
* The game data. Empty cells are represented as character that is stored in
* emptyBoard. Tokens are represented as characters that are stored in
* firstPlayer and secondPlayer.
*/
// data[row][column]
private final char[][] data;
/** Stores who's turn it is. */
private boolean isFirstPlayersTurn;
/** Stores the player who has won if a player has won. */
private char playerWon;
/**
* Instantiates a new connect four board.
*
* default configuration:
*
* firstPlayer is instantiate with '0'
* secondPlayer is instantiated with '1'
* emptyBoard is instantiated with '-'
* width is instantiated with 7;
* height is instantiated with 7
*/
public ConnectFourBoard() {
// initializing the board
playerWon = '-';
data = new char[HEIGHT][WIDTH];
isFirstPlayersTurn = true;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = EMPTY_BOARD;
}
}
}
/**
* Throws a token in the named column. The new token will be the character
* of the player who's turn it is and it will be placed one cell above the
* last thrown in token (row = height - existing tokens in column ).
*
* @param column
* the column the token should be thrown in
*/
public void throwIn(final int column) {
if (hasEnded()) {
throw new IllegalMethodCallException("Game has ended! Additional Turns are not allowed!");
} else if (column < 0 || column >= data[0].length) {
throw new IllegalArgumentException(
"The column is outside of the board. It has to be a number from 0 to " + (WIDTH - 1));
}
final int row = (HEIGHT - 1) - getTokenInColumn(column);
if (row < 0) {
throw new IllegalArgumentException("column is full");
}
char currentPlayer;
if (isFirstPlayersTurn) {
currentPlayer = FIRST_PLAYER;
} else {
currentPlayer = SECOND_PLAYER;
}
data[row][column] = currentPlayer;
// Check if someone has won
if (hasWonDiagonalTopLeft(row, column, currentPlayer) || haskWonVertically(row, column, currentPlayer)
|| hasWonHorizontal(row, column, currentPlayer) || hasWonDiagonalTopRight(row, column, currentPlayer)) {
playerWon = currentPlayer;
return;
}
isFirstPlayersTurn = !isFirstPlayersTurn;
}
private boolean haskWonVertically(final int row, final int column, final char currentPlayer) {
// Check below the token
int below = 0;
for (int i = 1; i <= 3; i++) {
if (isOnBoard(row + i, column) && data[row + i][column] == currentPlayer) {
below = i;
} else {
break;
}
}
// return true if more than four tokens are aligned vertically
return ((1 + below) >= 4);
}
private boolean hasWonHorizontal(final int row, final int column, final char currentPlayer) {
// Check horizontal
int horizontalLeft = 0;
for (int i = 1; i <= 3; i++) {
if (isOnBoard(row, column - i) && data[row][column - i] == currentPlayer) {
horizontalLeft = i;
} else {
break;
}
}
int horizontalRight = 0;
for (int i = 1; i <= 3; i++) {
if (isOnBoard(row, column + i) && data[row][column + i] == currentPlayer) {
horizontalRight = i;
} else {
break;
}
}
// return true if more than four tokens are aligned horizontally
return ((horizontalLeft + 1 + horizontalRight) >= 4);
}
private boolean hasWonDiagonalTopRight(final int row, final int column, final char currentPlayer) {
// Check diagonally topRight to bottomLeft
int topRight = 0;
for (int i = 1; i <= 3; i++) {
if (isOnBoard(row - i, column + i) && data[row - i][column + i] == currentPlayer) {
topRight = i;
} else {
break;
}
}
int bottomLeft = 0;
for (int i = 1; i <= 3; i++) {
if (isOnBoard(row + i, column - i) && data[row + i][column - i] == currentPlayer) {
bottomLeft = i;
} else {
break;
}
}
// return true if more than four tokens are aligned diagonally
return (topRight + 1 + bottomLeft >= 4);
}
private boolean hasWonDiagonalTopLeft(final int row, final int column, final char currentPlayer) {
// Check diagonally topLeft to bottomRight
int topLeft = 0;
for (int i = 1; i <= 3; i++) {
if (isOnBoard(row - i, column - i) && data[row - i][column - i] == currentPlayer) {
topLeft = i;
} else {
break;
}
}
int bottomRight = 0;
for (int i = 1; i <= 3; i++) {
if (isOnBoard(row + i, column + i) && data[row + i][column + i] == currentPlayer) {
bottomRight = i;
} else {
break;
}
}
// return true if more than four tokens are aligned diagonally
return (bottomRight + 1 + topLeft >= 4);
}
private int getTokenInColumn(final int column) {
for (int i = 0; i < data[column].length; i++) {
// Count elements starting from the bottom of the field
if (data[(HEIGHT - 1) - i][column] == '-') {
return i;
}
}
return HEIGHT;
}
/**
* Checks for if game has ended.
*
* @return true, if has ended
*/
public boolean hasEnded() {
if (playerWon == FIRST_PLAYER || playerWon == SECOND_PLAYER) {
return true;
} else {
return isFull();
}
}
/**
* Checks if the board is filled completely.
*
* @return true, if is full
*/
private boolean isFull() {
for (int i = 0; i < WIDTH; i++) {
if (getTokenInColumn(i) < HEIGHT) {
return false;
}
}
return true;
}
/**
* Checks if first player has won.
*
* @return true, if first player has won
*/
public boolean hasFirstPlayerWon() {
return (playerWon == FIRST_PLAYER);
}
/**
* Checks if second player has won.
*
* @return true, if seceond player has won
*/
public boolean hasSecondPlayerWon() {
return (playerWon == SECOND_PLAYER);
}
/**
* Gets one field/cell of the board.
*
* @param row
* the row or y-coordinate of the field
* @param column
* the column or x-coordinate of the field
* @return the field's value
*/
public char getField(final int row, final int column) {
if (!isOnBoard(row, column)) {
throw new IllegalArgumentException("Coordinates are not on the board.");
}
return data[row][column];
}
@Override
public String toString() {
String out = "";
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
out += Character.toString(data[y][x]);
}
out += "\n";
}
return out.trim();
}
/**
* Checks if coordinates are on board.
*
* @param row
* the row or y-coordinate
* @param column
* the column or x-coordinate
* @return true, if is on board
*/
public boolean isOnBoard(final int row, final int column) {
return !(row < 0 || row >= HEIGHT || column < 0 || column >= WIDTH);
}
}

View File

@@ -0,0 +1,29 @@
package edu.kit.informatik;
/**
* The Class ConnectFourGame.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class ConnectFourGame {
/** The input manager. */
private final InputManager inputManager;
/**
* Instantiates a new connect four game.
*/
public ConnectFourGame() {
inputManager = new InputManager();
}
/**
* Runs the game.
*/
public void run() {
while (!inputManager.isReadyToQuit()) {
inputManager.runCommand(Terminal.readLine());
}
}
}

View File

@@ -0,0 +1,112 @@
package edu.kit.informatik;
import edu.kit.informatik.exceptions.IllegalMethodCallException;
import edu.kit.informatik.exceptions.UserInputException;
/**
* The Class InputManager.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class InputManager {
private final String invalidCommand;
private final ConnectFourBoard board;
/** The boolean ready to quit used to store if game should end. */
private boolean readyToQuit;
/**
* Instantiates a new input manager.
*/
public InputManager() {
invalidCommand = "Error: Invalid command. Commands are: 'quit', "
+ "'field <row>,<column>', 'print', 'throwin <column>'";
board = new ConnectFourBoard();
readyToQuit = false;
}
/**
* Converts commands to actions that manipulates the Board.
*
* @param command
* the command as string
*/
public void runCommand(final String command) {
if (command == null) {
Terminal.printLine(invalidCommand);
return;
}
final String[] arr = command.split("[\\s]");
String[] parameters;
try {
parameters = arr[1].split("[\\,]");
} catch (final ArrayIndexOutOfBoundsException e) {
parameters = new String[0];
}
if (arr != null && arr[0] != null) {
try {
switch (arr[0]) {
case "quit":
if (parameters.length != 0) {
throw new UserInputException("quit expects zero parameters");
}
readyToQuit = true;
break;
case "field":
if (parameters.length != 2) {
throw new UserInputException("field expects two parameters: field <row>,<column>");
}
Terminal.printLine(Character.toString(
board.getField(Integer.parseInt(parameters[0]), Integer.parseInt(parameters[1]))));
break;
case "print":
if (parameters.length != 0) {
throw new UserInputException("print expects zero parameters");
}
Terminal.printLine(board.toString());
break;
case "throwin":
if (parameters.length != 1) {
throw new UserInputException("throwin expects one parameter: throwin <column>");
}
board.throwIn(Integer.parseInt(parameters[0]));
if (board.hasEnded()) {
if (board.hasFirstPlayerWon()) {
Terminal.printLine("P0 wins");
} else if (board.hasSecondPlayerWon()) {
Terminal.printLine("P1 wins");
} else {
Terminal.printLine("draw");
}
} else {
Terminal.printLine("success");
}
break;
default:
throw new UserInputException(invalidCommand);
}
} catch (final UserInputException e) {
Terminal.printLine("Error: " + e.getMessage());
} catch (final NumberFormatException e) {
Terminal.printLine("Error: expected number(s) as parameter(s)");
} catch (final IllegalArgumentException e) {
Terminal.printLine("Error: " + e.getMessage());
} catch (final IllegalMethodCallException e) {
Terminal.printLine("Error: " + e.getMessage());
}
}
}
/**
* Checks if is program ready to quit.
*
* @return true, if is ready to quit
*/
public boolean isReadyToQuit() {
return readyToQuit;
}
}

View File

@@ -0,0 +1,25 @@
package edu.kit.informatik;
/**
* 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) {
final ConnectFourGame game = new ConnectFourGame();
game.run();
}
}

View File

@@ -0,0 +1,64 @@
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,21 @@
package edu.kit.informatik.exceptions;
/**
* The Class IllegalMethodCallException.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class IllegalMethodCallException extends RuntimeException {
/**
* Instantiates a new illegal method call exception.
*
* @param string
* the string
*/
public IllegalMethodCallException(final String string) {
super(string);
}
}

View File

@@ -0,0 +1,21 @@
package edu.kit.informatik.exceptions;
/**
* The Class UserInputException.
*
* @author Hannes Kuchelmeister
* @version 1.0
*/
public class UserInputException extends Exception {
/**
* Instantiates a new user input exception.
*
* @param string
* the string
*/
public UserInputException(final String string) {
super(string);
}
}