여기 열거 형을 사용하십시오 :
enum Stone {BLAC, WHITE, NONE}
class Board {
static final int dimension = 19;
private Stone[][] board = new Stone[dimension][dimension];
// ...
}
편집을 :
이
여기에 작은 데모입니다 (보드없이 이미지의 크기가 조정, 단지 좋은 오래된 그래픽!) :
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GoBoardDemo {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(new GoPanel(19), BorderLayout.CENTER);
frame.setSize(600, 625);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class GoPanel extends JPanel {
Square[][] board;
boolean whiteToMove;
GoPanel(int dimension) {
board = new Square[dimension][dimension];
whiteToMove = true;
initBoard(dimension);
}
private void initBoard(int dimension) {
super.setLayout(new GridLayout(dimension, dimension));
for(int row = 0; row < dimension; row++) {
for(int col = 0; col < dimension; col++) {
board[row][col] = new Square(row, col);
super.add(board[row][col]);
}
}
repaint();
}
private class Square extends JPanel {
Stone stone;
final int row;
final int col;
Square(int r, int c) {
stone = Stone.NONE;
row = r;
col = c;
super.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent me) {
if(stone != Stone.NONE) return;
stone = whiteToMove ? Stone.WHITE : Stone.BLACK;
whiteToMove = !whiteToMove;
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w = super.getWidth();
int h = super.getHeight();
g.setColor(new Color(0xB78600));
g.fillRect(0, 0, w, h);
g.setColor(Color.BLACK);
if(row == 0 || row == board.length-1 || col == 0 || col == board.length-1) {
if(col == 0) {
g.drawLine(w/2, h/2, w, h/2);
if(row == 0) g.drawLine(w/2, h/2, w/2, h);
else if(row == 18) g.drawLine(w/2, h/2, w/2, 0);
else g.drawLine(w/2, 0, w/2, h);
}
else if(col == 18) {
g.drawLine(0, h/2, w/2, h/2);
if(row == 0) g.drawLine(w/2, h/2, w/2, h);
else if(row == 18) g.drawLine(w/2, h/2, w/2, 0);
else g.drawLine(w/2, 0, w/2, h);
}
else if(row == 0) {
g.drawLine(0, h/2, w, h/2);
g.drawLine(w/2, h/2, w/2, h);
}
else {
g.drawLine(0, h/2, w, h/2);
g.drawLine(w/2, h/2, w/2, 0);
}
} else {
g.drawLine(0, h/2, w, h/2);
g.drawLine(w/2, 0, w/2, h);
}
stone.paint(g, w);
}
}
}
enum Stone {
BLACK(Color.BLACK), WHITE(Color.WHITE), NONE(null);
final Color color;
private final static Random rand = new Random();
private Stone(Color c) {
color = c;
}
public void paint(Graphics g, int dimension) {
if(this == NONE) return;
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(color);
int x = 5;
g2d.fillOval(rand.nextInt(x), rand.nextInt(x), dimension-x, dimension-x);
}
}
무작위적인 내용은 유기농 느낌의 구현입니다. CPerkins가 말했습니다. 음, 어쨌든 그것을하려고했습니다.
@ skaffman : 숙제 문제라고 생각하십니까? 나는 실망했다. 어쩌면 게임은 Go이기 때문일 수 있습니다. – CPerkins
Skaffman이 분명히 그의 코멘트를 삭제했지만 이것은 내 개인 프로젝트입니다. 나는 확률 론적 AI를 적용하고 배우는 동안 바둑 게임 엔진을 구현하려고합니다. – codingbear
나는 그것을 듣고 기쁘다, codingbear. 스카프 만 (Skaffman)이 질문을 편집했고, 나는 그가 "숙제"라고 재검토했다고 생각했습니다. 숙제가 아니기 때문에 저는 그 태그를 제거 할 것입니다 : 그것은 사람들을 막아 버리는 경향이 있습니다. 귀하의 진전 상황을 지속적으로 알려 주시기 바랍니다. – CPerkins