* TDD 연습차.. ==== Board.java ==== {{{~cpp public class Board { final static int EMPTY = 0; int width, height; int boardState[][]; public Board(int width, int height) { this.width = width; this.height = height; boardState = new int[height][width]; } public int getState(int x, int y) { return boardState[y][x]; } public void putStone(int turn, int x, int y) { if(boardState[y][x] == EMPTY) { boardState[y][x] = turn; } } public boolean isInBoard(int x, int y) { return x >= 0 && x < width && y >= 0 && y < width; } public boolean isSameStone(int turn, int x, int y) { return isInBoard(x, y) && getState(x, y) == turn; } public void clearStones() { for(int i = 0 ; i < width ; ++i) for(int j = 0 ; j < height ; ++j) clearStone(i, j); } public void clearStone(int x, int y) { boardState[y][x] = EMPTY; } } }}} ==== Omok.java ==== {{{~cpp public class Omok { final static int BLACK = 1; final static int WHITE = 2; final static int WANTED = 5; Board board; int curTurn; public Omok(Board board) { this.board = board; curTurn = BLACK; } public Omok(int width, int height) { board = new Board(width, height); curTurn = BLACK; } private int commonCheck(int x, int y, int wanted, int dX1, int dX2, int dY1, int dY2) { int numOfStone = 1; for(int i = 1 ; i <= wanted - 1 ; ++i) { if( board.isSameStone(curTurn, x + i * dX1, y + i * dY1) ) ++numOfStone; if( board.isSameStone(curTurn, x + i * dX2, y + i * dY2) ) ++numOfStone; } return numOfStone; } private int checkHeight(int x, int y, int wanted) { return commonCheck(x, y, wanted, 0, 0, -1, 1); } private int checkWidth(int x, int y, int wanted) { return commonCheck(x, y, wanted, -1, 1, 0, 0); } private int checkBackSlash(int x, int y, int wanted) { return commonCheck(x, y, wanted, -1, 1, -1, 1); } private int checkSlash(int x, int y, int wanted) { return commonCheck(x, y, wanted, 1, -1, -1, 1); } public boolean doesAnyoneWin(int x, int y) { return checkHeight(x, y, WANTED) == WANTED || checkWidth(x, y, WANTED) == WANTED || checkBackSlash(x, y, WANTED) == WANTED || checkSlash(x, y, WANTED) == WANTED; } public boolean is33(int x, int y) { int count = 0; if( checkHeight(x,y,3) == 3 ) ++count; if( checkWidth(x,y,3) == 3 ) ++count; if( checkBackSlash(x,y,3) == 3 ) ++count; if( checkSlash(x,y,3) == 3 ) ++count; return count == 2; } public void putStone(int x, int y) { board.putStone(curTurn, x, y); } public int getState(int x, int y) { return board.getState(x, y); } public void toNextTurn() { curTurn = (curTurn == BLACK) ? WHITE : BLACK; } public String getTurnSymbol() { return curTurn == BLACK ? "흑" : "백"; } public void clearStone(int x, int y) { board.clearStone(x, y); } } }}} ==== OmokFrame.java ==== {{{~cpp import java.awt.*; import java.awt.event.*; import javax.swing.*; public class OmokFrame extends JFrame { final static int WIDTH = 20; final static int HEIGHT = 20; final static int BLOCKSIZE = 30; protected Omok omok; public OmokFrame(String arg) throws HeadlessException { super(arg); omok = new Omok(WIDTH, HEIGHT); getContentPane().addMouseListener( new PutStoneListener(this) ); setSize( (WIDTH + 2) * BLOCKSIZE , (HEIGHT + 2) * BLOCKSIZE ); setBackground( Color.WHITE ); setVisible(true); repaint(); } public void paint(Graphics g) { for(int i = 0 ; i < WIDTH ; ++i) g.drawLine(BLOCKSIZE + i * BLOCKSIZE, BLOCKSIZE, BLOCKSIZE + i * BLOCKSIZE, HEIGHT * BLOCKSIZE); for(int i = 0 ; i < HEIGHT ; ++i) g.drawLine(BLOCKSIZE, BLOCKSIZE + i * BLOCKSIZE, WIDTH * BLOCKSIZE, BLOCKSIZE + i * BLOCKSIZE); for(int i = 0 ; i < WIDTH ; ++i) { for(int j = 0 ; j < HEIGHT ; ++j) { if( omok.getState(i,j) == Omok.BLACK ) g.fillOval( (i+1) * BLOCKSIZE - BLOCKSIZE/2, (j+1) * BLOCKSIZE - BLOCKSIZE/2, BLOCKSIZE, BLOCKSIZE ); else if( omok.getState(i,j) == Omok.WHITE ) g.drawOval( (i+1) * BLOCKSIZE - BLOCKSIZE/2, (j+1) * BLOCKSIZE - BLOCKSIZE/2, BLOCKSIZE, BLOCKSIZE ); } } } public static void main(String args[]) { new OmokFrame("오목"); } public int getValidLocation(int clicked) { if( clicked < BLOCKSIZE / 2 ) return BLOCKSIZE; if( clicked > BLOCKSIZE * HEIGHT ) return BLOCKSIZE * HEIGHT; return clicked % BLOCKSIZE >= BLOCKSIZE / 2 ? (clicked / BLOCKSIZE + 1) * BLOCKSIZE : clicked / BLOCKSIZE * BLOCKSIZE; } public int getIndexFromCoordinate(int c) { return c / BLOCKSIZE - 1; } class PutStoneListener implements MouseListener { JFrame parent; public PutStoneListener(JFrame parent) { this.parent = parent; } public void mouseClicked(MouseEvent ev) { int x = getIndexFromCoordinate( getValidLocation( ev.getX() ) ); int y = getIndexFromCoordinate( getValidLocation( ev.getY() + 25 ) ); String message; omok.putStone(x, y); if( omok.is33(x,y) ) { message = "33입니다. 다시 하세요."; JOptionPane.showMessageDialog(parent, message); omok.clearStone(x,y); } else if( omok.doesAnyoneWin(x,y) ) { message = omok.getTurnSymbol() + "이 이겼습니다."; JOptionPane.showMessageDialog(parent, message); System.exit(0); } else { omok.toNextTurn(); message = omok.getTurnSymbol() + "차례입니다."; JOptionPane.showMessageDialog(parent, message); } repaint(); } public void mousePressed(MouseEvent ev) {} public void mouseReleased(MouseEvent ev) {} public void mouseEntered(MouseEvent ev) {} public void mouseExited(MouseEvent ev) {} } } }}} ==== OmokTestCase.java ==== {{{~cpp import junit.framework.TestCase; public class OmokTestCase extends TestCase { final static int BLACK = 1; final static int WHITE = 2; Board board; Omok omok; int width, height; public OmokTestCase(String arg) { super(arg); } public void setUp() { width = height = 5; board = new Board(width, height); omok = new Omok(board); } public void testCreationBoard() { for(int i = 0 ; i < height ; ++i) for(int j = 0 ; j < width ; ++j) assertEquals(0,board.getState(j,i)); } public void testPutStone() { int turnSet[] = {BLACK, WHITE, BLACK, BLACK}; int xSet[] = {0,0,0,1}; int ySet[] = {0,1,1,0}; int expectedSet[] = {BLACK, WHITE, WHITE, BLACK}; for(int i = 0 ; i < expectedSet.length ; ++i) { board.putStone(turnSet[i],xSet[i],ySet[i]); assertEquals(expectedSet[i],board.getState(xSet[i],ySet[i])); } } public void testCheckInBoard() { assertEquals( true, board.isInBoard(0,4) ); assertEquals( true, board.isInBoard(4,0) ); assertEquals( false, board.isInBoard(5,0) ); assertEquals( false, board.isInBoard(-1,6) ); } private void checkBoard(int turnSet[][][], int lastTurn, int x, int y, boolean expected) { for(int i = 0 ; i < turnSet.length ; ++i) { for(int j = 0 ; j < turnSet[i].length ; ++j) { board.putStone(i+1, turnSet[i][j][0], turnSet[i][j][1]); assertEquals(false, omok.doesAnyoneWin(turnSet[i][j][0], turnSet[i][j][1])); } } board.putStone(lastTurn, x, y); assertEquals(expected, omok.doesAnyoneWin(x, y)); } public void testCheckHeight() { int turnSet[][][] = { {{0,0},{0,1},{0,2},{0,4}}, {{1,0},{1,1},{1,2},{1,3}} }; checkBoard(turnSet, BLACK, 0, 3, true); } public void testCheckWidth() { int turnSet[][][] = { {{0,0},{1,0},{2,0},{3,0}}, {{0,1},{1,1},{2,1},{3,1}} }; checkBoard(turnSet, BLACK, 4, 0, true); } public void testCheckBackSlashDirection() { int turnSet[][][] = { {{0,0},{1,1},{2,2},{3,3}}, {{0,1},{1,2},{2,3},{3,4}} }; checkBoard(turnSet, BLACK, 4, 4, true); } public void testCheckSlashDirection() { int turnSet[][][] = { {{4,0},{3,1},{2,2},{1,3}}, {{0,1},{1,2},{2,3},{3,4}} }; checkBoard(turnSet, BLACK, 0, 4, true); } public void test6() { width = height = 6; board = new Board(width, height); omok = new Omok(board); int turnSet[][][] = { {{0,0},{0,1},{0,2},{0,3},{0,5}}, {{1,0},{1,1},{1,2},{1,3},{1,5}} }; checkBoard(turnSet, BLACK, 0, 4, false); } public void test33() { int turnSet[][][] = { {{0,0},{2,0},{0,2},{2,2}}, {{3,3},{4,4},{3,4},{4,3}} }; checkBoard(turnSet, BLACK, 1, 1, false); assertEquals(true, omok.is33(1, 1)); } } }}} ==== OmokUITestCase.java ==== {{{~cpp import junit.framework.TestCase; public class OmokUITestCase extends TestCase { OmokFrame of; public OmokUITestCase(String arg) { super(arg); } public void setUp() { of = new OmokFrame("오목"); } public void testPutStoneToValidLocation() { assertEquals( 50, of.getValidLocation(50) ); assertEquals( 100, of.getValidLocation(80) ); assertEquals( 100, of.getValidLocation(120) ); assertEquals( 50, of.getValidLocation(15) ); assertEquals( 550, of.getValidLocation(589) ); } public void testCoordinateToArrayIndex() { assertEquals( 0, of.getIndexFromCoordinate(50) ); assertEquals( 1, of.getIndexFromCoordinate(100) ); } } }}}