fix java shit

This commit is contained in:
Felix Steghofer
2022-12-04 01:45:18 +01:00
parent 3db4f94e6a
commit c3f4512037
38 changed files with 5 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,121 @@
package reversi.model;
/**
* Interface for a Reversi game, also known as Othello.
*
* A human plays against the machine.
*/
public interface Board extends Cloneable {
/**
* The number of rows and columns of the game grid. Originally 8.
* Here, even and at least 4.
*/
int SIZE = 8;
/**
* Get the player who should start or already has started the game.
*
* @return The player who makes the initial move.
*/
Player getFirstPlayer();
/**
* Get the player who owns the next game turn.
*
* @return The player who is allowed to make the next turn.
*/
Player next();
/**
* Execute a human move.
*
* @param row The slot's row number where a tile of the human player should
* be placed on.
* @param col The slot's column number where a tile of the human player
* should be placed on.
* @return {@code true} if and only if move was successful, e.g., the
* defined slot was not occupied and at least one tile of the
* machine was reversed.
* @throws IllegalMoveException If the game is already over, or it is not
* the human's turn.
* @throws IllegalArgumentException If the provided parameters are
* invalid, e.g., the defined slot was is not on the grid.
*/
boolean move(int row, int col);
/**
* Execute a machine move.
*
* @throws IllegalMoveException If the game is already over, or it is not
* the machine's turn.
*/
void machineMove();
/**
* Set the skill level of the machine.
*
* @param level The skill as a number, must be at least 1.
*/
void setLevel(int level);
/**
* Check if the game is over. Either one player has won or there is a tie,
* i.e., no player can perform a move any more.
*
* @return {@code true} if and only if the game is over.
*/
boolean gameOver();
/**
* Check if the game state is won.
*
* @return The winner or nobody in case of a tie.
*/
Player getWinner();
/**
* Get the number of human tiles currently placed on the grid.
*
* @return The number of human tiles.
*/
int getNumberOfHumanTiles();
/**
* Get the number of machine tiles currently placed on the grid.
*
* @return The number of machine tiles.
*/
int getNumberOfMachineTiles();
/**
* Get the content of the slot at the specified coordinates. Either it
* contains a tile of one of the two players already or it is empty.
*
* @param row The row of the slot in the game grid.
* @param col The column of the slot in the game grid.
* @return The slot's content.
*/
Player getSlot(int row, int col);
/**
* Deep-copy the board.
*
* @return A clone.
*/
Board clone();
/**
* Get the string representation of this board as row x column matrix. Each
* slot is represented by one the three chars '.', 'X', or 'O'. '.' means
* that the slot currently contains no tile. 'X' means that it contains a
* tile of the human player. 'O' means that it contains a machine tile. In
* contrast to the rows, the columns are whitespace separated.
*
* @return The string representation of the current Reversi game.
*/
@Override
String toString();
}

Binary file not shown.

View File

@@ -0,0 +1,42 @@
package reversi.model;
/**
* Class for a reversi field.
*/
public class Field {
/**
* <code>Row</code> of the field.
*/
private int row;
/**
* <code>Column</code> of the field.
*/
private int col;
/**
* Constructs a new field.
*
* @param row row of the field
* @param col column of the field
*/
public Field(final int row, final int col) {
this.row = row;
this.col = col;
}
/**
*
* @return The row of the field.
*/
public final int row() {
return row;
}
/**
*
* @return the column of the field.
*/
public final int col() {
return col;
}
}

Binary file not shown.

View File

