Skip to content

Instantly share code, notes, and snippets.

@shiracamus
Last active July 5, 2021 17:21
Show Gist options
  • Save shiracamus/f5f9c90a6ff6667872fd9b7f430e8747 to your computer and use it in GitHub Desktop.
Save shiracamus/f5f9c90a6ff6667872fd9b7f430e8747 to your computer and use it in GitHub Desktop.
enum Stone {
NONE("."),
BLACK("X"),
WHITE("O"),
INVALID("?");
private final String text;
private Stone(String text) {
this.text = text;
}
public String toString() {
return text;
}
public Stone turn() {
return this == BLACK ? WHITE :
this == WHITE ? BLACK :
this;
}
}
class Board {
private final Stone[][] cells;
public static final String X = "abcdefgh";
public static final String Y = "12345678";
public Board() {
this(8, 8);
}
public Board(int width, int height) {
cells = new Stone[height][width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
cells[y][x] = Stone.NONE;
}
}
}
public void print() {
System.out.println(" " + X);
for (int y = 0; y < cells.length; y++) {
System.out.print(Y.charAt(y));
for (int x = 0; x < cells[y].length; x++) {
System.out.print(cells[y][x]);
}
System.out.println();
}
}
public boolean isValid(int x, int y) {
return y >= 0 && y < cells.length && x >= 0 && x < cells[y].length;
}
public boolean put(int x, int y, Stone stone) {
if (!isValid(x, y)) {
return false;
}
cells[y][x] = stone;
return true;
}
public Stone at(int x, int y) {
return isValid(x, y) ? cells[y][x] : Stone.INVALID;
}
public int count(Stone stone) {
int counter = 0;
for (var row: cells) {
for (var cell: row) {
if (cell == stone) {
counter += 1;
}
}
}
return counter;
}
}
public class Othello {
private final Board board;
public Othello() {
board = new Board();
board.put(3, 3, Stone.WHITE);
board.put(4, 3, Stone.BLACK);
board.put(3, 4, Stone.BLACK);
board.put(4, 4, Stone.WHITE);
}
public void print() {
System.out.println("BLACK(" + Stone.BLACK + "):" + board.count(Stone.BLACK) +
" vs " +
"WHITE(" + Stone.WHITE + "):" + board.count(Stone.WHITE));
board.print();
}
public boolean canPut(int x, int y, Stone stone) {
if (board.at(x, y) != Stone.NONE) {
return false;
}
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dy == 0 && dx == 0) {
continue;
}
if (canReverse(x, y, stone, dx, dy)) {
return true;
}
}
}
return false;
}
private boolean canReverse(int x, int y, Stone stone, int dx, int dy) {
if (board.at(x + dx, y + dy) != stone.turn()) {
return false;
}
do {
x += dx;
y += dy;
} while (board.at(x, y) == stone.turn());
return board.at(x, y) == stone;
}
public boolean put(char x, char y, Stone stone) {
return put(Board.X.indexOf(x), Board.Y.indexOf(y), stone);
}
boolean put(int x, int y, Stone stone) {
if (!canPut(x, y, stone)) {
return false;
}
board.put(x, y, stone);
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dy == 0 && dx == 0) {
continue;
}
reverse(x, y, stone, dx, dy);
}
}
return true;
}
private void reverse(int x, int y, Stone stone, int dx, int dy) {
if (!canReverse(x, y, stone, dx, dy)) {
return;
}
while (board.at(x + dx, y + dy) == stone.turn()) {
x += dx;
y += dy;
board.put(x, y, stone);
}
}
public static void main(String[] args) {
System.out.println(Stone.WHITE.turn().name());
System.out.println(Stone.BLACK.turn().name());
Othello othello = new Othello();
othello.print();
var player = Stone.BLACK;
System.out.println(othello.put('a', '1', player));
System.out.println(othello.put('d', '3', player));
othello.print();
player = player.turn();
othello.put('c', '3', player);
othello.print();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment