# spring-sprout/telepathy

Merge pull request #40 from dimdim2/master

`퀴즈3번문제 풀이 추가`
2 parents 236fee9 + d662698 commit 6982971078248946e75ffaa845b3239cf61d7fc2 miracle0k committed May 16, 2012
71 src/quize/number03/daewon/DaewonAlwaysTurnLeft.java
46 src/quize/number03/daewon/Direction.java
 @@ -0,0 +1,46 @@ +package quize.number03.daewon; + +public enum Direction { + + NORTH(1), SOUTH(2), WEST(4), EAST(8); + + final int value; + + private Direction(int value) { + this.value = value; + } + + public Direction reverse() { + switch (this) { + case NORTH: + return SOUTH; + case SOUTH: + return NORTH; + case EAST: + return WEST; + case WEST: + return EAST; + default: + return null; + } + } + + public int getValue() { + return value; + } + + public Direction valueOf(int value) { + switch (value) { + case 1: + return NORTH; + case 2: + return SOUTH; + case 4: + return WEST; + case 8: + return EAST; + default: + throw new IllegalArgumentException(String.format("Invalid Value [Expected:1,2,4,8] [Arg:%d]", value)); + } + } +}
67 src/quize/number03/daewon/Maze.java
 @@ -0,0 +1,67 @@ +package quize.number03.daewon; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class Maze { + int columnSize; + int rowSize; + List rows = new ArrayList(); + + public void setRow(int rowIdx, MazeRow row) { + rows.set(rowIdx, row); + } + + public void setCell(int row, int col, MazeCell cell) { + rows.get(row).setColumn(col, cell); + } + + public MazeRow getRow(int row) { + int addRowCount = row - rows.size(); + for (int i = 0; i < addRowCount; i++) { + MazeRow mazeRow = new MazeRow(); + rows.add(mazeRow); + } + + return rows.get(row); + } + + public MazeCell getCell(int row, int col) { + int addRowCount = row - rows.size(); + for (int i = 0; i < addRowCount; i++) { + MazeRow mazeRow = new MazeRow(); + rows.add(mazeRow); + } + + return rows.get(row - 1).getCol(col); + } + + public int getRowSize() { + return rows.size(); + } + + public String toString() { + StringBuilder strBuf = new StringBuilder(columnSize * rowSize); + for (MazeRow row : rows) { + Map columns = row.getColumns(); + + Set keySet = columns.keySet(); + List idxs = new ArrayList(); + for (Integer colIdx : keySet) { + idxs.add(colIdx); + } + + Collections.sort(idxs); + for (Integer colIdx : idxs) { + strBuf.append(columns.get(colIdx).getHex()); + } + strBuf.append("\n"); + } + + return strBuf.toString(); + } + +}
42 src/quize/number03/daewon/MazeCell.java
 @@ -0,0 +1,42 @@ +package quize.number03.daewon; + +public class MazeCell { + + int value = 0x0; + + public MazeCell() { + super(); + } + + public MazeCell(int value) { + this.value = value; + } + + public int getNumber() { + return value; + } + + public String getHex() { + return String.format("%x", value); + } + + public void open(Direction direction) { + value = value | direction.getValue(); + } + + public void openNorth() { + open(Direction.NORTH); + } + + public void openSouth() { + open(Direction.SOUTH); + } + + public void openWest() { + open(Direction.WEST); + } + + public void openEast() { + open(Direction.EAST); + } +}
33 src/quize/number03/daewon/MazeRow.java
 @@ -0,0 +1,33 @@ +package quize.number03.daewon; + +import java.util.HashMap; +import java.util.Map; + +public class MazeRow { + Map columns = new HashMap(); + + public MazeRow() {} + + public void setColumn(int col, MazeCell cell) { + columns.put(col, cell); + } + + public MazeCell getCol(int col) { + MazeCell cell = columns.get(col); + if (cell == null) { + cell = new MazeCell(); + columns.put(col, cell); + } + + return cell; + } + + public Map getColumns() { + return columns; + } + + public int getColumnSize() { + return columns.size(); + } + +}
111 src/quize/number03/daewon/Mouse.java
 @@ -0,0 +1,111 @@ +package quize.number03.daewon; + +public class Mouse { + + private Direction direction = Direction.SOUTH; + private int numOfColumn = 0; + private int numOfRow = 0; + private int row = 0; + private int column = 0; + + public Mouse() {} + + public Mouse(Direction direction) { + this.direction = direction; + } + + public void forward() { + switch (direction) { + case NORTH: + row--; + break; + case EAST: + column++; + break; + case SOUTH: + row++; + break; + case WEST: + column--; + break; + default: + break; + } + + numOfColumn = Math.max(Math.abs(column), numOfColumn); + numOfRow = Math.max(Math.abs(row), numOfRow); + } + + public int getRow() { + return row; + } + + public void setRow(int row) { + this.row = row; + } + + public int getColumn() { + return column; + } + + public void setColumn(int column) { + this.column = column; + } + + public Direction reverse() { + turnLeft(); + return turnLeft(); + } + + public Direction turnLeft() { + if (direction == Direction.NORTH) { + this.direction = Direction.WEST; + + } else if (direction == Direction.WEST) { + this.direction = Direction.SOUTH; + + } else if (direction == Direction.SOUTH) { + this.direction = Direction.EAST; + + } else if (direction == Direction.EAST) { + this.direction = Direction.NORTH; + + } + + return this.direction; + } + + public Direction turnRight() { + if (direction == Direction.NORTH) { + this.direction = Direction.EAST; + + } else if (direction == Direction.EAST) { + this.direction = Direction.SOUTH; + + } else if (direction == Direction.SOUTH) { + this.direction = Direction.WEST; + + } else if (direction == Direction.WEST) { + this.direction = Direction.NORTH; + } + + return this.direction; + } + + public Direction getDirection() { + return direction; + } + + public void setDirection(Direction direction) { + this.direction = direction; + } + + public int getNumOfRow() { + return numOfRow; + } + + public int getNumOfColumn() { + return numOfColumn; + } + +}
67 src/quize/number04/DaewonSpeakingInTongues.java