@@ -0,0 +1,164 @@
package reversi.model;
import java.util.LinkedList;
/**
* Class for a gamenode. Is needed to build a gametree for e.g. minmax
* algorithm. A node represents a virtual move.
*
*/
public class GameNode {
/**
* Reversi-board of the node.
*/
private Reversi state;
/**
* Scoring of the node.
*/
private double score;
/**
* The Move.
*/
private Field move;
/**
* Children of the node.
*/
private LinkedList<GameNode> children;
/**
* Constructor for the root node.
*
* @param move Root move
* @param state Board of the root node
*/
GameNode(final Field move, final Reversi state) {
this.move = move;
this.state = state;
this.children = new LinkedList<GameNode>();
}
/**
* Constructor for node.
*
* @param move Move of node
* @param state Board of node
* @param parent Parent of node
*/
GameNode(final Field move, final Reversi state, final GameNode parent) {
this(move, state);
parent.children.add(this);
}
/**
*
* @param score Score to set
*/
public final void setScore(final double score) {
this.score = score;
}
/**
*
* @return Score
*/
public final double getScore() {
return score;
}
/**
*
* @return Board
*/
public final Reversi getState() {
return state;
}
/**
*
* @return Move
*/
public final Field getMove() {
return move;
}
/**
*
* @return Children
*/
public final LinkedList<GameNode> getChildren() {
return children;
}
/**
* Is needed to calculate the combined score of the parent node.
*
* @return GameNode with highest score
*/
public final GameNode getChildrensHighestScore() {
GameNode highest = children.getFirst();
for (GameNode node : children) {
if (node.getScore() > highest.getScore()) {
highest = node;
}
}
return highest;
}
/**
* Is needed to calculate the combined score of the parent node.
*
* @return GameNode with lowest score
*/
public final GameNode getChildrensLowestScore() {
GameNode lowest = children.getFirst();
for (GameNode node : children) {
if (node.getScore() < lowest.getScore()) {
lowest = node;
}
}
return lowest;
}
/**
*
* @return If node has at least one child
*/
public final boolean hasChild() {
return (children.size() > 0);
}
/**
* Set children of a node.
*/
public final void fillNode() {
for (Field move : generateChildren()) {
Reversi tmpBoard = this.getState().clone();
tmpBoard.performMove(move.row(), move.col());
new GameNode(move, tmpBoard, this);
}
}
/**
*
* @return <code>Linkedlist</code> with possible moves in the
* current reversi-board.
*/
private LinkedList<Field> generateChildren() {
LinkedList<Field> movesList = new LinkedList<Field>();
for (int row = 0; row < Board.SIZE; row++) {
for (int col = 0; col < Board.SIZE; col++) {
if (getState().isValidMove(row, col)) {
movesList.add(new Field(row, col));
}
}
}
return movesList;
}
}

Binary file not shown.

View File

