U E D R , A S I H C RSS

Java Study2002/상욱-2주차


답이 완전히 나오긴 했는데요.... 실제 의도와 똑같이 되지 않은 것이 흠이네요...ㅠ.ㅠ

~cpp 
import java.util.*;

public class s1{
	public static void main(String[] args) {
		
		Board board = new Board();
		Roach roach = new Roach();
		Observer observer = new Observer();
		int xRoach = 1 , yRoach = 1;
		int tempX = 0, tempY = 0;
		for (;observer.checkQuit() == true;){
			tempX = xRoach + roach.moveSide();
			tempY = yRoach + roach.moveUpandDown();
			if ( board.boardState(tempX, tempY) == true ) {
				observer.checkStay(tempX-1, tempY-1);
				xRoach = tempX;
				yRoach = tempY;
			}
			else
				continue;
		}
		observer.output();
	}

}

class Board{
	
	private boolean board_[][] = new boolean [12][12];
	public Board() {
		for (int i = 0 ; i <= 11 ; i++){
			for (int j = 0 ; j <= 11 ; j++){
				board_[i][j] = false;
			}
		}
		for (int k = 1 ; k <= 10 ; k++){
			for (int l = 1 ; l <= 10 ; l++){
				board_[k][l] = true;
			}
		}
	}
	
	public boolean boardState(int x , int y ) {
		if ( board_[x][y] == true )
			return true;
		else
			return false;
	}
}

class Roach{
	Random rand = new Random();
	public int randomNumber_1() {
		return rand.nextInt(10000);
	}
	
	public int randomNumber_2() {
		return rand.nextInt(40000);
	}

	public int moveUpandDown() {
		return (randomNumber_1()%3)-1;		// -1 is left, 1 is right.
	}
	public int moveSide() {
		return (randomNumber_2()%3)-1;		// -1 is up, 1 is down.
	}

	
}

class Observer{
	private int boardCount[][] = new int [10][10];
	
	public Observer() {
		for (int i = 0 ; i <= 9 ; i++){
			for (int j = 0 ; j <= 9 ; j++){
				boardCount[i][j] = 0;
			}
		}
	}
	
	public void checkStay(int x, int y) {
		boardCount[x][y] = boardCount[x][y] + 1;
	}
		
	public void output() {
		for (int i = 0 ; i <= 9 ; i++){
			for (int j = 0 ; j <= 9 ; j++){
				System.out.print(boardCount[i][j] + " ");
			}
			System.out.println();
		}
	}
	public boolean checkQuit() {
		for (int i = 0 ; i <= 9 ; i++){
			for (int j = 0 ; j <= 9 ; j++){
				if ( boardCount[i][j] == 0 )
					return true;
			}
		}
		return false;		
	}
}

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