mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Formatting
This commit is contained in:
@@ -3,34 +3,37 @@ package org.toop.game;
|
||||
import java.util.Arrays;
|
||||
|
||||
public abstract class Game {
|
||||
public enum State {
|
||||
NORMAL, DRAW, WIN,
|
||||
}
|
||||
public enum State {
|
||||
NORMAL,
|
||||
DRAW,
|
||||
WIN,
|
||||
}
|
||||
|
||||
public record Move(int position, char value) {}
|
||||
public record Move(int position, char value) {}
|
||||
|
||||
public static final char EMPTY = (char)0;
|
||||
public static final char EMPTY = (char) 0;
|
||||
|
||||
public final int rowSize;
|
||||
public final int columnSize;
|
||||
public final char[] board;
|
||||
public final int rowSize;
|
||||
public final int columnSize;
|
||||
public final char[] board;
|
||||
|
||||
protected Game(int rowSize, int columnSize) {
|
||||
assert rowSize > 0 && columnSize > 0;
|
||||
protected Game(int rowSize, int columnSize) {
|
||||
assert rowSize > 0 && columnSize > 0;
|
||||
|
||||
this.rowSize = rowSize;
|
||||
this.columnSize = columnSize;
|
||||
this.rowSize = rowSize;
|
||||
this.columnSize = columnSize;
|
||||
|
||||
board = new char[rowSize * columnSize];
|
||||
Arrays.fill(board, EMPTY);
|
||||
}
|
||||
board = new char[rowSize * columnSize];
|
||||
Arrays.fill(board, EMPTY);
|
||||
}
|
||||
|
||||
protected Game(Game other) {
|
||||
rowSize = other.rowSize;
|
||||
columnSize = other.columnSize;
|
||||
board = Arrays.copyOf(other.board, other.board.length);
|
||||
}
|
||||
protected Game(Game other) {
|
||||
rowSize = other.rowSize;
|
||||
columnSize = other.columnSize;
|
||||
board = Arrays.copyOf(other.board, other.board.length);
|
||||
}
|
||||
|
||||
public abstract Move[] getLegalMoves();
|
||||
public abstract State play(Move move);
|
||||
}
|
||||
public abstract Move[] getLegalMoves();
|
||||
|
||||
public abstract State play(Move move);
|
||||
}
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
package org.toop.game;
|
||||
|
||||
public abstract class TurnBasedGame extends Game {
|
||||
public final int turns;
|
||||
public final int turns;
|
||||
|
||||
protected int currentTurn;
|
||||
protected int currentTurn;
|
||||
|
||||
protected TurnBasedGame(int rowSize, int columnSize, int turns) {
|
||||
super(rowSize, columnSize);
|
||||
protected TurnBasedGame(int rowSize, int columnSize, int turns) {
|
||||
super(rowSize, columnSize);
|
||||
assert turns >= 2;
|
||||
this.turns = turns;
|
||||
}
|
||||
}
|
||||
|
||||
protected TurnBasedGame(TurnBasedGame other) {
|
||||
super(other);
|
||||
turns = other.turns;
|
||||
currentTurn = other.currentTurn;
|
||||
}
|
||||
protected TurnBasedGame(TurnBasedGame other) {
|
||||
super(other);
|
||||
turns = other.turns;
|
||||
currentTurn = other.currentTurn;
|
||||
}
|
||||
|
||||
protected void nextTurn() {
|
||||
currentTurn = (currentTurn + 1) % turns;
|
||||
}
|
||||
protected void nextTurn() {
|
||||
currentTurn = (currentTurn + 1) % turns;
|
||||
}
|
||||
|
||||
public int getCurrentTurn() { return currentTurn; }
|
||||
}
|
||||
public int getCurrentTurn() {
|
||||
return currentTurn;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,17 @@ package org.toop.game.othello;
|
||||
import org.toop.game.TurnBasedGame;
|
||||
|
||||
public final class Othello extends TurnBasedGame {
|
||||
Othello() {
|
||||
super(8, 8, 2);
|
||||
}
|
||||
Othello() {
|
||||
super(8, 8, 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Move[] getLegalMoves() {
|
||||
return new Move[0];
|
||||
}
|
||||
@Override
|
||||
public Move[] getLegalMoves() {
|
||||
return new Move[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public State play(Move move) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public State play(Move move) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import org.toop.game.AI;
|
||||
import org.toop.game.Game;
|
||||
|
||||
public final class OthelloAI extends AI<Othello> {
|
||||
@Override
|
||||
public Game.Move findBestMove(Othello game, int depth) {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Game.Move findBestMove(Othello game, int depth) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package org.toop.game.tictactoe;
|
||||
|
||||
import org.toop.game.TurnBasedGame;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import org.toop.game.TurnBasedGame;
|
||||
|
||||
public final class TicTacToe extends TurnBasedGame {
|
||||
private int movesLeft;
|
||||
@@ -19,8 +18,8 @@ public final class TicTacToe extends TurnBasedGame {
|
||||
|
||||
@Override
|
||||
public Move[] getLegalMoves() {
|
||||
final ArrayList<Move> legalMoves = new ArrayList<>();
|
||||
final char currentValue = getCurrentValue();
|
||||
final ArrayList<Move> legalMoves = new ArrayList<>();
|
||||
final char currentValue = getCurrentValue();
|
||||
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
if (board[i] == EMPTY) {
|
||||
@@ -44,12 +43,12 @@ public final class TicTacToe extends TurnBasedGame {
|
||||
return State.WIN;
|
||||
}
|
||||
|
||||
nextTurn();
|
||||
nextTurn();
|
||||
|
||||
if (movesLeft <= 2) {
|
||||
if (movesLeft <= 0 || checkForEarlyDraw(this)) {
|
||||
return State.DRAW;
|
||||
}
|
||||
if (movesLeft <= 0 || checkForEarlyDraw(this)) {
|
||||
return State.DRAW;
|
||||
}
|
||||
}
|
||||
|
||||
return State.NORMAL;
|
||||
@@ -60,7 +59,9 @@ public final class TicTacToe extends TurnBasedGame {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
final int index = i * 3;
|
||||
|
||||
if (board[index] != EMPTY && board[index] == board[index + 1] && board[index] == board[index + 2]) {
|
||||
if (board[index] != EMPTY
|
||||
&& board[index] == board[index + 1]
|
||||
&& board[index] == board[index + 2]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -83,7 +84,7 @@ public final class TicTacToe extends TurnBasedGame {
|
||||
|
||||
private boolean checkForEarlyDraw(TicTacToe game) {
|
||||
for (final Move move : game.getLegalMoves()) {
|
||||
final TicTacToe copy = new TicTacToe(game);
|
||||
final TicTacToe copy = new TicTacToe(game);
|
||||
|
||||
if (copy.play(move) == State.WIN || !checkForEarlyDraw(copy)) {
|
||||
return false;
|
||||
@@ -93,7 +94,7 @@ public final class TicTacToe extends TurnBasedGame {
|
||||
return true;
|
||||
}
|
||||
|
||||
private char getCurrentValue() {
|
||||
return currentTurn == 0? 'X' : 'O';
|
||||
}
|
||||
}
|
||||
private char getCurrentValue() {
|
||||
return currentTurn == 0 ? 'X' : 'O';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user