U E D R , A S I H C RSS

Mine Sweeper/황재선

~cpp MineSweeper.java

~cpp 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

/*
 * Created on 2005. 1. 2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MineSweeper {
	String [][] mineArr;
	
	public int [] inputSize()  {
		String input = input();
		String [] array = input.split(" ");
		int len = array.length;
		int [] intArray = new int[len];
		for (int i = 0; i < len; i++)
			intArray[i] = Integer.parseInt(array[i]);
		return intArray;
	}

	public String[][] inputChar(int []array) {
		
		int row = array[0];
		int col = array[1];
		
		mineArr = new String[row][col];
		
		for(int i = 0; i < row; i++) {
			String input = input();
			String [] arr = input.split("");
			
			for (int j = 0; j < col; j++)
				mineArr[i][j] = arr[j+1];
		}
		return mineArr;
	}
	
	public String input() {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String input = "";
		try {
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return input;
	}

	public String[][] findPosition() {
		int len = mineArr[0].length;
		
		for (int row = 0; row < mineArr.length; row++) {
			for (int col = 0; col < len; col++) {
				if (mineArr[row][col].compareTo("*") == 0)
					continue;
				countMine(row, col);	
			}
		}
		return mineArr;
	}

	private void countMine(int row, int col) {
		int count = 0;
		int [] posRow = {-1, -1, 0, 1, 1, 1, 0, -1};
		int [] posCol = {0, 1, 1, 1, 0, -1, -1, -1};
		
		for (int i = 0; i < posRow.length; i++) {
			if (row+posRow[i] >= 0 && col+posCol[i] >= 0 && 
					row+posRow[i] < mineArr.length && col+posCol[i] < mineArr[0].length)
				if (mineArr[row+posRow[i]][col+posCol[i]].compareTo("*") == 0)
					count++;
		}
		
		mineArr[row][col] = Integer.toString(count);		
	}

	private void printResult(Vector v) {	
		int size = v.size();
		int i = 0;
		while (i < size) {
			String [][] arr = (String [][]) v.get(i);
			int num = i+1;
			System.out.println("Field #" + num + ":");
			for (int row = 0; row < arr.length; row++) {
				for (int col = 0; col < arr[0].length; col++) {
					System.out.print(arr[row][col]);
				}
				System.out.println();
			}
			System.out.println();
			i++;
		}
	}

	static public void main(String [] args) {
		Vector v = new Vector();
		while (true) {
			MineSweeper m = new MineSweeper();
			int [] array = m.inputSize();
			if (array[0] == 0 && array[1] == 0) {
				m.printResult(v);
				break;
			}	
			else {
				m.inputChar(array);
				m.findPosition();
				v.add(m.mineArr);
			}			
		}		
	}
}

~cpp TestMineSweeper.java

~cpp 
import junit.framework.TestCase;
/*
 * Created on 2005. 1. 2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */


public class TestMineSweeper extends TestCase {
	MineSweeper m = new MineSweeper();
	
	String [][] arr;
	public void tInput() {
		int [] array = m.inputSize();
		assertEquals(4, array[0]);
		assertEquals(4, array[1]);
		assertEquals(2, array.length);

		arr = m.inputChar(array);
		assertEquals(4, arr.length);
		assertEquals(4, arr[0].length);
	}
	
	public void testFirstLine() {
		tInput();
		arr = m.findPosition();
		assertEquals("1", arr[0][1]);
		assertEquals("0", arr[0][2]);
		assertEquals("0", arr[0][3]);
		assertEquals("2", arr[1][0]);
		assertEquals("2", arr[1][1]);
		
	}
	
}

느낀점

  1. 생각보다 단순한 문제였다. 윈도우의 지뢰찾기가 생각나서 어려워했나보다. RandomWalk보다 훨씬 쉽다.
  2. 변수 명명이 아직도 고민된다. 배열은 무조건 array 혹은 arr으로, 키보드 입력은 input을 붙인다. 딱히 적당한 이름이 생각나지 않는다.
  3. 자바에서 console 모드의 키보드 입력를 처음 사용했다. c보다 불편하다.
  4. tdd가 힘들다. 무엇을 테스트해야할지 모르겠다. 키보드 입력과 결과부분을 테스트했다. 그냥 막코딩한것같다. 생각대로 풀려서 다행이다.


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:46
Processing time 0.0087 sec