Skip to content

Instantly share code, notes, and snippets.

@trpfrog
Created December 4, 2020 07:29
Show Gist options
  • Save trpfrog/bcdfcc015272ec71d38208be7829bf64 to your computer and use it in GitHub Desktop.
Save trpfrog/bcdfcc015272ec71d38208be7829bf64 to your computer and use it in GitHub Desktop.
1時間でオセロ作ったやつ (マジで1時間で作ったのでバグだらけだと思う)
package net.trpfrog.reversi;
public enum BoardStatus {
EMPTY, P1, P2
}
package net.trpfrog.reversi;
public class Reversi {
public static void main(String[] args) {
ReversiModel model = new ReversiModel();
new ReversiView(model);
}
}
package net.trpfrog.reversi;
import java.util.Arrays;
import java.util.stream.IntStream;
public class ReversiModel {
public final int BOARD_SIZE = 8;
public final int BOARD_PX = 100;
private static final int[] DX = {0, 1, 1, 1, 0, -1, -1, -1};
private static final int[] DY = {-1, -1, 0, 1, 1, 1, 0, -1};
private BoardStatus[][] board = new BoardStatus[BOARD_SIZE][BOARD_SIZE];
private BoardStatus turn = BoardStatus.P1;
public ReversiModel() {
for(int i = 0; i < BOARD_SIZE; i++) {
Arrays.fill(board[i], BoardStatus.EMPTY);
}
board[3][4] = board[4][3] = BoardStatus.P1;
board[3][3] = board[4][4] = BoardStatus.P2;
}
private BoardStatus getOpponent(BoardStatus p) {
if(p == BoardStatus.P1) {
return BoardStatus.P2;
} else if (p == BoardStatus.P2) {
return BoardStatus.P1;
}
return BoardStatus.EMPTY;
}
public BoardStatus getTurn() {
return turn;
}
public void toggleTurn() {
turn = getOpponent(turn);
}
public BoardStatus getBoardStatus(int x, int y) {
return board[x][y];
}
private boolean isSafe(int x, int y) {
return 0 <= x && x < BOARD_SIZE && 0 <= y && y < BOARD_SIZE;
}
public boolean canPlace(BoardStatus player, int x, int y) {
if(board[x][y] != BoardStatus.EMPTY) return false;
return IntStream.range(0, 8)
.filter(i -> isSafe(x+DX[i], y+DY[i]) && board[x+DX[i]][y+DY[i]] == getOpponent(player))
.anyMatch(i -> flip(player, x+DX[i], y+DY[i], DX[i], DY[i], false));
}
private boolean flip(BoardStatus player, int x, int y, int dx, int dy, boolean doFlip) {
if(!isSafe(x, y) || board[x][y] == BoardStatus.EMPTY) return false;
if(board[x][y] == player) return true;
if(flip(player, x+dx, y+dy, dx, dy, doFlip)) {
if(doFlip) board[x][y] = player;
return true;
}
return false;
}
public void flip(BoardStatus player, int x, int y) {
if(canPlace(player, x, y)){
board[x][y] = player;
}
IntStream.range(0, 8)
.filter(i -> isSafe(x+DX[i], y+DY[i]) && board[x+DX[i]][y+DY[i]] == getOpponent(player))
.forEach(i -> flip(player, x+DX[i], y+DY[i], DX[i], DY[i], true));
}
}
package net.trpfrog.reversi;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class ReversiView extends JFrame {
private final ReversiModel model;
public ReversiView(ReversiModel model) {
this.model = model;
setSize(800, 800);
add(new OthelloPanel());
setTitle("Reversi (Turn: Black)");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class OthelloPanel extends JPanel implements MouseListener {
public OthelloPanel() {
addMouseListener(this);
}
private void drawBoard(Graphics g) {
for(int i = 0; i < model.BOARD_SIZE; i++) {
for(int j = 0; j < model.BOARD_SIZE; j++) {
g.setColor(Color.BLACK);
g.fillRect(i * model.BOARD_PX, j * model.BOARD_PX, model.BOARD_PX, model.BOARD_PX);
g.setColor(new Color(0,120,0));
g.fillRect(i * model.BOARD_PX + 5, j * model.BOARD_PX + 5, model.BOARD_PX -10, model.BOARD_PX -10);
BoardStatus st = model.getBoardStatus(i, j);
if(st == BoardStatus.EMPTY) continue;
g.setColor(st == BoardStatus.P1 ? Color.BLACK : Color.WHITE);
g.fillOval(i * model.BOARD_PX + 10, j * model.BOARD_PX + 10, model.BOARD_PX -20, model.BOARD_PX -20);
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawBoard(g);
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX() / model.BOARD_PX;
int y = e.getY() / model.BOARD_PX;
if(model.canPlace(model.getTurn(), x, y)) {
model.flip(model.getTurn(), x, y);
model.toggleTurn();
if(model.getTurn() == BoardStatus.P1) {
setTitle("Reversi (Turn: Black)");
} else {
setTitle("Reversi (Turn: White)");
}
}
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment