안녕하세요. 자바 변수와, 객체, 메소드 관련해서 정리를 하고 있는데 제가 정리를 하는게 너무 미숙해서
고수님들께 도움을 청하려고 질문을 올렸습니다.
3가지의 클래스 파일이 있는데, 이 3가지의 구조 변수와 메소드, 객체를 각 클래스별로 정리하고
어떻게 서로 작동을 하는지에 대해서 나눠서 서로간의 관계를 나타내는 작업을 하고 있는데 좀 처럼
쉽게 되는거 같지않아서 질문을 올렸습니다.
제가 나름 정말 열심히 40분동안 그렸다지웠다하면서 한글파일에서 표로 정리한건데 아래의 도식화
해놓은 것이 그림 아래의 3개의 소스파일을 분석한것과 맞는지 궁금합니다 ㅠㅠ..
클래스는 빨간색으로 칠한 3개의 클래스 DotCombust와 Dotcom , Gamehelper가 있습니다.
메소드 안에 있는 객체와 변수들도 정리를 해주어야 하는데 어떻게 정리를 해줘야 효과적일지
고민입니다ㅠㅠ
import java.util.*;
public class DotCom {
private ArrayList<String> locationCells;
private String name;
public void setLocationCells(ArrayList<String> loc) {
locationCells = loc;
}
public void setName(String n) {
name = n;
}
public String checkYourself(String userInput) {
String result = "miss";
int index = locationCells.indexOf(userInput);
if (index >= 0) {
locationCells.remove(index);
if (locationCells.isEmpty()) {
result = "kill";
System.out.println("Ouch! You sunk " + name + " : ( ");
} else {
result = "hit";
} // close if
} // close if
return result;
} // close method
} // close class
import java.util.*;
public class DotComBust {
private GameHelper helper = new GameHelper();
private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
private int numOfGuesses = 0;
private void setUpGame() {
DotCom one = new DotCom();
one.setName("Pets.com");
DotCom two = new DotCom();
two.setName("eToys.com");
DotCom three = new DotCom();
three.setName("Go2.com");
dotComsList.add(one);
dotComsList.add(two);
dotComsList.add(three);
System.out.println("Your goal is to sink three dot coms.");
System.out.println("Pets.com, eToys.com, Go2.com");
System.out.println("Try to sink them all in the shortest amount of guesses");
for (DotCom dotComToSet : dotComsList) {
ArrayList<String> newLocation = helper.placeDotCom(3);
dotComToSet.setLocationCells(newLocation);
}
}
private void startPlaying() {
while(!dotComsList.isEmpty()) {
String userGuess = helper.getUserInput("Enter a guess");
checkUserGuess(userGuess);
} // close while
finishGame();
} // close startPlaying method
private void checkUserGuess(String userGuess) {
numOfGuesses++;
String result = "miss"; // assume a miss until told otherwise
for (DotCom dotComToTest : dotComsList) {
result = dotComToTest.checkYourself(userGuess);
if (result.equals("hit")) {
break;
}
if (result.equals("kill")) {
dotComsList.remove(dotComToTest); // he's gone
break;
}
} // close for
System.out.println(result);
}
private void finishGame() {
System.out.println("All Dot Coms are dead! Your stock is now worthless");
if (numOfGuesses <= 9) {
System.out.println("It only took you " + numOfGuesses + " guesses. You get the Enron award!");
} else {
System.out.println("Took you long enough. "+ numOfGuesses + " guesses.");
System.out.println("Too bad you didn't get out before your options sank.");
}
}
public static void main (String[] args) {
DotComBust game = new DotComBust();
game.setUpGame();
game.startPlaying();
}
}
import java.io.*;
import java.util.*;
class GameHelper {
private static final String alphabet = "abcdefg";
private int gridLength = 7;
private int gridSize = 49;
private int [] grid = new int[gridSize];
private int comCount = 0;
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(
new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0 ) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);
}
return inputLine.toLowerCase();
}
public ArrayList<String> placeDotCom(int comSize) {
ArrayList<String> alphaCells = new ArrayList<String>();
String [] alphacoords = new String [comSize];
String temp = null; // temporary String for concat
int [] coords = new int[comSize]; // current candidate coords
int attempts = 0; // current attempts counter
boolean success = false; // flag = found a good location ?
int location = 0; // current starting location
comCount++; // nth dot com to place
int incr = 1; // set horizontal increment
if ((comCount % 2) == 1) { // if odd dot com (place vertically)
incr = gridLength; // set vertical increment
}
while ( !success & attempts++ < 200 ) { // main search loop (32)
location = (int) (Math.random() * gridSize); // get random starting point
//System.out.print(" try " + location);
int x = 0; // nth position in dotcom to place
success = true; // assume success
while (success && x < comSize) { // look for adjacent unused spots
if (grid[location] == 0) { // if not already used
coords[x++] = location; // save location
location += incr; // try 'next' adjacent
if (location >= gridSize){ // out of bounds - 'bottom'
success = false; // failure
}
if (x>0 & (location % gridLength == 0)) { // out of bounds - right edge
success = false; // failure
}
} else { // found already used location
// System.out.print(" used " + location);
success = false; // failure
}
}
} // end while
int x = 0; // turn good location into alpha coords
int row = 0;
int column = 0;
// System.out.println("\n");
while (x < comSize) {
grid[coords[x]] = 1; // mark master grid pts. as 'used'
row = (int) (coords[x] / gridLength); // get row value
column = coords[x] % gridLength; // get numeric column value
temp = String.valueOf(alphabet.charAt(column)); // convert to alpha
alphaCells.add(temp.concat(Integer.toString(row)));
x++;
// System.out.print(" coord "+x+" = " + alphaCells.get(x-1));
}
// System.out.println("\n");
return alphaCells;
}
}