@@ -0,0 +1,75 @@
package reversi.model;
/**
* Class for a GameTree.
*/
public class GameTree {
/**
* Root gamenode of the gametree.
*/
private GameNode root;
/**
* Constructs a new gametree.
*
* @param board of the root node
* @param level depth of the tree
*/
GameTree(final Reversi board, final int level) {
this.root = new GameNode(new Field(0, 0), board.clone());
fillTree(root, level);
generateScores(root);
}
/**
*
* @return Root of the gametree.
*/
public final GameNode getRoot() {
return root;
}
/**
* Fill the Tree with nodes.
*
* @param node Node to fill (in most cases root gamenode)
* @param depth depth of the tree (level)
*/
private void fillTree(final GameNode node, final int depth) {
if (depth > 0) {
node.fillNode();
for (GameNode n : node.getChildren()) {
fillTree(n, (depth - 1));
}
}
}
/**
* Generates and sets the score of a Gamenode.
*
* @param node Gamenode to set
*/
public final void generateScores(final GameNode node) {
Reversi currentBoard = node.getState();
if (!node.hasChild()) {
node.setScore(currentBoard.score());
} else {
double score;
score = currentBoard.score();
for (GameNode child : node.getChildren()) {
generateScores(child);
}
if (!currentBoard.next().getType()) {
score += node.getChildrensHighestScore().getScore();
} else {
score += node.getChildrensLowestScore().getScore();
}
node.setScore(score);
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,31 @@
package reversi.model;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* Class for a Illegal move in reversi game.
*/
public class IllegalMoveException extends RuntimeException {
private static final long serialVersionUID = 240330120795325167L;
/**
* Prints an error or plays a "beep" if a illegal move is made.
* A beep is more common but not so easy to implement for various
* OS, so the sound is only played on Windows XP or 7.
*
* @param s Error to print
*/
public IllegalMoveException(final String s) {
if (System.getProperty("os.name").equals("Windows 7")
|| System.getProperty("os.name").equals("Windows XP")) {
Toolkit.getDefaultToolkit().beep();
} else {
JFrame screen = new JFrame();
JOptionPane.showMessageDialog(screen, s);
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,27 @@
package reversi.model;
/**
* Class for a player of reversi.
*/
public class Player {
/**
* Indicates if a player is human.
*/
private Boolean isHuman;
/**
* Constructs a new player.
*
* @param isHuman is the player human?
*/
public Player(final boolean isHuman) {
this.isHuman = isHuman;
}
/**
*
* @return Is player human?
*/
public final boolean getType() {
return isHuman;
}
}

Binary file not shown.

View File

@@ -0,0 +1,527 @@
package reversi.model;
/**
* Class for reversi game. A human player tries to beat a bot with modifiable
* difficulty level.
*/
public class Reversi implements Board {
private Player bot;
private Player human;
/**
* Player with the next move.
*/
private Player currentPlayer;
private Player firstPlayer;
/**
* Playboard saves the tiles.
*/
private Player[][] playBoard;
/**
* Gametree for the bot to calculate future moves.
*/
private GameTree gameTree;
/**
* Vectors to traverse the 8 directions around a tile.
*/
private Field[] vectors;
/**
* Saves if game is over.
*/
private boolean gameOver;
/**
* Standard weighting of the playboard. Used by the bot to calculate
* gametree.
*/
private double[][] weighting = new double[][]
{{9999, 5, 500, 200, 200, 500, 5, 9999},
{5, 1, 50, 150, 150, 50, 1, 5},
{500, 50, 250, 100, 100, 250, 50, 500},
{200, 150, 100, 50, 50, 100, 150, 200},
{200, 150, 100, 50, 50, 100, 150, 200},
{500, 50, 250, 100, 100, 250, 50, 500},
{5, 1, 50, 150, 150, 50, 1, 5},
{9999, 5, 500, 200, 200, 500, 5, 9999}};
/**
* Difficulty level for the human. Represents the depth of the gametree.
*/
private int level = 3;
/**
* Constructs a new reversi game.
*
* @param human Human player
* @param bot Machine player
* @param firstPlayer Player that starts
*/
public Reversi(Player human, Player bot, Player firstPlayer) {
this.bot = bot;
this.human = human;
this.firstPlayer = firstPlayer;
this.playBoard = setInitialPositions();
currentPlayer = firstPlayer;
this.vectors = generateVectors();
}
/**
* {@inheritDoc}
*/
public final Player getFirstPlayer() {
return firstPlayer;
}
/**
* {@inheritDoc}
*/
public final Player next() {
return currentPlayer;
}
/**
* {@inheritDoc}
*/
public final boolean move(final int row, final int col) {
if (currentPlayer != human) {
return false;
}
if (!isValidMove(row, col)) {
new IllegalMoveException("Error! invalid move at ("
+ (row + 1) + ", " + (col + 1) + ").");
return false;
}
performMove(row, col);
return true;
}
/**
* {@inheritDoc}
*/
public final void machineMove() {
if (currentPlayer != bot) {
return;
}
gameTree = new GameTree(this, level);
Field bestMove = gameTree.getRoot()
.getChildrensHighestScore().getMove();
performMove(bestMove.row(), bestMove.col());
}
/**
* {@inheritDoc}
*/
public final void setLevel(final int level) {
this.level = level;
}
/**
* {@inheritDoc}
*/
public final boolean gameOver() {
return gameOver;
}
/**
* {@inheritDoc}
*/
public final Player getWinner() {
if (getNumberOfHumanTiles() > getNumberOfMachineTiles()) {
return human;
} else if (getNumberOfHumanTiles() == getNumberOfMachineTiles()) {
return null;
} else {
return bot;
}
}
/**
* {@inheritDoc}
*/
public final int getNumberOfHumanTiles() {
return numberOfTiles(human);
}
/**
* {@inheritDoc}
*/
public final int getNumberOfMachineTiles() {
return numberOfTiles(bot);
}
/**
* {@inheritDoc}
*/
public final int getNumberOfTilesSet() {
int n = (getNumberOfHumanTiles() + getNumberOfMachineTiles());
return n;
}
/**
* {@inheritDoc}
*/
public final Player getSlot(final int row, final int col) {
return playBoard[row][col];
}
@Override
public final Reversi clone() {
Reversi boardCopy = new Reversi(human, bot, firstPlayer);
boardCopy.currentPlayer = currentPlayer;
Player[][] playBoardCopy = new Player[SIZE][SIZE];
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
playBoardCopy[row][col] = playBoard[row][col];
}
}
boardCopy.playBoard = playBoardCopy;
return boardCopy;
}
/**
* Generate the vectors for the 8 directions. array[0] = 0°,
* array[1] = 45°...
*
* @return <code>boolean-array</code> with all vector fields
*/
private Field[] generateVectors() {
Field[] vector = {
new Field(-1, 0),
new Field(-1, +1),
new Field(0, +1),
new Field(+1, +1),
new Field(+1, 0),
new Field(+1, -1),
new Field(0, -1),
new Field(-1, -1)};
return vector;
}
/**
* Sets two tiles/player at the beginning of the match in the middle of
* of the playboard.
*
* @return <code>Player-array</code>
*/
private Player[][] setInitialPositions() {
Player[][] playBoard = new Player[SIZE][SIZE];
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (((SIZE / 2) - 1 == row || SIZE / 2 == row)
&& ((SIZE - col - 1) == row)) {
playBoard[row][col] = firstPlayer;
} else if ((SIZE / 2 - 1 == row || SIZE / 2 == row)
&& (col == row)) {
playBoard[row][col] = getEnemy(firstPlayer);
} else {
playBoard[row][col] = null;
}
}
}
return playBoard;
}
/**
*
* @param self Player
* @return Enemy of the Player
*/
private Player getEnemy(final Player self) {
if (self == human) {
return bot;
} else {
return human;
}
}
/**
* Switches the currentplayer and checks if the next player can do a valid
* move. If both players cannot move the game is over.
* game is over
*/
private void switchPlayer() {
currentPlayer = getEnemy(currentPlayer);
if (!movePossible()) {
currentPlayer = getEnemy(currentPlayer);
if (!movePossible()) {
gameOver = true;
}
}
}
/**
* Switch a tile to the current player.
*
* @param row Row of field
* @param col Column of field
*/
private void switchTile(final int row, final int col) {
playBoard[row][col] = currentPlayer;
}
/**
* Switch all corresponding enemy tiles around a <code>field</code>.
*
* @param row Row of field
* @param col Column of field
*/
private void switchTiles(final int row, final int col) {
boolean[] surrounders = validMoves(row, col);
switchTile(row, col);
for (int i = 0; i < SIZE; i++) {
if (surrounders[i]) {
int x = row + vectors[i].row();
int y = col + vectors[i].col();
while (playBoard[x][y] != currentPlayer) {
switchTile(x, y);
x = x + vectors[i].row();
y = y + vectors[i].col();
}
}
}
}
private int numberOfTiles(Player player) {
int mount = 0;
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (playBoard[row][col] == player) {
mount++;
}
}
}
return mount;
}
/**
*
* @param row Row of the field
* @param col Column of the field
* @return <code>true</code> if field with <code>row</code> and
* <code>col</code> is not set by one of the
* player <br /> else if field is set
*/
private boolean isFree(final int row, final int col) {
if (playBoard[row][col] == null) {
return true;
}
return false;
}
/**
* Checks the hole playBoard if the current player can do a valid move.
*
* @return <code>true</code> if another move is possible.
*/
private boolean movePossible() {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (isValidMove(row, col)) {
return true;
}
}
}
return false;
}
/**
* Walk the board in a direction.
*
* @param row Row of field
* @param col Column of field
* @param x row direction to walk
* @param y column direction to walk
* @return if the direction has a valid move
*/
private boolean boardWalker(int row, int col, final int x, final int y) {
row = row + x;
col = col + y;
int counter = 0;
while (row < SIZE && row >= 0 && col < SIZE && col >= 0) {
if (playBoard[row][col] == null) {
return false;
} else if (playBoard[row][col] == currentPlayer) {
return (counter > 0);
}
row = row + x;
col = col + y;
counter++;
}
return false;
}
/**
* Checks a boolean array if it contains a value that is true.
*
* @param array Array
* @return <code>true</code> if any value is true
*/
private boolean checkBoolArray(final boolean[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i]) {
return true;
}
}
return false;
}
/**
*
* @param row Row of field
* @param col Column of field
* @return if the currentPlayer can do a move on field(row, col)
*/
final boolean isValidMove(final int row, final int col) {
return isFree(row, col)
&& checkBoolArray(validMoves(row, col));
}
/**
* Searches a field for valid moves.
*
* @param row Row of field
* @param col Column of field
* @return <code>Array</code> of enemy tiles: <br />
* array[0] is at 0°, array[1] is diagonal 45°,<br />
* array[2] right 90° and so on
*/
private boolean[] validMoves(final int row, final int col) {
boolean[] neighbourhood = new boolean[8];
for (int i = 0; i < neighbourhood.length; i++) {
neighbourhood[i] = boardWalker(row, col,
vectors[i].row(), vectors[i].col());
}
return neighbourhood;
}
/**
* Switches all tiles and changes switches player if possible.
*
* @param row Row of field
* @param col Column of field
*/
void performMove(final int row, final int col) {
switchTiles(row, col);
switchPlayer();
}
/**
* Calculates the score of a reversi.
*
* @return <code>score</code>
*/
double score() {
double score = (scoreT() + scoreM() + scoreP());
return score;
}
/**
* Calculates the weighting score.
*
* @return <code>scoreT</code>
*/
private double scoreT() {
double scoreSelf = 0;
double scoreEnemy = 0;
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (playBoard[row][col] == bot) {
scoreSelf += weighting[row][col];
} else if (playBoard[row][col] == human) {
scoreEnemy += weighting[row][col];
}
}
}
return (scoreSelf - (1.5 * scoreEnemy));
}
/**
* Calculates the mobility score.
*
* @return <code>scoreM</code>
*/
private double scoreM() {
double scoreSelf = 0;
double scoreEnemy = 0;
Player tmp = currentPlayer;
int n = getNumberOfTilesSet();
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
currentPlayer = bot;
if (isValidMove(row, col)) {
scoreSelf++;
}
currentPlayer = human;
if (isValidMove(row, col)) {
scoreEnemy++;
}
currentPlayer = bot;
}
}
currentPlayer = tmp;
return (64.0 / n) * (3.0 * scoreSelf - 4.0 * scoreEnemy);
}
/**
* Calculates the future-potential score.
*
* @return <code>scoreP</code>
*/
private double scoreP() {
double scoreSelf = 0;
double scoreEnemy = 0;
int n = getNumberOfTilesSet();
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (playBoard[row][col] == human) {
for (int i = 0; i < 8; i++) {
int x = row + vectors[i].row();
int y = col + vectors[i].col();
if ((x >= 0 && x < SIZE && y >= 0 && y < SIZE)
&& (playBoard[x][y] == null)) {
scoreSelf++;
}
}
} else if (playBoard[row][col] == bot) {
for (int i = 0; i < 8; i++) {
int x = row + vectors[i].row();
int y = col + vectors[i].col();
if ((x >= 0 && x < SIZE && y >= 0 && y < SIZE)
&& (playBoard[x][y] == null)) {
scoreEnemy++;
}
}
}
}
}
return (64.0 / (2 * n)) * (2.5 * scoreSelf - 3.0 * scoreEnemy);
}
}