Skip to content

Instantly share code, notes, and snippets.

@szdr
Created May 18, 2017 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szdr/45aad2a4782ef04dc2c5fb89bb43716a to your computer and use it in GitHub Desktop.
Save szdr/45aad2a4782ef04dc2c5fb89bb43716a to your computer and use it in GitHub Desktop.
Othello
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int[][] debugInputs = {
{4, 5},
{5, 5},
{5, 4},
{3, 5},
{2, 4},
{1, 3},
{2, 3},
{5, 3},
{3, 2},
{3, 1}
};
/*
static int[][] debugInputs = {
{4, 5},
{5, 3},
{4, 2},
{3, 5},
{6, 4},
{5, 5},
{4, 6},
{5, 4},
{2, 4}
};
*/
public static void main(String[] args) {
Othello oth = new Othello();
boolean debug = true;
if (debug) {
for (int[] input : debugInputs) {
oth.put(input[0], input[1]);
oth.printTable();
if (oth.isGameEnd()) {
oth.reportGame();
}
}
return;
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while (true) {
oth.printTable();
try {
System.out.println("Turn: " + oth.MARKER[oth.turn]);
System.out.print("Input i j:");
line = br.readLine();
String[] tokens = line.split(" ");
int put_i = Integer.parseInt(tokens[0]);
int put_j = Integer.parseInt(tokens[1]);
if (!oth.put(put_i, put_j)) {
System.out.println("You cannot put " + put_i + " " + put_j);
};
if (oth.isGameEnd()) {
oth.reportGame();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Othello {
static final int SIZE = 8;
static final int BLACK = 0;
static final int WHITE = 1;
static final int EMPTY = 2;
static final char[] MARKER = {'*', 'O', ' '};
static final int[] dx = {0, 1, 0, -1, 1, 1, -1, -1};
static final int[] dy = {1, 0, -1, 0, 1, -1, 1, -1};
int[][] table;
int turn = 0;
int cnt = 0;
Othello () {
this.table = new int[SIZE][];
for (int i = 0; i < SIZE; i++) {
table[i] = new int[SIZE];
for (int j = 0; j < SIZE; j++) {
table[i][j] = EMPTY;
}
}
table[SIZE / 2 - 1][SIZE / 2 - 1] = WHITE;
table[SIZE / 2][SIZE / 2] = WHITE;
table[SIZE / 2 - 1][SIZE / 2] = BLACK;
table[SIZE / 2][SIZE / 2 - 1] = BLACK;
}
boolean put(int i, int j) {
if (table[i][j] != EMPTY) {
return false;
}
boolean ret = false;
for (int d = 0; d < 8; d++) {
if (turnPieces(i, j, i, j, d, true, this.turn)) {
ret = true;
}
}
if (ret) {
table[i][j] = this.turn;
this.turn = getNextTurn();
cnt += 1;
}
return ret;
}
private boolean canPut(int turn) {
// 試しに置いてみてひっくり返せる場所があるか確かめる
for (int d = 0; d < 8; d++) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (turnPieces(i, j, i, j, d, false, turn)) {
return true;
}
}
}
}
return false;
}
public boolean isGameEnd() {
// 全部置いた or 両者置く場所がない
return (cnt == SIZE * SIZE) || (!canPut(BLACK) && !canPut(WHITE));
}
private int getNextTurn() {
// 相手が置けない時は再び自分のターンになる
int nextTurn = (this.turn + 1) % 2;
if (canPut(nextTurn)) {
return nextTurn;
} else {
return this.turn;
}
}
private boolean turnPieces(int i, int j, int put_i, int put_j, int dir, boolean is_turn, int turn) {
// 自分と同じ色のマスを発見したら(true), 戻りながら色を変える
// 置いた地点は無視
if (i == put_i && j == put_j) {
return turnPieces(i + dx[dir], j + dy[dir], put_i, put_j, dir, is_turn, turn);
}
if (i < 0 || i >= SIZE || j < 0 || j >= SIZE) {
return false;
}
if (table[i][j] == EMPTY) {
return false;
}
if (table[i][j] == turn) {
// 置いたコマの隣のコマが同じ色の時はfalse
int dist = Math.abs(i - put_i) + Math.abs(j - put_j);
// 上下左右
if (dir <= 3) {
return dist != 1;
// 斜め
} else {
return dist != 2;
}
}
boolean ret = turnPieces(i + dx[dir], j + dy[dir], put_i, put_j, dir, is_turn, turn);
if (ret && is_turn) {
table[i][j] = turn;
}
return ret;
}
public void printTable() {
System.out.print(" ");
for (int i = 0; i < SIZE; i++) {
System.out.print("|" + i);
}
System.out.println("|");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
System.out.print("-+");
}
System.out.println("-+");
System.out.print(i);
for (int j = 0; j < SIZE; j++) {
System.out.print("|" + MARKER[table[i][j]]);
}
System.out.println("|");
}
for (int i = 0; i < SIZE + 1; i++) {
System.out.print("-+");
}
System.out.println("");
System.out.println("");
}
public void reportGame() {
int black_cnt = 0;
int white_cnt = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if(table[i][j] == BLACK) {
black_cnt += 1;
} else if (table[i][j] == WHITE) {
white_cnt += 1;
}
}
}
System.out.println("*: " + black_cnt);
System.out.println("O: " + white_cnt);
if (black_cnt > white_cnt) {
System.out.println("win: *");
} else if (black_cnt < white_cnt) {
System.out.println("win: O");
} else {
System.out.println("draw");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment